Skip to content
Merged
Show file tree
Hide file tree
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
10 changes: 7 additions & 3 deletions src/rust/jsg-test/tests/drop_moved_traced_global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ fn traced_global_moved_out_in_drop_keeps_its_value() {
});
harness.run_in_context(|lock, _ctx| {
let stashed = stashed.take().expect("stashed above");
assert!(!stashed.is_empty(lock));
assert!(stashed.as_local(lock).is_object());
Ok(())
});
}

/// The value dies with the resource, in the GC that sweeps it. The `TracedGlobal` that `Drop`
/// moves out is then empty, and dropping it touches no handle.
/// moves out is then empty, `try_as_local` returns `None`, and dropping it touches no handle.
#[test]
fn traced_global_whose_value_died_with_the_resource_drops_cleanly() {
let _serial = SERIAL
Expand All @@ -127,7 +128,10 @@ fn traced_global_whose_value_died_with_the_resource_drops_cleanly() {
let baseline = crate::Harness::used_global_handles_size(lock);

// V8 reset the handle when its value died, so the drop has nothing left to release.
drop(stash_from_swept_resource(lock, ctx, "{}"));
let stashed = stash_from_swept_resource(lock, ctx, "{}");
assert!(stashed.is_empty(lock));
assert!(stashed.try_as_local(lock).is_none());
drop(stashed);
crate::Harness::request_gc(lock);
assert_eq!(crate::Harness::used_global_handles_size(lock), baseline);
Ok(())
Expand Down Expand Up @@ -159,7 +163,7 @@ fn traced_global_moved_out_after_a_scavenge_keeps_its_value() {
});
harness.run_in_context(|lock, _ctx| {
let stashed = stashed.take().expect("checked above");
assert!(stashed.as_local(lock).is_object());
assert!(stashed.try_as_local(lock).expect("not empty").is_object());
Ok(())
});
}
5 changes: 4 additions & 1 deletion src/rust/jsg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,10 @@ impl that visits through it must handle). The `jsg::` cells below detach a value
`#[jsg_resource]` value is dropped, if it implements `Drop` or a field's type says it needs it
(`Traced::needs_drop_detach`), every `jsg::Rc`, `jsg_cpp::Ref` and `TracedGlobal` it traces is
made strong, and every `jsg::Rc` forgets its parent, so that a `Drop` impl may move one out and
keep it. A `TracedGlobal` whose value died with the resource is empty by then. A hand-written
keep it. A `TracedGlobal` whose value died with the resource is empty by then: that happens
when the resource was the value's only holder and a full GC, not a scavenge, collected its
wrapper. Code that keeps such a handle checks it with `TracedGlobal::is_empty` or opens it with
`TracedGlobal::try_as_local`, since `as_local` panics on an empty handle. A hand-written
`impl Traced` for a type with its own `Drop` returns `true` from `needs_drop_detach`, and so
does the hand-written `Traced` impl of a `custom_trace` resource whose field's type needs it.
Wrappers such as `Option` and `Vec` pass it on, and a `#[jsg_resource]` says so when it
Expand Down
23 changes: 23 additions & 0 deletions src/rust/jsg/v8.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4618,12 +4618,35 @@ impl<T> TracedGlobal<T> {
}

/// Materializes this handle as a `Local` in the scope of `lock`.
///
/// Panics if the handle is empty; see [`TracedGlobal::is_empty`] and
/// [`TracedGlobal::try_as_local`].
pub fn as_local<'s>(&self, lock: &mut Lock<'s>) -> Local<'s, T> {
assert_in_isolate_of(lock, &self.state);
// SAFETY: the handle belongs to the lock's isolate, as checked above.
unsafe { global_to_local(lock, &self.strong_handle()) }
}

/// Whether the handle is empty, because GC made it weak and its value then died.
///
/// A handle that a resource's `Drop` moves out of a field is empty if the resource was its
/// value's only holder and a full GC collected the resource's wrapper. It is never empty
/// while a resource that GC traces through holds it.
pub fn is_empty(&self, lock: &mut Lock<'_>) -> bool {
assert_in_isolate_of(lock, &self.state);
self.strong_handle().ptr == 0
}

/// Materializes this handle as a `Local` in the scope of `lock`, or returns `None` if it is
/// empty; see [`TracedGlobal::is_empty`].
pub fn try_as_local<'s>(&self, lock: &mut Lock<'s>) -> Option<Local<'s, T>> {
if self.is_empty(lock) {
None
} else {
Some(self.as_local(lock))
}
}

/// Clears the traced reference, if any, without resetting it, and makes the strong handle
/// strong; see [`GcVisitor::visit_traced_global`]. The caller must hold the lock of the
/// handle's isolate, which must not be closed.
Expand Down
Loading