use std::ffi::CStr;
use std::os::raw::{c_char, c_void};
use std::ptr::{self, NonNull};
use std::slice;
use js::conversions::{ToJSValConvertible, jsstr_to_string};
use js::gc::Handle;
use js::glue::{
AppendToIdVector, CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, JS_GetReservedSlot,
RUST_FUNCTION_VALUE_TO_JITINFO,
};
use js::jsapi::{
AtomToLinearString, CallArgs, ExceptionStackBehavior, GetLinearStringCharAt,
GetLinearStringLength, GetNonCCWObjectGlobal, HandleId as RawHandleId,
HandleObject as RawHandleObject, Heap, JS_AtomizeStringN, JS_ClearPendingException,
JS_DeprecatedStringHasLatin1Chars, JS_GetLatin1StringCharsAndLength, JS_IsExceptionPending,
JS_IsGlobalObject, JS_MayResolveStandardClass, JS_NewEnumerateStandardClasses,
JS_ResolveStandardClass, JSAtom, JSAtomState, JSContext, JSJitInfo, JSObject, JSPROP_ENUMERATE,
JSTracer, MutableHandleIdVector as RawMutableHandleIdVector,
MutableHandleValue as RawMutableHandleValue, ObjectOpResult, PropertyKey, StringIsArrayIndex,
jsid,
};
use js::jsid::StringId;
use js::jsval::{JSVal, UndefinedValue};
use js::rust::wrappers::{
CallOriginalPromiseReject, JS_DefineProperty, JS_DeletePropertyById, JS_ForwardGetPropertyTo,
JS_GetPendingException, JS_GetPrototype, JS_HasOwnProperty, JS_HasPropertyById,
JS_SetPendingException, JS_SetProperty,
};
use js::rust::wrappers2::{JS_GetProperty, JS_HasProperty};
use js::rust::{
HandleId, HandleObject, HandleValue, MutableHandleValue, Runtime, ToString, get_object_class,
};
use js::{JS_CALLEE, rooted};
use malloc_size_of::MallocSizeOfOps;
use crate::DomTypes;
use crate::codegen::Globals::Globals;
use crate::codegen::InheritTypes::TopTypeId;
use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
use crate::conversions::{PrototypeCheck, private_from_proto_check};
use crate::error::throw_invalid_this;
use crate::interfaces::DomHelpers;
use crate::proxyhandler::{is_cross_origin_object, report_cross_origin_denial};
use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
use crate::str::DOMString;
use crate::trace::trace_object;
#[derive(Clone, Copy)]
pub struct DOMClass {
pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
pub depth: u8,
pub type_id: TopTypeId,
pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
pub global: Globals,
}
unsafe impl Sync for DOMClass {}
#[derive(Copy)]
#[repr(C)]
pub struct DOMJSClass {
pub base: js::jsapi::JSClass,
pub dom_class: DOMClass,
}
impl Clone for DOMJSClass {
fn clone(&self) -> DOMJSClass {
*self
}
}
unsafe impl Sync for DOMJSClass {}
pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
pub(crate) 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
}
pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
pub(crate) 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)
}
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
}
}
}
#[allow(clippy::result_unit_err)]
pub(crate) fn find_enum_value<'a, T>(
cx: &mut js::context::JSContext,
v: HandleValue,
pairs: &'a [(&'static str, T)],
) -> Result<(Option<&'a T>, DOMString), ()> {
match ptr::NonNull::new(unsafe { ToString(cx.raw_cx(), v) }) {
Some(jsstr) => {
let search = unsafe { jsstr_to_string(cx.raw_cx(), jsstr).into() };
Ok((
pairs
.iter()
.find(|&&(key, _)| search == key)
.map(|(_, ev)| ev),
search,
))
},
None => Err(()),
}
}
#[allow(clippy::result_unit_err)]
pub fn get_dictionary_property(
cx: &mut js::context::JSContext,
object: HandleObject,
property: &CStr,
rval: MutableHandleValue,
) -> Result<bool, ()> {
if object.get().is_null() {
return Ok(false);
}
let mut found = false;
if unsafe { !JS_HasProperty(cx, object, property.as_ptr(), &mut found) } {
return Err(());
}
if !found {
return Ok(false);
}
if unsafe { !JS_GetProperty(cx, object, property.as_ptr(), rval) } {
return Err(());
}
Ok(true)
}
#[allow(clippy::result_unit_err)]
pub fn set_dictionary_property(
cx: SafeJSContext,
object: HandleObject,
property: &CStr,
value: HandleValue,
) -> Result<(), ()> {
if object.get().is_null() {
return Err(());
}
unsafe {
if !JS_SetProperty(*cx, object, property.as_ptr(), value) {
return Err(());
}
}
Ok(())
}
#[allow(clippy::result_unit_err)]
pub fn define_dictionary_property(
cx: SafeJSContext,
object: HandleObject,
property: &CStr,
value: HandleValue,
) -> Result<(), ()> {
if object.get().is_null() {
return Err(());
}
unsafe {
if !JS_DefineProperty(
*cx,
object,
property.as_ptr(),
value,
JSPROP_ENUMERATE as u32,
) {
return Err(());
}
}
Ok(())
}
#[allow(clippy::result_unit_err)]
pub fn has_own_property(
cx: SafeJSContext,
object: HandleObject,
property: &CStr,
) -> Result<bool, ()> {
if object.get().is_null() {
return Ok(false);
}
let mut found = false;
unsafe {
if !JS_HasOwnProperty(*cx, object, property.as_ptr(), &mut found) {
return Err(());
}
}
Ok(found)
}
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)
}
pub(crate) unsafe fn delete_property_by_id(
cx: *mut JSContext,
object: HandleObject,
id: HandleId,
bp: *mut ObjectOpResult,
) -> bool {
JS_DeletePropertyById(cx, object, id, bp)
}
pub trait CallPolicy {
const INFO: CallPolicyInfo;
}
pub mod call_policies {
use super::*;
pub struct Normal;
pub struct TargetClassMaybeCrossOrigin;
pub struct LenientThis;
pub struct LenientThisTargetClassMaybeCrossOrigin;
pub struct CrossOriginCallable;
impl CallPolicy for Normal {
const INFO: CallPolicyInfo = CallPolicyInfo {
lenient_this: false,
needs_security_check_on_interface_match: false,
};
}
impl CallPolicy for TargetClassMaybeCrossOrigin {
const INFO: CallPolicyInfo = CallPolicyInfo {
lenient_this: false,
needs_security_check_on_interface_match: true,
};
}
impl CallPolicy for LenientThis {
const INFO: CallPolicyInfo = CallPolicyInfo {
lenient_this: true,
needs_security_check_on_interface_match: false,
};
}
impl CallPolicy for LenientThisTargetClassMaybeCrossOrigin {
const INFO: CallPolicyInfo = CallPolicyInfo {
lenient_this: true,
needs_security_check_on_interface_match: true,
};
}
impl CallPolicy for CrossOriginCallable {
const INFO: CallPolicyInfo = CallPolicyInfo {
lenient_this: false,
needs_security_check_on_interface_match: false,
};
}
}
#[derive(Clone, Copy, Eq, PartialEq)]
pub struct CallPolicyInfo {
pub lenient_this: bool,
pub needs_security_check_on_interface_match: bool,
}
unsafe fn generic_call<D: DomTypes, const EXCEPTION_TO_REJECTION: bool>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
CallPolicyInfo {
lenient_this,
needs_security_check_on_interface_match,
}: CallPolicyInfo,
call: unsafe extern "C" fn(
*const JSJitInfo,
*mut JSContext,
RawHandleObject,
*mut libc::c_void,
u32,
*mut JSVal,
) -> bool,
can_gc: CanGc,
) -> bool {
let mut cx = unsafe { js::context::JSContext::from_ptr(NonNull::new(cx).unwrap()) };
let args = CallArgs::from_vp(vp, argc);
let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx_no_gc(), vp));
let proto_id = (*info).__bindgen_anon_2.protoID;
let thisobj = args.thisv();
if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
throw_invalid_this((&mut cx).into(), proto_id);
return if EXCEPTION_TO_REJECTION {
exception_to_promise(cx.raw_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.raw_cx_no_gc(), 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.raw_cx_no_gc(), proto_check) {
Ok(val) => val,
Err(()) => {
if lenient_this {
debug_assert!(!JS_IsExceptionPending(cx.raw_cx_no_gc()));
*vp = UndefinedValue();
return true;
} else {
throw_invalid_this((&mut cx).into(), proto_id);
return if EXCEPTION_TO_REJECTION {
exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
} else {
false
};
}
},
};
if needs_security_check_on_interface_match {
let mut realm = js::realm::CurrentRealm::assert(&mut cx);
if is_cross_origin_object::<D>((&mut realm).into(), obj.handle().into()) &&
!<D as DomHelpers<D>>::is_platform_object_same_origin(&realm, obj.handle().into())
{
rooted!(&in(*realm) let mut void_jsid: js::jsapi::jsid);
let result =
report_cross_origin_denial::<D>(&mut realm, void_jsid.handle().into(), "call");
return if EXCEPTION_TO_REJECTION {
exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
} else {
result
};
}
} else {
}
call(
info,
cx.raw_cx(),
obj.handle().into(),
this as *mut libc::c_void,
argc,
vp,
)
}
pub(crate) unsafe extern "C" fn generic_method<
D: DomTypes,
Policy: CallPolicy,
const EXCEPTION_TO_REJECTION: bool,
>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<D, EXCEPTION_TO_REJECTION>(
cx,
argc,
vp,
Policy::INFO,
CallJitMethodOp,
CanGc::deprecated_note(),
)
}
pub(crate) unsafe extern "C" fn generic_getter<
D: DomTypes,
Policy: CallPolicy,
const EXCEPTION_TO_REJECTION: bool,
>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<D, EXCEPTION_TO_REJECTION>(
cx,
argc,
vp,
Policy::INFO,
CallJitGetterOp,
CanGc::deprecated_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
}
pub(crate) unsafe extern "C" fn generic_setter<D: DomTypes, Policy: CallPolicy>(
cx: *mut JSContext,
argc: libc::c_uint,
vp: *mut JSVal,
) -> bool {
generic_call::<D, false>(
cx,
argc,
vp,
Policy::INFO,
call_setter,
CanGc::deprecated_note(),
)
}
pub(crate) 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());
let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
if static_fn(cx, argc, vp) {
return true;
}
exception_to_promise(cx, args.rval(), CanGc::deprecated_note())
}
pub(crate) 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 {
JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
false
}
}
pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
let array = get_proto_or_iface_array(obj);
for proto in (*array).iter() {
if !proto.is_null() {
trace_object(
tracer,
"prototype",
&*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
);
}
}
}
pub(crate) unsafe extern "C" fn enumerate_global(
cx: *mut JSContext,
obj: RawHandleObject,
props: RawMutableHandleIdVector,
enumerable_only: bool,
) -> bool {
assert!(JS_IsGlobalObject(obj.get()));
JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
}
pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
cx: *mut JSContext,
obj: RawHandleObject,
props: RawMutableHandleIdVector,
enumerable_only: bool,
) -> bool {
let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
return false;
}
if enumerable_only {
return true;
}
let obj = Handle::from_raw(obj);
for (name, interface) in <D as DomHelpers<D>>::interface_map() {
if !(interface.enabled)(&mut cx, obj) {
continue;
}
let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
rooted!(&in(cx) let id = StringId(s));
if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
return false;
}
}
true
}
pub(crate) unsafe extern "C" fn may_resolve_global(
names: *const JSAtomState,
id: PropertyKey,
maybe_obj: *mut JSObject,
) -> bool {
JS_MayResolveStandardClass(names, id, maybe_obj)
}
pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
names: *const JSAtomState,
id: PropertyKey,
maybe_obj: *mut JSObject,
) -> bool {
if may_resolve_global(names, id, maybe_obj) {
return true;
}
let cx = Runtime::get()
.expect("There must be a JSContext active")
.as_ptr();
let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
return false;
};
<D as DomHelpers<D>>::interface_map().contains_key(bytes)
}
pub(crate) unsafe extern "C" fn resolve_global(
cx: *mut JSContext,
obj: RawHandleObject,
id: RawHandleId,
rval: *mut bool,
) -> bool {
assert!(JS_IsGlobalObject(obj.get()));
JS_ResolveStandardClass(cx, obj, id, rval)
}
pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
cx: *mut JSContext,
obj: RawHandleObject,
id: RawHandleId,
rval: *mut bool,
) -> bool {
let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
if !resolve_global(cx.raw_cx(), obj, id, rval) {
return false;
}
if *rval {
return true;
}
let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
*rval = false;
return true;
};
if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
(interface.define)(&mut cx, Handle::from_raw(obj));
*rval = true;
} else {
*rval = false;
}
true
}
unsafe fn latin1_bytes_from_id(cx: *mut JSContext, id: jsid) -> Result<&'static [u8], ()> {
if !id.is_string() {
return Err(());
}
let string = id.to_string();
if !JS_DeprecatedStringHasLatin1Chars(string) {
return Err(());
}
let mut length = 0;
let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
assert!(!ptr.is_null());
Ok(slice::from_raw_parts(ptr, length))
}