static PANIC_HANDLER_INIT: core::sync::atomic::AtomicBool =
core::sync::atomic::AtomicBool::new(false);
#[doc(hidden)]
pub fn initial_setup() {
if PANIC_HANDLER_INIT
.compare_exchange(
false,
true,
core::sync::atomic::Ordering::Relaxed,
core::sync::atomic::Ordering::Relaxed,
)
.is_err()
{
return;
}
unsafe {
ocaml_boxroot_sys::boxroot_setup();
}
#[cfg(not(feature = "no-panic-hook"))]
{
::std::panic::set_hook(Box::new(|info| unsafe {
let err = info.payload();
let msg = if err.is::<&str>() {
err.downcast_ref::<&str>().unwrap().to_string()
} else if err.is::<String>() {
err.downcast_ref::<String>().unwrap().clone()
} else {
format!("{:?}", err)
};
if let Some(err) = crate::Value::named("Rust_exception") {
crate::Error::raise_value(err, &msg);
}
crate::Error::raise_failure(&msg)
}))
}
}
#[macro_export]
macro_rules! body {
($gc:ident: $code:block) => {{
let $gc = unsafe { $crate::Runtime::recover_handle() };
$crate::initial_setup();
{
$code
}
}};
}
#[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
}};
}
#[macro_export]
macro_rules! import {
($vis:vis fn $name:ident($($arg:ident: $t:ty),*) $(-> $r:ty)?) => {
$vis unsafe fn $name(rt: &$crate::Runtime, $($arg: $t),*) -> Result<$crate::interop::default_to_unit!($($r)?), $crate::Error> {
use $crate::{ToValue, FromValue};
type R = $crate::interop::default_to_unit!($($r)?);
let ocaml_rs_named_func = match $crate::Value::named(stringify!($name)) {
Some(x) => x,
None => {
let msg = concat!(
stringify!($name),
" has not been registered using Callback.register"
);
return Err($crate::Error::Message(msg));
},
};
$(let $arg = $arg.to_value(rt);)*
let __unit = [$crate::Value::unit().raw()];
let __args = [$($arg.raw()),*];
let mut args = __args.as_slice();
if args.is_empty() {
args = &__unit;
}
let x = ocaml_rs_named_func.call_n(args)?;
Ok(R::from_value(x))
}
};
($($vis:vis fn $name:ident($($arg:ident: $t:ty),*) $(-> $r:ty)?;)+) => {
$(
$crate::import!($vis fn $name($($arg: $t),*) $(-> $r)?);
)*
}
}
#[macro_export]
macro_rules! function {
($x:expr, ($($argname:ident: $arg:ty),*) -> $r:ty) => {
|gc: &$crate::Runtime, $($argname: &$arg),*| -> Result<$r, $crate::Error> {
let args = [$($crate::ToValue::to_value($argname, gc)),*];
#[allow(unused_unsafe)]
unsafe { $crate::Value::call(&$x, gc, args) }
}
};
}