Skip to content

Commit 87cabf4

Browse files
authored
Indicate to the compiler that Rooted<T> may be uninitialized (#551)
Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
1 parent f5ed6f9 commit 87cabf4

File tree

4 files changed

+38
-23
lines changed

4 files changed

+38
-23
lines changed

mozjs-sys/src/jsgc.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ pub trait Rootable: crate::trace::Traceable + Sized {
104104
unsafe extern "C" fn trace(this: *mut c_void, trc: *mut JSTracer, _name: *const c_char) {
105105
let rooted = this as *mut Rooted<Self>;
106106
let rooted = rooted.as_mut().unwrap();
107-
<Self as crate::trace::Traceable>::trace(&mut rooted.ptr, trc);
107+
<Self as crate::trace::Traceable>::trace(rooted.ptr.assume_init_mut(), trc);
108108
}
109109
}
110110

@@ -132,7 +132,11 @@ pub struct RootedBase {
132132
pub struct Rooted<T: RootKind> {
133133
pub vtable: T::Vtable,
134134
pub base: RootedBase,
135-
pub ptr: T,
135+
136+
/// The rooted value
137+
///
138+
/// This will be initialied iff there is a `RootedGuard` for this `Rooted`
139+
pub ptr: mem::MaybeUninit<T>,
136140
}
137141

138142
/// Trait that provides a GC-safe default value for the given type, if one exists.

mozjs-sys/src/jsimpls.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use crate::jsid::VoidId;
2525
use crate::jsval::{JSVal, UndefinedValue};
2626

2727
use std::marker::PhantomData;
28+
use std::mem;
2829
use std::ops::Deref;
2930
use std::ops::DerefMut;
3031
use std::ptr;
@@ -143,7 +144,7 @@ impl<const N: usize> From<&Rooted<ValueArray<N>>> for JS::HandleValueArray {
143144
fn from(array: &Rooted<ValueArray<N>>) -> JS::HandleValueArray {
144145
JS::HandleValueArray {
145146
length_: N,
146-
elements_: unsafe { array.ptr.get_ptr() },
147+
elements_: unsafe { array.ptr.assume_init_ref().get_ptr() },
147148
}
148149
}
149150
}
@@ -435,7 +436,7 @@ impl<T: RootKind> JS::Rooted<T> {
435436
stack: ptr::null_mut(),
436437
prev: ptr::null_mut(),
437438
},
438-
ptr: unsafe { std::mem::zeroed() },
439+
ptr: mem::MaybeUninit::zeroed(),
439440
}
440441
}
441442

mozjs/src/conversions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -713,7 +713,7 @@ impl<C: Clone, T: FromJSValConvertible<Config = C>> FromJSValConvertible for Vec
713713
return Err(());
714714
}
715715

716-
if iterator.iterator.ptr.is_null() {
716+
if iterator.iterator.ptr.assume_init_ref().is_null() {
717717
return Ok(ConversionResult::Failure("Value is not iterable".into()));
718718
}
719719

mozjs/src/gc/root.rs

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::marker::PhantomData;
2+
use std::mem::MaybeUninit;
23
use std::ops::{Deref, DerefMut};
34
use std::ptr;
45

56
use crate::jsapi::{jsid, JSContext, JSFunction, JSObject, JSScript, JSString, Symbol, Value, JS};
6-
use mozjs_sys::jsgc::{Initialize, RootKind, Rooted};
7+
use mozjs_sys::jsgc::{RootKind, Rooted};
78

89
use crate::jsapi::Handle as RawHandle;
910
use crate::jsapi::HandleValue as RawHandleValue;
@@ -19,60 +20,69 @@ use mozjs_sys::jsgc::ValueArray;
1920
feature = "crown",
2021
crown::unrooted_must_root_lint::allow_unrooted_interior
2122
)]
22-
pub struct RootedGuard<'a, T: 'a + RootKind + Initialize> {
23+
pub struct RootedGuard<'a, T: 'a + RootKind> {
2324
root: &'a mut Rooted<T>,
2425
}
2526

26-
impl<'a, T: 'a + RootKind + Initialize> RootedGuard<'a, T> {
27+
impl<'a, T: 'a + RootKind> RootedGuard<'a, T> {
2728
pub fn new(cx: *mut JSContext, root: &'a mut Rooted<T>, initial: T) -> Self {
28-
root.ptr = initial;
29+
root.ptr.write(initial);
2930
unsafe {
3031
root.add_to_root_stack(cx);
3132
}
3233
RootedGuard { root }
3334
}
3435

3536
pub fn handle(&'a self) -> Handle<'a, T> {
36-
Handle::new(&self.root.ptr)
37+
Handle::new(&self)
3738
}
3839

3940
pub fn handle_mut(&mut self) -> MutableHandle<T> {
40-
unsafe { MutableHandle::from_marked_location(&mut self.root.ptr) }
41+
unsafe { MutableHandle::from_marked_location(self.deref_mut()) }
4142
}
4243

4344
pub fn get(&self) -> T
4445
where
4546
T: Copy,
4647
{
47-
self.root.ptr
48+
// SAFETY: The rooted value is initialized as long as we exist
49+
unsafe { self.root.ptr.assume_init() }
4850
}
4951

5052
pub fn set(&mut self, v: T) {
51-
self.root.ptr = v;
53+
// SAFETY: The rooted value is initialized as long as we exist
54+
unsafe {
55+
// Make sure the drop impl for T is called
56+
self.root.ptr.assume_init_drop()
57+
}
58+
self.root.ptr.write(v);
5259
}
5360
}
5461

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

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

68-
impl<'a, T: 'a + RootKind + Initialize> Drop for RootedGuard<'a, T> {
77+
impl<'a, T: 'a + RootKind> Drop for RootedGuard<'a, T> {
6978
fn drop(&mut self) {
70-
// SAFETY:
71-
// All implementations are expected to return meaningful defaults that
72-
// do not contain non-default GC pointers.
73-
if let Some(val) = unsafe { T::initial() } {
74-
self.root.ptr = val;
79+
// SAFETY: The rooted value is initialized as long as we exist
80+
unsafe {
81+
// Make sure the drop impl for T is called
82+
self.root.ptr.assume_init_drop()
7583
}
84+
self.root.ptr = MaybeUninit::zeroed();
85+
7686
unsafe {
7787
self.root.remove_from_root_stack();
7888
}

0 commit comments

Comments
 (0)