mirror of
https://github.com/servo/servo.git
synced 2025-08-29 17:18:23 +01:00
script: Move operations in window_named_properties::get_own_property_descriptor
& webdriver_handlers::clone_an_object
into unsafe blocks (#38951)
Testing: Covered by existing tests Part of https://github.com/servo/servo/issues/35955 Signed-off-by: Simon Wülker <simon.wuelker@arcor.de>
This commit is contained in:
parent
87fe202ded
commit
de6feb469a
3 changed files with 87 additions and 57 deletions
|
@ -467,38 +467,46 @@ unsafe fn clone_an_object(
|
||||||
// Step 2. Append value to `seen`.
|
// Step 2. Append value to `seen`.
|
||||||
seen.insert(hashable.clone());
|
seen.insert(hashable.clone());
|
||||||
|
|
||||||
let return_val = if is_array_like::<crate::DomTypeHolder>(cx, val) ||
|
let return_val = if unsafe {
|
||||||
is_arguments_object(cx, val)
|
is_array_like::<crate::DomTypeHolder>(cx, val) || is_arguments_object(cx, val)
|
||||||
{
|
} {
|
||||||
let mut result: Vec<JSValue> = Vec::new();
|
let mut result: Vec<JSValue> = Vec::new();
|
||||||
|
|
||||||
let length =
|
let get_property_result = unsafe {
|
||||||
match get_property::<u32>(cx, object_handle, "length", ConversionBehavior::Default) {
|
get_property::<u32>(cx, object_handle, "length", ConversionBehavior::Default)
|
||||||
Ok(length) => match length {
|
};
|
||||||
Some(length) => length,
|
let length = match get_property_result {
|
||||||
_ => return Err(WebDriverJSError::UnknownType),
|
Ok(length) => match length {
|
||||||
},
|
Some(length) => length,
|
||||||
Err(error) => {
|
_ => return Err(WebDriverJSError::UnknownType),
|
||||||
throw_dom_exception(
|
},
|
||||||
SafeJSContext::from_ptr(cx),
|
Err(error) => {
|
||||||
global_scope,
|
throw_dom_exception(
|
||||||
error,
|
unsafe { SafeJSContext::from_ptr(cx) },
|
||||||
CanGc::note(),
|
global_scope,
|
||||||
);
|
error,
|
||||||
return Err(WebDriverJSError::JSError);
|
CanGc::note(),
|
||||||
},
|
);
|
||||||
};
|
return Err(WebDriverJSError::JSError);
|
||||||
|
},
|
||||||
|
};
|
||||||
// Step 4. For each enumerable property in value, run the following substeps:
|
// Step 4. For each enumerable property in value, run the following substeps:
|
||||||
for i in 0..length {
|
for i in 0..length {
|
||||||
rooted!(in(cx) let mut item = UndefinedValue());
|
rooted!(in(cx) let mut item = UndefinedValue());
|
||||||
match get_property_jsval(cx, object_handle, &i.to_string(), item.handle_mut()) {
|
let get_property_result =
|
||||||
Ok(_) => match jsval_to_webdriver_inner(cx, global_scope, item.handle(), seen) {
|
unsafe { get_property_jsval(cx, object_handle, &i.to_string(), item.handle_mut()) };
|
||||||
Ok(converted_item) => result.push(converted_item),
|
match get_property_result {
|
||||||
err @ Err(_) => return err,
|
Ok(_) => {
|
||||||
|
let conversion_result =
|
||||||
|
unsafe { jsval_to_webdriver_inner(cx, global_scope, item.handle(), seen) };
|
||||||
|
match conversion_result {
|
||||||
|
Ok(converted_item) => result.push(converted_item),
|
||||||
|
err @ Err(_) => return err,
|
||||||
|
}
|
||||||
},
|
},
|
||||||
Err(error) => {
|
Err(error) => {
|
||||||
throw_dom_exception(
|
throw_dom_exception(
|
||||||
SafeJSContext::from_ptr(cx),
|
unsafe { SafeJSContext::from_ptr(cx) },
|
||||||
global_scope,
|
global_scope,
|
||||||
error,
|
error,
|
||||||
CanGc::note(),
|
CanGc::note(),
|
||||||
|
@ -511,13 +519,16 @@ unsafe fn clone_an_object(
|
||||||
} else {
|
} else {
|
||||||
let mut result = HashMap::new();
|
let mut result = HashMap::new();
|
||||||
|
|
||||||
let mut ids = IdVector::new(cx);
|
let mut ids = unsafe { IdVector::new(cx) };
|
||||||
if !GetPropertyKeys(
|
let succeeded = unsafe {
|
||||||
cx,
|
GetPropertyKeys(
|
||||||
object_handle.into(),
|
cx,
|
||||||
jsapi::JSITER_OWNONLY,
|
object_handle.into(),
|
||||||
ids.handle_mut(),
|
jsapi::JSITER_OWNONLY,
|
||||||
) {
|
ids.handle_mut(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if !succeeded {
|
||||||
return Err(WebDriverJSError::JSError);
|
return Err(WebDriverJSError::JSError);
|
||||||
}
|
}
|
||||||
for id in ids.iter() {
|
for id in ids.iter() {
|
||||||
|
@ -525,32 +536,40 @@ unsafe fn clone_an_object(
|
||||||
rooted!(in(cx) let mut desc = PropertyDescriptor::default());
|
rooted!(in(cx) let mut desc = PropertyDescriptor::default());
|
||||||
|
|
||||||
let mut is_none = false;
|
let mut is_none = false;
|
||||||
if !JS_GetOwnPropertyDescriptorById(
|
let succeeded = unsafe {
|
||||||
cx,
|
JS_GetOwnPropertyDescriptorById(
|
||||||
object_handle.into(),
|
cx,
|
||||||
id.handle().into(),
|
object_handle.into(),
|
||||||
desc.handle_mut().into(),
|
id.handle().into(),
|
||||||
&mut is_none,
|
desc.handle_mut().into(),
|
||||||
) {
|
&mut is_none,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if !succeeded {
|
||||||
return Err(WebDriverJSError::JSError);
|
return Err(WebDriverJSError::JSError);
|
||||||
}
|
}
|
||||||
|
|
||||||
rooted!(in(cx) let mut property = UndefinedValue());
|
rooted!(in(cx) let mut property = UndefinedValue());
|
||||||
if !JS_GetPropertyById(
|
let succeeded = unsafe {
|
||||||
cx,
|
JS_GetPropertyById(
|
||||||
object_handle.into(),
|
cx,
|
||||||
id.handle().into(),
|
object_handle.into(),
|
||||||
property.handle_mut().into(),
|
id.handle().into(),
|
||||||
) {
|
property.handle_mut().into(),
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if !succeeded {
|
||||||
return Err(WebDriverJSError::JSError);
|
return Err(WebDriverJSError::JSError);
|
||||||
}
|
}
|
||||||
|
|
||||||
if !property.is_undefined() {
|
if !property.is_undefined() {
|
||||||
let Some(name) = jsid_to_string(cx, id.handle()) else {
|
let name = unsafe { jsid_to_string(cx, id.handle()) };
|
||||||
|
let Some(name) = name else {
|
||||||
return Err(WebDriverJSError::JSError);
|
return Err(WebDriverJSError::JSError);
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Ok(value) =
|
if let Ok(value) =
|
||||||
jsval_to_webdriver_inner(cx, global_scope, property.handle(), seen)
|
unsafe { jsval_to_webdriver_inner(cx, global_scope, property.handle(), seen) }
|
||||||
{
|
{
|
||||||
result.insert(name.into(), value);
|
result.insert(name.into(), value);
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -89,11 +89,13 @@ unsafe extern "C" fn get_own_property_descriptor(
|
||||||
let cx = unsafe { SafeJSContext::from_ptr(cx) };
|
let cx = unsafe { SafeJSContext::from_ptr(cx) };
|
||||||
|
|
||||||
if id.is_symbol() {
|
if id.is_symbol() {
|
||||||
if id.get().asBits_ == SymbolId(GetWellKnownSymbol(*cx, SymbolCode::toStringTag)).asBits_ {
|
if id.get().asBits_ ==
|
||||||
|
SymbolId(unsafe { GetWellKnownSymbol(*cx, SymbolCode::toStringTag) }).asBits_
|
||||||
|
{
|
||||||
rooted!(in(*cx) let mut rval = UndefinedValue());
|
rooted!(in(*cx) let mut rval = UndefinedValue());
|
||||||
"WindowProperties".to_jsval(*cx, rval.handle_mut());
|
unsafe { "WindowProperties".to_jsval(*cx, rval.handle_mut()) };
|
||||||
set_property_descriptor(
|
set_property_descriptor(
|
||||||
RustMutableHandle::from_raw(desc),
|
unsafe { RustMutableHandle::from_raw(desc) },
|
||||||
rval.handle(),
|
rval.handle(),
|
||||||
JSPROP_READONLY.into(),
|
JSPROP_READONLY.into(),
|
||||||
unsafe { &mut *is_none },
|
unsafe { &mut *is_none },
|
||||||
|
@ -103,12 +105,15 @@ unsafe extern "C" fn get_own_property_descriptor(
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut found = false;
|
let mut found = false;
|
||||||
if !has_property_on_prototype(
|
let lookup_succeeded = unsafe {
|
||||||
*cx,
|
has_property_on_prototype(
|
||||||
RustHandle::from_raw(proxy),
|
*cx,
|
||||||
RustHandle::from_raw(id),
|
RustHandle::from_raw(proxy),
|
||||||
&mut found,
|
RustHandle::from_raw(id),
|
||||||
) {
|
&mut found,
|
||||||
|
)
|
||||||
|
};
|
||||||
|
if !lookup_succeeded {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if found {
|
if found {
|
||||||
|
@ -136,7 +141,9 @@ unsafe extern "C" fn get_own_property_descriptor(
|
||||||
.expect("global is not a window");
|
.expect("global is not a window");
|
||||||
if let Some(obj) = window.NamedGetter(s.into()) {
|
if let Some(obj) = window.NamedGetter(s.into()) {
|
||||||
rooted!(in(*cx) let mut rval = UndefinedValue());
|
rooted!(in(*cx) let mut rval = UndefinedValue());
|
||||||
obj.to_jsval(*cx, rval.handle_mut());
|
unsafe {
|
||||||
|
obj.to_jsval(*cx, rval.handle_mut());
|
||||||
|
}
|
||||||
set_property_descriptor(
|
set_property_descriptor(
|
||||||
unsafe { RustMutableHandle::from_raw(desc) },
|
unsafe { RustMutableHandle::from_raw(desc) },
|
||||||
rval.handle(),
|
rval.handle(),
|
||||||
|
|
|
@ -312,7 +312,11 @@ pub unsafe fn set_dictionary_property(
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns whether `proxy` has a property `id` on its prototype.
|
/// Computes whether `proxy` has a property `id` on its prototype and stores
|
||||||
|
/// the result in `found`.
|
||||||
|
///
|
||||||
|
/// Returns a boolean indicating whether the check succeeded.
|
||||||
|
/// If `false` is returned then the value of `found` is unspecified.
|
||||||
///
|
///
|
||||||
/// # Safety
|
/// # Safety
|
||||||
/// `cx` must point to a valid, non-null JSContext.
|
/// `cx` must point to a valid, non-null JSContext.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue