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::deprecated_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::deprecated_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>(
731 cx,
732 argc,
733 vp,
734 Policy::INFO,
735 call_setter,
736 CanGc::deprecated_note(),
737 )
738}
739
740pub(crate) unsafe extern "C" fn generic_static_promise_method(
746 cx: *mut JSContext,
747 argc: libc::c_uint,
748 vp: *mut JSVal,
749) -> bool {
750 let args = CallArgs::from_vp(vp, argc);
751
752 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx, vp));
753 assert!(!info.is_null());
754 let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
757 if static_fn(cx, argc, vp) {
758 return true;
759 }
760 exception_to_promise(cx, args.rval(), CanGc::deprecated_note())
761}
762
763pub(crate) unsafe fn exception_to_promise(
770 cx: *mut JSContext,
771 rval: RawMutableHandleValue,
772 _can_gc: CanGc,
773) -> bool {
774 rooted!(in(cx) let mut exception = UndefinedValue());
775 if !JS_GetPendingException(cx, exception.handle_mut()) {
776 return false;
777 }
778 JS_ClearPendingException(cx);
779 if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
780 promise.to_jsval(cx, MutableHandleValue::from_raw(rval));
781 true
782 } else {
783 JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
785 false
786 }
787}
788
789pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
795 let array = get_proto_or_iface_array(obj);
796 for proto in (*array).iter() {
797 if !proto.is_null() {
798 trace_object(
799 tracer,
800 "prototype",
801 &*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
802 );
803 }
804 }
805}
806
807pub(crate) unsafe extern "C" fn enumerate_global(
810 cx: *mut JSContext,
811 obj: RawHandleObject,
812 props: RawMutableHandleIdVector,
813 enumerable_only: bool,
814) -> bool {
815 assert!(JS_IsGlobalObject(obj.get()));
816 JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
817}
818
819pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
822 cx: *mut JSContext,
823 obj: RawHandleObject,
824 props: RawMutableHandleIdVector,
825 enumerable_only: bool,
826) -> bool {
827 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
828 if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
829 return false;
830 }
831
832 if enumerable_only {
833 return true;
836 }
837
838 let obj = Handle::from_raw(obj);
839 for (name, interface) in <D as DomHelpers<D>>::interface_map() {
840 if !(interface.enabled)(&mut cx, obj) {
841 continue;
842 }
843 let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
844 rooted!(&in(cx) let id = StringId(s));
845 if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
846 return false;
847 }
848 }
849 true
850}
851
852pub(crate) unsafe extern "C" fn may_resolve_global(
856 names: *const JSAtomState,
857 id: PropertyKey,
858 maybe_obj: *mut JSObject,
859) -> bool {
860 JS_MayResolveStandardClass(names, id, maybe_obj)
861}
862
863pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
867 names: *const JSAtomState,
868 id: PropertyKey,
869 maybe_obj: *mut JSObject,
870) -> bool {
871 if may_resolve_global(names, id, maybe_obj) {
872 return true;
873 }
874
875 let cx = Runtime::get()
876 .expect("There must be a JSContext active")
877 .as_ptr();
878 let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
879 return false;
880 };
881
882 <D as DomHelpers<D>>::interface_map().contains_key(bytes)
883}
884
885pub(crate) unsafe extern "C" fn resolve_global(
887 cx: *mut JSContext,
888 obj: RawHandleObject,
889 id: RawHandleId,
890 rval: *mut bool,
891) -> bool {
892 assert!(JS_IsGlobalObject(obj.get()));
893 JS_ResolveStandardClass(cx, obj, id, rval)
894}
895
896pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
898 cx: *mut JSContext,
899 obj: RawHandleObject,
900 id: RawHandleId,
901 rval: *mut bool,
902) -> bool {
903 let mut cx = js::context::JSContext::from_ptr(NonNull::new(cx).unwrap());
904 if !resolve_global(cx.raw_cx(), obj, id, rval) {
905 return false;
906 }
907
908 if *rval {
909 return true;
910 }
911 let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
912 *rval = false;
913 return true;
914 };
915
916 if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
917 (interface.define)(&mut cx, Handle::from_raw(obj));
918 *rval = true;
919 } else {
920 *rval = false;
921 }
922 true
923}
924
925unsafe fn latin1_bytes_from_id(cx: *mut JSContext, id: jsid) -> Result<&'static [u8], ()> {
930 if !id.is_string() {
931 return Err(());
932 }
933
934 let string = id.to_string();
935 if !JS_DeprecatedStringHasLatin1Chars(string) {
936 return Err(());
937 }
938 let mut length = 0;
939 let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
940 assert!(!ptr.is_null());
941 Ok(slice::from_raw_parts(ptr, length))
942}