Skip to content

Conversation

@anonrig
Copy link
Member

@anonrig anonrig commented Dec 3, 2025

This PR adds garbage collection support to Rust-owned JSG resources, enabling proper cleanup when V8 collects JavaScript wrapper objects.

@codspeed-hq

This comment was marked as off-topic.

Copy link
Contributor

@guybedford guybedford left a comment

Choose a reason for hiding this comment

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

Lovely work!

/// When a resource is wrapped for JavaScript, `State` stores a strong V8 Global handle to the
/// wrapper object. The `strong_ref_count` tracks how many Rust `Ref<R>` handles exist. When the
/// count reaches zero and a wrapper exists, the Global is made weak so V8 can garbage collect
/// the wrapper and trigger cleanup via the weak callback.
Copy link
Contributor

Choose a reason for hiding this comment

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

Neat!

}
}

pub fn unwrap<'a, R: Resource>(lock: &'a mut Lock, value: v8::Local<v8::Value>) -> &'a mut R {
Copy link
Contributor

Choose a reason for hiding this comment

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

It's a bit of a concern from a Rust perspective if you can obtain multiple mutable borrows this way, should we protect against that case?


impl<T: Resource> DerefMut for Ref<T> {
fn deref_mut(&mut self) -> &mut Self::Target {
unsafe { &mut (*self.instance).resource }
Copy link
Contributor

Choose a reason for hiding this comment

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

It seems like this permits multiple mut refs to the same data, do we need to consider protections?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants