0
0
Fork 0
mirror of https://github.com/denoland/rusty_v8.git synced 2024-11-28 16:21:04 -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 (), obj: *mut (),
trace: TraceFn, trace: TraceFn,
destroy: DestroyFn, destroy: DestroyFn,
) -> *mut (); ) -> *mut InnerMember;
fn cppgc__heap__enable_detached_garbage_collections_for_testing( fn cppgc__heap__enable_detached_garbage_collections_for_testing(
heap: *mut Heap, heap: *mut Heap,
@ -32,7 +32,7 @@ extern "C" {
stack_state: EmbedderStackState, 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 /// Process-global initialization of the garbage collector. Must be called before
@ -216,12 +216,25 @@ pub trait GarbageCollected {
fn trace(&self, _visitor: &Visitor) {} 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 /// Members are used to contain strong pointers to other garbage
/// collected objects. All members fields on garbage collected objects /// collected objects. All members fields on garbage collected objects
/// must be trace in the `trace` method. /// must be trace in the `trace` method.
#[repr(transparent)]
pub struct Member<T: GarbageCollected> { pub struct Member<T: GarbageCollected> {
pub handle: *mut (), pub handle: *mut InnerMember,
ptr: *mut T, _phantom: std::marker::PhantomData<T>,
} }
impl<T: GarbageCollected> Member<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. /// There are no guarantees that the object is alive and not garbage collected.
pub unsafe fn get(&self) -> &T { 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; type Target = T;
fn deref(&self) -> &Self::Target { 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, destroy,
); );
Member { handle, ptr: obj } Member {
handle,
_phantom: std::marker::PhantomData,
}
} }