Replace unsafe uses of HandleValueArray. (#34588)

* Replace unsafe uses of HandleValueArray.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Fix clippy lint.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2024-12-13 18:21:55 -05:00 committed by GitHub
parent 2328145c25
commit a85241e635
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 33 additions and 106 deletions

View file

@ -30,7 +30,7 @@ linker = "lld-link.exe"
[env]
MACOSX_DEPLOYMENT_TARGET = "13.0"
RUSTC_BOOTSTRAP = "crown,script,style_tests"
RUSTC_BOOTSTRAP = "crown,script,style_tests,mozjs"
[build]
rustdocflags = ["--document-private-items"]

6
Cargo.lock generated
View file

@ -4476,7 +4476,7 @@ dependencies = [
[[package]]
name = "mozjs"
version = "0.14.1"
source = "git+https://github.com/servo/mozjs#18ab014e77eee726545ed544ea83d555bdfa46a3"
source = "git+https://github.com/servo/mozjs#d6c3bd9a9a86a53c627355241b015fa7768ed688"
dependencies = [
"bindgen",
"cc",
@ -4488,8 +4488,8 @@ dependencies = [
[[package]]
name = "mozjs_sys"
version = "0.128.3-5"
source = "git+https://github.com/servo/mozjs#18ab014e77eee726545ed544ea83d555bdfa46a3"
version = "0.128.3-6"
source = "git+https://github.com/servo/mozjs#d6c3bd9a9a86a53c627355241b015fa7768ed688"
dependencies = [
"bindgen",
"cc",

View file

@ -72,7 +72,7 @@ image = { workspace = true }
indexmap = { workspace = true }
ipc-channel = { workspace = true }
itertools = { workspace = true }
js = { package = "mozjs", git = "https://github.com/servo/mozjs", features = ["streams"] }
js = { package = "mozjs", git = "https://github.com/servo/mozjs", features = ["streams", "crown"] }
jstraceable_derive = { path = "../jstraceable_derive" }
keyboard-types = { workspace = true }
libc = { workspace = true }

View file

@ -8170,7 +8170,10 @@ class CGIterableMethodGenerator(CGGeneric):
rooted!(in(*cx) let arg0 = ObjectValue(arg0));
rooted!(in(*cx) let mut call_arg1 = UndefinedValue());
rooted!(in(*cx) let mut call_arg2 = UndefinedValue());
let mut call_args = [UndefinedValue(), UndefinedValue(), ObjectValue(*_obj)];
rooted_vec!(let mut call_args);
call_args.push(UndefinedValue());
call_args.push(UndefinedValue());
call_args.push(ObjectValue(*_obj));
rooted!(in(*cx) let mut ignoredReturnVal = UndefinedValue());
// This has to be a while loop since get_iterable_length() may change during
@ -8186,8 +8189,8 @@ class CGIterableMethodGenerator(CGGeneric):
(*this).get_key_at_index(i).to_jsval(*cx, call_arg2.handle_mut());
call_args[0] = call_arg1.handle().get();
call_args[1] = call_arg2.handle().get();
let call_args = HandleValueArray { length_: 3, elements_: call_args.as_ptr() };
if !Call(*cx, arg1, arg0.handle(), &call_args,
let call_args_handle = HandleValueArray::from(&call_args);
if !Call(*cx, arg1, arg0.handle(), &call_args_handle,
ignoredReturnVal.handle_mut()) {
return false;
}

View file

@ -486,7 +486,7 @@ pub unsafe fn cross_origin_get(
*cx,
receiver,
getter_jsval.handle().into(),
&jsapi::HandleValueArray::new(),
&jsapi::HandleValueArray::empty(),
vp,
)
}

View file

@ -40,6 +40,7 @@ use std::ops::{Deref, DerefMut};
use indexmap::IndexMap;
/// A trait to allow tracing (only) DOM objects.
pub use js::gc::Traceable as JSTraceable;
pub use js::gc::{RootableVec, RootedVec};
use js::glue::{CallObjectTracer, CallScriptTracer, CallStringTracer, CallValueTracer};
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSScript, JSString, JSTracer, TraceKind};
use js::jsval::JSVal;
@ -60,7 +61,6 @@ use crate::dom::bindings::cell::DomRefCell;
use crate::dom::bindings::error::Error;
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
use crate::dom::bindings::reflector::{DomObject, Reflector};
use crate::dom::bindings::root::{Dom, DomRoot};
use crate::dom::bindings::str::{DOMString, USVString};
use crate::dom::htmlimageelement::SourceSet;
use crate::dom::htmlmediaelement::HTMLMediaElementFetchContext;
@ -479,9 +479,6 @@ where
}
}
/// Holds a set of JSTraceables that need to be rooted
pub use js::gc::RootedTraceableSet;
/// Roots any JSTraceable thing
///
/// If you have a valid DomObject, use DomRoot.
@ -548,75 +545,3 @@ impl<T: JSTraceable> DerefMut for RootedTraceableBox<T> {
self.0.deref_mut()
}
}
/// A vector of items to be rooted with `RootedVec`.
/// Guaranteed to be empty when not rooted.
/// Usage: `rooted_vec!(let mut v);` or if you have an
/// iterator of `DomRoot`s, `rooted_vec!(let v <- iterator);`.
#[allow(crown::unrooted_must_root)]
#[derive(JSTraceable)]
#[crown::unrooted_must_root_lint::allow_unrooted_interior]
pub struct RootableVec<T: JSTraceable> {
v: Vec<T>,
}
impl<T: JSTraceable> RootableVec<T> {
/// Create a vector of items of type T that can be rooted later.
pub fn new_unrooted() -> RootableVec<T> {
RootableVec { v: vec![] }
}
}
/// A vector of items that are rooted for the lifetime 'a.
#[crown::unrooted_must_root_lint::allow_unrooted_interior]
pub struct RootedVec<'a, T: 'static + JSTraceable> {
root: &'a mut RootableVec<T>,
}
impl<'a, T: 'static + JSTraceable> RootedVec<'a, T> {
/// Create a vector of items of type T that is rooted for
/// the lifetime of this struct
pub fn new(root: &'a mut RootableVec<T>) -> Self {
unsafe {
RootedTraceableSet::add(root);
}
RootedVec { root }
}
}
impl<'a, T: 'static + JSTraceable + DomObject> RootedVec<'a, Dom<T>> {
/// Create a vector of items of type `Dom<T>` that is rooted for
/// the lifetime of this struct
pub fn from_iter<I>(root: &'a mut RootableVec<Dom<T>>, iter: I) -> Self
where
I: Iterator<Item = DomRoot<T>>,
{
unsafe {
RootedTraceableSet::add(root);
}
root.v.extend(iter.map(|item| Dom::from_ref(&*item)));
RootedVec { root }
}
}
impl<'a, T: JSTraceable + 'static> Drop for RootedVec<'a, T> {
fn drop(&mut self) {
self.clear();
unsafe {
RootedTraceableSet::remove(self.root);
}
}
}
impl<'a, T: JSTraceable> Deref for RootedVec<'a, T> {
type Target = Vec<T>;
fn deref(&self) -> &Vec<T> {
&self.root.v
}
}
impl<'a, T: JSTraceable> DerefMut for RootedVec<'a, T> {
fn deref_mut(&mut self) -> &mut Vec<T> {
&mut self.root.v
}
}

