-
Notifications
You must be signed in to change notification settings - Fork 0
/
stubs.rs
80 lines (67 loc) · 2.47 KB
/
stubs.rs
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
use std::borrow::Borrow;
use crate::local_executor;
use crate::ptr::CamlRef;
use crate::util::ensure_rooted_value;
use ocaml::interop::{DynBox, OCaml};
///////////////////////////////////////////////////////////////////////////////
////////// Promise //////////
///////////////////////////////////////////////////////////////////////////////
#[ocaml::sig]
type Promise = crate::promise::Promise;
#[ocaml::func]
#[ocaml::sig("unit -> promise")]
pub fn lwti_promise_create() -> CamlRef<Promise> {
Promise::new().into()
}
#[ocaml::func]
#[ocaml::sig("promise -> 'a -> unit")]
pub fn lwti_promise_resolve(promise: CamlRef<Promise>, value: ocaml::Value) {
let value = ensure_rooted_value(value);
promise.resolve(value)
}
#[ocaml::func]
#[ocaml::sig("promise -> exn -> unit")]
pub fn lwti_promise_reject(promise: CamlRef<Promise>, exn: ocaml::Value) {
let exn = ensure_rooted_value(exn);
promise.reject(exn)
}
///////////////////////////////////////////////////////////////////////////////
////////// Executor //////////
///////////////////////////////////////////////////////////////////////////////
#[ocaml::sig]
type Executor = local_executor::LocalExecutor;
#[ocaml::func]
#[ocaml::sig("int -> executor")]
pub fn lwti_executor_create(notify_id: isize) -> CamlRef<Executor> {
let mut executor = Executor::new();
executor.set_notifier(crate::notification::Notification(notify_id));
executor.into()
}
/* #1 */
#[ocaml::func]
#[ocaml::sig("executor -> unit")]
pub fn lwti_executor_run_pending(executor: OCaml<DynBox<Executor>>) {
while Borrow::<Executor>::borrow(&executor).try_tick() {}
}
/* #2 */
// #[ocaml::func]
// #[ocaml::sig("executor -> unit")]
// pub fn lwti_executor_run_pending(executor: OCaml<DynBox<Executor>>) {
// let executor: &Executor = executor.borrow();
// while executor.try_tick() {}
// }
#[ocaml::func]
#[ocaml::sig("executor -> (int -> promise) -> unit")]
pub fn lwti_executor_test(executor: CamlRef<Executor>, f: ocaml::Value) {
let f = ensure_rooted_value(f);
let task = executor.spawn(async move {
let mut page_nb = 0;
let gc = unsafe { ocaml::interop::OCamlRuntime::recover_handle() };
let f_callable = ocaml::function!(f, (n: ocaml::Int) -> CamlRef<Promise>);
loop {
f_callable(&gc, &page_nb).unwrap().clone().await.unwrap();
page_nb = page_nb + 1;
}
});
task.detach();
}