diff --git a/core/cppgc.rs b/core/cppgc.rs index 74d30ff72..cd0d86c47 100644 --- a/core/cppgc.rs +++ b/core/cppgc.rs @@ -182,10 +182,12 @@ impl FunctionTemplateData { } } +#[derive(Debug)] pub struct SameObject { cell: std::cell::OnceCell>, _phantom_data: std::marker::PhantomData, } + impl SameObject { #[allow(clippy::new_without_default)] pub fn new() -> Self { @@ -212,4 +214,19 @@ impl SameObject { }) .clone() } + + pub fn set( + &self, + scope: &mut v8::HandleScope, + value: T, + ) -> Result<(), v8::Global> { + let obj = make_cppgc_object(scope, value); + self.cell.set(v8::Global::new(scope, obj)) + } + + pub fn try_unwrap(&self, scope: &mut v8::HandleScope) -> Option> { + let obj = self.cell.get()?; + let val = v8::Local::new(scope, obj); + try_unwrap_cppgc_object(scope, val.cast()) + } }