mirror of
https://github.com/servo/servo.git
synced 2025-09-30 00:29:14 +01:00
script: Make get_property_jsval
a safe function (#39137)
Accept the safe `JSContext` wraper to this function so that it can be safe. Some callers also become safe as well. Testing: This does not change behavior and is thus covered by existing tests. Fixes: #39129. Signed-off-by: Martin Robinson <mrobinson@igalia.com>
This commit is contained in:
parent
589a750cac
commit
e5fbb31452
3 changed files with 59 additions and 75 deletions
|
@ -37,11 +37,12 @@ use std::ffi;
|
|||
pub(crate) use js::conversions::{
|
||||
ConversionBehavior, ConversionResult, FromJSValConvertible, ToJSValConvertible,
|
||||
};
|
||||
use js::jsapi::{JS_IsExceptionPending, JSContext, JSObject};
|
||||
use js::jsapi::{JS_IsExceptionPending, JSContext as RawJSContext, JSObject};
|
||||
use js::jsval::UndefinedValue;
|
||||
use js::rust::wrappers::{JS_GetProperty, JS_HasProperty};
|
||||
use js::rust::{HandleObject, MutableHandleValue};
|
||||
pub(crate) use script_bindings::conversions::{is_dom_proxy, *};
|
||||
use script_bindings::script_runtime::JSContext;
|
||||
|
||||
use crate::dom::bindings::error::{Error, Fallible};
|
||||
use crate::dom::bindings::reflector::DomObject;
|
||||
|
@ -63,7 +64,7 @@ where
|
|||
/// Get a `DomRoot<T>` for a DOM object accessible from a `HandleObject`.
|
||||
pub(crate) fn root_from_handleobject<T>(
|
||||
obj: HandleObject,
|
||||
cx: *mut JSContext,
|
||||
cx: *mut RawJSContext,
|
||||
) -> Result<DomRoot<T>, ()>
|
||||
where
|
||||
T: DomObject + IDLInterface,
|
||||
|
@ -72,36 +73,38 @@ where
|
|||
}
|
||||
|
||||
/// Get a property from a JS object.
|
||||
pub(crate) unsafe fn get_property_jsval(
|
||||
cx: *mut JSContext,
|
||||
pub(crate) fn get_property_jsval(
|
||||
cx: JSContext,
|
||||
object: HandleObject,
|
||||
name: &str,
|
||||
mut rval: MutableHandleValue,
|
||||
) -> Fallible<()> {
|
||||
rval.set(UndefinedValue());
|
||||
let cname = match ffi::CString::new(name) {
|
||||
Ok(cname) => cname,
|
||||
Err(_) => return Ok(()),
|
||||
|
||||
let Ok(cname) = ffi::CString::new(name) else {
|
||||
return Ok(());
|
||||
};
|
||||
|
||||
let mut found = false;
|
||||
unsafe {
|
||||
if JS_HasProperty(cx, object, cname.as_ptr(), &mut found) && found {
|
||||
JS_GetProperty(cx, object, cname.as_ptr(), rval);
|
||||
if JS_IsExceptionPending(cx) {
|
||||
if !JS_HasProperty(*cx, object, cname.as_ptr(), &mut found) || !found {
|
||||
if JS_IsExceptionPending(*cx) {
|
||||
return Err(Error::JSFailed);
|
||||
}
|
||||
Ok(())
|
||||
} else if JS_IsExceptionPending(cx) {
|
||||
Err(Error::JSFailed)
|
||||
} else {
|
||||
Ok(())
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
JS_GetProperty(*cx, object, cname.as_ptr(), rval);
|
||||
if JS_IsExceptionPending(*cx) {
|
||||
return Err(Error::JSFailed);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Get a property from a JS object, and convert it to a Rust value.
|
||||
pub(crate) unsafe fn get_property<T>(
|
||||
cx: *mut JSContext,
|
||||
pub(crate) fn get_property<T>(
|
||||
cx: JSContext,
|
||||
object: HandleObject,
|
||||
name: &str,
|
||||
option: T::Config,
|
||||
|
@ -110,14 +113,14 @@ where
|
|||
T: FromJSValConvertible,
|
||||
{
|
||||
debug!("Getting property {}.", name);
|
||||
rooted!(in(cx) let mut result = UndefinedValue());
|
||||
unsafe { get_property_jsval(cx, object, name, result.handle_mut())? };
|
||||
rooted!(in(*cx) let mut result = UndefinedValue());
|
||||
get_property_jsval(cx, object, name, result.handle_mut())?;
|
||||
if result.is_undefined() {
|
||||
debug!("No property {}.", name);
|
||||
return Ok(None);
|
||||
}
|
||||
debug!("Converting property {}.", name);
|
||||
let value = unsafe { T::from_jsval(cx, result.handle(), option) };
|
||||
let value = unsafe { T::from_jsval(*cx, result.handle(), option) };
|
||||
match value {
|
||||
Ok(ConversionResult::Success(value)) => Ok(Some(value)),
|
||||
Ok(ConversionResult::Failure(_)) => Ok(None),
|
||||
|
|
|
@ -513,20 +513,17 @@ impl PaintWorkletGlobalScopeMethods<crate::DomTypeHolder> for PaintWorkletGlobal
|
|||
|
||||
// Step 4-6.
|
||||
let mut property_names: Vec<String> =
|
||||
unsafe { get_property(*cx, paint_obj.handle(), "inputProperties", ()) }?
|
||||
.unwrap_or_default();
|
||||
get_property(cx, paint_obj.handle(), "inputProperties", ())?.unwrap_or_default();
|
||||
let properties = property_names.drain(..).map(Atom::from).collect();
|
||||
|
||||
// Step 7-9.
|
||||
let input_arguments: Vec<String> =
|
||||
unsafe { get_property(*cx, paint_obj.handle(), "inputArguments", ()) }?
|
||||
.unwrap_or_default();
|
||||
get_property(cx, paint_obj.handle(), "inputArguments", ())?.unwrap_or_default();
|
||||
|
||||
// TODO: Steps 10-11.
|
||||
|
||||
// Steps 12-13.
|
||||
let alpha: bool =
|
||||
unsafe { get_property(*cx, paint_obj.handle(), "alpha", ()) }?.unwrap_or(true);
|
||||
let alpha: bool = get_property(cx, paint_obj.handle(), "alpha", ())?.unwrap_or(true);
|
||||
|
||||
// Step 14
|
||||
if unsafe { !IsConstructor(paint_obj.get()) } {
|
||||
|
@ -535,9 +532,7 @@ impl PaintWorkletGlobalScopeMethods<crate::DomTypeHolder> for PaintWorkletGlobal
|
|||
|
||||
// Steps 15-16
|
||||
rooted!(in(*cx) let mut prototype = UndefinedValue());
|
||||
unsafe {
|
||||
get_property_jsval(*cx, paint_obj.handle(), "prototype", prototype.handle_mut())?;
|
||||
}
|
||||
get_property_jsval(cx, paint_obj.handle(), "prototype", prototype.handle_mut())?;
|
||||
if !prototype.is_object() {
|
||||
return Err(Error::Type(String::from("Prototype is not an object.")));
|
||||
}
|
||||
|
@ -545,14 +540,7 @@ impl PaintWorkletGlobalScopeMethods<crate::DomTypeHolder> for PaintWorkletGlobal
|
|||
|
||||
// Steps 17-18
|
||||
rooted!(in(*cx) let mut paint_function = UndefinedValue());
|
||||
unsafe {
|
||||
get_property_jsval(
|
||||
*cx,
|
||||
prototype.handle(),
|
||||
"paint",
|
||||
paint_function.handle_mut(),
|
||||
)?;
|
||||
}
|
||||
get_property_jsval(cx, prototype.handle(), "paint", paint_function.handle_mut())?;
|
||||
if !paint_function.is_object() || unsafe { !IsCallable(paint_function.to_object()) } {
|
||||
return Err(Error::Type(String::from("Paint function is not callable.")));
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue