Miscellaneous script splitting preparation changes (#36216)

* script: Move num module to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Make JS reflector creation generic over DOM trait.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move bindings-specific lock to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move DOM proto array code to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move finalizer implementations to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move some error routines to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move some DOM interface conversion routines to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Make is_array_like generic over DOM trait.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Use generic interfaces for conditional exposure functions.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* script: Move a bunch of routines used by codegen to script_bindings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Formatting.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

* Fix clippy warnings.

Signed-off-by: Josh Matthews <josh@joshmatthews.net>

---------

Signed-off-by: Josh Matthews <josh@joshmatthews.net>
This commit is contained in:
Josh Matthews 2025-03-29 04:11:27 -04:00 committed by GitHub
parent 2c94110952
commit c30ad5a30e
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
28 changed files with 836 additions and 691 deletions

View file

@ -137,7 +137,7 @@ DOMInterfaces = {
},
'Document': {
'additionalTraits': ["crate::dom::document::DocumentHelpers<Self>"],
'additionalTraits': ["script_bindings::interfaces::DocumentHelpers"],
'canGc': ['Close', 'CreateElement', 'CreateElementNS', 'ImportNode', 'SetTitle', 'Write', 'Writeln', 'CreateEvent', 'CreateRange', 'Open', 'Open_', 'CreateComment', 'CreateAttribute', 'CreateAttributeNS', 'CreateDocumentFragment', 'CreateTextNode', 'CreateCDATASection', 'CreateProcessingInstruction', 'Prepend', 'Append', 'ReplaceChildren', 'SetBgColor', 'SetFgColor', 'Fonts', 'ElementFromPoint', 'ElementsFromPoint', 'ExitFullscreen', 'CreateExpression', 'CreateNSResolver', 'Evaluate'],
},
@ -527,6 +527,7 @@ DOMInterfaces = {
'ServoInternals': {
'inRealms': ['ReportMemory'],
'canGc': ['ReportMemory'],
'additionalTraits': ['script_bindings::interfaces::ServoInternalsHelpers'],
},
'ShadowRoot': {
@ -550,6 +551,7 @@ DOMInterfaces = {
'TestBinding': {
'inRealms': ['PromiseAttribute', 'PromiseNativeHandler'],
'canGc': ['InterfaceAttribute', 'GetInterfaceAttributeNullable', 'ReceiveInterface', 'ReceiveInterfaceSequence', 'ReceiveNullableInterface', 'PromiseAttribute', 'PromiseNativeHandler', 'PromiseResolveNative', 'PromiseRejectNative', 'PromiseRejectWithTypeError'],
'additionalTraits': ['script_bindings::interfaces::TestBindingHelpers'],
},
'TestWorklet': {
@ -580,6 +582,7 @@ DOMInterfaces = {
'WebGL2RenderingContext': {
'canGc': ['MakeXRCompatible'],
'additionalTraits': ['script_bindings::interfaces::WebGL2RenderingContextHelpers'],
},
'Window': {

View file

@ -498,7 +498,7 @@ class CGMethodCall(CGThing):
# XXXbz Now we're supposed to check for distinguishingArg being
# an array or a platform object that supports indexed
# properties... skip that last for now. It's a bit of a pain.
pickFirstSignature(f"{distinguishingArg}.get().is_object() && is_array_like(*cx, {distinguishingArg})",
pickFirstSignature(f"{distinguishingArg}.get().is_object() && is_array_like::<D>(*cx, {distinguishingArg})",
lambda s:
(s[1][distinguishingIndex].type.isSequence()
or s[1][distinguishingIndex].type.isObject()))
@ -1565,10 +1565,10 @@ def MemberCondition(pref, func, exposed, secure):
if pref:
conditions.append(f'Condition::Pref("{pref}")')
if func:
conditions.append(f'Condition::Func({func})')
conditions.append(f'Condition::Func(D::{func})')
if exposed:
conditions.extend([
f"Condition::Exposed(InterfaceObjectMap::Globals::{camel_to_upper_snake(i)})" for i in exposed
f"Condition::Exposed(Globals::{camel_to_upper_snake(i)})" for i in exposed
])
if len(conditions) == 0:
conditions.append("Condition::Satisfied")
@ -2369,7 +2369,7 @@ DOMClass {{
depth: {descriptor.prototypeDepth},
type_id: {DOMClassTypeId(descriptor)},
malloc_size_of: {mallocSizeOf} as unsafe fn(&mut _, _) -> _,
global: InterfaceObjectMap::Globals::{globals_},
global: Globals::{globals_},
}}"""
@ -2962,14 +2962,15 @@ class CGConstructorEnabled(CGAbstractMethod):
CGAbstractMethod.__init__(self, descriptor,
'ConstructorEnabled', 'bool',
[Argument("SafeJSContext", "aCx"),
Argument("HandleObject", "aObj")])
Argument("HandleObject", "aObj")],
templateArgs=['D: DomTypes'])
def definition_body(self):
conditions = []
iface = self.descriptor.interface
bits = " | ".join(sorted(
f"InterfaceObjectMap::Globals::{camel_to_upper_snake(i)}" for i in iface.exposureSet
f"Globals::{camel_to_upper_snake(i)}" for i in iface.exposureSet
))
conditions.append(f"is_exposed_in(aObj, {bits})")
@ -2981,14 +2982,14 @@ class CGConstructorEnabled(CGAbstractMethod):
func = iface.getExtendedAttribute("Func")
if func:
assert isinstance(func, list) and len(func) == 1
conditions.append(f"{func[0]}(aCx, aObj)")
conditions.append(f"D::{func[0]}(aCx, aObj)")
secure = iface.getExtendedAttribute("SecureContext")
if secure:
conditions.append("""
unsafe {
let in_realm_proof = AlreadyInRealm::assert_for_cx(aCx);
GlobalScope::from_context(*aCx, InRealm::Already(&in_realm_proof)).is_secure_context()
D::GlobalScope::from_context(*aCx, InRealm::Already(&in_realm_proof)).is_secure_context()
}
""")
@ -3004,8 +3005,8 @@ def InitLegacyUnforgeablePropertiesOnHolder(descriptor, properties):
"""
unforgeables = []
defineLegacyUnforgeableAttrs = "define_guarded_properties(cx, unforgeable_holder.handle(), %s, global);"
defineLegacyUnforgeableMethods = "define_guarded_methods(cx, unforgeable_holder.handle(), %s, global);"
defineLegacyUnforgeableAttrs = "define_guarded_properties::<D>(cx, unforgeable_holder.handle(), %s, global);"
defineLegacyUnforgeableMethods = "define_guarded_methods::<D>(cx, unforgeable_holder.handle(), %s, global);"
unforgeableMembers = [
(defineLegacyUnforgeableAttrs, properties.unforgeable_attrs),
@ -3175,7 +3176,7 @@ class CGWrapGlobalMethod(CGAbstractMethod):
("define_guarded_methods", self.properties.methods),
("define_guarded_constants", self.properties.consts)
]
members = [f"{function}(cx, obj.handle(), {array.variableName()}.get(), obj.handle());"
members = [f"{function}::<D>(cx, obj.handle(), {array.variableName()}.get(), obj.handle());"
for (function, array) in pairs if array.length() > 0]
membersStr = "\n".join(members)
@ -3413,7 +3414,7 @@ let global = incumbent_global.reflector().get_jsobject();\n"""
"""
let conditions = ${conditions};
let is_satisfied = conditions.iter().any(|c|
c.is_satisfied(
c.is_satisfied::<D>(
SafeJSContext::from_ptr(cx),
HandleObject::from_raw(obj),
global));
@ -3469,7 +3470,7 @@ class CGCreateInterfaceObjectsMethod(CGAbstractMethod):
rooted!(in(*cx) let proto = {proto});
assert!(!proto.is_null());
rooted!(in(*cx) let mut namespace = ptr::null_mut::<JSObject>());
create_namespace_object(cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
create_namespace_object::<D>(cx, global, proto.handle(), &NAMESPACE_OBJECT_CLASS,
{methods}, {constants}, {str_to_cstr(name)}, namespace.handle_mut());
assert!(!namespace.is_null());
assert!((*cache)[PrototypeList::Constructor::{id} as usize].is_null());
@ -3483,7 +3484,7 @@ assert!((*cache)[PrototypeList::Constructor::{id} as usize].is_null());
cName = str_to_cstr(name)
return CGGeneric(f"""
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_callback_interface_object(cx, global, sConstants.get(), {cName}, interface.handle_mut());
create_callback_interface_object::<D>(cx, global, sConstants.get(), {cName}, interface.handle_mut());
assert!(!interface.is_null());
assert!((*cache)[PrototypeList::Constructor::{name} as usize].is_null());
(*cache)[PrototypeList::Constructor::{name} as usize] = interface.get();
@ -3544,7 +3545,7 @@ assert!(!prototype_proto.is_null());"""))
code.append(CGGeneric(f"""
rooted!(in(*cx) let mut prototype = ptr::null_mut::<JSObject>());
create_interface_prototype_object(cx,
create_interface_prototype_object::<D>(cx,
global,
prototype_proto.handle(),
&PrototypeClass,
@ -3579,7 +3580,7 @@ assert!((*cache)[PrototypeList::ID::{proto_properties['id']} as usize].is_null()
assert!(!interface_proto.is_null());
rooted!(in(*cx) let mut interface = ptr::null_mut::<JSObject>());
create_noncallback_interface_object(cx,
create_noncallback_interface_object::<D>(cx,
global,
interface_proto.handle(),
INTERFACE_OBJECT_CLASS.get(),
@ -3870,7 +3871,7 @@ class CGDefineDOMInterfaceMethod(CGAbstractMethod):
return CGGeneric(
"define_dom_interface"
f"(cx, global, ProtoOrIfaceIndex::{self.variant}({self.id}),"
"CreateInterfaceObjects::<D>, ConstructorEnabled)"
"CreateInterfaceObjects::<D>, ConstructorEnabled::<D>)"
)
@ -5927,7 +5928,7 @@ class CGDOMJSProxyHandler_getOwnPropertyDescriptor(CGAbstractExternMethod):
""")
if indexedGetter:
get += "let index = get_array_index_from_id(*cx, Handle::from_raw(id));\n"
get += "let index = get_array_index_from_id(Handle::from_raw(id));\n"
attrs = "JSPROP_ENUMERATE"
if self.descriptor.operations['IndexedSetter'] is None:
@ -6042,7 +6043,7 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod):
indexedSetter = self.descriptor.operations['IndexedSetter']
if indexedSetter:
set += ("let index = get_array_index_from_id(*cx, Handle::from_raw(id));\n"
set += ("let index = get_array_index_from_id(Handle::from_raw(id));\n"
"if let Some(index) = index {\n"
" let this = UnwrapProxy::<D>(proxy);\n"
" let this = &*this;\n"
@ -6050,7 +6051,7 @@ class CGDOMJSProxyHandler_defineProperty(CGAbstractExternMethod):
" return (*opresult).succeed();\n"
"}\n")
elif self.descriptor.operations['IndexedGetter']:
set += ("if get_array_index_from_id(*cx, Handle::from_raw(id)).is_some() {\n"
set += ("if get_array_index_from_id(Handle::from_raw(id)).is_some() {\n"
" return (*opresult).failNoIndexedSetter();\n"
"}\n")
@ -6265,7 +6266,7 @@ class CGDOMJSProxyHandler_hasOwn(CGAbstractExternMethod):
""")
if indexedGetter:
indexed += ("let index = get_array_index_from_id(*cx, Handle::from_raw(id));\n"
indexed += ("let index = get_array_index_from_id(Handle::from_raw(id));\n"
"if let Some(index) = index {\n"
" let this = UnwrapProxy::<D>(proxy);\n"
" let this = &*this;\n"
@ -6357,7 +6358,7 @@ if !expando.is_null() {
indexedGetter = self.descriptor.operations['IndexedGetter']
if indexedGetter:
getIndexedOrExpando = ("let index = get_array_index_from_id(*cx, id_lt);\n"
getIndexedOrExpando = ("let index = get_array_index_from_id(id_lt);\n"
"if let Some(index) = index {\n"
" let this = UnwrapProxy::<D>(proxy);\n"
" let this = &*this;\n"

View file

@ -21,8 +21,10 @@ use js::rust::{
HandleId, HandleValue, MutableHandleValue, ToString, get_object_class, is_dom_class,
is_dom_object, maybe_wrap_value,
};
use num_traits::Float;
use crate::inheritance::Castable;
use crate::num::Finite;
use crate::reflector::{DomObject, Reflector};
use crate::root::DomRoot;
use crate::str::{ByteString, DOMString, USVString};
@ -408,3 +410,87 @@ pub unsafe fn jsid_to_string(cx: *mut JSContext, id: HandleId) -> Option<DOMStri
None
}
impl<T: Float + ToJSValConvertible> ToJSValConvertible for Finite<T> {
#[inline]
unsafe fn to_jsval(&self, cx: *mut JSContext, rval: MutableHandleValue) {
let value = **self;
value.to_jsval(cx, rval);
}
}
impl<T: Float + FromJSValConvertible<Config = ()>> FromJSValConvertible for Finite<T> {
type Config = ();
unsafe fn from_jsval(
cx: *mut JSContext,
value: HandleValue,
option: (),
) -> Result<ConversionResult<Finite<T>>, ()> {
let result = match FromJSValConvertible::from_jsval(cx, value, option)? {
ConversionResult::Success(v) => v,
ConversionResult::Failure(error) => {
// FIXME(emilio): Why throwing instead of propagating the error?
throw_type_error(cx, &error);
return Err(());
},
};
match Finite::new(result) {
Some(v) => Ok(ConversionResult::Success(v)),
None => {
throw_type_error(cx, "this argument is not a finite floating-point value");
Err(())
},
}
}
}
/// Get a `*const libc::c_void` for the given DOM object, unless it is a DOM
/// wrapper, and checking if the object is of the correct type.
///
/// Returns Err(()) if `obj` is a wrapper or if the object is not an object
/// for a DOM object of the given type (as defined by the proto_id and proto_depth).
#[inline]
#[allow(clippy::result_unit_err)]
unsafe fn private_from_proto_check_static(
obj: *mut JSObject,
proto_check: fn(&'static DOMClass) -> bool,
) -> Result<*const libc::c_void, ()> {
let dom_class = get_dom_class(obj).map_err(|_| ())?;
if proto_check(dom_class) {
trace!("good prototype");
Ok(private_from_object(obj))
} else {
trace!("bad prototype");
Err(())
}
}
/// Get a `*const T` for a DOM object accessible from a `JSObject`, where the DOM object
/// is guaranteed not to be a wrapper.
///
/// # Safety
/// `obj` must point to a valid, non-null JSObject.
#[allow(clippy::result_unit_err)]
pub unsafe fn native_from_object_static<T>(obj: *mut JSObject) -> Result<*const T, ()>
where
T: DomObject + IDLInterface,
{
private_from_proto_check_static(obj, T::derives).map(|ptr| ptr as *const T)
}
/// Get a `*const T` for a DOM object accessible from a `HandleValue`.
/// Caller is responsible for throwing a JS exception if needed in case of error.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
#[allow(clippy::result_unit_err)]
pub unsafe fn native_from_handlevalue<T>(v: HandleValue, cx: *mut JSContext) -> Result<*const T, ()>
where
T: DomObject + IDLInterface,
{
if !v.get().is_object() {
return Err(());
}
native_from_object(v.get().to_object(), cx)
}

View file

@ -2,6 +2,12 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use js::error::throw_type_error;
use js::jsapi::JS_IsExceptionPending;
use crate::codegen::PrototypeList::proto_id_to_name;
use crate::script_runtime::JSContext as SafeJSContext;
/// DOM exceptions that can be thrown by a native DOM method.
#[derive(Clone, Debug, MallocSizeOf)]
pub enum Error {
@ -69,3 +75,20 @@ pub type Fallible<T> = Result<T, Error>;
/// The return type for IDL operations that can throw DOM exceptions and
/// return `()`.
pub type ErrorResult = Fallible<()>;
/// Throw an exception to signal that a `JSObject` can not be converted to a
/// given DOM type.
pub fn throw_invalid_this(cx: SafeJSContext, proto_id: u16) {
debug_assert!(unsafe { !JS_IsExceptionPending(*cx) });
let error = format!(
"\"this\" object does not implement interface {}.",
proto_id_to_name(proto_id)
);
unsafe { throw_type_error(*cx, &error) };
}
pub fn throw_constructor_without_new(cx: SafeJSContext, name: &str) {
debug_assert!(unsafe { !JS_IsExceptionPending(*cx) });
let error = format!("{} constructor: 'new' is required", name);
unsafe { throw_type_error(*cx, &error) };
}

View file

@ -0,0 +1,74 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! Generic finalizer implementations for DOM binding implementations.
use std::any::type_name;
use std::{mem, ptr};
use js::glue::JS_GetReservedSlot;
use js::jsapi::JSObject;
use js::jsval::UndefinedValue;
use js::rust::GCMethods;
use crate::codegen::PrototypeList::PROTO_OR_IFACE_LENGTH;
use crate::utils::{ProtoOrIfaceArray, get_proto_or_iface_array};
use crate::weakref::{DOM_WEAK_SLOT, WeakBox, WeakReferenceable};
/// Drop the resources held by reserved slots of a global object
unsafe fn do_finalize_global(obj: *mut JSObject) {
let protolist = get_proto_or_iface_array(obj);
let list = (*protolist).as_mut_ptr();
for idx in 0..PROTO_OR_IFACE_LENGTH as isize {
let entry = list.offset(idx);
let value = *entry;
<*mut JSObject>::post_barrier(entry, value, ptr::null_mut());
}
let _: Box<ProtoOrIfaceArray> = Box::from_raw(protolist);
}
/// # Safety
/// `this` must point to a valid, non-null instance of T.
pub unsafe fn finalize_common<T>(this: *const T) {
if !this.is_null() {
// The pointer can be null if the object is the unforgeable holder of that interface.
let _ = Box::from_raw(this as *mut T);
}
debug!("{} finalize: {:p}", type_name::<T>(), this);
}
/// # Safety
/// `obj` must point to a valid, non-null JS object.
/// `this` must point to a valid, non-null instance of T.
pub unsafe fn finalize_global<T>(obj: *mut JSObject, this: *const T) {
do_finalize_global(obj);
finalize_common::<T>(this);
}
/// # Safety
/// `obj` must point to a valid, non-null JS object.
/// `this` must point to a valid, non-null instance of T.
pub unsafe fn finalize_weak_referenceable<T: WeakReferenceable>(
obj: *mut JSObject,
this: *const T,
) {
let mut slot = UndefinedValue();
JS_GetReservedSlot(obj, DOM_WEAK_SLOT, &mut slot);
let weak_box_ptr = slot.to_private() as *mut WeakBox<T>;
if !weak_box_ptr.is_null() {
let count = {
let weak_box = &*weak_box_ptr;
assert!(weak_box.value.get().is_some());
assert!(weak_box.count.get() > 0);
weak_box.value.set(None);
let count = weak_box.count.get() - 1;
weak_box.count.set(count);
count
};
if count == 0 {
mem::drop(Box::from_raw(weak_box_ptr));
}
}
finalize_common::<T>(this);
}

View file

@ -0,0 +1,24 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use js::rust::HandleObject;
use crate::script_runtime::JSContext;
pub trait DocumentHelpers {
fn ensure_safe_to_run_script_or_layout(&self);
}
pub trait ServoInternalsHelpers {
fn is_servo_internal(cx: JSContext, global: HandleObject) -> bool;
}
pub trait TestBindingHelpers {
fn condition_satisfied(cx: JSContext, global: HandleObject) -> bool;
fn condition_unsatisfied(cx: JSContext, global: HandleObject) -> bool;
}
pub trait WebGL2RenderingContextHelpers {
fn is_webgl2_enabled(cx: JSContext, global: HandleObject) -> bool;
}

View file

@ -21,9 +21,13 @@ pub mod callback;
pub mod constant;
pub mod conversions;
pub mod error;
pub mod finalize;
pub mod inheritance;
pub mod interfaces;
pub mod iterable;
pub mod like;
pub mod lock;
pub mod num;
pub mod record;
pub mod reflector;
pub mod root;

View file

@ -0,0 +1,37 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::sync::OnceLock;
/// A OnceLock wrapping a type that is not considered threadsafe by the Rust compiler, but
/// will be used in a threadsafe manner (it will not be mutated, after being initialized).
///
/// This is needed to allow using JS API types (which usually involve raw pointers) in static initializers,
/// when Servo guarantees through the use of OnceLock that only one thread will ever initialize
/// the value.
pub struct ThreadUnsafeOnceLock<T>(OnceLock<T>);
impl<T> ThreadUnsafeOnceLock<T> {
#[allow(clippy::new_without_default)]
pub const fn new() -> Self {
Self(OnceLock::new())
}
/// Initialize the value inside this lock. Panics if the lock has been previously initialized.
pub fn set(&self, val: T) {
assert!(self.0.set(val).is_ok());
}
/// Get a reference to the value inside this lock. Panics if the lock has not been initialized.
///
/// # Safety
/// The caller must ensure that it does not mutate value contained inside this lock
/// (using interior mutability).
pub unsafe fn get(&self) -> &T {
self.0.get().unwrap()
}
}
unsafe impl<T> Sync for ThreadUnsafeOnceLock<T> {}
unsafe impl<T> Send for ThreadUnsafeOnceLock<T> {}

View file

@ -0,0 +1,57 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
//! The `Finite<T>` struct.
use std::default::Default;
use std::ops::Deref;
use malloc_size_of::{MallocSizeOf, MallocSizeOfOps};
use num_traits::Float;
/// Encapsulates the IDL restricted float type.
#[derive(Clone, Copy, Eq, JSTraceable, PartialEq)]
pub struct Finite<T: Float>(T);
impl<T: Float> Finite<T> {
/// Create a new `Finite<T: Float>` safely.
pub fn new(value: T) -> Option<Finite<T>> {
if value.is_finite() {
Some(Finite(value))
} else {
None
}
}
/// Create a new `Finite<T: Float>`.
#[inline]
pub fn wrap(value: T) -> Finite<T> {
assert!(
value.is_finite(),
"Finite<T> doesn't encapsulate unrestricted value."
);
Finite(value)
}
}
impl<T: Float> Deref for Finite<T> {
type Target = T;
fn deref(&self) -> &T {
let Finite(value) = self;
value
}
}
impl<T: Float + MallocSizeOf> MallocSizeOf for Finite<T> {
fn size_of(&self, ops: &mut MallocSizeOfOps) -> usize {
(**self).size_of(ops)
}
}
impl<T: Float + Default> Default for Finite<T> {
fn default() -> Finite<T> {
Finite::wrap(T::default())
}
}

View file

@ -2,13 +2,40 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
use std::ffi::CString;
use std::os::raw::c_void;
use std::ptr::{self, NonNull};
use js::conversions::ToJSValConvertible;
use js::glue::{
CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, JS_GetReservedSlot,
RUST_FUNCTION_VALUE_TO_JITINFO,
};
use js::jsapi::{
AtomToLinearString, CallArgs, ExceptionStackBehavior, GetLinearStringCharAt,
GetLinearStringLength, GetNonCCWObjectGlobal, HandleObject as RawHandleObject,
JS_ClearPendingException, JS_IsExceptionPending, JSAtom, JSContext, JSJitInfo, JSObject,
MutableHandleValue as RawMutableHandleValue, ObjectOpResult, StringIsArrayIndex,
};
use js::jsval::{JSVal, UndefinedValue};
use js::rust::wrappers::{
CallOriginalPromiseReject, JS_DeletePropertyById, JS_ForwardGetPropertyTo,
JS_GetPendingException, JS_GetProperty, JS_GetPrototype, JS_HasProperty, JS_HasPropertyById,
JS_SetPendingException, JS_SetProperty,
};
use js::rust::{
HandleId, HandleObject, HandleValue, MutableHandleValue, ToString, get_object_class,
};
use js::{JS_CALLEE, rooted};
use malloc_size_of::MallocSizeOfOps;
use crate::codegen::Globals::Globals;
use crate::codegen::InheritTypes::TopTypeId;
use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH};
use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
use crate::conversions::{PrototypeCheck, jsstring_to_str, private_from_proto_check};
use crate::error::throw_invalid_this;
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
use crate::str::DOMString;
/// The struct that holds inheritance information for DOM object reflectors.
#[derive(Clone, Copy)]
@ -46,3 +73,456 @@ impl Clone for DOMJSClass {
}
}
unsafe impl Sync for DOMJSClass {}
/// The index of the slot where the object holder of that interface's
/// unforgeable members are defined.
pub const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
/// The index of the slot that contains a reference to the ProtoOrIfaceArray.
// All DOM globals must have a slot at DOM_PROTOTYPE_SLOT.
pub const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
/// The flag set on the `JSClass`es for DOM global objects.
// NOTE: This is baked into the Ion JIT as 0 in codegen for LGetDOMProperty and
// LSetDOMProperty. Those constants need to be changed accordingly if this value
// changes.
pub const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
/// Returns the ProtoOrIfaceArray for the given global object.
/// Fails if `global` is not a DOM global object.
///
/// # Safety
/// `global` must point to a valid, non-null JS object.
pub unsafe fn get_proto_or_iface_array(global: *mut JSObject) -> *mut ProtoOrIfaceArray {
assert_ne!(((*get_object_class(global)).flags & JSCLASS_DOM_GLOBAL), 0);
let mut slot = UndefinedValue();
JS_GetReservedSlot(global, DOM_PROTOTYPE_SLOT, &mut slot);
slot.to_private() as *mut ProtoOrIfaceArray
}
/// An array of *mut JSObject of size PROTO_OR_IFACE_LENGTH.
pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
/// Gets the property `id` on `proxy`'s prototype. If it exists, `*found` is
/// set to true and `*vp` to the value, otherwise `*found` is set to false.
///
/// Returns false on JSAPI failure.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `found` must point to a valid, non-null bool.
pub unsafe fn get_property_on_prototype(
cx: *mut JSContext,
proxy: HandleObject,
receiver: HandleValue,
id: HandleId,
found: *mut bool,
vp: MutableHandleValue,
) -> bool {
rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
if !JS_GetPrototype(cx, proxy, proto.handle_mut()) || proto.is_null() {
*found = false;
return true;
}
let mut has_property = false;
if !JS_HasPropertyById(cx, proto.handle(), id, &mut has_property) {
return false;
}
*found = has_property;
if !has_property {
return true;
}
JS_ForwardGetPropertyTo(cx, proto.handle(), id, receiver, vp)
}
/// Get an array index from the given `jsid`. Returns `None` if the given
/// `jsid` is not an integer.
pub fn get_array_index_from_id(id: HandleId) -> Option<u32> {
let raw_id = *id;
if raw_id.is_int() {
return Some(raw_id.to_int() as u32);
}
if raw_id.is_void() || !raw_id.is_string() {
return None;
}
unsafe {
let atom = raw_id.to_string() as *mut JSAtom;
let s = AtomToLinearString(atom);
if GetLinearStringLength(s) == 0 {
return None;
}
let chars = [GetLinearStringCharAt(s, 0)];
let first_char = char::decode_utf16(chars.iter().cloned())
.next()
.map_or('\0', |r| r.unwrap_or('\0'));
if first_char.is_ascii_lowercase() {
return None;
}
let mut i = 0;
if StringIsArrayIndex(s, &mut i) {
Some(i)
} else {
None
}
}
/*let s = jsstr_to_string(cx, RUST_JSID_TO_STRING(raw_id));
if s.len() == 0 {
return None;
}
let first = s.chars().next().unwrap();
if first.is_ascii_lowercase() {
return None;
}
let mut i: u32 = 0;
let is_array = if s.is_ascii() {
let chars = s.as_bytes();
StringIsArrayIndex1(chars.as_ptr() as *const _, chars.len() as u32, &mut i)
} else {
let chars = s.encode_utf16().collect::<Vec<u16>>();
let slice = chars.as_slice();
StringIsArrayIndex2(slice.as_ptr(), chars.len() as u32, &mut i)
};
if is_array {
Some(i)
} else {
None
}*/
}
/// Find the enum equivelent of a string given by `v` in `pairs`.
/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
/// `Ok((None, value))` if there was no matching string.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
#[allow(clippy::result_unit_err)]
pub unsafe fn find_enum_value<'a, T>(
cx: *mut JSContext,
v: HandleValue,
pairs: &'a [(&'static str, T)],
) -> Result<(Option<&'a T>, DOMString), ()> {
match ptr::NonNull::new(ToString(cx, v)) {
Some(jsstr) => {
let search = jsstring_to_str(cx, jsstr);
Ok((
pairs
.iter()
.find(|&&(key, _)| search == *key)
.map(|(_, ev)| ev),
search,
))
},
None => Err(()),
}
}
/// Get the property with name `property` from `object`.
/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
/// `Ok(false)` if there was no property with the given name.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
#[allow(clippy::result_unit_err)]
pub unsafe fn get_dictionary_property(
cx: *mut JSContext,
object: HandleObject,
property: &str,
rval: MutableHandleValue,
_can_gc: CanGc,
) -> Result<bool, ()> {
unsafe fn has_property(
cx: *mut JSContext,
object: HandleObject,
property: &CString,
found: &mut bool,
) -> bool {
JS_HasProperty(cx, object, property.as_ptr(), found)
}
unsafe fn get_property(
cx: *mut JSContext,
object: HandleObject,
property: &CString,
value: MutableHandleValue,
) -> bool {
JS_GetProperty(cx, object, property.as_ptr(), value)
}
let property = CString::new(property).unwrap();
if object.get().is_null() {
return Ok(false);
}
let mut found = false;
if !has_property(cx, object, &property, &mut found) {
return Err(());
}
if !found {
return Ok(false);
}
if !get_property(cx, object, &property, rval) {
return Err(());
}
Ok(true)
}
/// Set the property with name `property` from `object`.
/// Returns `Err(())` on JSAPI failure, or null object,
/// and Ok(()) otherwise
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
#[allow(clippy::result_unit_err)]
pub unsafe fn set_dictionary_property(
cx: *mut JSContext,
object: HandleObject,
property: &str,
value: HandleValue,
) -> Result<(), ()> {
if object.get().is_null() {
return Err(());
}
let property = CString::new(property).unwrap();
if !JS_SetProperty(cx, object, property.as_ptr(), value) {
return Err(());
}
Ok(())
}
/// Returns whether `proxy` has a property `id` on its prototype.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
pub unsafe fn has_property_on_prototype(
cx: *mut JSContext,
proxy: HandleObject,
id: HandleId,
found: &mut bool,
) -> bool {
rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
if !JS_GetPrototype(cx, proxy, proto.handle_mut()) {
return false;
}
assert!(!proto.is_null());
JS_HasPropertyById(cx, proto.handle(), id, found)
}
/// Deletes the property `id` from `object`.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
pub unsafe fn delete_property_by_id(
cx: *mut JSContext,
object: HandleObject,
id: HandleId,
bp: *mut ObjectOpResult,
) -> bool {
JS_DeletePropertyById(cx, object, id, bp)
}
unsafe fn generic_call<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
is_lenient: bool,
call: unsafe extern "C" fn(
*const JSJitInfo,
*mut JSContext,
RawHandleObject,
*mut libc::c_void,
u32,
*mut JSVal,
) -> bool,
can_gc: CanGc,
) -> bool {
let args = CallArgs::from_vp(vp, argc);
let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
let proto_id = (*info).__bindgen_anon_2.protoID;
let cx = SafeJSContext::from_ptr(cx);
let thisobj = args.thisv();
if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
throw_invalid_this(cx, proto_id);
return if EXCEPTION_TO_REJECTION {
exception_to_promise(*cx, args.rval(), can_gc)
} else {
false
};
}
rooted!(in(*cx) let obj = if thisobj.get().is_object() {
thisobj.get().to_object()
} else {
GetNonCCWObjectGlobal(JS_CALLEE(*cx, vp).to_object_or_null())
});
let depth = (*info).__bindgen_anon_3.depth as usize;
let proto_check = PrototypeCheck::Depth { depth, proto_id };
let this = match private_from_proto_check(obj.get(), *cx, proto_check) {
Ok(val) => val,
Err(()) => {
if is_lenient {
debug_assert!(!JS_IsExceptionPending(*cx));
*vp = UndefinedValue();
return true;
} else {
throw_invalid_this(cx, proto_id);
return if EXCEPTION_TO_REJECTION {
exception_to_promise(*cx, args.rval(), can_gc)
} else {
false
};
}
},
};
call(
info,
*cx,
obj.handle().into(),
this as *mut libc::c_void,
argc,
vp,
)
}
/// Generic method of IDL interface.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_method<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitMethodOp, CanGc::note())
}
/// Generic getter of IDL interface.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_getter<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, false, CallJitGetterOp, CanGc::note())
}
/// Generic lenient getter of IDL interface.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_lenient_getter<const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<EXCEPTION_TO_REJECTION>(cx, argc, vp, true, CallJitGetterOp, CanGc::note())
}
unsafe extern "C" fn call_setter(
info: *const JSJitInfo,
cx: *mut JSContext,
handle: RawHandleObject,
this: *mut libc::c_void,
argc: u32,
vp: *mut JSVal,
) -> bool {
if !CallJitSetterOp(info, cx, handle, this, argc, vp) {
return false;
}
*vp = UndefinedValue();
true
}
/// Generic setter of IDL interface.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_setter(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<false>(cx, argc, vp, false, call_setter, CanGc::note())
}
/// Generic lenient setter of IDL interface.
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_lenient_setter(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<false>(cx, argc, vp, true, call_setter, CanGc::note())
}
/// <https://searchfox.org/mozilla-central/rev/7279a1df13a819be254fd4649e07c4ff93e4bd45/dom/bindings/BindingUtils.cpp#3300>
/// # Safety
///
/// `cx` must point to a valid, non-null JSContext.
/// `vp` must point to a VALID, non-null JSVal.
pub unsafe extern "C" fn generic_static_promise_method(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
let args = CallArgs::from_vp(vp, argc);
let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
assert!(!info.is_null());
// TODO: we need safe wrappers for this in mozjs!
//assert_eq!((*info)._bitfield_1, JSJitInfo_OpType::StaticMethod as u8)
let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
if static_fn(cx, argc, vp) {
return true;
}
exception_to_promise(cx, args.rval(), CanGc::note())
}
/// Coverts exception to promise rejection
///
/// <https://searchfox.org/mozilla-central/rev/b220e40ff2ee3d10ce68e07d8a8a577d5558e2a2/dom/bindings/BindingUtils.cpp#3315>
///
/// # Safety
/// `cx` must point to a valid, non-null JSContext.
pub unsafe fn exception_to_promise(
cx: *mut JSContext,
rval: RawMutableHandleValue,
_can_gc: CanGc,
) -> bool {
rooted!(in(cx) let mut exception = UndefinedValue());
if !JS_GetPendingException(cx, exception.handle_mut()) {
return false;
}
JS_ClearPendingException(cx);
if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
promise.to_jsval(cx, MutableHandleValue::from_raw(rval));
true
} else {
// We just give up. Put the exception back.
JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
false
}
}

View file

@ -8,12 +8,12 @@
// This interface is entirely internal to Servo, and should not be accessible to
// web pages.
[Exposed=Window,
Func="dom::bindings::interface::is_servo_internal"]
Func="ServoInternals::is_servo_internal"]
interface ServoInternals {
Promise<object> reportMemory();
};
partial interface Navigator {
[Func="dom::bindings::interface::is_servo_internal"]
[Func="ServoInternals::is_servo_internal"]
readonly attribute ServoInternals servo;
};