0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-12-25 16:49:29 -05:00

Retrieve value from cppgc::Member handle (#1388)

This commit is contained in:
Divy Srivastava 2024-01-10 10:00:54 +05:30 committed by GitHub
parent 21e7d7b3f2
commit 9f10d76f59
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -22,7 +22,7 @@ extern "C" {
obj: *mut (),
trace: TraceFn,
destroy: DestroyFn,
) -> *mut ();
) -> *mut InnerMember;
fn cppgc__heap__enable_detached_garbage_collections_for_testing(
heap: *mut Heap,
@ -32,7 +32,7 @@ extern "C" {
stack_state: EmbedderStackState,
);
fn cppgc__visitor__trace(visitor: *const Visitor, member: *const ());
fn cppgc__visitor__trace(visitor: *const Visitor, member: *const InnerMember);
}
/// Process-global initialization of the garbage collector. Must be called before
@ -216,12 +216,25 @@ pub trait GarbageCollected {
fn trace(&self, _visitor: &Visitor) {}
}
#[repr(C)]
pub struct InnerMember {
inner: [usize; 2],
ptr: *mut (),
}
impl InnerMember {
pub unsafe fn get<T: GarbageCollected>(&self) -> &T {
unsafe { self.ptr.cast::<T>().as_ref().unwrap() }
}
}
/// Members are used to contain strong pointers to other garbage
/// collected objects. All members fields on garbage collected objects
/// must be trace in the `trace` method.
#[repr(transparent)]
pub struct Member<T: GarbageCollected> {
pub handle: *mut (),
ptr: *mut T,
pub handle: *mut InnerMember,
_phantom: std::marker::PhantomData<T>,
}
impl<T: GarbageCollected> Member<T> {
@ -231,7 +244,7 @@ impl<T: GarbageCollected> Member<T> {
///
/// There are no guarantees that the object is alive and not garbage collected.
pub unsafe fn get(&self) -> &T {
unsafe { &*self.ptr }
unsafe { (*self.handle).get() }
}
}
@ -239,7 +252,7 @@ impl<T: GarbageCollected> std::ops::Deref for Member<T> {
type Target = T;
fn deref(&self) -> &Self::Target {
unsafe { &*self.ptr }
unsafe { self.get() }
}
}
@ -290,5 +303,8 @@ pub unsafe fn make_garbage_collected_raw<T: GarbageCollected>(
destroy,
);
Member { handle, ptr: obj }
Member {
handle,
_phantom: std::marker::PhantomData,
}
}