use ocaml_boxroot_sys::{boxroot_setup, boxroot_teardown};
use ocaml_sys::{caml_shutdown, caml_startup};
use std::{marker::PhantomData, sync::Once};
use crate::{memory::OCamlRef, value::OCaml};
pub struct OCamlRuntime {
_private: (),
}
impl OCamlRuntime {
pub fn init() -> Self {
Self::init_persistent();
Self { _private: () }
}
pub fn init_persistent() {
static INIT: Once = Once::new();
INIT.call_once(|| {
let arg0 = "ocaml\0".as_ptr() as *const ocaml_sys::Char;
let c_args = vec![arg0, core::ptr::null()];
unsafe {
caml_startup(c_args.as_ptr());
}
})
}
#[inline(always)]
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)
}
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 {
boxroot_teardown();
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() };
}
}
#[no_mangle]
extern "C" fn ocaml_interop_setup(_unit: crate::RawOCaml) -> crate::RawOCaml {
unsafe { boxroot_setup() };
ocaml_sys::UNIT
}
#[no_mangle]
extern "C" fn ocaml_interop_teardown(_unit: crate::RawOCaml) -> crate::RawOCaml {
unsafe { boxroot_teardown() };
ocaml_sys::UNIT
}