use jl_sys::jl_throw;
use jlrs_sys::unsized_local_scope;
use crate::{
InstallJlrsCore,
data::managed::{module::JlrsCore, private::ManagedPriv, value::ValueRet},
init_jlrs,
memory::{
stack_frame::{PinnedFrame, StackFrame},
target::{
frame::{GcFrame, LocalFrame, LocalGcFrame, UnsizedLocalGcFrame},
unrooted::Unrooted,
},
},
private::Private,
runtime::state::set_started_from_julia,
};
pub struct CCall<'context> {
frame: PinnedFrame<'context, 0>,
}
impl<'context> CCall<'context> {
#[inline]
#[deprecated = "Use weak_handle instead"]
pub unsafe fn new(frame: &'context mut StackFrame<0>) -> Self {
unsafe { CCall { frame: frame.pin() } }
}
#[deprecated = "Use weak_handle and WithStack instead"]
pub fn scope<T, F>(&mut self, func: F) -> T
where
for<'scope> F: FnOnce(GcFrame<'scope>) -> T,
{
unsafe {
let stack = self.frame.stack_frame().sync_stack();
let frame = GcFrame::base(stack);
let ret = func(frame);
stack.pop_roots(0);
ret
}
}
#[inline]
#[deprecated = "Use weak_handle instead"]
pub unsafe fn local_scope<T, F, const N: usize>(func: F) -> T
where
for<'scope> F: FnOnce(LocalGcFrame<'scope, N>) -> T,
{
unsafe {
let mut local_frame = LocalFrame::new();
let pinned = local_frame.pin();
let res = func(LocalGcFrame::new(&pinned));
pinned.pop();
res
}
}
#[inline]
#[deprecated = "Use weak_handle instead"]
pub unsafe fn unsized_local_scope<T, F>(&self, size: usize, func: F) -> T
where
for<'inner> F: FnOnce(UnsizedLocalGcFrame<'inner>) -> T,
{
unsafe {
let mut func = Some(func);
unsized_local_scope(size, |frame| {
let frame = UnsizedLocalGcFrame::new(frame);
func.take().unwrap()(frame)
})
}
}
}
#[inline]
pub unsafe fn throw_exception(exception: ValueRet) -> ! {
unsafe { jl_throw(exception.ptr().as_ptr()) }
}
#[doc(hidden)]
#[inline]
pub unsafe fn throw_borrow_exception() -> ! {
unsafe {
let unrooted = Unrooted::new();
let err = JlrsCore::borrow_error(&unrooted).instance().unwrap();
jl_throw(err.unwrap(Private))
}
}
#[inline(never)]
pub unsafe fn init_jlrs_wrapped(install_jlrs_core: &InstallJlrsCore) {
unsafe {
set_started_from_julia();
init_jlrs(install_jlrs_core, false);
}
}