diff --git a/src/handle.rs b/src/handle.rs index 45ec44cc..439a020b 100644 --- a/src/handle.rs +++ b/src/handle.rs @@ -131,12 +131,12 @@ impl Global { pub fn new(isolate: &mut Isolate, handle: impl Handle) -> Self { let HandleInfo { data, host } = handle.get_handle_info(); host.assert_match_isolate(isolate); - unsafe { Self::from_raw(isolate, data) } + unsafe { Self::new_raw(isolate, data) } } - /// Converts a raw pointer created with [`Global::into_raw()`] back to its - /// original `Global`. - pub unsafe fn from_raw(isolate: &mut Isolate, data: NonNull) -> Self { + /// Implementation helper function that contains the code that can be shared + /// between `Global::new()` and `Global::clone()`. + unsafe fn new_raw(isolate: *mut Isolate, data: NonNull) -> Self { let data = data.cast().as_ptr(); let data = v8__Global__New(isolate, data) as *const T; let data = NonNull::new_unchecked(data as *mut _); @@ -159,6 +159,16 @@ impl Global { data } + /// Converts a raw pointer created with [`Global::into_raw()`] back to its + /// original `Global`. + pub unsafe fn from_raw(isolate: &mut Isolate, data: NonNull) -> Self { + let isolate_handle = isolate.thread_safe_handle(); + Self { + data, + isolate_handle, + } + } + pub fn open<'a>(&'a self, scope: &mut Isolate) -> &'a T { Handle::open(self, scope) } @@ -172,7 +182,7 @@ impl Global { impl Clone for Global { fn clone(&self) -> Self { let HandleInfo { data, host } = self.get_handle_info(); - unsafe { Self::from_raw(host.get_isolate().as_mut(), data) } + unsafe { Self::new_raw(host.get_isolate().as_mut(), data) } } }