use crate::isolate::Isolate;
use crate::scope::Scope;
use crate::scope::ScopeDefinition;
use crate::scope_traits::ToLocalOrReturnsLocal;
use crate::InIsolate;
use crate::Local;
use crate::Value;
extern "C" {
fn v8__HandleScope__CONSTRUCT(buf: *mut HandleScope, isolate: *mut Isolate);
fn v8__HandleScope__DESTRUCT(this: &mut HandleScope);
fn v8__EscapableHandleScope__CONSTRUCT(
buf: *mut EscapableHandleScope,
isolate: *mut Isolate,
);
fn v8__EscapableHandleScope__DESTRUCT(this: &mut EscapableHandleScope);
fn v8__EscapableHandleScope__Escape(
this: &mut EscapableHandleScope,
value: *mut Value,
) -> *mut Value;
}
#[repr(C)]
pub struct HandleScope([usize; 3]);
impl<'s> HandleScope {
pub fn new<P>(parent: &'s mut P) -> Scope<'s, Self, P>
where
P: InIsolate,
{
let isolate: *mut Isolate = parent.isolate();
Scope::new(isolate, parent)
}
}
unsafe impl<'s> ScopeDefinition<'s> for HandleScope {
type Args = *mut Isolate;
unsafe fn enter_scope(buf: *mut Self, isolate: *mut Isolate) {
v8__HandleScope__CONSTRUCT(buf, isolate);
}
}
impl Drop for HandleScope {
fn drop(&mut self) {
unsafe { v8__HandleScope__DESTRUCT(self) }
}
}
#[repr(C)]
pub struct EscapableHandleScope([usize; 4]);
impl<'s> EscapableHandleScope {
pub fn new<'p: 's, P>(parent: &'s mut P) -> Scope<'s, Self, P>
where
P: ToLocalOrReturnsLocal<'p>,
{
let isolate: *mut Isolate = parent.isolate();
Scope::new(isolate, parent)
}
pub(crate) unsafe fn escape<'p, T>(
&mut self,
value: Local<T>,
) -> Local<'p, T> {
Local::from_raw(v8__EscapableHandleScope__Escape(
self,
value.as_ptr() as *mut Value,
) as *mut T)
.unwrap()
}
}
unsafe impl<'s> ScopeDefinition<'s> for EscapableHandleScope {
type Args = *mut Isolate;
unsafe fn enter_scope(buf: *mut Self, isolate: *mut Isolate) {
v8__EscapableHandleScope__CONSTRUCT(buf, isolate);
}
}
impl Drop for EscapableHandleScope {
fn drop(&mut self) {
unsafe { v8__EscapableHandleScope__DESTRUCT(self) }
}
}