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
use ocaml_sys::{caml_shutdown, caml_startup};
use std::marker::PhantomData;
use crate::{memory::GCFrame, memory::OCamlRef, value::OCaml};
pub struct OCamlRuntime {
_private: (),
}
impl OCamlRuntime {
pub fn init() -> Self {
OCamlRuntime::init_persistent();
OCamlRuntime { _private: () }
}
pub fn init_persistent() {
let arg0 = "ocaml".as_ptr() as *const i8;
let c_args = vec![arg0, core::ptr::null()];
unsafe { caml_startup(c_args.as_ptr()) }
}
pub unsafe fn recover_handle() -> &'static mut Self {
static mut RUNTIME: OCamlRuntime = OCamlRuntime { _private: () };
&mut RUNTIME
}
pub fn releasing_runtime<T, F>(&mut self, f: F) -> T
where
F: FnOnce() -> T,
{
OCamlBlockingSection::new().perform(f)
}
#[doc(hidden)]
pub fn open_frame<'a, 'gc>(&'a self) -> GCFrame<'gc> {
Default::default()
}
pub fn get<'tmp, T>(&'tmp self, reference: OCamlRef<T>) -> OCaml<'tmp, T> {
OCaml {
_marker: PhantomData,
raw: unsafe { reference.get_raw() },
}
}
}
impl Drop for OCamlRuntime {
fn drop(&mut self) {
unsafe { caml_shutdown() }
}
}
struct OCamlBlockingSection {}
impl OCamlBlockingSection {
fn new() -> Self {
Self {}
}
fn perform<T, F>(self, f: F) -> T
where
F: FnOnce() -> T,
{
unsafe { ocaml_sys::caml_enter_blocking_section() };
f()
}
}
impl Drop for OCamlBlockingSection {
fn drop(&mut self) {
unsafe { ocaml_sys::caml_leave_blocking_section() };
}
}