View file

@ -723,7 +723,7 @@ impl CustomElementDefinition {
{
// Go into the constructor's realm
let _ac = JSAutoRealm::new(*cx, self.constructor.callback());
let args = HandleValueArray::new();
let args = HandleValueArray::empty();
if unsafe { !Construct1(*cx, constructor.handle(), &args, element.handle_mut()) } {
return Err(Error::JSFailed);
}
@ -908,7 +908,7 @@ fn run_upgrade_constructor(
// Go into the constructor's realm
let _ac = JSAutoRealm::new(*cx, constructor.callback());
let args = HandleValueArray::new();
let args = HandleValueArray::empty();
// Step 8.2
if unsafe {
!Construct1(

View file

@ -78,7 +78,7 @@ impl GamepadButtonList {
GamepadButton::new(global, false, false), // Right button in left cluster
GamepadButton::new(global, false, false), // Center button in center cluster
];
rooted_vec!(let buttons <- standard_buttons.iter().map(|button| DomRoot::from_ref(&**button)));
rooted_vec!(let buttons <- standard_buttons.iter().map(DomRoot::as_traced));
Self::new(global, buttons.r())
}
}

View file

@ -2139,7 +2139,7 @@ impl Node {
Node::adopt(node, &parent.owner_doc());
}
// Step 2.
rooted_vec!(let removed_nodes <- parent.children());
rooted_vec!(let removed_nodes <- parent.children().map(|c| DomRoot::as_traced(&c)));
// Step 3.
rooted_vec!(let mut added_nodes);
let added_nodes = if let Some(node) = node.as_ref() {

View file

@ -268,7 +268,7 @@ impl PaintWorkletGlobalScope {
Entry::Occupied(entry) => paint_instance.set(entry.get().get()),
Entry::Vacant(entry) => {
// Step 5.2-5.3
let args = HandleValueArray::new();
let args = HandleValueArray::empty();
rooted!(in(*cx) let mut result = null_mut::<JSObject>());
unsafe {
Construct1(*cx, class_constructor.handle(), &args, result.handle_mut());
@ -306,23 +306,22 @@ impl PaintWorkletGlobalScope {
// TODO: Step 10
// Steps 11-12
debug!("Invoking paint function {}.", name);
rooted_vec!(let arguments_values <- arguments.iter().cloned()
.map(|argument| CSSStyleValue::new(self.upcast(), argument)));
let arguments_value_vec: Vec<JSVal> = arguments_values
.iter()
.map(|argument| ObjectValue(argument.reflector().get_jsobject().get()))
.collect();
let arguments_value_array =
unsafe { HandleValueArray::from_rooted_slice(&arguments_value_vec) };
rooted_vec!(let mut arguments_values);
for argument in arguments {
let style_value = CSSStyleValue::new(self.upcast(), argument.clone());
arguments_values.push(ObjectValue(style_value.reflector().get_jsobject().get()));
}
let arguments_value_array = HandleValueArray::from(&arguments_values);
rooted!(in(*cx) let argument_object = unsafe { NewArrayObject(*cx, &arguments_value_array) });
let args_slice = [
ObjectValue(rendering_context.reflector().get_jsobject().get()),
ObjectValue(paint_size.reflector().get_jsobject().get()),
ObjectValue(properties.reflector().get_jsobject().get()),
ObjectValue(argument_object.get()),
];
let args = unsafe { HandleValueArray::from_rooted_slice(&args_slice) };
rooted_vec!(let mut callback_args);
callback_args.push(ObjectValue(
rendering_context.reflector().get_jsobject().get(),
));
callback_args.push(ObjectValue(paint_size.reflector().get_jsobject().get()));
callback_args.push(ObjectValue(properties.reflector().get_jsobject().get()));
callback_args.push(ObjectValue(argument_object.get()));
let args = HandleValueArray::from(&callback_args);
rooted!(in(*cx) let mut result = UndefinedValue());
unsafe {

View file

@ -255,7 +255,7 @@ pub unsafe fn jsval_to_webdriver(
cx,
object.handle(),
name.as_ptr(),
&HandleValueArray::new(),
&HandleValueArray::empty(),
value.handle_mut(),
) {
jsval_to_webdriver(cx, global_scope, value.handle())