-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathasync_mod.rs
More file actions
182 lines (170 loc) · 6.77 KB
/
Copy pathasync_mod.rs
File metadata and controls
182 lines (170 loc) · 6.77 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
use crate::{async_runtime, StdFunction, StdlibModule, StdlibRegistry};
use indexmap::IndexMap;
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use techscript_runtime::{error::RuntimeError, value::RuntimeValue, RuntimeContext};
impl StdlibRegistry {
pub fn register_async(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"spawn_async".to_string(),
Rc::new(StdFunction {
name: "spawn_async".to_string(),
arity: 1,
callback: |_ctx, args| {
let callback = args[0].clone();
let mut fut_map = IndexMap::new();
fut_map.insert(
"state".to_string(),
RuntimeValue::Str("pending".to_string()),
);
fut_map.insert("value".to_string(), RuntimeValue::Null);
let future = RuntimeValue::Map {
entries: Rc::new(RefCell::new(fut_map)),
is_const: false,
};
let fut_clone = future.clone();
if let RuntimeValue::Function(func) = callback {
let func_ptr = Box::into_raw(Box::new(func)) as usize;
async_runtime::spawn_task(fut_clone, 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![]).map_err(|e| format!("{:?}", e))
});
}
Ok(future)
},
}),
);
self.register_module(
"std.async",
StdlibModule {
name: "std.async".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
pub fn register_future(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"make_future".to_string(),
Rc::new(StdFunction {
name: "make_future".to_string(),
arity: 0,
callback: |_ctx, _args| {
let mut fut_map = IndexMap::new();
fut_map.insert(
"state".to_string(),
RuntimeValue::Str("pending".to_string()),
);
fut_map.insert("value".to_string(), RuntimeValue::Null);
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(fut_map)),
is_const: false,
})
},
}),
);
self.register_module(
"std.future",
StdlibModule {
name: "std.future".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
pub fn register_channel(&mut self) {
let mut exports: HashMap<String, Rc<dyn techscript_runtime::function::Callable>> =
HashMap::new();
exports.insert(
"make_channel".to_string(),
Rc::new(StdFunction {
name: "make_channel".to_string(),
arity: 0,
callback: |ctx, _args| {
let (tx, rx) = std::sync::mpsc::channel::<RuntimeValue>();
let tx_id = ctx.resources.borrow_mut().insert(tx);
let rx_id = ctx.resources.borrow_mut().insert(rx);
let mut map = IndexMap::new();
map.insert("_tx_handle".to_string(), RuntimeValue::Int(tx_id as i64));
map.insert("_rx_handle".to_string(), RuntimeValue::Int(rx_id as i64));
Ok(RuntimeValue::Map {
entries: Rc::new(RefCell::new(map)),
is_const: false,
})
},
}),
);
exports.insert(
"send_channel".to_string(),
Rc::new(StdFunction {
name: "send_channel".to_string(),
arity: 2,
callback: |ctx, args| {
if let RuntimeValue::Map { entries, .. } = &args[0] {
let handle_id = entries
.borrow()
.get("_tx_handle")
.cloned()
.unwrap_or(RuntimeValue::Null)
.try_into_int()? as u32;
let resources = ctx.resources.borrow();
if let Some(tx) =
resources.get::<std::sync::mpsc::Sender<RuntimeValue>>(handle_id)
{
tx.send(args[1].clone()).ok();
}
}
Ok(RuntimeValue::Null)
},
}),
);
exports.insert(
"recv_channel".to_string(),
Rc::new(StdFunction {
name: "recv_channel".to_string(),
arity: 1,
callback: |ctx, args| {
if let RuntimeValue::Map { entries, .. } = &args[0] {
let handle_id = entries
.borrow()
.get("_rx_handle")
.cloned()
.unwrap_or(RuntimeValue::Null)
.try_into_int()? as u32;
let resources = ctx.resources.borrow();
if let Some(rx) =
resources.get::<std::sync::mpsc::Receiver<RuntimeValue>>(handle_id)
{
if let Ok(val) = rx.recv() {
return Ok(val);
}
}
}
Ok(RuntimeValue::Null)
},
}),
);
self.register_module(
"std.channel",
StdlibModule {
name: "std.channel".to_string(),
version: "1.0.0".to_string(),
exports,
required_capabilities: Vec::new(),
},
);
}
}