Use explicit reborrows with mozjs::MutableHandle (#35892)

* Explicitly reborrow MutableHandles

Signed-off-by: Greg Morenz <greg-morenz@droid.cafe>

* Unify jsapi_wrappers

Signed-off-by: Greg Morenz <greg-morenz@droid.cafe>

* Format mozjs changes

Signed-off-by: Greg Morenz <greg-morenz@droid.cafe>

* Update mozjs version

Signed-off-by: Greg Morenz <greg-morenz@droid.cafe>

---------

Signed-off-by: Greg Morenz <greg-morenz@droid.cafe>
This commit is contained in:
Greg Morenz 2025-03-22 21:23:52 -04:00 committed by GitHub
parent 40270cb626
commit 4ecf0909e5
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
14 changed files with 67 additions and 44 deletions

23
Cargo.lock generated
View file

@ -520,7 +520,7 @@ dependencies = [
"bitflags 2.9.0", "bitflags 2.9.0",
"cexpr", "cexpr",
"clang-sys", "clang-sys",
"itertools 0.13.0", "itertools 0.10.5",
"proc-macro2", "proc-macro2",
"quote", "quote",
"regex", "regex",
@ -1065,7 +1065,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c" checksum = "117725a109d387c937a1533ce01b450cbde6b88abceea8473c4d7a85853cda3c"
dependencies = [ dependencies = [
"lazy_static", "lazy_static",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2031,7 +2031,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d" checksum = "33d852cb9b869c2a9b3df2f71a3074817f01e1844f839a144f5fcef059a4eb5d"
dependencies = [ dependencies = [
"libc", "libc",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -2557,7 +2557,7 @@ dependencies = [
"gobject-sys", "gobject-sys",
"libc", "libc",
"system-deps", "system-deps",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -4017,7 +4017,7 @@ checksum = "e04d7f318608d35d4b61ddd75cbdaee86b023ebe2bd5a66ee0915f0bf93095a9"
dependencies = [ dependencies = [
"hermit-abi 0.5.0", "hermit-abi 0.5.0",
"libc", "libc",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -4702,11 +4702,10 @@ dependencies = [
[[package]] [[package]]
name = "mozjs" name = "mozjs"
version = "0.14.1" version = "0.14.1"
source = "git+https://github.com/servo/mozjs#87cabf4e9ddf9fafe19713a3d6bc8c5e6105544c" source = "git+https://github.com/servo/mozjs#26c35182c1b2b7fb2fbfa93b82669533d92c3a60"
dependencies = [ dependencies = [
"bindgen 0.71.1", "bindgen 0.71.1",
"cc", "cc",
"lazy_static",
"libc", "libc",
"log", "log",
"mozjs_sys", "mozjs_sys",
@ -4714,8 +4713,8 @@ dependencies = [
[[package]] [[package]]
name = "mozjs_sys" name = "mozjs_sys"
version = "0.128.6-1" version = "0.128.9-0"
source = "git+https://github.com/servo/mozjs#87cabf4e9ddf9fafe19713a3d6bc8c5e6105544c" source = "git+https://github.com/servo/mozjs#26c35182c1b2b7fb2fbfa93b82669533d92c3a60"
dependencies = [ dependencies = [
"bindgen 0.71.1", "bindgen 0.71.1",
"cc", "cc",
@ -6190,7 +6189,7 @@ dependencies = [
"errno", "errno",
"libc", "libc",
"linux-raw-sys", "linux-raw-sys",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -7528,7 +7527,7 @@ dependencies = [
"getrandom", "getrandom",
"once_cell", "once_cell",
"rustix", "rustix",
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]
@ -8837,7 +8836,7 @@ version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"windows-sys 0.59.0", "windows-sys 0.52.0",
] ]
[[package]] [[package]]

View file

@ -620,13 +620,15 @@ unsafe impl<T> crate::dom::bindings::trace::JSTraceable for HeapBufferSource<T>
pub(crate) fn create_buffer_source<T>( pub(crate) fn create_buffer_source<T>(
cx: JSContext, cx: JSContext,
data: &[T::Element], data: &[T::Element],
dest: MutableHandleObject, mut dest: MutableHandleObject,
_can_gc: CanGc, _can_gc: CanGc,
) -> Result<TypedArray<T, *mut JSObject>, ()> ) -> Result<TypedArray<T, *mut JSObject>, ()>
where where
T: TypedArrayElement + TypedArrayElementCreator, T: TypedArrayElement + TypedArrayElementCreator,
{ {
let res = unsafe { TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Slice(data), dest) }; let res = unsafe {
TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Slice(data), dest.reborrow())
};
if res.is_err() { if res.is_err() {
Err(()) Err(())
@ -638,13 +640,15 @@ where
fn create_buffer_source_with_length<T>( fn create_buffer_source_with_length<T>(
cx: JSContext, cx: JSContext,
len: usize, len: usize,
dest: MutableHandleObject, mut dest: MutableHandleObject,
_can_gc: CanGc, _can_gc: CanGc,
) -> Result<TypedArray<T, *mut JSObject>, ()> ) -> Result<TypedArray<T, *mut JSObject>, ()>
where where
T: TypedArrayElement + TypedArrayElementCreator, T: TypedArrayElement + TypedArrayElementCreator,
{ {
let res = unsafe { TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Length(len), dest) }; let res = unsafe {
TypedArray::<T, *mut JSObject>::create(*cx, CreateWith::Length(len), dest.reborrow())
};
if res.is_err() { if res.is_err() {
Err(()) Err(())

View file

@ -36,7 +36,7 @@ impl CachedFrozenArray {
} }
let array = f(); let array = f();
to_frozen_array(array.as_slice(), cx, retval, can_gc); to_frozen_array(array.as_slice(), cx, retval.reborrow(), can_gc);
// Safety: need to create the Heap value in its final memory location before setting it. // Safety: need to create the Heap value in its final memory location before setting it.
*self.frozen_value.borrow_mut() = Some(Heap::default()); *self.frozen_value.borrow_mut() = Some(Heap::default());

View file

@ -241,7 +241,7 @@ pub(crate) fn create_interface_prototype_object(
regular_properties: &[Guard<&'static [JSPropertySpec]>], regular_properties: &[Guard<&'static [JSPropertySpec]>],
constants: &[Guard<&[ConstantSpec]>], constants: &[Guard<&[ConstantSpec]>],
unscopable_names: &[&CStr], unscopable_names: &[&CStr],
rval: MutableHandleObject, mut rval: MutableHandleObject,
) { ) {
create_object( create_object(
cx, cx,
@ -251,7 +251,7 @@ pub(crate) fn create_interface_prototype_object(
regular_methods, regular_methods,
regular_properties, regular_properties,
constants, constants,
rval, rval.reborrow(),
); );
if !unscopable_names.is_empty() { if !unscopable_names.is_empty() {
@ -289,7 +289,7 @@ pub(crate) fn create_noncallback_interface_object(
name: &CStr, name: &CStr,
length: u32, length: u32,
legacy_window_alias_names: &[&CStr], legacy_window_alias_names: &[&CStr],
rval: MutableHandleObject, mut rval: MutableHandleObject,
) { ) {
create_object( create_object(
cx, cx,
@ -299,7 +299,7 @@ pub(crate) fn create_noncallback_interface_object(
static_methods, static_methods,
static_properties, static_properties,
constants, constants,
rval, rval.reborrow(),
); );
unsafe { unsafe {
assert!(JS_LinkConstructorAndPrototype( assert!(JS_LinkConstructorAndPrototype(
@ -694,7 +694,7 @@ pub(crate) fn get_desired_proto(
global.handle(), global.handle(),
ProtoOrIfaceIndex::ID(proto_id), ProtoOrIfaceIndex::ID(proto_id),
creator, creator,
desired_proto, desired_proto.reborrow(),
); );
if desired_proto.is_null() { if desired_proto.is_null() {
return Err(()); return Err(());

View file

@ -45,8 +45,17 @@ pub(crate) fn create_namespace_object(
methods: &[Guard<&'static [JSFunctionSpec]>], methods: &[Guard<&'static [JSFunctionSpec]>],
constants: &[Guard<&'static [ConstantSpec]>], constants: &[Guard<&'static [ConstantSpec]>],
name: &CStr, name: &CStr,
rval: MutableHandleObject, mut rval: MutableHandleObject,
) { ) {
create_object(cx, global, proto, &class.0, methods, &[], constants, rval); create_object(
cx,
global,
proto,
&class.0,
methods,
&[],
constants,
rval.reborrow(),
);
define_on_global_object(cx, global, name, rval.handle()); define_on_global_object(cx, global, name, rval.handle());
} }

View file

@ -172,7 +172,7 @@ pub(crate) unsafe fn ensure_expando_object(
mut expando: MutableHandleObject, mut expando: MutableHandleObject,
) { ) {
assert!(is_dom_proxy(obj.get())); assert!(is_dom_proxy(obj.get()));
get_expando_object(obj, expando); get_expando_object(obj, expando.reborrow());
if expando.is_null() { if expando.is_null() {
expando.set(JS_NewObjectWithGivenProto( expando.set(JS_NewObjectWithGivenProto(
cx, cx,

View file

@ -122,10 +122,10 @@ pub(crate) use script_bindings::utils::{DOMClass, DOMJSClass};
pub(crate) fn to_frozen_array<T: ToJSValConvertible>( pub(crate) fn to_frozen_array<T: ToJSValConvertible>(
convertibles: &[T], convertibles: &[T],
cx: SafeJSContext, cx: SafeJSContext,
rval: MutableHandleValue, mut rval: MutableHandleValue,
_can_gc: CanGc, _can_gc: CanGc,
) { ) {
unsafe { convertibles.to_jsval(*cx, rval) }; unsafe { convertibles.to_jsval(*cx, rval.reborrow()) };
rooted!(in(*cx) let obj = rval.to_object()); rooted!(in(*cx) let obj = rval.to_object());
unsafe { JS_FreezeObject(*cx, RawHandleObject::from(obj.handle())) }; unsafe { JS_FreezeObject(*cx, RawHandleObject::from(obj.handle())) };

View file

@ -139,7 +139,7 @@ impl CustomElementRegistry {
fn check_prototype( fn check_prototype(
&self, &self,
constructor: HandleObject, constructor: HandleObject,
prototype: MutableHandleValue, mut prototype: MutableHandleValue,
) -> ErrorResult { ) -> ErrorResult {
unsafe { unsafe {
// Step 10.1 // Step 10.1
@ -147,7 +147,7 @@ impl CustomElementRegistry {
*GlobalScope::get_cx(), *GlobalScope::get_cx(),
constructor, constructor,
c"prototype".as_ptr(), c"prototype".as_ptr(),
prototype, prototype.reborrow(),
) { ) {
return Err(Error::JSFailed); return Err(Error::JSFailed);
} }

View file

@ -20,7 +20,7 @@ use js::jsapi::{
NewUCRegExpObject, ObjectIsDate, RegExpFlag_Unicode, RegExpFlags, NewUCRegExpObject, ObjectIsDate, RegExpFlag_Unicode, RegExpFlags,
}; };
use js::jsval::UndefinedValue; use js::jsval::UndefinedValue;
use js::rust::jsapi_wrapped::{CheckRegExpSyntax, ExecuteRegExpNoStatics, ObjectIsRegExp}; use js::rust::wrappers::{CheckRegExpSyntax, ExecuteRegExpNoStatics, ObjectIsRegExp};
use js::rust::{HandleObject, MutableHandleObject}; use js::rust::{HandleObject, MutableHandleObject};
use net_traits::blob_url_store::get_blob_origin; use net_traits::blob_url_store::get_blob_origin;
use net_traits::filemanager_thread::FileManagerThreadMsg; use net_traits::filemanager_thread::FileManagerThreadMsg;
@ -3010,7 +3010,7 @@ fn check_js_regex_syntax(cx: SafeJSContext, pattern: &str) -> bool {
RegExpFlags { RegExpFlags {
flags_: RegExpFlag_Unicode, flags_: RegExpFlag_Unicode,
}, },
&mut exception.handle_mut(), exception.handle_mut(),
); );
if !valid { if !valid {
@ -3063,7 +3063,7 @@ fn matches_js_regex(cx: SafeJSContext, regex_obj: HandleObject, value: &str) ->
value.len(), value.len(),
&mut index, &mut index,
true, true,
&mut rval.handle_mut(), rval.handle_mut(),
); );
if ok { if ok {

View file

@ -1216,14 +1216,21 @@ impl WebGL2RenderingContextMethods<crate::DomTypeHolder> for WebGL2RenderingCont
); );
handle_potential_webgl_error!( handle_potential_webgl_error!(
self.base, self.base,
self.get_specific_fb_attachment_param(cx, &fb, target, attachment, pname, rval), self.get_specific_fb_attachment_param(
cx,
&fb,
target,
attachment,
pname,
rval.reborrow()
),
rval.set(NullValue()) rval.set(NullValue())
) )
} else { } else {
// The default framebuffer is bound to the target // The default framebuffer is bound to the target
handle_potential_webgl_error!( handle_potential_webgl_error!(
self.base, self.base,
self.get_default_fb_attachment_param(attachment, pname, rval), self.get_default_fb_attachment_param(attachment, pname, rval.reborrow()),
rval.set(NullValue()) rval.set(NullValue())
) )
} }

View file

@ -3552,7 +3552,7 @@ impl WebGLRenderingContextMethods<crate::DomTypeHolder> for WebGLRenderingContex
constants::VERTEX_ATTRIB_ARRAY_STRIDE => retval.set(Int32Value(data.stride as i32)), constants::VERTEX_ATTRIB_ARRAY_STRIDE => retval.set(Int32Value(data.stride as i32)),
constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING => unsafe { constants::VERTEX_ATTRIB_ARRAY_BUFFER_BINDING => unsafe {
if let Some(buffer) = data.buffer() { if let Some(buffer) = data.buffer() {
buffer.to_jsval(*cx, retval); buffer.to_jsval(*cx, retval.reborrow());
} else { } else {
retval.set(NullValue()); retval.set(NullValue());
} }

View file

@ -1441,7 +1441,12 @@ impl XMLHttpRequest {
let json_text = decode_to_utf16_with_bom_removal(&bytes, UTF_8); let json_text = decode_to_utf16_with_bom_removal(&bytes, UTF_8);
// Step 5 // Step 5
unsafe { unsafe {
if !JS_ParseJSON(*cx, json_text.as_ptr(), json_text.len() as u32, rval) { if !JS_ParseJSON(
*cx,
json_text.as_ptr(),
json_text.len() as u32,
rval.reborrow(),
) {
JS_ClearPendingException(*cx); JS_ClearPendingException(*cx);
return rval.set(NullValue()); return rval.set(NullValue());
} }

View file

@ -27,8 +27,7 @@ use js::jsapi::{
ThrowOnModuleEvaluationFailure, Value, ThrowOnModuleEvaluationFailure, Value,
}; };
use js::jsval::{JSVal, PrivateValue, UndefinedValue}; use js::jsval::{JSVal, PrivateValue, UndefinedValue};
use js::rust::jsapi_wrapped::JS_GetPendingException; use js::rust::wrappers::{JS_GetPendingException, JS_SetPendingException};
use js::rust::wrappers::JS_SetPendingException;
use js::rust::{ use js::rust::{
CompileOptionsWrapper, Handle, HandleObject as RustHandleObject, HandleValue, IntoHandle, CompileOptionsWrapper, Handle, HandleObject as RustHandleObject, HandleValue, IntoHandle,
MutableHandleObject as RustMutableHandleObject, transform_str_to_source_text, MutableHandleObject as RustMutableHandleObject, transform_str_to_source_text,
@ -488,7 +487,7 @@ impl ModuleTree {
warn!("fail to compile module script of {}", url); warn!("fail to compile module script of {}", url);
rooted!(in(*cx) let mut exception = UndefinedValue()); rooted!(in(*cx) let mut exception = UndefinedValue());
assert!(JS_GetPendingException(*cx, &mut exception.handle_mut())); assert!(JS_GetPendingException(*cx, exception.handle_mut()));
JS_ClearPendingException(*cx); JS_ClearPendingException(*cx);
return Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed( return Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed(
@ -531,7 +530,7 @@ impl ModuleTree {
warn!("fail to link & instantiate module"); warn!("fail to link & instantiate module");
rooted!(in(*cx) let mut exception = UndefinedValue()); rooted!(in(*cx) let mut exception = UndefinedValue());
assert!(JS_GetPendingException(*cx, &mut exception.handle_mut())); assert!(JS_GetPendingException(*cx, exception.handle_mut()));
JS_ClearPendingException(*cx); JS_ClearPendingException(*cx);
Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed( Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed(
@ -577,7 +576,7 @@ impl ModuleTree {
warn!("fail to evaluate module"); warn!("fail to evaluate module");
rooted!(in(*cx) let mut exception = UndefinedValue()); rooted!(in(*cx) let mut exception = UndefinedValue());
assert!(JS_GetPendingException(*cx, &mut exception.handle_mut())); assert!(JS_GetPendingException(*cx, exception.handle_mut()));
JS_ClearPendingException(*cx); JS_ClearPendingException(*cx);
Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed( Err(RethrowError(RootedTraceableBox::from_box(Heap::boxed(

View file

@ -6394,13 +6394,13 @@ let cx = SafeJSContext::from_ptr(cx);
{maybeCrossOriginGet} {maybeCrossOriginGet}
let proxy_lt = Handle::from_raw(proxy); let proxy_lt = Handle::from_raw(proxy);
let vp_lt = MutableHandle::from_raw(vp); let mut vp_lt = MutableHandle::from_raw(vp);
let id_lt = Handle::from_raw(id); let id_lt = Handle::from_raw(id);
let receiver_lt = Handle::from_raw(receiver); let receiver_lt = Handle::from_raw(receiver);
{getIndexedOrExpando} {getIndexedOrExpando}
let mut found = false; let mut found = false;
if !get_property_on_prototype(*cx, proxy_lt, receiver_lt, id_lt, &mut found, vp_lt) {{ if !get_property_on_prototype(*cx, proxy_lt, receiver_lt, id_lt, &mut found, vp_lt.reborrow()) {{
return false; return false;
}} }}
@ -7285,7 +7285,7 @@ impl{self.generic} Clone for {self.makeClassName(self.dictionary)}{self.genericS
memberInserts = [memberInsert(m) for m in self.memberInfo] memberInserts = [memberInsert(m) for m in self.memberInfo]
if d.parent: if d.parent:
memberInserts = [CGGeneric("self.parent.to_jsobject(cx, obj);\n")] + memberInserts memberInserts = [CGGeneric("self.parent.to_jsobject(cx, obj.reborrow());\n")] + memberInserts
selfName = self.makeClassName(d) selfName = self.makeClassName(d)
if self.membersNeedTracing(): if self.membersNeedTracing():