script_bindings/
utils.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use 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/// The struct that holds inheritance information for DOM object reflectors.
53#[derive(Clone, Copy)]
54pub struct DOMClass {
55    /// A list of interfaces that this object implements, in order of decreasing
56    /// derivedness.
57    pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
58
59    /// The last valid index of `interface_chain`.
60    pub depth: u8,
61
62    /// The type ID of that interface.
63    pub type_id: TopTypeId,
64
65    /// The MallocSizeOf function wrapper for that interface.
66    pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
67
68    /// The `Globals` flag for this global interface, if any.
69    pub global: Globals,
70}
71unsafe impl Sync for DOMClass {}
72
73/// The JSClass used for DOM object reflectors.
74#[derive(Copy)]
75#[repr(C)]
76pub struct DOMJSClass {
77    /// The actual JSClass.
78    pub base: js::jsapi::JSClass,
79    /// Associated data for DOM object reflectors.
80    pub dom_class: DOMClass,
81}
82impl Clone for DOMJSClass {
83    fn clone(&self) -> DOMJSClass {
84        *self
85    }
86}
87unsafe impl Sync for DOMJSClass {}
88
89/// The index of the slot where the object holder of that interface's
90/// unforgeable members are defined.
91pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
92
93/// The index of the slot that contains a reference to the ProtoOrIfaceArray.
94// All DOM globals must have a slot at DOM_PROTOTYPE_SLOT.
95pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
96
97/// The flag set on the `JSClass`es for DOM global objects.
98// NOTE: This is baked into the Ion JIT as 0 in codegen for LGetDOMProperty and
99// LSetDOMProperty. Those constants need to be changed accordingly if this value
100// changes.
101pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
102
103/// Returns the ProtoOrIfaceArray for the given global object.
104/// Fails if `global` is not a DOM global object.
105///
106/// # Safety
107/// `global` must point to a valid, non-null JS object.
108pub(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
115/// An array of *mut JSObject of size PROTO_OR_IFACE_LENGTH.
116pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
117
118/// Gets the property `id` on  `proxy`'s prototype. If it exists, `*found` is
119/// set to true and `*vp` to the value, otherwise `*found` is set to false.
120///
121/// Returns false on JSAPI failure.
122///
123/// # Safety
124/// `cx` must point to a valid, non-null JSContext.
125/// `found` must point to a valid, non-null bool.
126pub(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
151/// Get an array index from the given `jsid`. Returns `None` if the given
152/// `jsid` is not an integer.
153pub 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    /*let s = jsstr_to_string(cx, RUST_JSID_TO_STRING(raw_id));
187    if s.len() == 0 {
188        return None;
189    }
190
191    let first = s.chars().next().unwrap();
192    if first.is_ascii_lowercase() {
193        return None;
194    }
195
196    let mut i: u32 = 0;
197    let is_array = if s.is_ascii() {
198        let chars = s.as_bytes();
199        StringIsArrayIndex1(chars.as_ptr() as *const _, chars.len() as u32, &mut i)
200    } else {
201        let chars = s.encode_utf16().collect::<Vec<u16>>();
202        let slice = chars.as_slice();
203        StringIsArrayIndex2(slice.as_ptr(), chars.len() as u32, &mut i)
204    };
205
206    if is_array {
207        Some(i)
208    } else {
209        None
210    }*/
211}
212
213/// Find the enum equivelent of a string given by `v` in `pairs`.
214/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
215/// `Ok((None, value))` if there was no matching string.
216///
217/// # Safety
218/// `cx` must point to a valid, non-null JSContext.
219#[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/// Get the property with name `property` from `object`.
241/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
242/// `Ok(false)` if there was no property with the given name.
243///
244/// # Safety
245/// `cx` must point to a valid, non-null JSContext.
246#[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/// Set the property with name `property` from `object`.
292/// Returns `Err(())` on JSAPI failure, or null object,
293/// and Ok(()) otherwise
294#[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/// Define an own enumerable data property with name `property` on `object`.
315/// Returns `Err(())` on JSAPI failure, or null object,
316/// and Ok(()) otherwise.
317#[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/// Checks whether `object` has an own property named `property`.
344/// Returns `Err(())` on JSAPI failure (there is a pending exception),
345/// and `Ok(false)` for null objects or when the property is not own.
346#[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
366/// Computes whether `proxy` has a property `id` on its prototype and stores
367/// the result in `found`.
368///
369/// Returns a boolean indicating whether the check succeeded.
370/// If `false` is returned then the value of `found` is unspecified.
371///
372/// # Safety
373/// `cx` must point to a valid, non-null JSContext.
374pub 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
388/// Deletes the property `id` from `object`.
389///
390/// # Safety
391/// `cx` must point to a valid, non-null JSContext.
392pub(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/// Controls various details of an IDL operation, such as whether a
443/// `[`[`LegacyLenientThis`][1]`]` attribute is specified and preconditions that
444/// affect the outcome of the "[perform a security check][2]" steps.
445///
446/// [1]: https://heycam.github.io/webidl/#LegacyLenientThis
447/// [2]: https://html.spec.whatwg.org/multipage/#integration-with-idl
448#[derive(Clone, Copy, Eq, PartialEq)]
449pub struct CallPolicyInfo {
450    /// Specifies whether a `[LegacyLenientThis]` attribute is specified on the
451    /// interface member this operation is associated with.
452    pub lenient_this: bool,
453    /// Indicates whether a [security check][1] is required if the target object
454    /// implements this operation's interface.
455    ///
456    /// Regardless of this value, performing a security check is always
457    /// necessary if the target object doesn't implement this operation's
458    /// interface.
459    ///
460    /// This field is `false` iff any of the following are true:
461    ///
462    ///  - The operation is not implemented by any cross-origin objects (i.e.,
463    ///    any `Window` or `Location` objects).
464    ///
465    ///  - The operation is defined as cross origin (i.e., it's included in
466    ///    [`CrossOriginProperties`][2]`(obj)`, given `obj` implementing the
467    ///    operation's interface).
468    ///
469    /// [1]: https://html.spec.whatwg.org/multipage/#integration-with-idl
470    /// [2]: https://html.spec.whatwg.org/multipage/#crossoriginproperties-(-o-)
471    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    // <https://heycam.github.io/webidl/#es-operations>
499    //
500    // > To create an operation function, given an operation `op`, a namespace
501    // > or interface `target`, and a Realm `realm`:
502    // >
503    // > 2. Let `steps` be the following series of steps, [...]
504    // >
505    // > 2.1.2.1. Let `esValue` be the `this` value, if it is not `null` or
506    // >          `undefined`, or `realm`’s global object otherwise. [...]
507    // >
508    // > 2.1.2.2. If `esValue` is a platform object, then perform a security
509    // >          check, passing `esValue`, `id`, and "method".
510    // >
511    // > 2.1.2.3. If `esValue` does not implement the interface `target`, throw
512    // >          a `TypeError`. [...]
513    //
514    // <https://html.spec.whatwg.org/multipage/#integration-with-idl>
515    //
516    // > When perform a security check is invoked, with a `platformObject`,
517    // > `identifier`, and `type`, run these steps:
518    // >
519    // > 1. If `platformObject` is not a `Window` or `Location` object, then
520    // >    return.
521    // >
522    // > 2. For each `e` of `! CrossOriginProperties(platformObject)`:
523    // >
524    // > 2.1. If `SameValue(e.[[Property]], identifier)` is true, then:
525    // >
526    // > 2.1.1. If type is "method" and `e` has neither `[[NeedsGet]]` nor
527    // >        `[[NeedsSet]]`, then return. [... ditto for other types]
528    // >
529    // > 3. If `! IsPlatformObjectSameOrigin(platformObject)` is false, then
530    // >    throw a "SecurityError" `DOMException`.
531    //
532    // According to the above steps, the outcome of an IDL operation is
533    // determined be the following boolean variables:
534    //
535    //  - `this_same_origin`: The current principals object subsumes that of
536    //    `thisobj`
537    //  - `this_class_cross_origin`: `thisobj`'s class provides a cross-origin
538    //    member
539    //  - `cross_origin_operation`: The tuple `(operation_name, operation_type)`
540    //    (e.g., `("focus", "method")`) is a member of
541    //    `CrossOriginProperties(thisobj)`
542    //  - `this_implements_operation`: `thisobj`'s class implements the
543    //    current operation.
544    //
545    // The Karnaugh-esque map of the expected outcome is shown below:
546    //
547    //                            this_same_origin
548    //                                ,-------,
549    //                            ,---+---+---+---,
550    //                            | T | T | o | o |
551    //                          ,-+---+---+---+---+
552    //                          | | S | T | o | S |
553    //  this_class_cross_origin | +---+---+---+---+-,
554    //                          | | T | T | o | o | |
555    //                          '-+---+---+---+---+ | cross_origin_operation
556    //                            | T | T |   |   | |
557    //                            '---+---+---+---+-'
558    //                                    '-------'
559    //                            this_implements_operation
560    //
561    //       T: TypeError (generated by WebIDL opration function step 2.1.2.3)
562    //       S: SecurityError (generated by HTML security check step 3)
563    //       o: OK
564    //   blank: don't-care (impossible cases)
565    //
566    // Under some circumstances, we can rule out some cases from this map.
567    // E.g., if the operation is known to be not implemented by any cross-origin
568    // objects, `this_implements_operation → ¬this_class_cross_origin`, so in
569    // this case, we don't have to perform the security check at all if
570    // `thisobj`'s class implements the expected interface. `CallPolicyInfo::
571    // needs_security_check_on_interface_match` indicates whether this applies.
572
573    let thisobj = args.thisv();
574    if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
575        // `thisobj` is not a platform object, so the security check is not
576        // invoked in this case
577        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            // [this_implements_operation == false]
596            //
597            // For now, We don't check the conditions for `SecurityError` in
598            // this case, following WebKit's behavior.
599            //
600            // FIXME: Implement a different browser or the specification's
601            //        behavior? (They all differ subtly.) Some behavior is more
602            //        challenging to implement - for example, implementing the
603            //        specification's behavior requires `generic_call`'s code to
604            //        have access to the current IDL operation's name and type
605            //        and the target object's `CrossOriginProperties`.
606            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    // [this_implements_operation == true]
622
623    if needs_security_check_on_interface_match {
624        let mut realm = js::realm::CurrentRealm::assert(&mut cx);
625        // [cross_origin_operation == false]
626        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            // [this_class_cross_origin == true && this_same_origin == false]
630            // Throw a `SecurityError` `DOMException`.
631            // FIXME: `Handle<jsid>` could have a default constructor
632            //        like `Handle<Value>::null`
633            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        // [(cross_origin_operation == true && this_class_cross_origin == true)
644        //  || cross_origin_operation == false && this_class_cross_origin == false]
645    }
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
657/// Generic method of IDL interface.
658///
659/// # Safety
660/// `cx` must point to a valid, non-null JSContext.
661/// `vp` must point to a VALID, non-null JSVal.
662pub(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
681/// Generic getter of IDL interface.
682///
683/// # Safety
684/// `cx` must point to a valid, non-null JSContext.
685/// `vp` must point to a VALID, non-null JSVal.
686pub(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
720/// Generic setter of IDL interface.
721///
722/// # Safety
723/// `cx` must point to a valid, non-null JSContext.
724/// `vp` must point to a VALID, non-null JSVal.
725pub(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
733/// <https://searchfox.org/mozilla-central/rev/7279a1df13a819be254fd4649e07c4ff93e4bd45/dom/bindings/BindingUtils.cpp#3300>
734/// # Safety
735///
736/// `cx` must point to a valid, non-null JSContext.
737/// `vp` must point to a VALID, non-null JSVal.
738pub(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    // TODO: we need safe wrappers for this in mozjs!
748    // assert_eq!((*info)._bitfield_1, JSJitInfo_OpType::StaticMethod as u8)
749    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
756/// Coverts exception to promise rejection
757///
758/// <https://searchfox.org/mozilla-central/rev/b220e40ff2ee3d10ce68e07d8a8a577d5558e2a2/dom/bindings/BindingUtils.cpp#3315>
759///
760/// # Safety
761/// `cx` must point to a valid, non-null JSContext.
762pub(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        // We just give up.  Put the exception back.
777        JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
778        false
779    }
780}
781
782/// Trace the resources held by reserved slots of a global object
783///
784/// # Safety
785/// `tracer` must point to a valid, non-null JSTracer.
786/// `obj` must point to a valid, non-null JSObject.
787pub(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
800/// Enumerate lazy properties of a global object.
801/// Modeled after <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/BindingUtils.cpp#L2814>
802pub(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
812/// Enumerate lazy properties of a global object that is a Window.
813/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/base/nsGlobalWindowInner.cpp#3297>
814pub(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        // All WebIDL interface names are defined as non-enumerable, so there's
827        // no point in checking them if we're only returning enumerable names.
828        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
845/// Returns true if the resolve hook for this global may resolve the provided id.
846/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/bindings/BindingUtils.cpp#2809>
847/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
848pub(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
856/// Returns true if the resolve hook for this window may resolve the provided id.
857/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/base/nsGlobalWindowInner.cpp#3275>
858/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
859pub(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
878/// Resolve a lazy global property, for interface objects and named constructors.
879pub(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
889/// Resolve a lazy global property for a Window global.
890pub(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
918/// Returns a slice of bytes corresponding to the bytes in the provided string id.
919/// Returns an error if the id is not a string, or the string contains non-latin1 characters.
920/// # Safety
921/// The slice is only valid as long as the original id is not garbage collected.
922unsafe 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}