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
#[macro_export]
macro_rules! local {
($($local:ident),*) => {
#[allow(unused_mut)]
$(let mut $local = $crate::Value($crate::sys::UNIT);)*
#[allow(unused_unsafe)]
$crate::sys::caml_param!($($local.0),*);
}
}
#[macro_export]
macro_rules! frame {
(($($param:ident),*) $code:block) => {
{
#[allow(unused_unsafe)]
let caml_frame = unsafe { $crate::sys::local_roots() };
$crate::local!($($param),*);
#[allow(unused_mut)]
let mut res = || { $code };
let res = res();
#[allow(unused_unsafe)]
unsafe { $crate::sys::set_local_roots(caml_frame) };
res
}
}
}
#[macro_export]
#[cfg(feature = "no-std")]
macro_rules! body {
($(($($param:expr),*))? $code:block) => {
$crate::sys::caml_body!($(($($param.0),*))? $code);
}
}
#[cfg(not(feature = "no-std"))]
static PANIC_HANDLER_INIT: std::sync::atomic::AtomicBool =
std::sync::atomic::AtomicBool::new(false);
#[cfg(not(feature = "no-std"))]
#[doc(hidden)]
pub fn init_panic_handler() {
if PANIC_HANDLER_INIT.compare_and_swap(false, true, std::sync::atomic::Ordering::Relaxed) {
return;
}
::std::panic::set_hook(Box::new(|info| {
let err = info.payload();
let msg = if err.is::<&str>() {
err.downcast_ref::<&str>().unwrap()
} else if err.is::<String>() {
err.downcast_ref::<String>().unwrap().as_ref()
} else {
"rust panic"
};
crate::Error::raise_failure(msg)
}))
}
#[macro_export]
#[cfg(not(feature = "no-std"))]
macro_rules! body {
($(($($param:expr),*))? $code:block) => {{
$crate::init_panic_handler();
#[allow(unused_unsafe)]
let caml_frame = unsafe { $crate::sys::local_roots() };
$(
$crate::sys::caml_param!($($param.0),*);
)?
#[allow(unused_mut)]
let mut res = || {$code };
let res = res();
#[allow(unused_unsafe)]
unsafe { $crate::sys::set_local_roots(caml_frame) };
res
}}
}
#[macro_export]
macro_rules! array {
($($x:expr),*) => {{
$crate::ToValue::to_value(&vec![$($crate::ToValue::to_value(&$x)),*])
}}
}
#[macro_export]
macro_rules! list {
($($x:expr),*) => {{
let mut l = $crate::list::empty();
for i in (&[$($x),*]).into_iter().rev() {
$crate::list::push_hd(&mut l, $crate::ToValue::to_value(i));
}
l
}};
}