Skip to content

Commit 72820bf

Browse files
authored
feat(cppgc): add get and unwrap utlities to SameObject (#1059)
1 parent cce028f commit 72820bf

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

core/cppgc.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,10 +182,12 @@ impl FunctionTemplateData {
182182
}
183183
}
184184

185+
#[derive(Debug)]
185186
pub struct SameObject<T: GarbageCollected + 'static> {
186187
cell: std::cell::OnceCell<v8::Global<v8::Object>>,
187188
_phantom_data: std::marker::PhantomData<T>,
188189
}
190+
189191
impl<T: GarbageCollected + 'static> SameObject<T> {
190192
#[allow(clippy::new_without_default)]
191193
pub fn new() -> Self {
@@ -212,4 +214,19 @@ impl<T: GarbageCollected + 'static> SameObject<T> {
212214
})
213215
.clone()
214216
}
217+
218+
pub fn set(
219+
&self,
220+
scope: &mut v8::HandleScope,
221+
value: T,
222+
) -> Result<(), v8::Global<v8::Object>> {
223+
let obj = make_cppgc_object(scope, value);
224+
self.cell.set(v8::Global::new(scope, obj))
225+
}
226+
227+
pub fn try_unwrap(&self, scope: &mut v8::HandleScope) -> Option<Ptr<T>> {
228+
let obj = self.cell.get()?;
229+
let val = v8::Local::new(scope, obj);
230+
try_unwrap_cppgc_object(scope, val.cast())
231+
}
215232
}

0 commit comments

Comments
 (0)