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, v) }) {
227 Some(jsstr) => {
228 let search = unsafe { jsstr_to_string(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
241pub(crate) fn get_dictionary_property(
245 cx: &mut js::context::JSContext,
246 object: HandleObject,
247 property: &CStr,
248 rval: MutableHandleValue,
249) -> Result<bool, ()> {
250 if object.get().is_null() {
251 return Ok(false);
252 }
253
254 let mut found = false;
255 if unsafe { !JS_HasProperty(cx, object, property.as_ptr(), &mut found) } {
256 return Err(());
257 }
258
259 if !found {
260 return Ok(false);
261 }
262
263 if unsafe { !JS_GetProperty(cx, object, property.as_ptr(), rval) } {
264 return Err(());
265 }
266
267 Ok(true)
268}
269
270#[allow(clippy::result_unit_err)]
274pub fn set_dictionary_property(
275 cx: SafeJSContext,
276 object: HandleObject,
277 property: &CStr,
278 value: HandleValue,
279) -> Result<(), ()> {
280 if object.get().is_null() {
281 return Err(());
282 }
283
284 unsafe {
285 if !JS_SetProperty(*cx, object, property.as_ptr(), value) {
286 return Err(());
287 }
288 }
289
290 Ok(())
291}
292
293#[allow(clippy::result_unit_err)]
297pub fn define_dictionary_property(
298 cx: SafeJSContext,
299 object: HandleObject,
300 property: &CStr,
301 value: HandleValue,
302) -> Result<(), ()> {
303 if object.get().is_null() {
304 return Err(());
305 }
306
307 unsafe {
308 if !JS_DefineProperty(
309 *cx,
310 object,
311 property.as_ptr(),
312 value,
313 JSPROP_ENUMERATE as u32,
314 ) {
315 return Err(());
316 }
317 }
318
319 Ok(())
320}
321
322#[allow(clippy::result_unit_err)]
326pub fn has_own_property(
327 cx: SafeJSContext,
328 object: HandleObject,
329 property: &CStr,
330) -> Result<bool, ()> {
331 if object.get().is_null() {
332 return Ok(false);
333 }
334
335 let mut found = false;
336 unsafe {
337 if !JS_HasOwnProperty(*cx, object, property.as_ptr(), &mut found) {
338 return Err(());
339 }
340 }
341
342 Ok(found)
343}
344
345pub unsafe fn has_property_on_prototype(
354 cx: *mut JSContext,
355 proxy: HandleObject,
356 id: HandleId,
357 found: &mut bool,
358) -> bool {
359 rooted!(in(cx) let mut proto = ptr::null_mut::<JSObject>());
360 if !JS_GetPrototype(cx, proxy, proto.handle_mut()) {
361 return false;
362 }
363 assert!(!proto.is_null());
364 JS_HasPropertyById(cx, proto.handle(), id, found)
365}
366
367pub(crate) unsafe fn delete_property_by_id(
372 cx: *mut JSContext,
373 object: HandleObject,
374 id: HandleId,
375 bp: *mut ObjectOpResult,
376) -> bool {
377 JS_DeletePropertyById(cx, object, id, bp)
378}
379
380pub trait CallPolicy {
381 const INFO: CallPolicyInfo;
382}
383pub mod call_policies {
384 use super::*;
385 pub struct Normal;
386 pub struct TargetClassMaybeCrossOrigin;
387 pub struct LenientThis;
388 pub struct LenientThisTargetClassMaybeCrossOrigin;
389 pub struct CrossOriginCallable;
390 impl CallPolicy for Normal {
391 const INFO: CallPolicyInfo = CallPolicyInfo {
392 lenient_this: false,
393 needs_security_check_on_interface_match: false,
394 };
395 }
396 impl CallPolicy for TargetClassMaybeCrossOrigin {
397 const INFO: CallPolicyInfo = CallPolicyInfo {
398 lenient_this: false,
399 needs_security_check_on_interface_match: true,
400 };
401 }
402 impl CallPolicy for LenientThis {
403 const INFO: CallPolicyInfo = CallPolicyInfo {
404 lenient_this: true,
405 needs_security_check_on_interface_match: false,
406 };
407 }
408 impl CallPolicy for LenientThisTargetClassMaybeCrossOrigin {
409 const INFO: CallPolicyInfo = CallPolicyInfo {
410 lenient_this: true,
411 needs_security_check_on_interface_match: true,
412 };
413 }
414 impl CallPolicy for CrossOriginCallable {
415 const INFO: CallPolicyInfo = CallPolicyInfo {
416 lenient_this: false,
417 needs_security_check_on_interface_match: false,
418 };
419 }
420}
421#[derive(Clone, Copy, Eq, PartialEq)]
428pub struct CallPolicyInfo {
429 pub lenient_this: bool,
432 pub needs_security_check_on_interface_match: bool,
451}
452
453unsafe fn generic_call<D: DomTypes, const EXCEPTION_TO_REJECTION: bool>(
454 cx: *mut JSContext,
455 argc: libc::c_uint,
456 vp: *mut JSVal,
457 CallPolicyInfo {
458 lenient_this,
459 needs_security_check_on_interface_match,
460 }: CallPolicyInfo,
461 call: unsafe extern "C" fn(
462 *const JSJitInfo,
463 *mut JSContext,
464 RawHandleObject,
465 *mut libc::c_void,
466 u32,
467 *mut JSVal,
468 ) -> bool,
469 can_gc: CanGc,
470) -> bool {
471 let mut cx = unsafe { js::context::JSContext::from_ptr(NonNull::new(cx).unwrap()) };
472 let args = CallArgs::from_vp(vp, argc);
473
474 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx_no_gc(), vp));
475 let proto_id = (*info).__bindgen_anon_2.protoID;
476
477 let thisobj = args.thisv();
553 if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
554 throw_invalid_this((&mut cx).into(), proto_id);
557 return if EXCEPTION_TO_REJECTION {
558 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
559 } else {
560 false
561 };
562 }
563
564 rooted!(&in(cx) let obj = if thisobj.get().is_object() {
565 thisobj.get().to_object()
566 } else {
567 GetNonCCWObjectGlobal(JS_CALLEE(cx.raw_cx_no_gc(), vp).to_object_or_null())
568 });
569 let depth = (*info).__bindgen_anon_3.depth as usize;
570 let proto_check = PrototypeCheck::Depth { depth, proto_id };
571 let this = match private_from_proto_check(obj.get(), cx.raw_cx_no_gc(), proto_check) {
572 Ok(val) => val,
573 Err(()) => {
574 if lenient_this {
586 debug_assert!(!JS_IsExceptionPending(cx.raw_cx_no_gc()));
587 *vp = UndefinedValue();
588 return true;
589 } else {
590 throw_invalid_this((&mut cx).into(), proto_id);
591 return if EXCEPTION_TO_REJECTION {
592 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
593 } else {
594 false
595 };
596 }
597 },
598 };
599
600 if needs_security_check_on_interface_match {
603 let mut realm = js::realm::CurrentRealm::assert(&mut cx);
604 if is_cross_origin_object::<D>((&mut realm).into(), obj.handle().into()) &&
606 !<D as DomHelpers<D>>::is_platform_object_same_origin(&realm, obj.handle().into())
607 {
608 rooted!(&in(*realm) let mut void_jsid: js::jsapi::jsid);
613 let result = report_cross_origin_denial::<D>(&mut realm, void_jsid.handle(), "call");
614 return if EXCEPTION_TO_REJECTION {
615 exception_to_promise(cx.raw_cx(), args.rval(), can_gc)
616 } else {
617 result
618 };
619 }
620 } else {
621 }
624
625 call(
626 info,
627 cx.raw_cx(),
628 obj.handle().into(),
629 this as *mut libc::c_void,
630 argc,
631 vp,
632 )
633}
634
635pub(crate) unsafe extern "C" fn generic_method<
641 D: DomTypes,
642 Policy: CallPolicy,
643 const EXCEPTION_TO_REJECTION: bool,
644>(
645 cx: *mut JSContext,
646 argc: libc::c_uint,
647 vp: *mut JSVal,
648) -> bool {
649 generic_call::<D, EXCEPTION_TO_REJECTION>(
650 cx,
651 argc,
652 vp,
653 Policy::INFO,
654 CallJitMethodOp,
655 CanGc::deprecated_note(),
656 )
657}
658
659pub(crate) unsafe extern "C" fn generic_getter<
665 D: DomTypes,
666 Policy: CallPolicy,
667 const EXCEPTION_TO_REJECTION: bool,
668>(
669 cx: *mut JSContext,
670 argc: libc::c_uint,
671 vp: *mut JSVal,
672) -> bool {
673 generic_call::<D, EXCEPTION_TO_REJECTION>(
674 cx,
675 argc,
676 vp,
677 Policy::INFO,
678 CallJitGetterOp,
679 CanGc::deprecated_note(),
680 )
681}
682
683unsafe extern "C" fn call_setter(
684 info: *const JSJitInfo,
685 cx: *mut JSContext,
686 handle: RawHandleObject,
687 this: *mut libc::c_void,
688 argc: u32,
689 vp: *mut JSVal,
690) -> bool {
691 if !CallJitSetterOp(info, cx, handle, this, argc, vp) {
692 return false;
693 }
694 *vp = UndefinedValue();
695 true
696}
697
698pub(crate) unsafe extern "C" fn generic_setter<D: DomTypes, Policy: CallPolicy>(
704 cx: *mut JSContext,
705 argc: libc::c_uint,
706 vp: *mut JSVal,
707) -> bool {
708 generic_call::<D, false>(
709 cx,
710 argc,
711 vp,
712 Policy::INFO,
713 call_setter,
714 CanGc::deprecated_note(),
715 )
716}
717
718pub(crate) unsafe extern "C" fn generic_static_promise_method(
724 cx: *mut JSContext,
725 argc: libc::c_uint,
726 vp: *mut JSVal,
727) -> bool {
728 let args = CallArgs::from_vp(vp, argc);
729
730 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
731 assert!(!info.is_null());
732 let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
735 if static_fn(cx, argc, vp) {
736 return true;
737 }
738 exception_to_promise(cx, args.rval(), CanGc::deprecated_note())
739}
740
741pub(crate) unsafe fn exception_to_promise(
748 cx: *mut JSContext,
749 rval: RawMutableHandleValue,
750 _can_gc: CanGc,
751) -> bool {
752 rooted!(in(cx) let mut exception = UndefinedValue());
753 if !JS_GetPendingException(cx, exception.handle_mut()) {
754 return false;
755 }
756 JS_ClearPendingException(cx);
757 if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
758 promise.to_jsval(cx, MutableHandleValue::from_raw(rval));
759 true
760 } else {
761 JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
763 false
764 }
765}
766
767pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
773 let array = get_proto_or_iface_array(obj);
774 for proto in (*array).iter() {
775 if !proto.is_null() {
776 trace_object(
777 tracer,
778 "prototype",
779 &*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
780 );
781 }
782 }
783}
784
785pub(crate) unsafe extern "C" fn enumerate_global(
788 cx: *mut JSContext,
789 obj: RawHandleObject,
790 props: RawMutableHandleIdVector,
791 enumerable_only: bool,
792) -> bool {
793 assert!(JS_IsGlobalObject(obj.get()));
794 JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
795}
796
797pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
800 cx: *mut JSContext,
801 obj: RawHandleObject,
802 props: RawMutableHandleIdVector,
803 enumerable_only: bool,
804) -> bool {
805 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
806 if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
807 return false;
808 }
809
810 if enumerable_only {
811 return true;
814 }
815
816 let obj = Handle::from_raw(obj);
817 for (name, interface) in <D as DomHelpers<D>>::interface_map() {
818 if !(interface.enabled)(&mut cx, obj) {
819 continue;
820 }
821 let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
822 rooted!(&in(cx) let id = StringId(s));
823 if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
824 return false;
825 }
826 }
827 true
828}
829
830pub(crate) unsafe extern "C" fn may_resolve_global(
834 names: *const JSAtomState,
835 id: PropertyKey,
836 maybe_obj: *mut JSObject,
837) -> bool {
838 JS_MayResolveStandardClass(names, id, maybe_obj)
839}
840
841pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
845 names: *const JSAtomState,
846 id: PropertyKey,
847 maybe_obj: *mut JSObject,
848) -> bool {
849 if may_resolve_global(names, id, maybe_obj) {
850 return true;
851 }
852
853 let cx = Runtime::get()
854 .expect("There must be a JSContext active")
855 .as_ptr();
856 let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
857 return false;
858 };
859
860 <D as DomHelpers<D>>::interface_map().contains_key(bytes)
861}
862
863pub(crate) unsafe extern "C" fn resolve_global(
865 cx: *mut JSContext,
866 obj: RawHandleObject,
867 id: RawHandleId,
868 rval: *mut bool,
869) -> bool {
870 assert!(JS_IsGlobalObject(obj.get()));
871 JS_ResolveStandardClass(cx, obj, id, rval)
872}
873
874pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
876 cx: *mut JSContext,
877 obj: RawHandleObject,
878 id: RawHandleId,
879 rval: *mut bool,
880) -> bool {
881 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
882 if !resolve_global(cx.raw_cx(), obj, id, rval) {
883 return false;
884 }
885
886 if *rval {
887 return true;
888 }
889 let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
890 *rval = false;
891 return true;
892 };
893
894 if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
895 (interface.define)(&mut cx, Handle::from_raw(obj));
896 *rval = true;
897 } else {
898 *rval = false;
899 }
900 true
901}
902
903unsafe fn latin1_bytes_from_id(cx: *mut JSContext, id: jsid) -> Result<&'static [u8], ()> {
908 if !id.is_string() {
909 return Err(());
910 }
911
912 let string = id.to_string();
913 if !JS_DeprecatedStringHasLatin1Chars(string) {
914 return Err(());
915 }
916 let mut length = 0;
917 let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
918 assert!(!ptr.is_null());
919 Ok(slice::from_raw_parts(ptr, length))
920}