From f040d4a78f0e8feeba99ce5a727c8ce0290f9003 Mon Sep 17 00:00:00 2001 From: Greg Morenz Date: Tue, 25 Mar 2025 20:58:29 -0400 Subject: [PATCH 1/2] Remove DerefMut impl for MutableHandle Signed-off-by: Greg Morenz --- mozjs/src/gc/root.rs | 6 ------ 1 file changed, 6 deletions(-) diff --git a/mozjs/src/gc/root.rs b/mozjs/src/gc/root.rs index 770a2f0f60..a07d643c80 100644 --- a/mozjs/src/gc/root.rs +++ b/mozjs/src/gc/root.rs @@ -249,12 +249,6 @@ impl<'a, T> Deref for MutableHandle<'a, T> { } } -impl<'a, T> DerefMut for MutableHandle<'a, T> { - fn deref_mut(&mut self) -> &mut T { - unsafe { &mut *self.ptr } - } -} - impl HandleValue<'static> { pub fn null() -> Self { unsafe { Self::from_raw(RawHandleValue::null()) } From b198191b6f81f509af803096b617c880723def20 Mon Sep 17 00:00:00 2001 From: Greg Morenz Date: Tue, 25 Mar 2025 22:21:11 -0400 Subject: [PATCH 2/2] Add access methods to MutableHandle Signed-off-by: Greg Morenz --- mozjs/src/gc/root.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/mozjs/src/gc/root.rs b/mozjs/src/gc/root.rs index a07d643c80..1682a8ba74 100644 --- a/mozjs/src/gc/root.rs +++ b/mozjs/src/gc/root.rs @@ -216,6 +216,14 @@ impl<'a, T> MutableHandle<'a, T> { unsafe { *self.ptr = v } } + /// Safety: GC must not run during the lifetime of the returned reference. + pub unsafe fn as_mut<'b>(&'b mut self) -> &'b mut T + where + 'a: 'b, + { + &mut *(self.ptr) + } + /// Creates a copy of this object, with a shorter lifetime, that holds a /// mutable borrow on the original object. When you write code that wants /// to use a `MutableHandle` more than once, you will typically need to @@ -241,6 +249,13 @@ impl<'a, T> MutableHandle<'a, T> { } } +impl<'a, T> MutableHandle<'a, Option> { + pub fn take(&mut self) -> Option { + // Safety: No GC occurs during take call + unsafe { self.as_mut().take() } + } +} + impl<'a, T> Deref for MutableHandle<'a, T> { type Target = T;