1use std::ffi::CStr;
6use std::os::raw::{c_char, c_void};
7use std::ptr::{self, NonNull};
8use std::slice;
9
10use js::context::{JSContext, RawJSContext};
11use js::conversions::{ToJSValConvertible, jsstr_to_string};
12use js::gc::Handle;
13use js::glue::{AppendToIdVector, JS_GetReservedSlot, RUST_FUNCTION_VALUE_TO_JITINFO};
14use js::jsapi::{
15 AtomToLinearString, CallArgs, ExceptionStackBehavior, GetLinearStringCharAt,
16 GetLinearStringLength, GetNonCCWObjectGlobal, HandleId as RawHandleId,
17 HandleObject as RawHandleObject, Heap, JS_AtomizeStringN, JS_DeprecatedStringHasLatin1Chars,
18 JS_GetLatin1StringCharsAndLength, JS_IsGlobalObject, JS_MayResolveStandardClass,
19 JS_NewEnumerateStandardClasses, JS_ResolveStandardClass, JSAtom, JSAtomState, JSJitInfo,
20 JSObject, JSPROP_ENUMERATE, JSTracer, MutableHandleIdVector as RawMutableHandleIdVector,
21 MutableHandleValue as RawMutableHandleValue, PropertyKey, StringIsArrayIndex, jsid,
22};
23use js::jsid::StringId;
24use js::jsval::{JSVal, UndefinedValue};
25use js::rust::wrappers2::{
26 CallJitGetterOp, CallJitMethodOp, CallJitSetterOp, CallOriginalPromiseReject,
27 JS_ClearPendingException, JS_DefineProperty, JS_ForwardGetPropertyTo, JS_GetPendingException,
28 JS_GetProperty, JS_GetPrototype, JS_HasOwnProperty, JS_HasProperty, JS_HasPropertyById,
29 JS_IsExceptionPending, JS_SetPendingException, JS_SetProperty,
30};
31use js::rust::{
32 HandleId, HandleObject, HandleValue, MutableHandleValue, Runtime, ToString, get_object_class,
33};
34use js::{JS_CALLEE, rooted};
35use malloc_size_of::MallocSizeOfOps;
36
37use crate::DomTypes;
38use crate::codegen::Globals::Globals;
39use crate::codegen::InheritTypes::TopTypeId;
40use crate::codegen::PrototypeList::{self, MAX_PROTO_CHAIN_LENGTH, PROTO_OR_IFACE_LENGTH};
41use crate::conversions::{PrototypeCheck, private_from_proto_check};
42use crate::error::throw_invalid_this;
43use crate::interfaces::DomHelpers;
44use crate::proxyhandler::{
45 is_cross_origin_object, is_platform_object_same_origin, report_cross_origin_denial,
46};
47use crate::str::DOMString;
48use crate::trace::trace_object;
49
50#[derive(Clone, Copy)]
52pub struct DOMClass {
53 pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
56
57 pub depth: u8,
59
60 pub type_id: TopTypeId,
62
63 pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
65
66 pub global: Globals,
68}
69unsafe impl Sync for DOMClass {}
70
71#[derive(Copy)]
73#[repr(C)]
74pub struct DOMJSClass {
75 pub base: js::jsapi::JSClass,
77 pub dom_class: DOMClass,
79}
80impl Clone for DOMJSClass {
81 fn clone(&self) -> DOMJSClass {
82 *self
83 }
84}
85unsafe impl Sync for DOMJSClass {}
86
87pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
90
91pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
94
95pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
100
101pub(crate) unsafe fn get_proto_or_iface_array(global: *mut JSObject) -> *mut ProtoOrIfaceArray {
107 assert_ne!(((*get_object_class(global)).flags & JSCLASS_DOM_GLOBAL), 0);
108 let mut slot = UndefinedValue();
109 JS_GetReservedSlot(global, DOM_PROTOTYPE_SLOT, &mut slot);
110 slot.to_private() as *mut ProtoOrIfaceArray
111}
112
113pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
115
116pub(crate) fn get_property_on_prototype(
121 cx: &mut JSContext,
122 proxy: HandleObject,
123 receiver: HandleValue,
124 id: HandleId,
125 found: &mut bool,
126 vp: MutableHandleValue,
127) -> bool {
128 rooted!(&in(cx) let mut proto = ptr::null_mut::<JSObject>());
129 if unsafe { !JS_GetPrototype(cx, proxy, proto.handle_mut()) || proto.is_null() } {
130 *found = false;
131 return true;
132 }
133 let mut has_property = false;
134 if unsafe { !JS_HasPropertyById(cx, proto.handle(), id, &mut has_property) } {
135 return false;
136 }
137 *found = has_property;
138 if !has_property {
139 return true;
140 }
141
142 unsafe { JS_ForwardGetPropertyTo(cx, proto.handle(), id, receiver, vp) }
143}
144
145pub fn get_array_index_from_id(id: HandleId) -> Option<u32> {
148 let raw_id = *id;
149 if raw_id.is_int() {
150 return Some(raw_id.to_int() as u32);
151 }
152
153 if raw_id.is_void() || !raw_id.is_string() {
154 return None;
155 }
156
157 unsafe {
158 let atom = raw_id.to_string() as *mut JSAtom;
159 let s = AtomToLinearString(atom);
160 if GetLinearStringLength(s) == 0 {
161 return None;
162 }
163
164 let chars = [GetLinearStringCharAt(s, 0)];
165 let first_char = char::decode_utf16(chars.iter().cloned())
166 .next()
167 .map_or('\0', |r| r.unwrap_or('\0'));
168 if first_char.is_ascii_lowercase() {
169 return None;
170 }
171
172 let mut i = 0;
173 if StringIsArrayIndex(s, &mut i) {
174 Some(i)
175 } else {
176 None
177 }
178 }
179
180 }
206
207pub(crate) fn find_enum_value<'a, T>(
211 cx: &mut JSContext,
212 v: HandleValue,
213 pairs: &'a [(&'static str, T)],
214) -> Result<(Option<&'a T>, DOMString), ()> {
215 match NonNull::new(unsafe { ToString(cx, v) }) {
216 Some(jsstr) => {
217 let search = unsafe { jsstr_to_string(cx, jsstr) }.into();
218 Ok((
219 pairs
220 .iter()
221 .find(|&&(key, _)| search == key)
222 .map(|(_, ev)| ev),
223 search,
224 ))
225 },
226 None => Err(()),
227 }
228}
229
230pub(crate) fn get_dictionary_property(
234 cx: &mut JSContext,
235 object: HandleObject,
236 property: &CStr,
237 rval: MutableHandleValue,
238) -> Result<bool, ()> {
239 if object.get().is_null() {
240 return Ok(false);
241 }
242
243 let mut found = false;
244 if unsafe { !JS_HasProperty(cx, object, property.as_ptr(), &mut found) } {
245 return Err(());
246 }
247
248 if !found {
249 return Ok(false);
250 }
251
252 if unsafe { !JS_GetProperty(cx, object, property.as_ptr(), rval) } {
253 return Err(());
254 }
255
256 Ok(true)
257}
258
259#[expect(clippy::result_unit_err)]
263pub fn set_dictionary_property(
264 cx: &mut JSContext,
265 object: HandleObject,
266 property: &CStr,
267 value: HandleValue,
268) -> Result<(), ()> {
269 if object.get().is_null() {
270 return Err(());
271 }
272
273 if unsafe { !JS_SetProperty(cx, object, property.as_ptr(), value) } {
274 return Err(());
275 }
276
277 Ok(())
278}
279
280#[expect(clippy::result_unit_err)]
284pub fn define_dictionary_property(
285 cx: &mut JSContext,
286 object: HandleObject,
287 property: &CStr,
288 value: HandleValue,
289) -> Result<(), ()> {
290 if object.get().is_null() {
291 return Err(());
292 }
293
294 if unsafe {
295 !JS_DefineProperty(
296 cx,
297 object,
298 property.as_ptr(),
299 value,
300 JSPROP_ENUMERATE as u32,
301 )
302 } {
303 return Err(());
304 }
305
306 Ok(())
307}
308
309#[expect(clippy::result_unit_err)]
313pub fn has_own_property(
314 cx: &mut JSContext,
315 object: HandleObject,
316 property: &CStr,
317) -> Result<bool, ()> {
318 if object.get().is_null() {
319 return Ok(false);
320 }
321
322 let mut found = false;
323 if unsafe { !JS_HasOwnProperty(cx, object, property.as_ptr(), &mut found) } {
324 return Err(());
325 }
326
327 Ok(found)
328}
329
330pub fn has_property_on_prototype(
336 cx: &mut JSContext,
337 proxy: HandleObject,
338 id: HandleId,
339 found: &mut bool,
340) -> bool {
341 rooted!(&in(cx) let mut proto = ptr::null_mut::<JSObject>());
342 if unsafe { !JS_GetPrototype(cx, proxy, proto.handle_mut()) } {
343 return false;
344 }
345 assert!(!proto.is_null());
346 unsafe { JS_HasPropertyById(cx, proto.handle(), id, found) }
347}
348
349pub trait CallPolicy {
350 const INFO: CallPolicyInfo;
351}
352pub mod call_policies {
353 use super::*;
354 pub struct Normal;
355 pub struct TargetClassMaybeCrossOrigin;
356 pub struct LenientThis;
357 pub struct LenientThisTargetClassMaybeCrossOrigin;
358 pub struct CrossOriginCallable;
359 impl CallPolicy for Normal {
360 const INFO: CallPolicyInfo = CallPolicyInfo {
361 lenient_this: false,
362 needs_security_check_on_interface_match: false,
363 };
364 }
365 impl CallPolicy for TargetClassMaybeCrossOrigin {
366 const INFO: CallPolicyInfo = CallPolicyInfo {
367 lenient_this: false,
368 needs_security_check_on_interface_match: true,
369 };
370 }
371 impl CallPolicy for LenientThis {
372 const INFO: CallPolicyInfo = CallPolicyInfo {
373 lenient_this: true,
374 needs_security_check_on_interface_match: false,
375 };
376 }
377 impl CallPolicy for LenientThisTargetClassMaybeCrossOrigin {
378 const INFO: CallPolicyInfo = CallPolicyInfo {
379 lenient_this: true,
380 needs_security_check_on_interface_match: true,
381 };
382 }
383 impl CallPolicy for CrossOriginCallable {
384 const INFO: CallPolicyInfo = CallPolicyInfo {
385 lenient_this: false,
386 needs_security_check_on_interface_match: false,
387 };
388 }
389}
390#[derive(Clone, Copy, Eq, PartialEq)]
397pub struct CallPolicyInfo {
398 pub lenient_this: bool,
401 pub needs_security_check_on_interface_match: bool,
420}
421
422unsafe fn generic_call<D: DomTypes, const EXCEPTION_TO_REJECTION: bool>(
423 cx: &mut JSContext,
424 argc: libc::c_uint,
425 vp: *mut JSVal,
426 CallPolicyInfo {
427 lenient_this,
428 needs_security_check_on_interface_match,
429 }: CallPolicyInfo,
430 call: unsafe fn(
431 *const JSJitInfo,
432 &mut JSContext,
433 HandleObject,
434 *mut libc::c_void,
435 u32,
436 *mut JSVal,
437 ) -> bool,
438) -> bool {
439 let args = CallArgs::from_vp(vp, argc);
440
441 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx_no_gc(), vp));
442 let proto_id = (*info).__bindgen_anon_2.protoID;
443
444 let thisobj = args.thisv();
520 if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
521 throw_invalid_this(cx, proto_id);
524 return if EXCEPTION_TO_REJECTION {
525 exception_to_promise(cx, args.rval())
526 } else {
527 false
528 };
529 }
530
531 rooted!(&in(cx) let obj = if thisobj.get().is_object() {
532 thisobj.get().to_object()
533 } else {
534 GetNonCCWObjectGlobal(JS_CALLEE(cx.raw_cx_no_gc(), vp).to_object_or_null())
535 });
536 let depth = (*info).__bindgen_anon_3.depth as usize;
537 let proto_check = PrototypeCheck::Depth { depth, proto_id };
538 let this = match private_from_proto_check(obj.get(), cx.raw_cx_no_gc(), proto_check) {
539 Ok(val) => val,
540 Err(()) => {
541 if lenient_this {
553 debug_assert!(!JS_IsExceptionPending(cx));
554 *vp = UndefinedValue();
555 return true;
556 } else {
557 throw_invalid_this(cx, proto_id);
558 return if EXCEPTION_TO_REJECTION {
559 exception_to_promise(cx, args.rval())
560 } else {
561 false
562 };
563 }
564 },
565 };
566
567 if needs_security_check_on_interface_match {
570 let mut realm = js::realm::CurrentRealm::assert(cx);
571 if is_cross_origin_object::<D>(&mut realm, obj.handle()) &&
573 !is_platform_object_same_origin(&realm, obj.handle())
574 {
575 rooted!(&in(*realm) let mut void_jsid: jsid);
580 let result = report_cross_origin_denial::<D>(&mut realm, void_jsid.handle(), "call");
581 return if EXCEPTION_TO_REJECTION {
582 exception_to_promise(cx, args.rval())
583 } else {
584 result
585 };
586 }
587 } else {
588 }
591
592 call(info, cx, obj.handle(), this as *mut libc::c_void, argc, vp)
593}
594
595pub(crate) unsafe extern "C" fn generic_method<
601 D: DomTypes,
602 Policy: CallPolicy,
603 const EXCEPTION_TO_REJECTION: bool,
604>(
605 cx: *mut RawJSContext,
606 argc: libc::c_uint,
607 vp: *mut JSVal,
608) -> bool {
609 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
611 let cx = &mut cx;
612
613 generic_call::<D, EXCEPTION_TO_REJECTION>(cx, argc, vp, Policy::INFO, CallJitMethodOp)
614}
615
616pub(crate) unsafe extern "C" fn generic_getter<
622 D: DomTypes,
623 Policy: CallPolicy,
624 const EXCEPTION_TO_REJECTION: bool,
625>(
626 cx: *mut RawJSContext,
627 argc: libc::c_uint,
628 vp: *mut JSVal,
629) -> bool {
630 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
632 let cx = &mut cx;
633
634 generic_call::<D, EXCEPTION_TO_REJECTION>(cx, argc, vp, Policy::INFO, CallJitGetterOp)
635}
636
637unsafe fn call_setter(
638 info: *const JSJitInfo,
639 cx: &mut JSContext,
640 handle: HandleObject,
641 this: *mut libc::c_void,
642 argc: u32,
643 vp: *mut JSVal,
644) -> bool {
645 if !CallJitSetterOp(info, cx, handle, this, argc, vp) {
646 return false;
647 }
648 *vp = UndefinedValue();
649 true
650}
651
652pub(crate) unsafe extern "C" fn generic_setter<D: DomTypes, Policy: CallPolicy>(
658 cx: *mut RawJSContext,
659 argc: libc::c_uint,
660 vp: *mut JSVal,
661) -> bool {
662 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
664 let cx = &mut cx;
665
666 generic_call::<D, false>(cx, argc, vp, Policy::INFO, call_setter)
667}
668
669pub(crate) unsafe extern "C" fn generic_static_promise_method(
675 cx: *mut RawJSContext,
676 argc: libc::c_uint,
677 vp: *mut JSVal,
678) -> bool {
679 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
681 let cx = &mut cx;
682
683 let args = CallArgs::from_vp(vp, argc);
684
685 let info = RUST_FUNCTION_VALUE_TO_JITINFO(JS_CALLEE(cx.raw_cx(), vp));
686 assert!(!info.is_null());
687 let static_fn = (*info).__bindgen_anon_1.staticMethod.unwrap();
690 if static_fn(cx.raw_cx(), argc, vp) {
691 return true;
692 }
693 exception_to_promise(cx, args.rval())
694}
695
696pub(crate) fn exception_to_promise(cx: &mut JSContext, rval: RawMutableHandleValue) -> bool {
700 unsafe {
701 rooted!(&in(cx) let mut exception = UndefinedValue());
702 if !JS_GetPendingException(cx, exception.handle_mut()) {
703 return false;
704 }
705 JS_ClearPendingException(cx);
706 if let Some(promise) = NonNull::new(CallOriginalPromiseReject(cx, exception.handle())) {
707 promise.safe_to_jsval(cx, MutableHandleValue::from_raw(rval));
708 true
709 } else {
710 JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
712 false
713 }
714 }
715}
716
717pub(crate) unsafe fn trace_global(tracer: *mut JSTracer, obj: *mut JSObject) {
723 let array = get_proto_or_iface_array(obj);
724 for proto in (*array).iter() {
725 if !proto.is_null() {
726 trace_object(
727 tracer,
728 "prototype",
729 &*(proto as *const *mut JSObject as *const Heap<*mut JSObject>),
730 );
731 }
732 }
733}
734
735pub(crate) unsafe extern "C" fn enumerate_global(
738 cx: *mut RawJSContext,
739 obj: RawHandleObject,
740 props: RawMutableHandleIdVector,
741 enumerable_only: bool,
742) -> bool {
743 assert!(JS_IsGlobalObject(obj.get()));
744 JS_NewEnumerateStandardClasses(cx, obj, props, enumerable_only)
745}
746
747pub(crate) unsafe extern "C" fn enumerate_window<D: DomTypes>(
750 cx: *mut RawJSContext,
751 obj: RawHandleObject,
752 props: RawMutableHandleIdVector,
753 enumerable_only: bool,
754) -> bool {
755 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
756 if !enumerate_global(cx.raw_cx(), obj, props, enumerable_only) {
757 return false;
758 }
759
760 if enumerable_only {
761 return true;
764 }
765
766 let obj = Handle::from_raw(obj);
767 for (name, interface) in <D as DomHelpers<D>>::interface_map() {
768 if !(interface.enabled)(&mut cx, obj) {
769 continue;
770 }
771 let s = JS_AtomizeStringN(cx.raw_cx(), name.as_ptr() as *const c_char, name.len());
772 rooted!(&in(cx) let id = StringId(s));
773 if s.is_null() || !AppendToIdVector(props, id.handle().into()) {
774 return false;
775 }
776 }
777 true
778}
779
780pub(crate) unsafe extern "C" fn may_resolve_global(
784 names: *const JSAtomState,
785 id: PropertyKey,
786 maybe_obj: *mut JSObject,
787) -> bool {
788 JS_MayResolveStandardClass(names, id, maybe_obj)
789}
790
791pub(crate) unsafe extern "C" fn may_resolve_window<D: DomTypes>(
795 names: *const JSAtomState,
796 id: PropertyKey,
797 maybe_obj: *mut JSObject,
798) -> bool {
799 if may_resolve_global(names, id, maybe_obj) {
800 return true;
801 }
802
803 let cx = Runtime::get()
804 .expect("There must be a JSContext active")
805 .as_ptr();
806 let Ok(bytes) = latin1_bytes_from_id(cx, id) else {
807 return false;
808 };
809
810 <D as DomHelpers<D>>::interface_map().contains_key(bytes)
811}
812
813pub(crate) unsafe extern "C" fn resolve_global(
815 cx: *mut RawJSContext,
816 obj: RawHandleObject,
817 id: RawHandleId,
818 rval: *mut bool,
819) -> bool {
820 assert!(JS_IsGlobalObject(obj.get()));
821 JS_ResolveStandardClass(cx, obj, id, rval)
822}
823
824pub(crate) unsafe extern "C" fn resolve_window<D: DomTypes>(
826 cx: *mut RawJSContext,
827 obj: RawHandleObject,
828 id: RawHandleId,
829 rval: *mut bool,
830) -> bool {
831 let mut cx = JSContext::from_ptr(NonNull::new(cx).unwrap());
832 if !resolve_global(cx.raw_cx(), obj, id, rval) {
833 return false;
834 }
835
836 if *rval {
837 return true;
838 }
839 let Ok(bytes) = latin1_bytes_from_id(cx.raw_cx(), *id) else {
840 *rval = false;
841 return true;
842 };
843
844 if let Some(interface) = <D as DomHelpers<D>>::interface_map().get(bytes) {
845 (interface.define)(&mut cx, Handle::from_raw(obj));
846 *rval = true;
847 } else {
848 *rval = false;
849 }
850 true
851}
852
853unsafe fn latin1_bytes_from_id(cx: *mut RawJSContext, id: jsid) -> Result<&'static [u8], ()> {
858 if !id.is_string() {
859 return Err(());
860 }
861
862 let string = id.to_string();
863 if !JS_DeprecatedStringHasLatin1Chars(string) {
864 return Err(());
865 }
866 let mut length = 0;
867 let ptr = JS_GetLatin1StringCharsAndLength(cx, ptr::null(), string, &mut length);
868 assert!(!ptr.is_null());
869 Ok(slice::from_raw_parts(ptr, length))
870}