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::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/// The struct that holds inheritance information for DOM object reflectors.
51#[derive(Clone, Copy)]
52pub struct DOMClass {
53    /// A list of interfaces that this object implements, in order of decreasing
54    /// derivedness.
55    pub interface_chain: [PrototypeList::ID; MAX_PROTO_CHAIN_LENGTH],
56
57    /// The last valid index of `interface_chain`.
58    pub depth: u8,
59
60    /// The type ID of that interface.
61    pub type_id: TopTypeId,
62
63    /// The MallocSizeOf function wrapper for that interface.
64    pub malloc_size_of: unsafe fn(ops: &mut MallocSizeOfOps, *const c_void) -> usize,
65
66    /// The `Globals` flag for this global interface, if any.
67    pub global: Globals,
68}
69unsafe impl Sync for DOMClass {}
70
71/// The JSClass used for DOM object reflectors.
72#[derive(Copy)]
73#[repr(C)]
74pub struct DOMJSClass {
75    /// The actual JSClass.
76    pub base: js::jsapi::JSClass,
77    /// Associated data for DOM object reflectors.
78    pub dom_class: DOMClass,
79}
80impl Clone for DOMJSClass {
81    fn clone(&self) -> DOMJSClass {
82        *self
83    }
84}
85unsafe impl Sync for DOMJSClass {}
86
87/// The index of the slot where the object holder of that interface's
88/// unforgeable members are defined.
89pub(crate) const DOM_PROTO_UNFORGEABLE_HOLDER_SLOT: u32 = 0;
90
91/// The index of the slot that contains a reference to the ProtoOrIfaceArray.
92// All DOM globals must have a slot at DOM_PROTOTYPE_SLOT.
93pub(crate) const DOM_PROTOTYPE_SLOT: u32 = js::JSCLASS_GLOBAL_SLOT_COUNT;
94
95/// The flag set on the `JSClass`es for DOM global objects.
96// NOTE: This is baked into the Ion JIT as 0 in codegen for LGetDOMProperty and
97// LSetDOMProperty. Those constants need to be changed accordingly if this value
98// changes.
99pub(crate) const JSCLASS_DOM_GLOBAL: u32 = js::JSCLASS_USERBIT1;
100
101/// Returns the ProtoOrIfaceArray for the given global object.
102/// Fails if `global` is not a DOM global object.
103///
104/// # Safety
105/// `global` must point to a valid, non-null JS object.
106pub(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
113/// An array of *mut JSObject of size PROTO_OR_IFACE_LENGTH.
114pub type ProtoOrIfaceArray = [*mut JSObject; PROTO_OR_IFACE_LENGTH];
115
116/// Gets the property `id` on  `proxy`'s prototype. If it exists, `*found` is
117/// set to true and `*vp` to the value, otherwise `*found` is set to false.
118///
119/// Returns false on JSAPI failure.
120pub(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
145/// Get an array index from the given `jsid`. Returns `None` if the given
146/// `jsid` is not an integer.
147pub 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    /*let s = jsstr_to_string(cx, RUST_JSID_TO_STRING(raw_id));
181    if s.len() == 0 {
182        return None;
183    }
184
185    let first = s.chars().next().unwrap();
186    if first.is_ascii_lowercase() {
187        return None;
188    }
189
190    let mut i: u32 = 0;
191    let is_array = if s.is_ascii() {
192        let chars = s.as_bytes();
193        StringIsArrayIndex1(chars.as_ptr() as *const _, chars.len() as u32, &mut i)
194    } else {
195        let chars = s.encode_utf16().collect::<Vec<u16>>();
196        let slice = chars.as_slice();
197        StringIsArrayIndex2(slice.as_ptr(), chars.len() as u32, &mut i)
198    };
199
200    if is_array {
201        Some(i)
202    } else {
203        None
204    }*/
205}
206
207/// Find the enum equivelent of a string given by `v` in `pairs`.
208/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
209/// `Ok((None, value))` if there was no matching string.
210pub(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
230/// Get the property with name `property` from `object`.
231/// Returns `Err(())` on JSAPI failure (there is a pending exception), and
232/// `Ok(false)` if there was no property with the given name.
233pub(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/// Set the property with name `property` from `object`.
260/// Returns `Err(())` on JSAPI failure, or null object,
261/// and Ok(()) otherwise
262#[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/// Define an own enumerable data property with name `property` on `object`.
281/// Returns `Err(())` on JSAPI failure, or null object,
282/// and Ok(()) otherwise.
283#[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/// Checks whether `object` has an own property named `property`.
310/// Returns `Err(())` on JSAPI failure (there is a pending exception),
311/// and `Ok(false)` for null objects or when the property is not own.
312#[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
330/// Computes whether `proxy` has a property `id` on its prototype and stores
331/// the result in `found`.
332///
333/// Returns a boolean indicating whether the check succeeded.
334/// If `false` is returned then the value of `found` is unspecified.
335pub 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/// Controls various details of an IDL operation, such as whether a
391/// `[`[`LegacyLenientThis`][1]`]` attribute is specified and preconditions that
392/// affect the outcome of the "[perform a security check][2]" steps.
393///
394/// [1]: https://heycam.github.io/webidl/#LegacyLenientThis
395/// [2]: https://html.spec.whatwg.org/multipage/#integration-with-idl
396#[derive(Clone, Copy, Eq, PartialEq)]
397pub struct CallPolicyInfo {
398    /// Specifies whether a `[LegacyLenientThis]` attribute is specified on the
399    /// interface member this operation is associated with.
400    pub lenient_this: bool,
401    /// Indicates whether a [security check][1] is required if the target object
402    /// implements this operation's interface.
403    ///
404    /// Regardless of this value, performing a security check is always
405    /// necessary if the target object doesn't implement this operation's
406    /// interface.
407    ///
408    /// This field is `false` iff any of the following are true:
409    ///
410    ///  - The operation is not implemented by any cross-origin objects (i.e.,
411    ///    any `Window` or `Location` objects).
412    ///
413    ///  - The operation is defined as cross origin (i.e., it's included in
414    ///    [`CrossOriginProperties`][2]`(obj)`, given `obj` implementing the
415    ///    operation's interface).
416    ///
417    /// [1]: https://html.spec.whatwg.org/multipage/#integration-with-idl
418    /// [2]: https://html.spec.whatwg.org/multipage/#crossoriginproperties-(-o-)
419    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    // <https://heycam.github.io/webidl/#es-operations>
445    //
446    // > To create an operation function, given an operation `op`, a namespace
447    // > or interface `target`, and a Realm `realm`:
448    // >
449    // > 2. Let `steps` be the following series of steps, [...]
450    // >
451    // > 2.1.2.1. Let `esValue` be the `this` value, if it is not `null` or
452    // >          `undefined`, or `realm`’s global object otherwise. [...]
453    // >
454    // > 2.1.2.2. If `esValue` is a platform object, then perform a security
455    // >          check, passing `esValue`, `id`, and "method".
456    // >
457    // > 2.1.2.3. If `esValue` does not implement the interface `target`, throw
458    // >          a `TypeError`. [...]
459    //
460    // <https://html.spec.whatwg.org/multipage/#integration-with-idl>
461    //
462    // > When perform a security check is invoked, with a `platformObject`,
463    // > `identifier`, and `type`, run these steps:
464    // >
465    // > 1. If `platformObject` is not a `Window` or `Location` object, then
466    // >    return.
467    // >
468    // > 2. For each `e` of `! CrossOriginProperties(platformObject)`:
469    // >
470    // > 2.1. If `SameValue(e.[[Property]], identifier)` is true, then:
471    // >
472    // > 2.1.1. If type is "method" and `e` has neither `[[NeedsGet]]` nor
473    // >        `[[NeedsSet]]`, then return. [... ditto for other types]
474    // >
475    // > 3. If `! IsPlatformObjectSameOrigin(platformObject)` is false, then
476    // >    throw a "SecurityError" `DOMException`.
477    //
478    // According to the above steps, the outcome of an IDL operation is
479    // determined be the following boolean variables:
480    //
481    //  - `this_same_origin`: The current principals object subsumes that of
482    //    `thisobj`
483    //  - `this_class_cross_origin`: `thisobj`'s class provides a cross-origin
484    //    member
485    //  - `cross_origin_operation`: The tuple `(operation_name, operation_type)`
486    //    (e.g., `("focus", "method")`) is a member of
487    //    `CrossOriginProperties(thisobj)`
488    //  - `this_implements_operation`: `thisobj`'s class implements the
489    //    current operation.
490    //
491    // The Karnaugh-esque map of the expected outcome is shown below:
492    //
493    //                            this_same_origin
494    //                                ,-------,
495    //                            ,---+---+---+---,
496    //                            | T | T | o | o |
497    //                          ,-+---+---+---+---+
498    //                          | | S | T | o | S |
499    //  this_class_cross_origin | +---+---+---+---+-,
500    //                          | | T | T | o | o | |
501    //                          '-+---+---+---+---+ | cross_origin_operation
502    //                            | T | T |   |   | |
503    //                            '---+---+---+---+-'
504    //                                    '-------'
505    //                            this_implements_operation
506    //
507    //       T: TypeError (generated by WebIDL opration function step 2.1.2.3)
508    //       S: SecurityError (generated by HTML security check step 3)
509    //       o: OK
510    //   blank: don't-care (impossible cases)
511    //
512    // Under some circumstances, we can rule out some cases from this map.
513    // E.g., if the operation is known to be not implemented by any cross-origin
514    // objects, `this_implements_operation → ¬this_class_cross_origin`, so in
515    // this case, we don't have to perform the security check at all if
516    // `thisobj`'s class implements the expected interface. `CallPolicyInfo::
517    // needs_security_check_on_interface_match` indicates whether this applies.
518
519    let thisobj = args.thisv();
520    if !thisobj.get().is_null_or_undefined() && !thisobj.get().is_object() {
521        // `thisobj` is not a platform object, so the security check is not
522        // invoked in this case
523        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            // [this_implements_operation == false]
542            //
543            // For now, We don't check the conditions for `SecurityError` in
544            // this case, following WebKit's behavior.
545            //
546            // FIXME: Implement a different browser or the specification's
547            //        behavior? (They all differ subtly.) Some behavior is more
548            //        challenging to implement - for example, implementing the
549            //        specification's behavior requires `generic_call`'s code to
550            //        have access to the current IDL operation's name and type
551            //        and the target object's `CrossOriginProperties`.
552            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    // [this_implements_operation == true]
568
569    if needs_security_check_on_interface_match {
570        let mut realm = js::realm::CurrentRealm::assert(cx);
571        // [cross_origin_operation == false]
572        if is_cross_origin_object::<D>(&mut realm, obj.handle()) &&
573            !is_platform_object_same_origin(&realm, obj.handle())
574        {
575            // [this_class_cross_origin == true && this_same_origin == false]
576            // Throw a `SecurityError` `DOMException`.
577            // FIXME: `Handle<jsid>` could have a default constructor
578            //        like `Handle<Value>::null`
579            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        // [(cross_origin_operation == true && this_class_cross_origin == true)
589        //  || cross_origin_operation == false && this_class_cross_origin == false]
590    }
591
592    call(info, cx, obj.handle(), this as *mut libc::c_void, argc, vp)
593}
594
595/// Generic method of IDL interface.
596///
597/// # Safety
598/// `cx` must point to a valid, non-null JSContext.
599/// `vp` must point to a VALID, non-null JSVal.
600pub(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    // SAFETY: it is safe to construct a JSContext from engine hook.
610    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
616/// Generic getter of IDL interface.
617///
618/// # Safety
619/// `cx` must point to a valid, non-null JSContext.
620/// `vp` must point to a VALID, non-null JSVal.
621pub(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    // SAFETY: it is safe to construct a JSContext from engine hook.
631    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
652/// Generic setter of IDL interface.
653///
654/// # Safety
655/// `cx` must point to a valid, non-null JSContext.
656/// `vp` must point to a VALID, non-null JSVal.
657pub(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    // SAFETY: it is safe to construct a JSContext from engine hook.
663    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
669/// <https://searchfox.org/mozilla-central/rev/7279a1df13a819be254fd4649e07c4ff93e4bd45/dom/bindings/BindingUtils.cpp#3300>
670/// # Safety
671///
672/// `cx` must point to a valid, non-null JSContext.
673/// `vp` must point to a VALID, non-null JSVal.
674pub(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    // SAFETY: it is safe to construct a JSContext from engine hook.
680    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    // TODO: we need safe wrappers for this in mozjs!
688    // assert_eq!((*info)._bitfield_1, JSJitInfo_OpType::StaticMethod as u8)
689    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
696/// Coverts exception to promise rejection
697///
698/// <https://searchfox.org/mozilla-central/rev/b220e40ff2ee3d10ce68e07d8a8a577d5558e2a2/dom/bindings/BindingUtils.cpp#3315>
699pub(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            // We just give up. Put the exception back.
711            JS_SetPendingException(cx, exception.handle(), ExceptionStackBehavior::Capture);
712            false
713        }
714    }
715}
716
717/// Trace the resources held by reserved slots of a global object
718///
719/// # Safety
720/// `tracer` must point to a valid, non-null JSTracer.
721/// `obj` must point to a valid, non-null JSObject.
722pub(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
735/// Enumerate lazy properties of a global object.
736/// Modeled after <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/bindings/BindingUtils.cpp#L2814>
737pub(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
747/// Enumerate lazy properties of a global object that is a Window.
748/// <https://github.com/mozilla/gecko-dev/blob/3fd619f47/dom/base/nsGlobalWindowInner.cpp#3297>
749pub(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        // All WebIDL interface names are defined as non-enumerable, so there's
762        // no point in checking them if we're only returning enumerable names.
763        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
780/// Returns true if the resolve hook for this global may resolve the provided id.
781/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/bindings/BindingUtils.cpp#2809>
782/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
783pub(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
791/// Returns true if the resolve hook for this window may resolve the provided id.
792/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/dom/base/nsGlobalWindowInner.cpp#3275>
793/// <https://searchfox.org/mozilla-central/rev/f3c8c63a097b61bb1f01e13629b9514e09395947/js/public/Class.h#283-291>
794pub(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
813/// Resolve a lazy global property, for interface objects and named constructors.
814pub(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
824/// Resolve a lazy global property for a Window global.
825pub(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
853/// Returns a slice of bytes corresponding to the bytes in the provided string id.
854/// Returns an error if the id is not a string, or the string contains non-latin1 characters.
855/// # Safety
856/// The slice is only valid as long as the original id is not garbage collected.
857unsafe 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}