1use std::ffi::CStr;
6use std::os::raw::{c_char, c_void};
7use std::ptr::{self, NonNull};
8use std::slice;
9
10use js::conversions::{ToJSValConvertible, jsstr_to_string};
11use js::gc::Handle;
12use js::glue::{
13 AppendToIdVector, CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, JS_GetReservedSlot,
14 RUST_FUNCTION_VALUE_TO_JITINFO,
15};
16use js::jsapi::{
17 AtomToLinearString, CallArgs, ExceptionStackBehavior, GetLinearStringCharAt,
18 GetLinearStringLength, GetNonCCWObjectGlobal, HandleId as RawHandleId,
19 HandleObject as RawHandleObject, Heap, JS_AtomizeStringN, JS_ClearPendingException,
20 JS_DeprecatedStringHasLatin1Chars, JS_GetLatin1StringCharsAndLength, JS_IsExceptionPending,
21 JS_IsGlobalObject, JS_MayResolveStandardClass, JS_NewEnumerateStandardClasses,
22 JS_ResolveStandardClass, JSAtom, JSAtomState, JSContext, JSJitInfo, JSObject, JSPROP_ENUMERATE,
23 JSTracer, MutableHandleIdVector as RawMutableHandleIdVector,
24 MutableHandleValue as RawMutableHandleValue, ObjectOpResult, PropertyKey, StringIsArrayIndex,
25 jsid,
26};
27use js::jsid::StringId;
28use js::jsval::{JSVal, UndefinedValue};
29use js::rust::wrappers::{
30 CallOriginalPromiseReject, JS_DefineProperty, JS_DeletePropertyById, JS_ForwardGetPropertyTo,
31 JS_GetPendingException, JS_GetProperty, JS_GetPrototype, JS_HasOwnProperty, JS_HasProperty,
32 JS_HasPropertyById, JS_SetPendingException, JS_SetProperty,
33};
34use js::rust::{
35 HandleId, HandleObject, HandleValue, MutableHandleValue, Runtime, ToString, get_object_class,
36};
37use js::{JS_CALLEE, rooted};
38use malloc_size_of::MallocSizeOfOps;
39
40use crate::DomTypes;
41use crate::codegen::Globals::Globals;
42use crate::codegen::InheritTypes::TopTypeId;
43use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
44use crate::conversions::{PrototypeCheck, private_from_proto_check};
45use crate::error::throw_invalid_this;
46use crate::interfaces::DomHelpers;
47use crate::proxyhandler::{is_cross_origin_object, report_cross_origin_denial};
48use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
49use crate::str::DOMString;
50use crate::trace::trace_object;
51
52#[derive(Clone, Copy)]
54pub struct DOMClass {
55 pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
58
59 pub depth: u8,
61
62 pub type_id: TopTypeId,
64
65 pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
67
68 pub global: Globals,
70}
71unsafe impl Sync for DOMClass {}
72
73#[derive(Copy)]
75#[repr(C)]
76pub struct DOMJSClass {
77 pub base: js::jsapi::JSClass,
79 pub dom_class: DOMClass,
81}
82impl Clone for DOMJSClass {
83 fn clone(&self) -> DOMJSClass {
84 *self
85 }
86}
87unsafe impl Sync for DOMJSClass {}
88
89pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
92
93pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
96
97pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
102
103pub(crate) unsafe fn get_proto_or_iface_array(global: *mut JSObject) -> *mut ProtoOrIfaceArray {
109 assert_ne!(((*get_object_class(global)).flags & JSCLASS_DOM_GLOBAL), 0);
110 let mut slot = UndefinedValue();
111 JS_GetReservedSlot(global, DOM_PROTOTYPE_SLOT, &mut slot);
112 slot.to_private() as *mut ProtoOrIfaceArray
113}
114
115pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
117
118pub(crate) unsafe fn get_property_on_prototype(
127 cx: *mut JSContext,
128 proxy: HandleObject,
129 receiver: HandleValue,
130 id: HandleId,
131 found: *mut bool,
132 vp: MutableHandleValue,
133) -> bool {
134 rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
135 if !JS_GetPrototype(cx, proxy, proto.handle_mut()) || proto.is_null() {
136 *found = false;
137 return true;
138 }
139 let mut has_property = false;
140 if !JS_HasPropertyById(cx, proto.handle(), id, &mut has_property) {
141 return false;
142 }
143 *found = has_property;
144 if !has_property {
145 return true;
146 }
147
148 JS_ForwardGetPropertyTo(cx, proto.handle(), id, receiver, vp)
149}
150
151pub fn get_array_index_from_id(id: HandleId) -> Option<u32> {
154 let raw_id = *id;
155 if raw_id.is_int() {
156 return Some(raw_id.to_int() as u32);
157 }
158
159 if raw_id.is_void() || !raw_id.is_string() {
160 return None;
161 }
162
163 unsafe {
164 let atom = raw_id.to_string() as *mut JSAtom;
165 let s = AtomToLinearString(atom);
166 if GetLinearStringLength(s) == 0 {
167 return None;
168 }
169
170 let chars = [GetLinearStringCharAt(s, 0)];
171 let first_char = char::decode_utf16(chars.iter().cloned())
172 .next()
173 .map_or('\0', |r| r.unwrap_or('\0'));
174 if first_char.is_ascii_lowercase() {
175 return None;
176 }
177
178 let mut i = 0;
179 if StringIsArrayIndex(s, &mut i) {
180 Some(i)
181 } else {
182 None
183 }
184 }
185
186 }
212
213#[allow(clippy::result_unit_err)]
220pub(crate) unsafe fn find_enum_value<'a, T>(
221 cx: *mut JSContext,
222 v: HandleValue,
223 pairs: &'a [(&'static str, T)],
224) -> Result<(Option<&'a T>, DOMString), ()> {
225 match ptr::NonNull::new(ToString(cx, v)) {
226 Some(jsstr) => {
227 let search = jsstr_to_string(cx, jsstr).into();
228 Ok((
229 pairs
230 .iter()
231 .find(|&&(key, _)| search == key)
232 .map(|(_, ev)| ev),
233 search,
234 ))
235 },
236 None => Err(()),
237 }
238}
239
240#[allow(clippy::result_unit_err)]
247pub unsafe fn get_dictionary_property(
248 cx: *mut JSContext,
249 object: HandleObject,
250 property: &CStr,
251 rval: MutableHandleValue,
252 _can_gc: CanGc,
253) -> Result<bool, ()> {
254 unsafe fn has_property(
255 cx: *mut JSContext,
256 object: HandleObject,
257 property: &CStr,
258 found: &mut bool,
259 ) -> bool {
260 JS_HasProperty(cx, object, property.as_ptr(), found)
261 }
262 unsafe fn get_property(
263 cx: *mut JSContext,
264 object: HandleObject,
265 property: &CStr,
266 value: MutableHandleValue,
267 ) -> bool {
268 JS_GetProperty(cx, object, property.as_ptr(), value)
269 }
270
271 if object.get().is_null() {
272 return Ok(false);
273 }
274
275 let mut found = false;
276 if !has_property(cx, object, property, &mut found) {
277 return Err(());
278 }
279
280 if !found {
281 return Ok(false);
282 }
283
284 if !get_property(cx, object, property, rval) {
285 return Err(());
286 }
287
288 Ok(true)
289}
290
291#[allow(clippy::result_unit_err)]
295pub fn set_dictionary_property(
296 cx: SafeJSContext,
297 object: HandleObject,
298 property: &CStr,
299 value: HandleValue,
300) -> Result<(), ()> {
301 if object.get().is_null() {
302 return Err(());
303 }
304
305 unsafe {
306 if !JS_SetProperty(*cx, object, property.as_ptr(), value) {
307 return Err(());
308 }
309 }
310
311 Ok(())
312}
313
314#[allow(clippy::result_unit_err)]
318pub fn define_dictionary_property(
319 cx: SafeJSContext,
320 object: HandleObject,
321 property: &CStr,
322 value: HandleValue,
323) -> Result<(), ()> {
324 if object.get().is_null() {
325 return Err(());
326 }
327
328 unsafe {
329 if !JS_DefineProperty(
330 *cx,
331 object,
332 property.as_ptr(),
333 value,
334 JSPROP_ENUMERATE as u32,
335 ) {
336 return Err(());
337 }
338 }
339
340 Ok(())
341}
342
343#[allow(clippy::result_unit_err)]
347pub fn has_own_property(
348 cx: SafeJSContext,
349 object: HandleObject,
350 property: &CStr,
351) -> Result<bool, ()> {
352 if object.get().is_null() {
353 return Ok(false);
354 }
355
356 let mut found = false;
357 unsafe {
358 if !JS_HasOwnProperty(*cx, object, property.as_ptr(), &mut found) {
359 return Err(());
360 }
361 }
362
363 Ok(found)
364}
365
366pub unsafe fn has_property_on_prototype(
375 cx: *mut JSContext,
376 proxy: HandleObject,
377 id: HandleId,
378 found: &mut bool,
379) -> bool {
380 rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
381 if !JS_GetPrototype(cx, proxy, proto.handle_mut()) {
382 return false;
383 }
384 assert!(!proto.is_null());
385 JS_HasPropertyById(cx, proto.handle(), id, found)
386}
387
388pub(crate) unsafe fn delete_property_by_id(
393 cx: *mut JSContext,
394 object: HandleObject,
395 id: HandleId,
396 bp: *mut ObjectOpResult,
397) -> bool {
398 JS_DeletePropertyById(cx, object, id, bp)
399}
400
401pub trait CallPolicy {
402 const INFO: CallPolicyInfo;
403}
404pub mod call_policies {
405 use super::*;
406 pub struct Normal;
407 pub struct TargetClassMaybeCrossOrigin;
408 pub struct LenientThis;
409 pub struct LenientThisTargetClassMaybeCrossOrigin;
410 pub struct CrossOriginCallable;
411 impl CallPolicy for Normal {
412 const INFO: CallPolicyInfo = CallPolicyInfo {
413 lenient_this: false,
414 needs_security_check_on_interface_match: false,
415 };
416 }
417 impl CallPolicy for TargetClassMaybeCrossOrigin {
418 const INFO: CallPolicyInfo = CallPolicyInfo {
419 lenient_this: false,
420 needs_security_check_on_interface_match: true,
421 };
422 }
423 impl CallPolicy for LenientThis {
424 const INFO: CallPolicyInfo = CallPolicyInfo {
425 lenient_this: true,
426 needs_security_check_on_interface_match: false,
427 };
428 }
429 impl CallPolicy for LenientThisTargetClassMaybeCrossOrigin {
430 const INFO: CallPolicyInfo = CallPolicyInfo {
431 lenient_this: true,
432 needs_security_check_on_interface_match: true,
433 };
434 }
435 impl CallPolicy for CrossOriginCallable {
436 const INFO: CallPolicyInfo = CallPolicyInfo {
437 lenient_this: false,
438 needs_security_check_on_interface_match: false,
439 };
440 }
441}
442#[derive(Clone, Copy, Eq, PartialEq)]
449pub struct CallPolicyInfo {
450 pub lenient_this: bool,
453 pub needs_security_check_on_interface_match: bool,
472}
473
474unsafe fn generic_call<D: DomTypes, const EXCEPTION_TO_REJECTION: bool>(
475 cx: *mut JSContext,
476 argc: libc::c_uint,
477 vp: *mut JSVal,
478 CallPolicyInfo {
479 lenient_this,
480 needs_security_check_on_interface_match,
481 }: CallPolicyInfo,
482 call: unsafe extern "C" fn(
483 *const JSJitInfo,
484 *mut JSContext,
485 RawHandleObject,
486 *mut libc::c_void,
487 u32,
488 *mut JSVal,
489 ) -> bool,
490 can_gc: CanGc,
491) -> bool {
492 let mut cx = unsafe { js::context::JSContext::from_ptr(NonNull::new(cx).unwrap()) };
493 let args = CallArgs::from_vp(vp, argc);
494
495 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx_no_gc(), vp));
496 let proto_id = (*info).__bindgen_anon_2.protoID;
497
498 let thisobj = args.thisv();
574 if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
575 throw_invalid_this((&mut cx).into(), proto_id);
578 return if EXCEPTION_TO_REJECTION {
579 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
580 } else {
581 false
582 };
583 }
584
585 rooted!(&in(cx) let obj = if thisobj.get().is_object() {
586 thisobj.get().to_object()
587 } else {
588 GetNonCCWObjectGlobal(JS_CALLEE(cx.raw_cx_no_gc(), vp).to_object_or_null())
589 });
590 let depth = (*info).__bindgen_anon_3.depth as usize;
591 let proto_check = PrototypeCheck::Depth { depth, proto_id };
592 let this = match private_from_proto_check(obj.get(), cx.raw_cx_no_gc(), proto_check) {
593 Ok(val) => val,
594 Err(()) => {
595 if lenient_this {
607 debug_assert!(!JS_IsExceptionPending(cx.raw_cx_no_gc()));
608 *vp = UndefinedValue();
609 return true;
610 } else {
611 throw_invalid_this((&mut cx).into(), proto_id);
612 return if EXCEPTION_TO_REJECTION {
613 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
614 } else {
615 false
616 };
617 }
618 },
619 };
620
621 if needs_security_check_on_interface_match {
624 let mut realm = js::realm::CurrentRealm::assert(&mut cx);
625 if is_cross_origin_object::<D>((&mut realm).into(), obj.handle().into()) &&
627 !<D as DomHelpers<D>>::is_platform_object_same_origin(&realm, obj.handle().into())
628 {
629 rooted!(&in(*realm) let mut void_jsid: js::jsapi::jsid);
634 let result =
635 report_cross_origin_denial::<D>(&mut realm, void_jsid.handle().into(), "call");
636 return if EXCEPTION_TO_REJECTION {
637 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
638 } else {
639 result
640 };
641 }
642 } else {
643 }
646
647 call(
648 info,
649 cx.raw_cx(),
650 obj.handle().into(),
651 this as *mut libc::c_void,
652 argc,
653 vp,
654 )
655}
656
657pub(crate) unsafe extern "C" fn generic_method<
663 D: DomTypes,
664 Policy: CallPolicy,
665 const EXCEPTION_TO_REJECTION: bool,
666>(
667 cx: *mut JSContext,
668 argc: libc::c_uint,
669 vp: *mut JSVal,
670) -> bool {
671 generic_call::<D, EXCEPTION_TO_REJECTION>(
672 cx,
673 argc,
674 vp,
675 Policy::INFO,
676 CallJitMethodOp,
677 CanGc::note(),
678 )
679}
680
681pub(crate) unsafe extern "C" fn generic_getter<
687 D: DomTypes,
688 Policy: CallPolicy,
689 const EXCEPTION_TO_REJECTION: bool,
690>(
691 cx: *mut JSContext,
692 argc: libc::c_uint,
693 vp: *mut JSVal,
694) -> bool {
695 generic_call::<D, EXCEPTION_TO_REJECTION>(
696 cx,
697 argc,
698 vp,
699 Policy::INFO,
700 CallJitGetterOp,
701 CanGc::note(),
702 )
703}
704
705unsafe extern "C" fn call_setter(
706 info: *const JSJitInfo,
707 cx: *mut JSContext,
708 handle: RawHandleObject,
709 this: *mut libc::c_void,
710 argc: u32,
711 vp: *mut JSVal,
712) -> bool {
713 if !CallJitSetterOp(info, cx, handle, this, argc, vp) {
714 return false;
715 }
716 *vp = UndefinedValue();
717 true
718}
719
720pub(crate) unsafe extern "C" fn generic_setter<D: DomTypes, Policy: CallPolicy>(
726 cx: *mut JSContext,
727 argc: libc::c_uint,
728 vp: *mut JSVal,
729) -> bool {
730 generic_call::<D, false>(cx, argc, vp, Policy::INFO, call_setter, CanGc::note())
731}
732
733pub(crate) unsafe extern "C" fn generic_static_promise_method(
739 cx: *mut JSContext,
740 argc: libc::c_uint,
741 vp: *mut JSVal,
742) -> bool {
743 let args = CallArgs::from_vp(vp, argc);
744
745 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
746 assert!(!info.is_null());
747 let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
750 if static_fn(cx, argc, vp) {
751 return true;
752 }
753 exception_to_promise(cx, args.rval(), CanGc::note())
754}
755
756pub(crate) unsafe fn exception_to_promise(
763 cx: *mut JSContext,
764 rval: RawMutableHandleValue,
765 _can_gc: CanGc,
766) -> bool {
767 rooted!(in(cx) let mut exception = UndefinedValue());
768 if !JS_GetPendingException(cx, exception.handle_mut()) {
769 return false;
770 }
771 JS_ClearPendingException(cx);
772 if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
773 promise.to_jsval(cx, MutableHandleValue::from_raw(rval));
774 true
775 } else {
776 JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
778 false
779 }
780}
781
782pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
788 let array = get_proto_or_iface_array(obj);
789 for proto in (*array).iter() {
790 if !proto.is_null() {
791 trace_object(
792 tracer,
793 "prototype",
794 &*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
795 );
796 }
797 }
798}
799
800pub(crate) unsafe extern "C" fn enumerate_global(
803 cx: *mut JSContext,
804 obj: RawHandleObject,
805 props: RawMutableHandleIdVector,
806 enumerable_only: bool,
807) -> bool {
808 assert!(JS_IsGlobalObject(obj.get()));
809 JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
810}
811
812pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
815 cx: *mut JSContext,
816 obj: RawHandleObject,
817 props: RawMutableHandleIdVector,
818 enumerable_only: bool,
819) -> bool {
820 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
821 if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
822 return false;
823 }
824
825 if enumerable_only {
826 return true;
829 }
830
831 let obj = Handle::from_raw(obj);
832 for (name, interface) in <D as DomHelpers<D>>::interface_map() {
833 if !(interface.enabled)(&mut cx, obj) {
834 continue;
835 }
836 let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
837 rooted!(&in(cx) let id = StringId(s));
838 if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
839 return false;
840 }
841 }
842 true
843}
844
845pub(crate) unsafe extern "C" fn may_resolve_global(
849 names: *const JSAtomState,
850 id: PropertyKey,
851 maybe_obj: *mut JSObject,
852) -> bool {
853 JS_MayResolveStandardClass(names, id, maybe_obj)
854}
855
856pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
860 names: *const JSAtomState,
861 id: PropertyKey,
862 maybe_obj: *mut JSObject,
863) -> bool {
864 if may_resolve_global(names, id, maybe_obj) {
865 return true;
866 }
867
868 let cx = Runtime::get()
869 .expect("There must be a JSContext active")
870 .as_ptr();
871 let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
872 return false;
873 };
874
875 <D as DomHelpers<D>>::interface_map().contains_key(bytes)
876}
877
878pub(crate) unsafe extern "C" fn resolve_global(
880 cx: *mut JSContext,
881 obj: RawHandleObject,
882 id: RawHandleId,
883 rval: *mut bool,
884) -> bool {
885 assert!(JS_IsGlobalObject(obj.get()));
886 JS_ResolveStandardClass(cx, obj, id, rval)
887}
888
889pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
891 cx: *mut JSContext,
892 obj: RawHandleObject,
893 id: RawHandleId,
894 rval: *mut bool,
895) -> bool {
896 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
897 if !resolve_global(cx.raw_cx(), obj, id, rval) {
898 return false;
899 }
900
901 if *rval {
902 return true;
903 }
904 let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
905 *rval = false;
906 return true;
907 };
908
909 if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
910 (interface.define)(&mut cx, Handle::from_raw(obj));
911 *rval = true;
912 } else {
913 *rval = false;
914 }
915 true
916}
917
918unsafe fn latin1_bytes_from_id(cx: *mut JSContext, id: jsid) -> Result<&'static [u8], ()> {
923 if !id.is_string() {
924 return Err(());
925 }
926
927 let string = id.to_string();
928 if !JS_DeprecatedStringHasLatin1Chars(string) {
929 return Err(());
930 }
931 let mut length = 0;
932 let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
933 assert!(!ptr.is_null());
934 Ok(slice::from_raw_parts(ptr, length))
935}