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:
Simon Wülker 2025-08-26 23:48:25 +02:00 committed by GitHub
parent 87fe202ded
commit de6feb469a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 87 additions and 57 deletions

View file

@ -467,20 +467,22 @@ 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)
};
let length = match get_property_result {
Ok(length) => match length { Ok(length) => match length {
Some(length) => length, Some(length) => length,
_ => return Err(WebDriverJSError::UnknownType), _ => return Err(WebDriverJSError::UnknownType),
}, },
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(),
@ -491,14 +493,20 @@ unsafe fn clone_an_object(
// 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()) };
match get_property_result {
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), Ok(converted_item) => result.push(converted_item),
err @ Err(_) => return err, 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 {
GetPropertyKeys(
cx, cx,
object_handle.into(), object_handle.into(),
jsapi::JSITER_OWNONLY, jsapi::JSITER_OWNONLY,
ids.handle_mut(), 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 {
JS_GetOwnPropertyDescriptorById(
cx, cx,
object_handle.into(), object_handle.into(),
id.handle().into(), id.handle().into(),
desc.handle_mut().into(), desc.handle_mut().into(),
&mut is_none, &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 {
JS_GetPropertyById(
cx, cx,
object_handle.into(), object_handle.into(),
id.handle().into(), id.handle().into(),
property.handle_mut().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 {

View file

@ -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 {
has_property_on_prototype(
*cx, *cx,
RustHandle::from_raw(proxy), RustHandle::from_raw(proxy),
RustHandle::from_raw(id), RustHandle::from_raw(id),
&mut found, &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());
unsafe {
obj.to_jsval(*cx, rval.handle_mut()); 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(),

View file

@ -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.