Skip to main content

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_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/// The struct that holds inheritance information for DOM object reflectors.
54#[derive(Clone, Copy)]
55pub struct DOMClass {
56    /// A list of interfaces that this object implements, in order of decreasing
57    /// derivedness.
58    pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
59
60    /// The last valid index of `interface_chain`.
61    pub depth: u8,
62
63    /// The type ID of that interface.
64    pub type_id: TopTypeId,
65
66    /// The MallocSizeOf function wrapper for that interface.
67    pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
68
69    /// The `Globals` flag for this global interface, if any.
70    pub global: Globals,
71}
72unsafe impl Sync for DOMClass {}
73
74/// The JSClass used for DOM object reflectors.
75#[derive(Copy)]
76#[repr(C)]
77pub struct DOMJSClass {
78    /// The actual JSClass.
79    pub base: js::jsapi::JSClass,
80    /// Associated data for DOM object reflectors.
81    pub dom_class: DOMClass,
82}
83impl Clone for DOMJSClass {
84    fn clone(&self) -> DOMJSClass {
85        *self
86    }
87}
88unsafe impl Sync for DOMJSClass {}
89
90/// The index of the slot where the object holder of that interface's
91/// unforgeable members are defined.
92pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
93
94/// The index of the slot that contains a reference to the ProtoOrIfaceArray.
95// All DOM globals must have a slot at DOM_PROTOTYPE_SLOT.
96pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
97
98/// The flag set on the `JSClass`es for DOM global objects.
99// NOTE: This is baked into the Ion JIT as 0 in codegen for LGetDOMProperty and
100// LSetDOMProperty. Those constants need to be changed accordingly if this value
101// changes.
102pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
103
104/// Returns the ProtoOrIfaceArray for the given global object.
105/// Fails if `global` is not a DOM global object.
106///
107/// # Safety
108/// `global` must point to a valid, non-null JS object.
109pub(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
116/// An array of *mut JSObject of size PROTO_OR_IFACE_LENGTH.
117pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
118
119/// Gets the property `id` on  `proxy`'s prototype. If it exists, `*found` is
120/// set to true and `*vp` to the value, otherwise `*found` is set to false.
121///
122/// Returns false on JSAPI failure.
123///
124/// # Safety
125/// `cx` must point to a valid, non-null JSContext.
126/// `found` must point to a valid, non-null bool.
127pub(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
152/// Get an array index from the given `jsid`. Returns `None` if the given
153/// `jsid` is not an integer.
154pub 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    /*let s = jsstr_to_string(cx, RUST_JSID_TO_STRING(raw_id));
188    if s.len() == 0 {
189        return None;
190    }
191
192    let first = s.chars().next().unwrap();
193    if first.is_ascii_lowercase() {
194        return None;
195    }
196
197    let mut i: u32 = 0;
198    let is_array = if s.is_ascii() {
199        let chars = s.as_bytes();
200        StringIsArrayIndex1(chars.as_ptr() as *const _, chars.len() as u32, &mut i)
201    } else {
202        let chars = s.encode_utf16().collect::<Vec<u16>>();
203        let slice = chars.as_slice();
204        StringIsArrayIndex2(slice.as_ptr(), chars.len() as u32, &mut i)
205    };
206
207    if is_array {
208        Some(i)
209    } else {
210        None
211    }*/
212}
213
214/// Find the enum equivelent of a string given by `v` in `pairs`.
215/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
216/// `Ok((None, value))` if there was no matching string.
217///
218/// # Safety
219/// `cx` must point to a valid, non-null JSContext.
220#[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/// Get the property with name `property` from `object`.
242/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
243/// `Ok(false)` if there was no property with the given name.
244#[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/// Set the property with name `property` from `object`.
272/// Returns `Err(())` on JSAPI failure, or null object,
273/// and Ok(()) otherwise
274#[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/// Define an own enumerable data property with name `property` on `object`.
295/// Returns `Err(())` on JSAPI failure, or null object,
296/// and Ok(()) otherwise.
297#[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/// Checks whether `object` has an own property named `property`.
324/// Returns `Err(())` on JSAPI failure (there is a pending exception),
325/// and `Ok(false)` for null objects or when the property is not own.
326#[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
346/// Computes whether `proxy` has a property `id` on its prototype and stores
347/// the result in `found`.
348///
349/// Returns a boolean indicating whether the check succeeded.
350/// If `false` is returned then the value of `found` is unspecified.
351///
352/// # Safety
353/// `cx` must point to a valid, non-null JSContext.
354pub 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
368/// Deletes the property `id` from `object`.
369///
370/// # Safety
371/// `cx` must point to a valid, non-null JSContext.
372pub(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/// Controls various details of an IDL operation, such as whether a
423/// `[`[`LegacyLenientThis`][1]`]` attribute is specified and preconditions that
424/// affect the outcome of the "[perform a security check][2]" steps.
425///
426/// [1]: https://heycam.github.io/webidl/#LegacyLenientThis
427/// [2]: https://html.spec.whatwg.org/multipage/#integration-with-idl
428#[derive(Clone, Copy, Eq, PartialEq)]
429pub struct CallPolicyInfo {
430    /// Specifies whether a `[LegacyLenientThis]` attribute is specified on the
431    /// interface member this operation is associated with.
432    pub lenient_this: bool,
433    /// Indicates whether a [security check][1] is required if the target object
434    /// implements this operation's interface.
435    ///
436    /// Regardless of this value, performing a security check is always
437    /// necessary if the target object doesn't implement this operation's
438    /// interface.
439    ///
440    /// This field is `false` iff any of the following are true:
441    ///
442    ///  - The operation is not implemented by any cross-origin objects (i.e.,
443    ///    any `Window` or `Location` objects).
444    ///
445    ///  - The operation is defined as cross origin (i.e., it's included in
446    ///    [`CrossOriginProperties`][2]`(obj)`, given `obj` implementing the
447    ///    operation's interface).
448    ///
449    /// [1]: https://html.spec.whatwg.org/multipage/#integration-with-idl
450    /// [2]: https://html.spec.whatwg.org/multipage/#crossoriginproperties-(-o-)
451    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    // <https://heycam.github.io/webidl/#es-operations>
479    //
480    // > To create an operation function, given an operation `op`, a namespace
481    // > or interface `target`, and a Realm `realm`:
482    // >
483    // > 2. Let `steps` be the following series of steps, [...]
484    // >
485    // > 2.1.2.1. Let `esValue` be the `this` value, if it is not `null` or
486    // >          `undefined`, or `realm`’s global object otherwise. [...]
487    // >
488    // > 2.1.2.2. If `esValue` is a platform object, then perform a security
489    // >          check, passing `esValue`, `id`, and "method".
490    // >
491    // > 2.1.2.3. If `esValue` does not implement the interface `target`, throw
492    // >          a `TypeError`. [...]
493    //
494    // <https://html.spec.whatwg.org/multipage/#integration-with-idl>
495    //
496    // > When perform a security check is invoked, with a `platformObject`,
497    // > `identifier`, and `type`, run these steps:
498    // >
499    // > 1. If `platformObject` is not a `Window` or `Location` object, then
500    // >    return.
501    // >
502    // > 2. For each `e` of `! CrossOriginProperties(platformObject)`:
503    // >
504    // > 2.1. If `SameValue(e.[[Property]], identifier)` is true, then:
505    // >
506    // > 2.1.1. If type is "method" and `e` has neither `[[NeedsGet]]` nor
507    // >        `[[NeedsSet]]`, then return. [... ditto for other types]
508    // >
509    // > 3. If `! IsPlatformObjectSameOrigin(platformObject)` is false, then
510    // >    throw a "SecurityError" `DOMException`.
511    //
512    // According to the above steps, the outcome of an IDL operation is
513    // determined be the following boolean variables:
514    //
515    //  - `this_same_origin`: The current principals object subsumes that of
516    //    `thisobj`
517    //  - `this_class_cross_origin`: `thisobj`'s class provides a cross-origin
518    //    member
519    //  - `cross_origin_operation`: The tuple `(operation_name, operation_type)`
520    //    (e.g., `("focus", "method")`) is a member of
521    //    `CrossOriginProperties(thisobj)`
522    //  - `this_implements_operation`: `thisobj`'s class implements the
523    //    current operation.
524    //
525    // The Karnaugh-esque map of the expected outcome is shown below:
526    //
527    //                            this_same_origin
528    //                                ,-------,
529    //                            ,---+---+---+---,
530    //                            | T | T | o | o |
531    //                          ,-+---+---+---+---+
532    //                          | | S | T | o | S |
533    //  this_class_cross_origin | +---+---+---+---+-,
534    //                          | | T | T | o | o | |
535    //                          '-+---+---+---+---+ | cross_origin_operation
536    //                            | T | T |   |   | |
537    //                            '---+---+---+---+-'
538    //                                    '-------'
539    //                            this_implements_operation
540    //
541    //       T: TypeError (generated by WebIDL opration function step 2.1.2.3)
542    //       S: SecurityError (generated by HTML security check step 3)
543    //       o: OK
544    //   blank: don't-care (impossible cases)
545    //
546    // Under some circumstances, we can rule out some cases from this map.
547    // E.g., if the operation is known to be not implemented by any cross-origin
548    // objects, `this_implements_operation → ¬this_class_cross_origin`, so in
549    // this case, we don't have to perform the security check at all if
550    // `thisobj`'s class implements the expected interface. `CallPolicyInfo::
551    // needs_security_check_on_interface_match` indicates whether this applies.
552
553    let thisobj = args.thisv();
554    if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
555        // `thisobj` is not a platform object, so the security check is not
556        // invoked in this case
557        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            // [this_implements_operation == false]
576            //
577            // For now, We don't check the conditions for `SecurityError` in
578            // this case, following WebKit's behavior.
579            //
580            // FIXME: Implement a different browser or the specification's
581            //        behavior? (They all differ subtly.) Some behavior is more
582            //        challenging to implement - for example, implementing the
583            //        specification's behavior requires `generic_call`'s code to
584            //        have access to the current IDL operation's name and type
585            //        and the target object's `CrossOriginProperties`.
586            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    // [this_implements_operation == true]
602
603    if needs_security_check_on_interface_match {
604        let mut realm = js::realm::CurrentRealm::assert(&mut cx);
605        // [cross_origin_operation == false]
606        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            // [this_class_cross_origin == true && this_same_origin == false]
610            // Throw a `SecurityError` `DOMException`.
611            // FIXME: `Handle<jsid>` could have a default constructor
612            //        like `Handle<Value>::null`
613            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        // [(cross_origin_operation == true && this_class_cross_origin == true)
624        //  || cross_origin_operation == false && this_class_cross_origin == false]
625    }
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
637/// Generic method of IDL interface.
638///
639/// # Safety
640/// `cx` must point to a valid, non-null JSContext.
641/// `vp` must point to a VALID, non-null JSVal.
642pub(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
661/// Generic getter of IDL interface.
662///
663/// # Safety
664/// `cx` must point to a valid, non-null JSContext.
665/// `vp` must point to a VALID, non-null JSVal.
666pub(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
700/// Generic setter of IDL interface.
701///
702/// # Safety
703/// `cx` must point to a valid, non-null JSContext.
704/// `vp` must point to a VALID, non-null JSVal.
705pub(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
720/// <https://searchfox.org/mozilla-central/rev/7279a1df13a819be254fd4649e07c4ff93e4bd45/dom/bindings/BindingUtils.cpp#3300>
721/// # Safety
722///
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_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    // TODO: we need safe wrappers for this in mozjs!
735    // assert_eq!((*info)._bitfield_1, JSJitInfo_OpType::StaticMethod as u8)
736    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
743/// Coverts exception to promise rejection
744///
745/// <https://searchfox.org/mozilla-central/rev/b220e40ff2ee3d10ce68e07d8a8a577d5558e2a2/dom/bindings/BindingUtils.cpp#3315>
746///
747/// # Safety
748/// `cx` must point to a valid, non-null JSContext.
749pub(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        // We just give up.  Put the exception back.
764        JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
765        false
766    }
767}
768
769/// Trace the resources held by reserved slots of a global object
770///
771/// # Safety
772/// `tracer` must point to a valid, non-null JSTracer.
773/// `obj` must point to a valid, non-null JSObject.
774pub(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
787/// Enumerate lazy properties of a global object.
788/// Modeled after <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/BindingUtils.cpp#L2814>
789pub(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
799/// Enumerate lazy properties of a global object that is a Window.
800/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/base/nsGlobalWindowInner.cpp#3297>
801pub(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        // All WebIDL interface names are defined as non-enumerable, so there's
814        // no point in checking them if we're only returning enumerable names.
815        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
832/// Returns true if the resolve hook for this global may resolve the provided id.
833/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/bindings/BindingUtils.cpp#2809>
834/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
835pub(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
843/// Returns true if the resolve hook for this window may resolve the provided id.
844/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/base/nsGlobalWindowInner.cpp#3275>
845/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
846pub(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
865/// Resolve a lazy global property, for interface objects and named constructors.
866pub(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
876/// Resolve a lazy global property for a Window global.
877pub(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
905/// Returns a slice of bytes corresponding to the bytes in the provided string id.
906/// Returns an error if the id is not a string, or the string contains non-latin1 characters.
907/// # Safety
908/// The slice is only valid as long as the original id is not garbage collected.
909unsafe 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}