Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cppgc): add get and unwrap utlities to SameObject #1059

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the debug output is probably quite useless; any specific need for it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intent is that this would allow #[derive(Debug)] to be added to DOMQuadInner, but I don't have a particularly strong opinion.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh i see, that makes sense!

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(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when is this useful? sameobject should always just be set once and forget, any reason get cannot be used in your scenario?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although get is sufficient, I think it may be useful when we want to assign a value in advance in a constructor or static method.
https://github.com/denoland/deno/blob/dfa4af12b093ac3a504d7fe98004ad7affd1c91a/ext/geometry/lib.rs#L311-L315

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

&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())
}
}
Loading