Skip to content

Commit

Permalink
feat(cppgc): add get and unwrap utlities to SameObject (#1059)
Browse files Browse the repository at this point in the history
  • Loading branch information
petamoriken authored Jan 25, 2025
1 parent cce028f commit 72820bf
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions core/cppgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,10 +182,12 @@ impl FunctionTemplateData {
}
}

#[derive(Debug)]
pub struct SameObject<T: GarbageCollected + 'static> {
cell: std::cell::OnceCell<v8::Global<v8::Object>>,
_phantom_data: std::marker::PhantomData<T>,
}

impl<T: GarbageCollected + 'static> SameObject<T> {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Expand All @@ -212,4 +214,19 @@ impl<T: GarbageCollected + 'static> SameObject<T> {
})
.clone()
}

pub fn set(
&self,
scope: &mut v8::HandleScope,
value: T,
) -> Result<(), v8::Global<v8::Object>> {
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<Ptr<T>> {
let obj = self.cell.get()?;
let val = v8::Local::new(scope, obj);
try_unwrap_cppgc_object(scope, val.cast())
}
}

0 comments on commit 72820bf

Please sign in to comment.