diff options
author | Josh Matthews <josh@joshmatthews.net> | 2024-12-13 18:21:55 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-12-13 23:21:55 +0000 |
commit | a85241e6353a912b312f45de6e9d4668c67761f8 (patch) | |
tree | 7447a7743fb8697b4e95ff6305096783d3c25cf7 | |
parent | 2328145c256b816dd9e43a54a56c9be041ce10a3 (diff) | |
download | servo-a85241e6353a912b312f45de6e9d4668c67761f8.tar.gz servo-a85241e6353a912b312f45de6e9d4668c67761f8.zip |
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>
-rw-r--r-- | .cargo/config.toml | 2 | ||||
-rw-r--r-- | Cargo.lock | 6 | ||||
-rw-r--r-- | components/script/Cargo.toml | 2 | ||||
-rw-r--r-- | components/script/dom/bindings/codegen/CodegenRust.py | 9 | ||||
-rw-r--r-- | components/script/dom/bindings/proxyhandler.rs | 2 | ||||
-rw-r--r-- | components/script/dom/bindings/trace.rs | 77 | ||||
-rw-r--r-- | components/script/dom/customelementregistry.rs | 4 | ||||
-rw-r--r-- | components/script/dom/gamepadbuttonlist.rs | 2 | ||||
-rw-r--r-- | components/script/dom/node.rs | 2 | ||||
-rw-r--r-- | components/script/dom/paintworkletglobalscope.rs | 31 | ||||
-rw-r--r-- | components/script/webdriver_handlers.rs | 2 |
11 files changed, 33 insertions, 106 deletions
diff --git a/.cargo/config.toml b/.cargo/config.toml index 81e8e9feb67..19269c29e82 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -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"] diff --git a/Cargo.lock b/Cargo.lock index 81002a32f70..7fd992d0293 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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", diff --git a/components/script/Cargo.toml b/components/script/Cargo.toml index 5975d14b678..6a9a5bf9a9c 100644 --- a/components/script/Cargo.toml +++ b/components/script/Cargo.toml @@ -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 } diff --git a/components/script/dom/bindings/codegen/CodegenRust.py b/components/script/dom/bindings/codegen/CodegenRust.py index fdb01a67a35..e25ed68d3e8 100644 --- a/components/script/dom/bindings/codegen/CodegenRust.py +++ b/components/script/dom/bindings/codegen/CodegenRust.py @@ -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; } diff --git a/components/script/dom/bindings/proxyhandler.rs b/components/script/dom/bindings/proxyhandler.rs index 7634f53c19b..91fad86a0cd 100644 --- a/components/script/dom/bindings/proxyhandler.rs +++ b/components/script/dom/bindings/proxyhandler.rs @@ -486,7 +486,7 @@ pub unsafe fn cross_origin_get( *cx, receiver, getter_jsval.handle().into(), - &jsapi::HandleValueArray::new(), + &jsapi::HandleValueArray::empty(), vp, ) } diff --git a/components/script/dom/bindings/trace.rs b/components/script/dom/bindings/trace.rs index 9e1373279ab..d7c0848987b 100644 --- a/components/script/dom/bindings/trace.rs +++ b/components/script/dom/bindings/trace.rs @@ -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 - } -} diff --git a/components/script/dom/customelementregistry.rs b/components/script/dom/customelementregistry.rs index 729088e90f0..c1e0c15a949 100644 --- a/components/script/dom/customelementregistry.rs +++ b/components/script/dom/customelementregistry.rs @@ -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( diff --git a/components/script/dom/gamepadbuttonlist.rs b/components/script/dom/gamepadbuttonlist.rs index 6a7aeb8389f..1d95eecc1a2 100644 --- a/components/script/dom/gamepadbuttonlist.rs +++ b/components/script/dom/gamepadbuttonlist.rs @@ -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()) } } diff --git a/components/script/dom/node.rs b/components/script/dom/node.rs index a7cfa0dc772..961a8ddd701 100644 --- a/components/script/dom/node.rs +++ b/components/script/dom/node.rs @@ -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() { diff --git a/components/script/dom/paintworkletglobalscope.rs b/components/script/dom/paintworkletglobalscope.rs index b83bd73b0f6..9f13f0a8337 100644 --- a/components/script/dom/paintworkletglobalscope.rs +++ b/components/script/dom/paintworkletglobalscope.rs @@ -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 { diff --git a/components/script/webdriver_handlers.rs b/components/script/webdriver_handlers.rs index 288c8fd1d7f..2cd65a354b7 100644 --- a/components/script/webdriver_handlers.rs +++ b/components/script/webdriver_handlers.rs @@ -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()) |