mirror of
https://github.com/servo/servo.git
synced 2025-06-06 16:45:39 +00:00
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:
parent
2328145c25
commit
a85241e635
11 changed files with 33 additions and 106 deletions
|
@ -30,7 +30,7 @@ linker = "lld-link.exe"
|
||||||
|
|
||||||
[env]
|
[env]
|
||||||
MACOSX_DEPLOYMENT_TARGET = "13.0"
|
MACOSX_DEPLOYMENT_TARGET = "13.0"
|
||||||
RUSTC_BOOTSTRAP = "crown,script,style_tests"
|
RUSTC_BOOTSTRAP = "crown,script,style_tests,mozjs"
|
||||||
|
|
||||||
[build]
|
[build]
|
||||||
rustdocflags = ["--document-private-items"]
|
rustdocflags = ["--document-private-items"]
|
||||||
|
|
6
Cargo.lock
generated
6
Cargo.lock
generated
|
@ -4476,7 +4476,7 @@ dependencies = [
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mozjs"
|
name = "mozjs"
|
||||||
version = "0.14.1"
|
version = "0.14.1"
|
||||||
source = "git+https://github.com/servo/mozjs#18ab014e77eee726545ed544ea83d555bdfa46a3"
|
source = "git+https://github.com/servo/mozjs#d6c3bd9a9a86a53c627355241b015fa7768ed688"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"cc",
|
"cc",
|
||||||
|
@ -4488,8 +4488,8 @@ dependencies = [
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "mozjs_sys"
|
name = "mozjs_sys"
|
||||||
version = "0.128.3-5"
|
version = "0.128.3-6"
|
||||||
source = "git+https://github.com/servo/mozjs#18ab014e77eee726545ed544ea83d555bdfa46a3"
|
source = "git+https://github.com/servo/mozjs#d6c3bd9a9a86a53c627355241b015fa7768ed688"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bindgen",
|
"bindgen",
|
||||||
"cc",
|
"cc",
|
||||||
|
|
|
@ -72,7 +72,7 @@ image = { workspace = true }
|
||||||
indexmap = { workspace = true }
|
indexmap = { workspace = true }
|
||||||
ipc-channel = { workspace = true }
|
ipc-channel = { workspace = true }
|
||||||
itertools = { 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" }
|
jstraceable_derive = { path = "../jstraceable_derive" }
|
||||||
keyboard-types = { workspace = true }
|
keyboard-types = { workspace = true }
|
||||||
libc = { workspace = true }
|
libc = { workspace = true }
|
||||||
|
|
|
@ -8170,7 +8170,10 @@ class CGIterableMethodGenerator(CGGeneric):
|
||||||
rooted!(in(*cx) let arg0 = ObjectValue(arg0));
|
rooted!(in(*cx) let arg0 = ObjectValue(arg0));
|
||||||
rooted!(in(*cx) let mut call_arg1 = UndefinedValue());
|
rooted!(in(*cx) let mut call_arg1 = UndefinedValue());
|
||||||
rooted!(in(*cx) let mut call_arg2 = 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());
|
rooted!(in(*cx) let mut ignoredReturnVal = UndefinedValue());
|
||||||
|
|
||||||
// This has to be a while loop since get_iterable_length() may change during
|
// 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());
|
(*this).get_key_at_index(i).to_jsval(*cx, call_arg2.handle_mut());
|
||||||
call_args[0] = call_arg1.handle().get();
|
call_args[0] = call_arg1.handle().get();
|
||||||
call_args[1] = call_arg2.handle().get();
|
call_args[1] = call_arg2.handle().get();
|
||||||
let call_args = HandleValueArray { length_: 3, elements_: call_args.as_ptr() };
|
let call_args_handle = HandleValueArray::from(&call_args);
|
||||||
if !Call(*cx, arg1, arg0.handle(), &call_args,
|
if !Call(*cx, arg1, arg0.handle(), &call_args_handle,
|
||||||
ignoredReturnVal.handle_mut()) {
|
ignoredReturnVal.handle_mut()) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -486,7 +486,7 @@ pub unsafe fn cross_origin_get(
|
||||||
*cx,
|
*cx,
|
||||||
receiver,
|
receiver,
|
||||||
getter_jsval.handle().into(),
|
getter_jsval.handle().into(),
|
||||||
&jsapi::HandleValueArray::new(),
|
&jsapi::HandleValueArray::empty(),
|
||||||
vp,
|
vp,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,6 +40,7 @@ use std::ops::{Deref, DerefMut};
|
||||||
use indexmap::IndexMap;
|
use indexmap::IndexMap;
|
||||||
/// A trait to allow tracing (only) DOM objects.
|
/// A trait to allow tracing (only) DOM objects.
|
||||||
pub use js::gc::Traceable as JSTraceable;
|
pub use js::gc::Traceable as JSTraceable;
|
||||||
|
pub use js::gc::{RootableVec, RootedVec};
|
||||||
use js::glue::{CallObjectTracer, CallScriptTracer, CallStringTracer, CallValueTracer};
|
use js::glue::{CallObjectTracer, CallScriptTracer, CallStringTracer, CallValueTracer};
|
||||||
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSScript, JSString, JSTracer, TraceKind};
|
use js::jsapi::{GCTraceKindToAscii, Heap, JSObject, JSScript, JSString, JSTracer, TraceKind};
|
||||||
use js::jsval::JSVal;
|
use js::jsval::JSVal;
|
||||||
|
@ -60,7 +61,6 @@ use crate::dom::bindings::cell::DomRefCell;
|
||||||
use crate::dom::bindings::error::Error;
|
use crate::dom::bindings::error::Error;
|
||||||
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
|
use crate::dom::bindings::refcounted::{Trusted, TrustedPromise};
|
||||||
use crate::dom::bindings::reflector::{DomObject, Reflector};
|
use crate::dom::bindings::reflector::{DomObject, Reflector};
|
||||||
use crate::dom::bindings::root::{Dom, DomRoot};
|
|
||||||
use crate::dom::bindings::str::{DOMString, USVString};
|
use crate::dom::bindings::str::{DOMString, USVString};
|
||||||
use crate::dom::htmlimageelement::SourceSet;
|
use crate::dom::htmlimageelement::SourceSet;
|
||||||
use crate::dom::htmlmediaelement::HTMLMediaElementFetchContext;
|
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
|
/// Roots any JSTraceable thing
|
||||||
///
|
///
|
||||||
/// If you have a valid DomObject, use DomRoot.
|
/// If you have a valid DomObject, use DomRoot.
|
||||||
|
@ -548,75 +545,3 @@ impl<T: JSTraceable> DerefMut for RootedTraceableBox<T> {
|
||||||
self.0.deref_mut()
|
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
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -723,7 +723,7 @@ impl CustomElementDefinition {
|
||||||
{
|
{
|
||||||
// Go into the constructor's realm
|
// Go into the constructor's realm
|
||||||
let _ac = JSAutoRealm::new(*cx, self.constructor.callback());
|
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()) } {
|
if unsafe { !Construct1(*cx, constructor.handle(), &args, element.handle_mut()) } {
|
||||||
return Err(Error::JSFailed);
|
return Err(Error::JSFailed);
|
||||||
}
|
}
|
||||||
|
@ -908,7 +908,7 @@ fn run_upgrade_constructor(
|
||||||
|
|
||||||
// Go into the constructor's realm
|
// Go into the constructor's realm
|
||||||
let _ac = JSAutoRealm::new(*cx, constructor.callback());
|
let _ac = JSAutoRealm::new(*cx, constructor.callback());
|
||||||
let args = HandleValueArray::new();
|
let args = HandleValueArray::empty();
|
||||||
// Step 8.2
|
// Step 8.2
|
||||||
if unsafe {
|
if unsafe {
|
||||||
!Construct1(
|
!Construct1(
|
||||||
|
|
|
@ -78,7 +78,7 @@ impl GamepadButtonList {
|
||||||
GamepadButton::new(global, false, false), // Right button in left cluster
|
GamepadButton::new(global, false, false), // Right button in left cluster
|
||||||
GamepadButton::new(global, false, false), // Center button in center 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())
|
Self::new(global, buttons.r())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -2139,7 +2139,7 @@ impl Node {
|
||||||
Node::adopt(node, &parent.owner_doc());
|
Node::adopt(node, &parent.owner_doc());
|
||||||
}
|
}
|
||||||
// Step 2.
|
// Step 2.
|
||||||
rooted_vec!(let removed_nodes <- parent.children());
|
rooted_vec!(let removed_nodes <- parent.children().map(|c| DomRoot::as_traced(&c)));
|
||||||
// Step 3.
|
// Step 3.
|
||||||
rooted_vec!(let mut added_nodes);
|
rooted_vec!(let mut added_nodes);
|
||||||
let added_nodes = if let Some(node) = node.as_ref() {
|
let added_nodes = if let Some(node) = node.as_ref() {
|
||||||
|
|
|
@ -268,7 +268,7 @@ impl PaintWorkletGlobalScope {
|
||||||
Entry::Occupied(entry) => paint_instance.set(entry.get().get()),
|
Entry::Occupied(entry) => paint_instance.set(entry.get().get()),
|
||||||
Entry::Vacant(entry) => {
|
Entry::Vacant(entry) => {
|
||||||
// Step 5.2-5.3
|
// Step 5.2-5.3
|
||||||
let args = HandleValueArray::new();
|
let args = HandleValueArray::empty();
|
||||||
rooted!(in(*cx) let mut result = null_mut::<JSObject>());
|
rooted!(in(*cx) let mut result = null_mut::<JSObject>());
|
||||||
unsafe {
|
unsafe {
|
||||||
Construct1(*cx, class_constructor.handle(), &args, result.handle_mut());
|
Construct1(*cx, class_constructor.handle(), &args, result.handle_mut());
|
||||||
|
@ -306,23 +306,22 @@ impl PaintWorkletGlobalScope {
|
||||||
// TODO: Step 10
|
// TODO: Step 10
|
||||||
// Steps 11-12
|
// Steps 11-12
|
||||||
debug!("Invoking paint function {}.", name);
|
debug!("Invoking paint function {}.", name);
|
||||||
rooted_vec!(let arguments_values <- arguments.iter().cloned()
|
rooted_vec!(let mut arguments_values);
|
||||||
.map(|argument| CSSStyleValue::new(self.upcast(), argument)));
|
for argument in arguments {
|
||||||
let arguments_value_vec: Vec<JSVal> = arguments_values
|
let style_value = CSSStyleValue::new(self.upcast(), argument.clone());
|
||||||
.iter()
|
arguments_values.push(ObjectValue(style_value.reflector().get_jsobject().get()));
|
||||||
.map(|argument| ObjectValue(argument.reflector().get_jsobject().get()))
|
}
|
||||||
.collect();
|
let arguments_value_array = HandleValueArray::from(&arguments_values);
|
||||||
let arguments_value_array =
|
|
||||||
unsafe { HandleValueArray::from_rooted_slice(&arguments_value_vec) };
|
|
||||||
rooted!(in(*cx) let argument_object = unsafe { NewArrayObject(*cx, &arguments_value_array) });
|
rooted!(in(*cx) let argument_object = unsafe { NewArrayObject(*cx, &arguments_value_array) });
|
||||||
|
|
||||||
let args_slice = [
|
rooted_vec!(let mut callback_args);
|
||||||
ObjectValue(rendering_context.reflector().get_jsobject().get()),
|
callback_args.push(ObjectValue(
|
||||||
ObjectValue(paint_size.reflector().get_jsobject().get()),
|
rendering_context.reflector().get_jsobject().get(),
|
||||||
ObjectValue(properties.reflector().get_jsobject().get()),
|
));
|
||||||
ObjectValue(argument_object.get()),
|
callback_args.push(ObjectValue(paint_size.reflector().get_jsobject().get()));
|
||||||
];
|
callback_args.push(ObjectValue(properties.reflector().get_jsobject().get()));
|
||||||
let args = unsafe { HandleValueArray::from_rooted_slice(&args_slice) };
|
callback_args.push(ObjectValue(argument_object.get()));
|
||||||
|
let args = HandleValueArray::from(&callback_args);
|
||||||
|
|
||||||
rooted!(in(*cx) let mut result = UndefinedValue());
|
rooted!(in(*cx) let mut result = UndefinedValue());
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|
|
@ -255,7 +255,7 @@ pub unsafe fn jsval_to_webdriver(
|
||||||
cx,
|
cx,
|
||||||
object.handle(),
|
object.handle(),
|
||||||
name.as_ptr(),
|
name.as_ptr(),
|
||||||
&HandleValueArray::new(),
|
&HandleValueArray::empty(),
|
||||||
value.handle_mut(),
|
value.handle_mut(),
|
||||||
) {
|
) {
|
||||||
jsval_to_webdriver(cx, global_scope, value.handle())
|
jsval_to_webdriver(cx, global_scope, value.handle())
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue