Skip to content

Commit

Permalink
Indicate to the compiler that Rooted<T> may be uninitialized (#551)
Browse files Browse the repository at this point in the history
Signed-off-by: Simon Wülker <[email protected]>
  • Loading branch information
simonwuelker authored Jan 26, 2025
1 parent f5ed6f9 commit 87cabf4
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
8 changes: 6 additions & 2 deletions mozjs-sys/src/jsgc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ pub trait Rootable: crate::trace::Traceable + Sized {
unsafe extern "C" fn trace(this: *mut c_void, trc: *mut JSTracer, _name: *const c_char) {
let rooted = this as *mut Rooted<Self>;
let rooted = rooted.as_mut().unwrap();
<Self as crate::trace::Traceable>::trace(&mut rooted.ptr, trc);
<Self as crate::trace::Traceable>::trace(rooted.ptr.assume_init_mut(), trc);
}
}

Expand Down Expand Up @@ -132,7 +132,11 @@ pub struct RootedBase {
pub struct Rooted<T: RootKind> {
pub vtable: T::Vtable,
pub base: RootedBase,
pub ptr: T,

/// The rooted value
///
/// This will be initialied iff there is a `RootedGuard` for this `Rooted`
pub ptr: mem::MaybeUninit<T>,
}

/// Trait that provides a GC-safe default value for the given type, if one exists.
Expand Down
5 changes: 3 additions & 2 deletions mozjs-sys/src/jsimpls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ use crate::jsid::VoidId;
use crate::jsval::{JSVal, UndefinedValue};

use std::marker::PhantomData;
use std::mem;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr;
Expand Down Expand Up @@ -143,7 +144,7 @@ impl<const N: usize> From<&Rooted<ValueArray<N>>> for JS::HandleValueArray {
fn from(array: &Rooted<ValueArray<N>>) -> JS::HandleValueArray {
JS::HandleValueArray {
length_: N,
elements_: unsafe { array.ptr.get_ptr() },
elements_: unsafe { array.ptr.assume_init_ref().get_ptr() },
}
}
}
Expand Down Expand Up @@ -435,7 +436,7 @@ impl<T: RootKind> JS::Rooted<T> {
stack: ptr::null_mut(),
prev: ptr::null_mut(),
},
ptr: unsafe { std::mem::zeroed() },
ptr: mem::MaybeUninit::zeroed(),
}
}

Expand Down
2 changes: 1 addition & 1 deletion mozjs/src/conversions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -713,7 +713,7 @@ impl<C: Clone, T: FromJSValConvertible<Config = C>> FromJSValConvertible for Vec
return Err(());
}

if iterator.iterator.ptr.is_null() {
if iterator.iterator.ptr.assume_init_ref().is_null() {
return Ok(ConversionResult::Failure("Value is not iterable".into()));
}

Expand Down
46 changes: 28 additions & 18 deletions mozjs/src/gc/root.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use std::marker::PhantomData;
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
use std::ptr;

use crate::jsapi::{jsid, JSContext, JSFunction, JSObject, JSScript, JSString, Symbol, Value, JS};
use mozjs_sys::jsgc::{Initialize, RootKind, Rooted};
use mozjs_sys::jsgc::{RootKind, Rooted};

use crate::jsapi::Handle as RawHandle;
use crate::jsapi::HandleValue as RawHandleValue;
Expand All @@ -19,60 +20,69 @@ use mozjs_sys::jsgc::ValueArray;
feature = "crown",
crown::unrooted_must_root_lint::allow_unrooted_interior
)]
pub struct RootedGuard<'a, T: 'a + RootKind + Initialize> {
pub struct RootedGuard<'a, T: 'a + RootKind> {
root: &'a mut Rooted<T>,
}

impl<'a, T: 'a + RootKind + Initialize> RootedGuard<'a, T> {
impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
pub fn new(cx: *mut JSContext, root: &'a mut Rooted<T>, initial: T) -> Self {
root.ptr = initial;
root.ptr.write(initial);
unsafe {
root.add_to_root_stack(cx);
}
RootedGuard { root }
}

pub fn handle(&'a self) -> Handle<'a, T> {
Handle::new(&self.root.ptr)
Handle::new(&self)
}

pub fn handle_mut(&mut self) -> MutableHandle<T> {
unsafe { MutableHandle::from_marked_location(&mut self.root.ptr) }
unsafe { MutableHandle::from_marked_location(self.deref_mut()) }
}

pub fn get(&self) -> T
where
T: Copy,
{
self.root.ptr
// SAFETY: The rooted value is initialized as long as we exist
unsafe { self.root.ptr.assume_init() }
}

pub fn set(&mut self, v: T) {
self.root.ptr = v;
// SAFETY: The rooted value is initialized as long as we exist
unsafe {
// Make sure the drop impl for T is called
self.root.ptr.assume_init_drop()
}
self.root.ptr.write(v);
}
}

impl<'a, T: 'a + RootKind + Initialize> Deref for RootedGuard<'a, T> {
impl<'a, T: 'a + RootKind> Deref for RootedGuard<'a, T> {
type Target = T;
fn deref(&self) -> &T {
&self.root.ptr
// SAFETY: The rooted value is initialized as long as we exist
unsafe { self.root.ptr.assume_init_ref() }
}
}

impl<'a, T: 'a + RootKind + Initialize> DerefMut for RootedGuard<'a, T> {
impl<'a, T: 'a + RootKind> DerefMut for RootedGuard<'a, T> {
fn deref_mut(&mut self) -> &mut T {
&mut self.root.ptr
// SAFETY: The rooted value is initialized as long as we exist
unsafe { self.root.ptr.assume_init_mut() }
}
}

impl<'a, T: 'a + RootKind + Initialize> Drop for RootedGuard<'a, T> {
impl<'a, T: 'a + RootKind> Drop for RootedGuard<'a, T> {
fn drop(&mut self) {
// SAFETY:
// All implementations are expected to return meaningful defaults that
// do not contain non-default GC pointers.
if let Some(val) = unsafe { T::initial() } {
self.root.ptr = val;
// SAFETY: The rooted value is initialized as long as we exist
unsafe {
// Make sure the drop impl for T is called
self.root.ptr.assume_init_drop()
}
self.root.ptr = MaybeUninit::zeroed();

unsafe {
self.root.remove_from_root_stack();
}
Expand Down

0 comments on commit 87cabf4

Please sign in to comment.