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_GetPrototype, JS_HasOwnProperty, JS_HasPropertyById,
32 JS_SetPendingException, JS_SetProperty,
33};
34use js::rust::wrappers2::{JS_GetProperty, JS_HasProperty};
35use js::rust::{
36 HandleId, HandleObject, HandleValue, MutableHandleValue, Runtime, ToString, get_object_class,
37};
38use js::{JS_CALLEE, rooted};
39use malloc_size_of::MallocSizeOfOps;
40
41use crate::DomTypes;
42use crate::codegen::Globals::Globals;
43use crate::codegen::InheritTypes::TopTypeId;
44use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
45use crate::conversions::{PrototypeCheck, private_from_proto_check};
46use crate::error::throw_invalid_this;
47use crate::interfaces::DomHelpers;
48use crate::proxyhandler::{is_cross_origin_object, report_cross_origin_denial};
49use crate::script_runtime::{CanGc, JSContext as SafeJSContext};
50use crate::str::DOMString;
51use crate::trace::trace_object;
52
53#[derive(Clone, Copy)]
55pub struct DOMClass {
56 pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
59
60 pub depth: u8,
62
63 pub type_id: TopTypeId,
65
66 pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
68
69 pub global: Globals,
71}
72unsafe impl Sync for DOMClass {}
73
74#[derive(Copy)]
76#[repr(C)]
77pub struct DOMJSClass {
78 pub base: js::jsapi::JSClass,
80 pub dom_class: DOMClass,
82}
83impl Clone for DOMJSClass {
84 fn clone(&self) -> DOMJSClass {
85 *self
86 }
87}
88unsafe impl Sync for DOMJSClass {}
89
90pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
93
94pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
97
98pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
103
104pub(crate) unsafe fn get_proto_or_iface_array(global: *mut JSObject) -> *mut ProtoOrIfaceArray {
110 assert_ne!(((*get_object_class(global)).flags & JSCLASS_DOM_GLOBAL), 0);
111 let mut slot = UndefinedValue();
112 JS_GetReservedSlot(global, DOM_PROTOTYPE_SLOT, &mut slot);
113 slot.to_private() as *mut ProtoOrIfaceArray
114}
115
116pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
118
119pub(crate) unsafe fn get_property_on_prototype(
128 cx: *mut JSContext,
129 proxy: HandleObject,
130 receiver: HandleValue,
131 id: HandleId,
132 found: *mut bool,
133 vp: MutableHandleValue,
134) -> bool {
135 rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
136 if !JS_GetPrototype(cx, proxy, proto.handle_mut()) || proto.is_null() {
137 *found = false;
138 return true;
139 }
140 let mut has_property = false;
141 if !JS_HasPropertyById(cx, proto.handle(), id, &mut has_property) {
142 return false;
143 }
144 *found = has_property;
145 if !has_property {
146 return true;
147 }
148
149 JS_ForwardGetPropertyTo(cx, proto.handle(), id, receiver, vp)
150}
151
152pub fn get_array_index_from_id(id: HandleId) -> Option<u32> {
155 let raw_id = *id;
156 if raw_id.is_int() {
157 return Some(raw_id.to_int() as u32);
158 }
159
160 if raw_id.is_void() || !raw_id.is_string() {
161 return None;
162 }
163
164 unsafe {
165 let atom = raw_id.to_string() as *mut JSAtom;
166 let s = AtomToLinearString(atom);
167 if GetLinearStringLength(s) == 0 {
168 return None;
169 }
170
171 let chars = [GetLinearStringCharAt(s, 0)];
172 let first_char = char::decode_utf16(chars.iter().cloned())
173 .next()
174 .map_or('\0', |r| r.unwrap_or('\0'));
175 if first_char.is_ascii_lowercase() {
176 return None;
177 }
178
179 let mut i = 0;
180 if StringIsArrayIndex(s, &mut i) {
181 Some(i)
182 } else {
183 None
184 }
185 }
186
187 }
213
214#[allow(clippy::result_unit_err)]
221pub(crate) fn find_enum_value<'a, T>(
222 cx: &mut js::context::JSContext,
223 v: HandleValue,
224 pairs: &'a [(&'static str, T)],
225) -> Result<(Option<&'a T>, DOMString), ()> {
226 match ptr::NonNull::new(unsafe { ToString(cx.raw_cx(), v) }) {
227 Some(jsstr) => {
228 let search = unsafe { jsstr_to_string(cx.raw_cx(), jsstr).into() };
229 Ok((
230 pairs
231 .iter()
232 .find(|&&(key, _)| search == key)
233 .map(|(_, ev)| ev),
234 search,
235 ))
236 },
237 None => Err(()),
238 }
239}
240
241#[allow(clippy::result_unit_err)]
245pub fn get_dictionary_property(
246 cx: &mut js::context::JSContext,
247 object: HandleObject,
248 property: &CStr,
249 rval: MutableHandleValue,
250) -> Result<bool, ()> {
251 if object.get().is_null() {
252 return Ok(false);
253 }
254
255 let mut found = false;
256 if unsafe { !JS_HasProperty(cx, object, property.as_ptr(), &mut found) } {
257 return Err(());
258 }
259
260 if !found {
261 return Ok(false);
262 }
263
264 if unsafe { !JS_GetProperty(cx, object, property.as_ptr(), rval) } {
265 return Err(());
266 }
267
268 Ok(true)
269}
270
271#[allow(clippy::result_unit_err)]
275pub fn set_dictionary_property(
276 cx: SafeJSContext,
277 object: HandleObject,
278 property: &CStr,
279 value: HandleValue,
280) -> Result<(), ()> {
281 if object.get().is_null() {
282 return Err(());
283 }
284
285 unsafe {
286 if !JS_SetProperty(*cx, object, property.as_ptr(), value) {
287 return Err(());
288 }
289 }
290
291 Ok(())
292}
293
294#[allow(clippy::result_unit_err)]
298pub fn define_dictionary_property(
299 cx: SafeJSContext,
300 object: HandleObject,
301 property: &CStr,
302 value: HandleValue,
303) -> Result<(), ()> {
304 if object.get().is_null() {
305 return Err(());
306 }
307
308 unsafe {
309 if !JS_DefineProperty(
310 *cx,
311 object,
312 property.as_ptr(),
313 value,
314 JSPROP_ENUMERATE as u32,
315 ) {
316 return Err(());
317 }
318 }
319
320 Ok(())
321}
322
323#[allow(clippy::result_unit_err)]
327pub fn has_own_property(
328 cx: SafeJSContext,
329 object: HandleObject,
330 property: &CStr,
331) -> Result<bool, ()> {
332 if object.get().is_null() {
333 return Ok(false);
334 }
335
336 let mut found = false;
337 unsafe {
338 if !JS_HasOwnProperty(*cx, object, property.as_ptr(), &mut found) {
339 return Err(());
340 }
341 }
342
343 Ok(found)
344}
345
346pub unsafe fn has_property_on_prototype(
355 cx: *mut JSContext,
356 proxy: HandleObject,
357 id: HandleId,
358 found: &mut bool,
359) -> bool {
360 rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
361 if !JS_GetPrototype(cx, proxy, proto.handle_mut()) {
362 return false;
363 }
364 assert!(!proto.is_null());
365 JS_HasPropertyById(cx, proto.handle(), id, found)
366}
367
368pub(crate) unsafe fn delete_property_by_id(
373 cx: *mut JSContext,
374 object: HandleObject,
375 id: HandleId,
376 bp: *mut ObjectOpResult,
377) -> bool {
378 JS_DeletePropertyById(cx, object, id, bp)
379}
380
381pub trait CallPolicy {
382 const INFO: CallPolicyInfo;
383}
384pub mod call_policies {
385 use super::*;
386 pub struct Normal;
387 pub struct TargetClassMaybeCrossOrigin;
388 pub struct LenientThis;
389 pub struct LenientThisTargetClassMaybeCrossOrigin;
390 pub struct CrossOriginCallable;
391 impl CallPolicy for Normal {
392 const INFO: CallPolicyInfo = CallPolicyInfo {
393 lenient_this: false,
394 needs_security_check_on_interface_match: false,
395 };
396 }
397 impl CallPolicy for TargetClassMaybeCrossOrigin {
398 const INFO: CallPolicyInfo = CallPolicyInfo {
399 lenient_this: false,
400 needs_security_check_on_interface_match: true,
401 };
402 }
403 impl CallPolicy for LenientThis {
404 const INFO: CallPolicyInfo = CallPolicyInfo {
405 lenient_this: true,
406 needs_security_check_on_interface_match: false,
407 };
408 }
409 impl CallPolicy for LenientThisTargetClassMaybeCrossOrigin {
410 const INFO: CallPolicyInfo = CallPolicyInfo {
411 lenient_this: true,
412 needs_security_check_on_interface_match: true,
413 };
414 }
415 impl CallPolicy for CrossOriginCallable {
416 const INFO: CallPolicyInfo = CallPolicyInfo {
417 lenient_this: false,
418 needs_security_check_on_interface_match: false,
419 };
420 }
421}
422#[derive(Clone, Copy, Eq, PartialEq)]
429pub struct CallPolicyInfo {
430 pub lenient_this: bool,
433 pub needs_security_check_on_interface_match: bool,
452}
453
454unsafe fn generic_call<D: DomTypes, const EXCEPTION_TO_REJECTION: bool>(
455 cx: *mut JSContext,
456 argc: libc::c_uint,
457 vp: *mut JSVal,
458 CallPolicyInfo {
459 lenient_this,
460 needs_security_check_on_interface_match,
461 }: CallPolicyInfo,
462 call: unsafe extern "C" fn(
463 *const JSJitInfo,
464 *mut JSContext,
465 RawHandleObject,
466 *mut libc::c_void,
467 u32,
468 *mut JSVal,
469 ) -> bool,
470 can_gc: CanGc,
471) -> bool {
472 let mut cx = unsafe { js::context::JSContext::from_ptr(NonNull::new(cx).unwrap()) };
473 let args = CallArgs::from_vp(vp, argc);
474
475 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx_no_gc(), vp));
476 let proto_id = (*info).__bindgen_anon_2.protoID;
477
478 let thisobj = args.thisv();
554 if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
555 throw_invalid_this((&mut cx).into(), proto_id);
558 return if EXCEPTION_TO_REJECTION {
559 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
560 } else {
561 false
562 };
563 }
564
565 rooted!(&in(cx) let obj = if thisobj.get().is_object() {
566 thisobj.get().to_object()
567 } else {
568 GetNonCCWObjectGlobal(JS_CALLEE(cx.raw_cx_no_gc(), vp).to_object_or_null())
569 });
570 let depth = (*info).__bindgen_anon_3.depth as usize;
571 let proto_check = PrototypeCheck::Depth { depth, proto_id };
572 let this = match private_from_proto_check(obj.get(), cx.raw_cx_no_gc(), proto_check) {
573 Ok(val) => val,
574 Err(()) => {
575 if lenient_this {
587 debug_assert!(!JS_IsExceptionPending(cx.raw_cx_no_gc()));
588 *vp = UndefinedValue();
589 return true;
590 } else {
591 throw_invalid_this((&mut cx).into(), proto_id);
592 return if EXCEPTION_TO_REJECTION {
593 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
594 } else {
595 false
596 };
597 }
598 },
599 };
600
601 if needs_security_check_on_interface_match {
604 let mut realm = js::realm::CurrentRealm::assert(&mut cx);
605 if is_cross_origin_object::<D>((&mut realm).into(), obj.handle().into()) &&
607 !<D as DomHelpers<D>>::is_platform_object_same_origin(&realm, obj.handle().into())
608 {
609 rooted!(&in(*realm) let mut void_jsid: js::jsapi::jsid);
614 let result =
615 report_cross_origin_denial::<D>(&mut realm, void_jsid.handle().into(), "call");
616 return if EXCEPTION_TO_REJECTION {
617 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
618 } else {
619 result
620 };
621 }
622 } else {
623 }
626
627 call(
628 info,
629 cx.raw_cx(),
630 obj.handle().into(),
631 this as *mut libc::c_void,
632 argc,
633 vp,
634 )
635}
636
637pub(crate) unsafe extern "C" fn generic_method<
643 D: DomTypes,
644 Policy: CallPolicy,
645 const EXCEPTION_TO_REJECTION: bool,
646>(
647 cx: *mut JSContext,
648 argc: libc::c_uint,
649 vp: *mut JSVal,
650) -> bool {
651 generic_call::<D, EXCEPTION_TO_REJECTION>(
652 cx,
653 argc,
654 vp,
655 Policy::INFO,
656 CallJitMethodOp,
657 CanGc::deprecated_note(),
658 )
659}
660
661pub(crate) unsafe extern "C" fn generic_getter<
667 D: DomTypes,
668 Policy: CallPolicy,
669 const EXCEPTION_TO_REJECTION: bool,
670>(
671 cx: *mut JSContext,
672 argc: libc::c_uint,
673 vp: *mut JSVal,
674) -> bool {
675 generic_call::<D, EXCEPTION_TO_REJECTION>(
676 cx,
677 argc,
678 vp,
679 Policy::INFO,
680 CallJitGetterOp,
681 CanGc::deprecated_note(),
682 )
683}
684
685unsafe extern "C" fn call_setter(
686 info: *const JSJitInfo,
687 cx: *mut JSContext,
688 handle: RawHandleObject,
689 this: *mut libc::c_void,
690 argc: u32,
691 vp: *mut JSVal,
692) -> bool {
693 if !CallJitSetterOp(info, cx, handle, this, argc, vp) {
694 return false;
695 }
696 *vp = UndefinedValue();
697 true
698}
699
700pub(crate) unsafe extern "C" fn generic_setter<D: DomTypes, Policy: CallPolicy>(
706 cx: *mut JSContext,
707 argc: libc::c_uint,
708 vp: *mut JSVal,
709) -> bool {
710 generic_call::<D, false>(
711 cx,
712 argc,
713 vp,
714 Policy::INFO,
715 call_setter,
716 CanGc::deprecated_note(),
717 )
718}
719
720pub(crate) unsafe extern "C" fn generic_static_promise_method(
726 cx: *mut JSContext,
727 argc: libc::c_uint,
728 vp: *mut JSVal,
729) -> bool {
730 let args = CallArgs::from_vp(vp, argc);
731
732 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
733 assert!(!info.is_null());
734 let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
737 if static_fn(cx, argc, vp) {
738 return true;
739 }
740 exception_to_promise(cx, args.rval(), CanGc::deprecated_note())
741}
742
743pub(crate) unsafe fn exception_to_promise(
750 cx: *mut JSContext,
751 rval: RawMutableHandleValue,
752 _can_gc: CanGc,
753) -> bool {
754 rooted!(in(cx) let mut exception = UndefinedValue());
755 if !JS_GetPendingException(cx, exception.handle_mut()) {
756 return false;
757 }
758 JS_ClearPendingException(cx);
759 if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
760 promise.to_jsval(cx, MutableHandleValue::from_raw(rval));
761 true
762 } else {
763 JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
765 false
766 }
767}
768
769pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
775 let array = get_proto_or_iface_array(obj);
776 for proto in (*array).iter() {
777 if !proto.is_null() {
778 trace_object(
779 tracer,
780 "prototype",
781 &*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
782 );
783 }
784 }
785}
786
787pub(crate) unsafe extern "C" fn enumerate_global(
790 cx: *mut JSContext,
791 obj: RawHandleObject,
792 props: RawMutableHandleIdVector,
793 enumerable_only: bool,
794) -> bool {
795 assert!(JS_IsGlobalObject(obj.get()));
796 JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
797}
798
799pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
802 cx: *mut JSContext,
803 obj: RawHandleObject,
804 props: RawMutableHandleIdVector,
805 enumerable_only: bool,
806) -> bool {
807 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
808 if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
809 return false;
810 }
811
812 if enumerable_only {
813 return true;
816 }
817
818 let obj = Handle::from_raw(obj);
819 for (name, interface) in <D as DomHelpers<D>>::interface_map() {
820 if !(interface.enabled)(&mut cx, obj) {
821 continue;
822 }
823 let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
824 rooted!(&in(cx) let id = StringId(s));
825 if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
826 return false;
827 }
828 }
829 true
830}
831
832pub(crate) unsafe extern "C" fn may_resolve_global(
836 names: *const JSAtomState,
837 id: PropertyKey,
838 maybe_obj: *mut JSObject,
839) -> bool {
840 JS_MayResolveStandardClass(names, id, maybe_obj)
841}
842
843pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
847 names: *const JSAtomState,
848 id: PropertyKey,
849 maybe_obj: *mut JSObject,
850) -> bool {
851 if may_resolve_global(names, id, maybe_obj) {
852 return true;
853 }
854
855 let cx = Runtime::get()
856 .expect("There must be a JSContext active")
857 .as_ptr();
858 let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
859 return false;
860 };
861
862 <D as DomHelpers<D>>::interface_map().contains_key(bytes)
863}
864
865pub(crate) unsafe extern "C" fn resolve_global(
867 cx: *mut JSContext,
868 obj: RawHandleObject,
869 id: RawHandleId,
870 rval: *mut bool,
871) -> bool {
872 assert!(JS_IsGlobalObject(obj.get()));
873 JS_ResolveStandardClass(cx, obj, id, rval)
874}
875
876pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
878 cx: *mut JSContext,
879 obj: RawHandleObject,
880 id: RawHandleId,
881 rval: *mut bool,
882) -> bool {
883 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
884 if !resolve_global(cx.raw_cx(), obj, id, rval) {
885 return false;
886 }
887
888 if *rval {
889 return true;
890 }
891 let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
892 *rval = false;
893 return true;
894 };
895
896 if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
897 (interface.define)(&mut cx, Handle::from_raw(obj));
898 *rval = true;
899 } else {
900 *rval = false;
901 }
902 true
903}
904
905unsafe fn latin1_bytes_from_id(cx: *mut JSContext, id: jsid) -> Result<&'static [u8], ()> {
910 if !id.is_string() {
911 return Err(());
912 }
913
914 let string = id.to_string();
915 if !JS_DeprecatedStringHasLatin1Chars(string) {
916 return Err(());
917 }
918 let mut length = 0;
919 let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
920 assert!(!ptr.is_null());
921 Ok(slice::from_raw_parts(ptr, length))
922}