-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread.rs
More file actions
71 lines (67 loc) · 2.51 KB
/
Copy paththread.rs
File metadata and controls
71 lines (67 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use crate::{StdFunction, StdlibModule, StdlibRegistry};
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{
context::{Capability, RuntimeContext},
error::RuntimeError,
value::RuntimeValue,
};
impl StdlibRegistry {
pub fn register_thread(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"spawn".to_string(),
Rc::new(StdFunction {
name: "spawn".to_string(),
arity: 1,
callback: |ctx, args| {
let callback = args[0].clone();
if let RuntimeValue::Function(func) = callback {
let func_ptr = Box::into_raw(Box::new(func)) as usize;
let handle = std::thread::spawn(move || {
let func = unsafe {
Box::from_raw(
func_ptr as *mut Rc<dyn techscript_runtime::function::Callable>,
)
};
let mut ctx =
RuntimeContext::new(techscript_runtime::RuntimeConfig::default());
func.call(&mut ctx, vec![]).ok();
});
let handle_id = ctx.resources.borrow_mut().insert(handle);
return Ok(RuntimeValue::Int(handle_id as i64));
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"join".to_string(),
Rc::new(StdFunction {
name: "join".to_string(),
arity: 1,
callback: |ctx, args| {
let handle_id = args[0].try_into_int()? as u32;
if let Some(handle) = ctx
.resources
.borrow_mut()
.remove::<std::thread::JoinHandle<()>>(handle_id)
{
handle.join().ok();
}
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.thread",
StdlibModule {
name: "std.thread".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: vec![Capability::Process],
},
);
}
}