Skip to main content

mozjs/
rust.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 file,
3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5//! Rust wrappers around the raw JS apis
6
7use std::cell::Cell;
8use std::char;
9use std::default::Default;
10use std::ffi::{c_char, c_void, CStr, CString};
11use std::marker::PhantomData;
12use std::mem;
13use std::mem::MaybeUninit;
14use std::ops::{ControlFlow, Deref, DerefMut};
15use std::ptr::{self, NonNull};
16use std::slice;
17use std::str;
18use std::sync::atomic::{AtomicU32, Ordering};
19use std::sync::{Arc, Mutex, RwLock};
20
21use self::wrappers::{
22    StackGCVectorStringAtIndex, StackGCVectorStringLength, StackGCVectorValueAtIndex,
23    StackGCVectorValueLength,
24};
25use crate::consts::{JSCLASS_GLOBAL_SLOT_COUNT, JSCLASS_RESERVED_SLOTS_MASK};
26use crate::consts::{JSCLASS_IS_DOMJSCLASS, JSCLASS_IS_GLOBAL};
27use crate::conversions::jsstr_to_string;
28use crate::default_heapsize;
29pub use crate::gc::*;
30use crate::glue::AppendToRootedObjectVector;
31use crate::glue::{CreateRootedIdVector, CreateRootedObjectVector};
32use crate::glue::{
33    DeleteCompileOptions, DeleteRootedObjectVector, DescribeScriptedCaller, DestroyRootedIdVector,
34    PendingExceptionStackInfo,
35};
36use crate::glue::{DeleteJSAutoStructuredCloneBuffer, NewJSAutoStructuredCloneBuffer};
37use crate::glue::{
38    GetIdVectorAddress, GetObjectVectorAddress, NewCompileOptions, SliceRootedIdVector,
39};
40use crate::jsapi;
41use crate::jsapi::glue::{DeleteRealmOptions, JS_Init, JS_NewRealmOptions};
42use crate::jsapi::js;
43use crate::jsapi::js::frontend::InitialStencilAndDelazifications;
44use crate::jsapi::mozilla::Utf8Unit;
45use crate::jsapi::shadow::BaseShape;
46use crate::jsapi::HandleObjectVector as RawHandleObjectVector;
47use crate::jsapi::HandleValue as RawHandleValue;
48use crate::jsapi::JS_AddExtraGCRootsTracer;
49use crate::jsapi::MutableHandleIdVector as RawMutableHandleIdVector;
50use crate::jsapi::MutableHandleValue as RawMutableHandleValue;
51use crate::jsapi::{already_AddRefed, jsid};
52use crate::jsapi::{BuildStackString, CaptureCurrentStack, StackFormat};
53use crate::jsapi::{HandleValueArray, StencilRelease};
54use crate::jsapi::{InitSelfHostedCode, IsWindowSlow};
55use crate::jsapi::{JSAutoStructuredCloneBuffer, JSStructuredCloneCallbacks, StructuredCloneScope};
56use crate::jsapi::{JSClass, JSClassOps, JSContext, Realm, JSCLASS_RESERVED_SLOTS_SHIFT};
57use crate::jsapi::{JSErrorReport, JSFunctionSpec, JSGCParamKey};
58use crate::jsapi::{JSObject, JSPropertySpec, JSRuntime};
59use crate::jsapi::{JSString, Object, PersistentRootedIdVector};
60use crate::jsapi::{JS_DefineFunctions, JS_DefineProperties, JS_DestroyContext, JS_ShutDown};
61use crate::jsapi::{JS_EnumerateStandardClasses, JS_GlobalObjectTraceHook};
62use crate::jsapi::{JS_MayResolveStandardClass, JS_NewContext, JS_ResolveStandardClass};
63use crate::jsapi::{JS_RequestInterruptCallback, JS_RequestInterruptCallbackCanWait};
64use crate::jsapi::{JS_SetGCParameter, JS_SetNativeStackQuota, JS_WrapObject, JS_WrapValue};
65use crate::jsapi::{JS_StackCapture_AllFrames, JS_StackCapture_MaxFrames};
66use crate::jsapi::{PersistentRootedObjectVector, ReadOnlyCompileOptions, RootingContext};
67use crate::jsapi::{
68    RootedObject, RootedValue, ToUint32Slow, ToUint64Slow, ToWindowProxyIfWindowSlow,
69};
70use crate::jsapi::{SetWarningReporter, SourceText, ToBooleanSlow};
71use crate::jsapi::{ToInt32Slow, ToInt64Slow, ToNumberSlow, ToStringSlow, ToUint16Slow};
72use crate::jsval::{JSVal, ObjectValue, UndefinedValue};
73use crate::panic::maybe_resume_unwind;
74use crate::realm::AutoRealm;
75use log::{debug, warn};
76use mozjs_sys::jsapi::JS::SavedFrameResult;
77pub use mozjs_sys::jsgc::{GCMethods, IntoHandle, IntoMutableHandle};
78pub use mozjs_sys::trace::Traceable as Trace;
79
80use crate::rooted;
81
82// From Gecko:
83// Our "default" stack is what we use in configurations where we don't have a compelling reason to
84// do things differently. This is effectively 1MB on 64-bit platforms.
85const STACK_QUOTA: usize = 128 * 8 * 1024;
86
87// From Gecko:
88// The JS engine permits us to set different stack limits for system code,
89// trusted script, and untrusted script. We have tests that ensure that
90// we can always execute 10 "heavy" (eval+with) stack frames deeper in
91// privileged code. Our stack sizes vary greatly in different configurations,
92// so satisfying those tests requires some care. Manual measurements of the
93// number of heavy stack frames achievable gives us the following rough data,
94// ordered by the effective categories in which they are grouped in the
95// JS_SetNativeStackQuota call (which predates this analysis).
96//
97// (NB: These numbers may have drifted recently - see bug 938429)
98// OSX 64-bit Debug: 7MB stack, 636 stack frames => ~11.3k per stack frame
99// OSX64 Opt: 7MB stack, 2440 stack frames => ~3k per stack frame
100//
101// Linux 32-bit Debug: 2MB stack, 426 stack frames => ~4.8k per stack frame
102// Linux 64-bit Debug: 4MB stack, 455 stack frames => ~9.0k per stack frame
103//
104// Windows (Opt+Debug): 900K stack, 235 stack frames => ~3.4k per stack frame
105//
106// Linux 32-bit Opt: 1MB stack, 272 stack frames => ~3.8k per stack frame
107// Linux 64-bit Opt: 2MB stack, 316 stack frames => ~6.5k per stack frame
108//
109// We tune the trusted/untrusted quotas for each configuration to achieve our
110// invariants while attempting to minimize overhead. In contrast, our buffer
111// between system code and trusted script is a very unscientific 10k.
112const SYSTEM_CODE_BUFFER: usize = 10 * 1024;
113
114// Gecko's value on 64-bit.
115const TRUSTED_SCRIPT_BUFFER: usize = 8 * 12800;
116
117trait ToResult {
118    fn to_result(self) -> Result<(), ()>;
119}
120
121impl ToResult for bool {
122    fn to_result(self) -> Result<(), ()> {
123        if self {
124            Ok(())
125        } else {
126            Err(())
127        }
128    }
129}
130
131// ___________________________________________________________________________
132// friendly Rustic API to runtimes
133
134pub struct RealmOptions(*mut jsapi::RealmOptions);
135
136impl Deref for RealmOptions {
137    type Target = jsapi::RealmOptions;
138    fn deref(&self) -> &Self::Target {
139        unsafe { &*self.0 }
140    }
141}
142
143impl DerefMut for RealmOptions {
144    fn deref_mut(&mut self) -> &mut Self::Target {
145        unsafe { &mut *self.0 }
146    }
147}
148
149impl Default for RealmOptions {
150    fn default() -> RealmOptions {
151        RealmOptions(unsafe { JS_NewRealmOptions() })
152    }
153}
154
155impl Drop for RealmOptions {
156    fn drop(&mut self) {
157        unsafe { DeleteRealmOptions(self.0) }
158    }
159}
160
161thread_local!(static CONTEXT: Cell<Option<NonNull<JSContext>>> = Cell::new(None));
162
163#[derive(PartialEq)]
164enum EngineState {
165    Uninitialized,
166    InitFailed,
167    Initialized,
168    ShutDown,
169}
170
171static ENGINE_STATE: Mutex<EngineState> = Mutex::new(EngineState::Uninitialized);
172
173#[derive(Debug)]
174pub enum JSEngineError {
175    AlreadyInitialized,
176    AlreadyShutDown,
177    InitFailed,
178}
179
180/// A handle that must be kept alive in order to create new Runtimes.
181/// When this handle is dropped, the engine is shut down and cannot
182/// be reinitialized.
183pub struct JSEngine {
184    /// The count of alive handles derived from this initialized instance.
185    outstanding_handles: Arc<AtomicU32>,
186    // Ensure this type cannot be sent between threads.
187    marker: PhantomData<*mut ()>,
188}
189
190pub struct JSEngineHandle(Arc<AtomicU32>);
191
192impl Clone for JSEngineHandle {
193    fn clone(&self) -> JSEngineHandle {
194        self.0.fetch_add(1, Ordering::SeqCst);
195        JSEngineHandle(self.0.clone())
196    }
197}
198
199impl Drop for JSEngineHandle {
200    fn drop(&mut self) {
201        self.0.fetch_sub(1, Ordering::SeqCst);
202    }
203}
204
205impl JSEngine {
206    /// Initialize the JS engine to prepare for creating new JS runtimes.
207    pub fn init() -> Result<JSEngine, JSEngineError> {
208        let mut state = ENGINE_STATE.lock().unwrap();
209        match *state {
210            EngineState::Initialized => return Err(JSEngineError::AlreadyInitialized),
211            EngineState::InitFailed => return Err(JSEngineError::InitFailed),
212            EngineState::ShutDown => return Err(JSEngineError::AlreadyShutDown),
213            EngineState::Uninitialized => (),
214        }
215        if unsafe { !JS_Init() } {
216            *state = EngineState::InitFailed;
217            Err(JSEngineError::InitFailed)
218        } else {
219            *state = EngineState::Initialized;
220            Ok(JSEngine {
221                outstanding_handles: Arc::new(AtomicU32::new(0)),
222                marker: PhantomData,
223            })
224        }
225    }
226
227    pub fn can_shutdown(&self) -> bool {
228        self.outstanding_handles.load(Ordering::SeqCst) == 0
229    }
230
231    /// Create a handle to this engine.
232    pub fn handle(&self) -> JSEngineHandle {
233        self.outstanding_handles.fetch_add(1, Ordering::SeqCst);
234        JSEngineHandle(self.outstanding_handles.clone())
235    }
236}
237
238/// Shut down the JS engine, invalidating any existing runtimes and preventing
239/// any new ones from being created.
240impl Drop for JSEngine {
241    fn drop(&mut self) {
242        let mut state = ENGINE_STATE.lock().unwrap();
243        if *state == EngineState::Initialized {
244            assert_eq!(
245                self.outstanding_handles.load(Ordering::SeqCst),
246                0,
247                "There are outstanding JS engine handles"
248            );
249            *state = EngineState::ShutDown;
250            unsafe {
251                JS_ShutDown();
252            }
253        }
254    }
255}
256
257pub fn transform_str_to_source_text(source: &str) -> SourceText<Utf8Unit> {
258    SourceText {
259        units_: source.as_ptr() as *const _,
260        length_: source.len() as u32,
261        ownsUnits_: false,
262        _phantom_0: PhantomData,
263    }
264}
265
266pub fn transform_u16_to_source_text(source: &[u16]) -> SourceText<u16> {
267    SourceText {
268        units_: source.as_ptr() as *const _,
269        length_: source.len() as u32,
270        ownsUnits_: false,
271        _phantom_0: PhantomData,
272    }
273}
274
275/// A handle to a Runtime that will be used to create a new runtime in another
276/// thread. This handle and the new runtime must be destroyed before the original
277/// runtime can be dropped.
278pub struct ParentRuntime {
279    /// Raw pointer to the underlying SpiderMonkey runtime.
280    parent: *mut JSRuntime,
281    /// Handle to ensure the JS engine remains running while this handle exists.
282    engine: JSEngineHandle,
283    /// The number of children of the runtime that created this ParentRuntime value.
284    children_of_parent: Arc<()>,
285}
286unsafe impl Send for ParentRuntime {}
287
288/// A wrapper for the `JSContext` structure in SpiderMonkey.
289pub struct Runtime {
290    /// Safe SpiderMonkey context.
291    cx: crate::context::JSContext,
292    /// The engine that this runtime is associated with.
293    engine: JSEngineHandle,
294    /// If this Runtime was created with a parent, this member exists to ensure
295    /// that that parent's count of outstanding children (see [outstanding_children])
296    /// remains accurate and will be automatically decreased when this Runtime value
297    /// is dropped.
298    _parent_child_count: Option<Arc<()>>,
299    /// The strong references to this value represent the number of child runtimes
300    /// that have been created using this Runtime as a parent. Since Runtime values
301    /// must be associated with a particular thread, we cannot simply use Arc<Runtime>
302    /// to represent the resulting ownership graph and risk destroying a Runtime on
303    /// the wrong thread.
304    outstanding_children: Arc<()>,
305    /// An `Option` that holds the same pointer as `cx`.
306    /// This is shared with all [`ThreadSafeJSContext`]s, so
307    /// they can detect when it's destroyed on the main thread.
308    thread_safe_handle: Arc<RwLock<Option<NonNull<JSContext>>>>,
309}
310
311impl Runtime {
312    /// Get the `JSContext` for this thread.
313    ///
314    /// This will eventually be removed for in favour of [crate::context::JSContext]
315    pub fn get() -> Option<NonNull<JSContext>> {
316        CONTEXT.with(|context| context.get())
317    }
318
319    /// Create a [`ThreadSafeJSContext`] that can detect when this `Runtime` is destroyed.
320    pub fn thread_safe_js_context(&self) -> ThreadSafeJSContext {
321        // Existence of `ThreadSafeJSContext` does not actually break invariant of
322        // JSContext, because it can be used for limited subset of methods and they do not trigger GC
323        ThreadSafeJSContext(self.thread_safe_handle.clone())
324    }
325
326    /// Creates a new `JSContext`.
327    pub fn new(engine: JSEngineHandle) -> Runtime {
328        unsafe { Self::create(engine, None) }
329    }
330
331    /// Signal that a new child runtime will be created in the future, and ensure
332    /// that this runtime will not allow itself to be destroyed before the new
333    /// child runtime. Returns a handle that can be passed to `create_with_parent`
334    /// in order to create a new runtime on another thread that is associated with
335    /// this runtime.
336    pub fn prepare_for_new_child(&self) -> ParentRuntime {
337        ParentRuntime {
338            parent: self.rt(),
339            engine: self.engine.clone(),
340            children_of_parent: self.outstanding_children.clone(),
341        }
342    }
343
344    /// Creates a new `JSContext` with a parent runtime. If the parent does not outlive
345    /// the new runtime, its destructor will assert.
346    ///
347    /// Unsafety:
348    /// If panicking does not abort the program, any threads with child runtimes will
349    /// continue executing after the thread with the parent runtime panics, but they
350    /// will be in an invalid and undefined state.
351    pub unsafe fn create_with_parent(parent: ParentRuntime) -> Runtime {
352        Self::create(parent.engine.clone(), Some(parent))
353    }
354
355    unsafe fn create(engine: JSEngineHandle, parent: Option<ParentRuntime>) -> Runtime {
356        let parent_runtime = parent.as_ref().map_or(ptr::null_mut(), |r| r.parent);
357        let js_context = NonNull::new(JS_NewContext(
358            default_heapsize + (ChunkSize as u32),
359            parent_runtime,
360        ))
361        .unwrap();
362
363        // Unconstrain the runtime's threshold on nominal heap size, to avoid
364        // triggering GC too often if operating continuously near an arbitrary
365        // finite threshold. This leaves the maximum-JS_malloc-bytes threshold
366        // still in effect to cause periodical, and we hope hygienic,
367        // last-ditch GCs from within the GC's allocator.
368        JS_SetGCParameter(js_context.as_ptr(), JSGCParamKey::JSGC_MAX_BYTES, u32::MAX);
369
370        JS_AddExtraGCRootsTracer(js_context.as_ptr(), Some(trace_traceables), ptr::null_mut());
371
372        JS_SetNativeStackQuota(
373            js_context.as_ptr(),
374            STACK_QUOTA,
375            STACK_QUOTA - SYSTEM_CODE_BUFFER,
376            STACK_QUOTA - SYSTEM_CODE_BUFFER - TRUSTED_SCRIPT_BUFFER,
377        );
378
379        CONTEXT.with(|context| {
380            assert!(context.get().is_none());
381            context.set(Some(js_context));
382        });
383
384        #[cfg(target_pointer_width = "64")]
385        let cache = crate::jsapi::__BindgenOpaqueArray::<u64, 2>::default();
386        #[cfg(target_pointer_width = "32")]
387        let cache = crate::jsapi::__BindgenOpaqueArray::<u32, 2>::default();
388
389        InitSelfHostedCode(js_context.as_ptr(), cache, None);
390
391        SetWarningReporter(js_context.as_ptr(), Some(report_warning));
392
393        Runtime {
394            engine,
395            _parent_child_count: parent.map(|p| p.children_of_parent),
396            cx: crate::context::JSContext::from_ptr(js_context),
397            outstanding_children: Arc::new(()),
398            thread_safe_handle: Arc::new(RwLock::new(Some(js_context))),
399        }
400    }
401
402    /// Returns the `JSRuntime` object.
403    pub fn rt(&self) -> *mut JSRuntime {
404        unsafe { wrappers2::JS_GetRuntime(self.cx_no_gc()) }
405    }
406
407    /// Returns the `JSContext` object.
408    pub fn cx<'rt>(&'rt mut self) -> &'rt mut crate::context::JSContext {
409        &mut self.cx
410    }
411
412    /// Returns the `JSContext` object.
413    pub fn cx_no_gc<'rt>(&'rt self) -> &'rt crate::context::JSContext {
414        &self.cx
415    }
416}
417
418pub fn evaluate_script(
419    cx: &mut crate::context::JSContext,
420    glob: HandleObject,
421    script: &str,
422    rval: MutableHandleValue,
423    options: CompileOptionsWrapper,
424) -> Result<(), ()> {
425    debug!(
426        "Evaluating script from {} with content {}",
427        options.filename(),
428        script
429    );
430
431    let mut realm = AutoRealm::new_from_handle(cx, glob);
432
433    unsafe {
434        let mut source = transform_str_to_source_text(&script);
435        if !wrappers2::Evaluate2(&mut realm, options.ptr, &mut source, rval.into()) {
436            debug!("...err!");
437            maybe_resume_unwind();
438            Err(())
439        } else {
440            // we could return the script result but then we'd have
441            // to root it and so forth and, really, who cares?
442            debug!("...ok!");
443            Ok(())
444        }
445    }
446}
447
448impl Drop for Runtime {
449    fn drop(&mut self) {
450        self.thread_safe_handle.write().unwrap().take();
451        assert!(
452            Arc::get_mut(&mut self.outstanding_children).is_some(),
453            "This runtime still has live children."
454        );
455        unsafe {
456            JS_DestroyContext(self.cx.raw_cx());
457
458            CONTEXT.with(|context| {
459                assert!(context.take().is_some());
460            });
461        }
462    }
463}
464
465/// A version of the [`JSContext`] that can be used from other threads and is thus
466/// `Send` and `Sync`. This should only ever expose operations that are marked as
467/// thread-safe by the SpiderMonkey API, ie ones that only atomic fields in JSContext.
468#[derive(Clone)]
469pub struct ThreadSafeJSContext(Arc<RwLock<Option<NonNull<JSContext>>>>);
470
471unsafe impl Send for ThreadSafeJSContext {}
472unsafe impl Sync for ThreadSafeJSContext {}
473
474impl ThreadSafeJSContext {
475    /// Call `JS_RequestInterruptCallback` from the SpiderMonkey API.
476    /// This is thread-safe according to
477    /// <https://searchfox.org/mozilla-central/rev/7a85a111b5f42cdc07f438e36f9597c4c6dc1d48/js/public/Interrupt.h#19>
478    pub fn request_interrupt_callback(&self) {
479        if let Some(cx) = self.0.read().unwrap().as_ref() {
480            unsafe {
481                JS_RequestInterruptCallback(cx.as_ptr());
482            }
483        }
484    }
485
486    /// Call `JS_RequestInterruptCallbackCanWait` from the SpiderMonkey API.
487    /// This is thread-safe according to
488    /// <https://searchfox.org/mozilla-central/rev/7a85a111b5f42cdc07f438e36f9597c4c6dc1d48/js/public/Interrupt.h#19>
489    pub fn request_interrupt_callback_can_wait(&self) {
490        if let Some(cx) = self.0.read().unwrap().as_ref() {
491            unsafe {
492                JS_RequestInterruptCallbackCanWait(cx.as_ptr());
493            }
494        }
495    }
496}
497
498const ChunkShift: usize = 20;
499const ChunkSize: usize = 1 << ChunkShift;
500
501#[cfg(target_pointer_width = "32")]
502const ChunkLocationOffset: usize = ChunkSize - 2 * 4 - 8;
503
504// ___________________________________________________________________________
505// Wrappers around things in jsglue.cpp
506
507pub struct RootedObjectVectorWrapper {
508    pub ptr: *mut PersistentRootedObjectVector,
509}
510
511impl RootedObjectVectorWrapper {
512    pub fn new(cx: *mut JSContext) -> RootedObjectVectorWrapper {
513        RootedObjectVectorWrapper {
514            ptr: unsafe { CreateRootedObjectVector(cx) },
515        }
516    }
517
518    pub fn append(&self, obj: *mut JSObject) -> bool {
519        unsafe { AppendToRootedObjectVector(self.ptr, obj) }
520    }
521
522    pub fn handle(&self) -> RawHandleObjectVector {
523        RawHandleObjectVector {
524            ptr: unsafe { GetObjectVectorAddress(self.ptr) },
525        }
526    }
527}
528
529impl Drop for RootedObjectVectorWrapper {
530    fn drop(&mut self) {
531        unsafe { DeleteRootedObjectVector(self.ptr) }
532    }
533}
534
535pub struct CompileOptionsWrapper {
536    pub ptr: *mut ReadOnlyCompileOptions,
537    filename: CString,
538}
539
540impl CompileOptionsWrapper {
541    pub fn new(cx: &crate::context::JSContext, filename: CString, line: u32) -> Self {
542        let ptr = unsafe { wrappers2::NewCompileOptions(cx, filename.as_ptr(), line) };
543        assert!(!ptr.is_null());
544        Self { ptr, filename }
545    }
546    /// # Safety
547    /// `cx` must point to a non-null, valid [`JSContext`].
548    /// To create an instance from safe code, use [`Runtime::new_compile_options`].
549    pub unsafe fn new_raw(cx: *mut JSContext, filename: CString, line: u32) -> Self {
550        let ptr = NewCompileOptions(cx, filename.as_ptr(), line);
551        assert!(!ptr.is_null());
552        Self { ptr, filename }
553    }
554
555    pub fn filename(&self) -> &str {
556        self.filename.to_str().expect("Guaranteed by new")
557    }
558
559    pub fn set_introduction_type(&mut self, introduction_type: &'static CStr) {
560        unsafe {
561            (*self.ptr)._base.introductionType = introduction_type.as_ptr();
562        }
563    }
564
565    pub fn set_muted_errors(&mut self, muted_errors: bool) {
566        unsafe {
567            (*self.ptr)._base.mutedErrors_ = muted_errors;
568        }
569    }
570
571    pub fn set_is_run_once(&mut self, is_run_once: bool) {
572        unsafe {
573            (*self.ptr).isRunOnce = is_run_once;
574        }
575    }
576
577    pub fn set_no_script_rval(&mut self, no_script_rval: bool) {
578        unsafe {
579            (*self.ptr).noScriptRval = no_script_rval;
580        }
581    }
582}
583
584impl Drop for CompileOptionsWrapper {
585    fn drop(&mut self) {
586        unsafe { DeleteCompileOptions(self.ptr) }
587    }
588}
589
590pub struct JSAutoStructuredCloneBufferWrapper {
591    ptr: NonNull<JSAutoStructuredCloneBuffer>,
592}
593
594impl JSAutoStructuredCloneBufferWrapper {
595    pub unsafe fn new(
596        scope: StructuredCloneScope,
597        callbacks: *const JSStructuredCloneCallbacks,
598    ) -> Self {
599        let raw_ptr = NewJSAutoStructuredCloneBuffer(scope, callbacks);
600        Self {
601            ptr: NonNull::new(raw_ptr).unwrap(),
602        }
603    }
604
605    pub fn as_raw_ptr(&self) -> *mut JSAutoStructuredCloneBuffer {
606        self.ptr.as_ptr()
607    }
608}
609
610impl Drop for JSAutoStructuredCloneBufferWrapper {
611    fn drop(&mut self) {
612        unsafe {
613            DeleteJSAutoStructuredCloneBuffer(self.ptr.as_ptr());
614        }
615    }
616}
617
618pub struct Stencil {
619    inner: already_AddRefed<InitialStencilAndDelazifications>,
620}
621
622/*unsafe impl Send for Stencil {}
623unsafe impl Sync for Stencil {}*/
624
625impl Drop for Stencil {
626    fn drop(&mut self) {
627        if self.is_null() {
628            return;
629        }
630        unsafe {
631            StencilRelease(self.inner.mRawPtr);
632        }
633    }
634}
635
636impl Deref for Stencil {
637    type Target = *mut InitialStencilAndDelazifications;
638
639    fn deref(&self) -> &Self::Target {
640        &self.inner.mRawPtr
641    }
642}
643
644impl Stencil {
645    pub fn is_null(&self) -> bool {
646        self.inner.mRawPtr.is_null()
647    }
648}
649
650// ___________________________________________________________________________
651// Fast inline converters
652
653#[inline]
654pub unsafe fn ToBoolean(v: HandleValue) -> bool {
655    let val = *v.ptr.as_ptr();
656
657    if val.is_boolean() {
658        return val.to_boolean();
659    }
660
661    if val.is_int32() {
662        return val.to_int32() != 0;
663    }
664
665    if val.is_null_or_undefined() {
666        return false;
667    }
668
669    if val.is_double() {
670        let d = val.to_double();
671        return !d.is_nan() && d != 0f64;
672    }
673
674    if val.is_symbol() {
675        return true;
676    }
677
678    ToBooleanSlow(v.into())
679}
680
681#[inline]
682pub unsafe fn ToNumber(cx: *mut JSContext, v: HandleValue) -> Result<f64, ()> {
683    let val = *v.ptr.as_ptr();
684    if val.is_number() {
685        return Ok(val.to_number());
686    }
687
688    let mut out = Default::default();
689    if ToNumberSlow(cx, v.into_handle(), &mut out) {
690        Ok(out)
691    } else {
692        Err(())
693    }
694}
695
696#[inline]
697unsafe fn convert_from_int32<T: Default + Copy>(
698    cx: *mut JSContext,
699    v: HandleValue,
700    conv_fn: unsafe extern "C" fn(*mut JSContext, RawHandleValue, *mut T) -> bool,
701) -> Result<T, ()> {
702    let val = *v.ptr.as_ptr();
703    if val.is_int32() {
704        let intval: i64 = val.to_int32() as i64;
705        // TODO: do something better here that works on big endian
706        let intval = *(&intval as *const i64 as *const T);
707        return Ok(intval);
708    }
709
710    let mut out = Default::default();
711    if conv_fn(cx, v.into(), &mut out) {
712        Ok(out)
713    } else {
714        Err(())
715    }
716}
717
718#[inline]
719pub unsafe fn ToInt32(cx: *mut JSContext, v: HandleValue) -> Result<i32, ()> {
720    convert_from_int32::<i32>(cx, v, ToInt32Slow)
721}
722
723#[inline]
724pub unsafe fn ToUint32(cx: *mut JSContext, v: HandleValue) -> Result<u32, ()> {
725    convert_from_int32::<u32>(cx, v, ToUint32Slow)
726}
727
728#[inline]
729pub unsafe fn ToUint16(cx: *mut JSContext, v: HandleValue) -> Result<u16, ()> {
730    convert_from_int32::<u16>(cx, v, ToUint16Slow)
731}
732
733#[inline]
734pub unsafe fn ToInt64(cx: *mut JSContext, v: HandleValue) -> Result<i64, ()> {
735    convert_from_int32::<i64>(cx, v, ToInt64Slow)
736}
737
738#[inline]
739pub unsafe fn ToUint64(cx: *mut JSContext, v: HandleValue) -> Result<u64, ()> {
740    convert_from_int32::<u64>(cx, v, ToUint64Slow)
741}
742
743#[inline]
744pub unsafe fn ToString(cx: *mut JSContext, v: HandleValue) -> *mut JSString {
745    let val = *v.ptr.as_ptr();
746    if val.is_string() {
747        return val.to_string();
748    }
749
750    ToStringSlow(cx, v.into())
751}
752
753pub unsafe fn ToWindowProxyIfWindow(obj: *mut JSObject) -> *mut JSObject {
754    if is_window(obj) {
755        ToWindowProxyIfWindowSlow(obj)
756    } else {
757        obj
758    }
759}
760
761pub unsafe extern "C" fn report_warning(_cx: *mut JSContext, report: *mut JSErrorReport) {
762    fn latin1_to_string(bytes: &[u8]) -> String {
763        bytes
764            .iter()
765            .map(|c| char::from_u32(*c as u32).unwrap())
766            .collect()
767    }
768
769    let fnptr = (*report)._base.filename.data_;
770    let fname = if !fnptr.is_null() {
771        let c_str = CStr::from_ptr(fnptr);
772        latin1_to_string(c_str.to_bytes())
773    } else {
774        "none".to_string()
775    };
776
777    let lineno = (*report)._base.lineno;
778    let column = (*report)._base.column._base;
779
780    let msg_ptr = (*report)._base.message_.data_ as *const u8;
781    let msg_len = (0usize..)
782        .find(|&i| *msg_ptr.offset(i as isize) == 0)
783        .unwrap();
784    let msg_slice = slice::from_raw_parts(msg_ptr, msg_len);
785    let msg = str::from_utf8_unchecked(msg_slice);
786
787    warn!("Warning at {}:{}:{}: {}\n", fname, lineno, column, msg);
788}
789
790pub struct IdVector(*mut PersistentRootedIdVector);
791
792impl IdVector {
793    pub unsafe fn new(cx: *mut JSContext) -> IdVector {
794        let vector = CreateRootedIdVector(cx);
795        assert!(!vector.is_null());
796        IdVector(vector)
797    }
798
799    pub fn handle_mut(&mut self) -> RawMutableHandleIdVector {
800        RawMutableHandleIdVector {
801            ptr: unsafe { GetIdVectorAddress(self.0) },
802        }
803    }
804}
805
806impl Drop for IdVector {
807    fn drop(&mut self) {
808        unsafe { DestroyRootedIdVector(self.0) }
809    }
810}
811
812impl Deref for IdVector {
813    type Target = [jsid];
814
815    fn deref(&self) -> &[jsid] {
816        unsafe {
817            let mut length = 0;
818            let pointer = SliceRootedIdVector(self.0, &mut length);
819            slice::from_raw_parts(pointer, length)
820        }
821    }
822}
823
824/// Defines methods on `obj`. The last entry of `methods` must contain zeroed
825/// memory.
826///
827/// # Failures
828///
829/// Returns `Err` on JSAPI failure.
830///
831/// # Panics
832///
833/// Panics if the last entry of `methods` does not contain zeroed memory.
834///
835/// # Safety
836///
837/// - `cx` must be valid.
838/// - This function calls into unaudited C++ code.
839pub unsafe fn define_methods(
840    cx: *mut JSContext,
841    obj: HandleObject,
842    methods: &'static [JSFunctionSpec],
843) -> Result<(), ()> {
844    assert!({
845        match methods.last() {
846            Some(&JSFunctionSpec {
847                name,
848                call,
849                nargs,
850                flags,
851                selfHostedName,
852            }) => {
853                name.string_.is_null()
854                    && call.is_zeroed()
855                    && nargs == 0
856                    && flags == 0
857                    && selfHostedName.is_null()
858            }
859            None => false,
860        }
861    });
862
863    JS_DefineFunctions(cx, obj.into(), methods.as_ptr()).to_result()
864}
865
866/// Defines attributes on `obj`. The last entry of `properties` must contain
867/// zeroed memory.
868///
869/// # Failures
870///
871/// Returns `Err` on JSAPI failure.
872///
873/// # Panics
874///
875/// Panics if the last entry of `properties` does not contain zeroed memory.
876///
877/// # Safety
878///
879/// - `cx` must be valid.
880/// - This function calls into unaudited C++ code.
881pub unsafe fn define_properties(
882    cx: *mut JSContext,
883    obj: HandleObject,
884    properties: &'static [JSPropertySpec],
885) -> Result<(), ()> {
886    assert!({
887        match properties.last() {
888            Some(spec) => spec.is_zeroed(),
889            None => false,
890        }
891    });
892
893    JS_DefineProperties(cx, obj.into(), properties.as_ptr()).to_result()
894}
895
896static SIMPLE_GLOBAL_CLASS_OPS: JSClassOps = JSClassOps {
897    addProperty: None,
898    delProperty: None,
899    enumerate: Some(JS_EnumerateStandardClasses),
900    newEnumerate: None,
901    resolve: Some(JS_ResolveStandardClass),
902    mayResolve: Some(JS_MayResolveStandardClass),
903    finalize: None,
904    call: None,
905    construct: None,
906    trace: Some(JS_GlobalObjectTraceHook),
907};
908
909/// This is a simple `JSClass` for global objects, primarily intended for tests.
910pub static SIMPLE_GLOBAL_CLASS: JSClass = JSClass {
911    name: c"Global".as_ptr(),
912    flags: JSCLASS_IS_GLOBAL
913        | ((JSCLASS_GLOBAL_SLOT_COUNT & JSCLASS_RESERVED_SLOTS_MASK)
914            << JSCLASS_RESERVED_SLOTS_SHIFT),
915    cOps: &SIMPLE_GLOBAL_CLASS_OPS as *const JSClassOps,
916    spec: ptr::null(),
917    ext: ptr::null(),
918    oOps: ptr::null(),
919};
920
921#[inline]
922unsafe fn get_object_group(obj: *mut JSObject) -> *mut BaseShape {
923    assert!(!obj.is_null());
924    let obj = obj as *mut Object;
925    (*(*obj).shape).base
926}
927
928#[inline]
929pub unsafe fn get_object_class(obj: *mut JSObject) -> *const JSClass {
930    (*get_object_group(obj)).clasp as *const _
931}
932
933#[inline]
934pub unsafe fn get_object_realm(obj: *mut JSObject) -> *mut Realm {
935    (*get_object_group(obj)).realm
936}
937
938#[inline]
939pub unsafe fn get_context_realm(cx: *mut JSContext) -> *mut Realm {
940    let cx = cx as *mut RootingContext;
941    (*cx).realm_
942}
943
944#[inline]
945pub fn is_dom_class(class: &JSClass) -> bool {
946    class.flags & JSCLASS_IS_DOMJSCLASS != 0
947}
948
949#[inline]
950pub unsafe fn is_dom_object(obj: *mut JSObject) -> bool {
951    is_dom_class(&*get_object_class(obj))
952}
953
954#[inline]
955pub unsafe fn is_window(obj: *mut JSObject) -> bool {
956    (*get_object_class(obj)).flags & JSCLASS_IS_GLOBAL != 0 && IsWindowSlow(obj)
957}
958
959#[inline]
960pub unsafe fn try_to_outerize(mut rval: MutableHandleValue) {
961    let obj = rval.to_object();
962    if is_window(obj) {
963        let obj = ToWindowProxyIfWindowSlow(obj);
964        assert!(!obj.is_null());
965        rval.set(ObjectValue(&mut *obj));
966    }
967}
968
969#[inline]
970pub unsafe fn try_to_outerize_object(mut rval: MutableHandleObject) {
971    if is_window(*rval) {
972        let obj = ToWindowProxyIfWindowSlow(*rval);
973        assert!(!obj.is_null());
974        rval.set(obj);
975    }
976}
977
978#[inline]
979pub unsafe fn maybe_wrap_object(cx: *mut JSContext, mut obj: MutableHandleObject) {
980    if get_object_realm(*obj) != get_context_realm(cx) {
981        assert!(JS_WrapObject(cx, obj.reborrow().into()));
982    }
983    try_to_outerize_object(obj);
984}
985
986#[inline]
987pub unsafe fn maybe_wrap_object_value(cx: *mut JSContext, rval: MutableHandleValue) {
988    assert!(rval.is_object());
989    let obj = rval.to_object();
990    if get_object_realm(obj) != get_context_realm(cx) {
991        assert!(JS_WrapValue(cx, rval.into()));
992    } else if is_dom_object(obj) {
993        try_to_outerize(rval);
994    }
995}
996
997#[inline]
998pub unsafe fn maybe_wrap_object_or_null_value(cx: *mut JSContext, rval: MutableHandleValue) {
999    assert!(rval.is_object_or_null());
1000    if !rval.is_null() {
1001        maybe_wrap_object_value(cx, rval);
1002    }
1003}
1004
1005#[inline]
1006pub unsafe fn maybe_wrap_value(cx: *mut JSContext, rval: MutableHandleValue) {
1007    if rval.is_string() {
1008        assert!(JS_WrapValue(cx, rval.into()));
1009    } else if rval.is_object() {
1010        maybe_wrap_object_value(cx, rval);
1011    }
1012}
1013
1014/// Like `JSJitInfo::new_bitfield_1`, but usable in `const` contexts.
1015#[macro_export]
1016macro_rules! new_jsjitinfo_bitfield_1 {
1017    (
1018        $type_: expr,
1019        $aliasSet_: expr,
1020        $returnType_: expr,
1021        $isInfallible: expr,
1022        $isMovable: expr,
1023        $isEliminatable: expr,
1024        $isAlwaysInSlot: expr,
1025        $isLazilyCachedInSlot: expr,
1026        $isTypedMethod: expr,
1027        $slotIndex: expr,
1028    ) => {
1029        0 | (($type_ as u32) << 0u32)
1030            | (($aliasSet_ as u32) << 4u32)
1031            | (($returnType_ as u32) << 8u32)
1032            | (($isInfallible as u32) << 16u32)
1033            | (($isMovable as u32) << 17u32)
1034            | (($isEliminatable as u32) << 18u32)
1035            | (($isAlwaysInSlot as u32) << 19u32)
1036            | (($isLazilyCachedInSlot as u32) << 20u32)
1037            | (($isTypedMethod as u32) << 21u32)
1038            | (($slotIndex as u32) << 22u32)
1039    };
1040}
1041
1042#[derive(Debug, Default)]
1043pub struct ScriptedCaller {
1044    pub filename: String,
1045    pub line: u32,
1046    pub col: u32,
1047}
1048
1049pub unsafe fn describe_scripted_caller(cx: *mut JSContext) -> Result<ScriptedCaller, ()> {
1050    let mut buf = [0; 1024];
1051    let mut line = 0;
1052    let mut col = 0;
1053    if !DescribeScriptedCaller(cx, buf.as_mut_ptr(), buf.len(), &mut line, &mut col) {
1054        return Err(());
1055    }
1056    let filename = CStr::from_ptr((&buf) as *const _ as *const _);
1057    Ok(ScriptedCaller {
1058        filename: String::from_utf8_lossy(filename.to_bytes()).into_owned(),
1059        line,
1060        col,
1061    })
1062}
1063
1064pub struct ErrorInfo {
1065    pub message: String,
1066    pub filename: String,
1067    pub line: u32,
1068    pub col: u32,
1069}
1070
1071unsafe extern "C" fn fill_string_callback(ptr: *const c_char, len: usize, target: *mut c_void) {
1072    assert!(!ptr.is_null());
1073    let target = &mut *(target as *mut String);
1074
1075    let slice = slice::from_raw_parts(ptr as *const u8, len);
1076    target.push_str(str::from_utf8_unchecked(slice));
1077}
1078
1079/// Retrieve error info from the pending exception stack, by clearing it.
1080/// Return None if there isn't one or if it is a warning.
1081pub unsafe fn error_info_from_exception_stack(
1082    cx: *mut JSContext,
1083    rval: RawMutableHandleValue,
1084) -> Option<ErrorInfo> {
1085    let mut message = String::new();
1086    let mut filename = String::new();
1087
1088    let mut line = 0;
1089    let mut col = 0;
1090
1091    if !PendingExceptionStackInfo(
1092        cx,
1093        Some(fill_string_callback),
1094        &raw mut message as *mut c_void,
1095        &raw mut filename as *mut c_void,
1096        &mut line,
1097        &mut col,
1098        rval,
1099    ) {
1100        return None;
1101    }
1102
1103    Some(ErrorInfo {
1104        message,
1105        filename,
1106        line,
1107        col,
1108    })
1109}
1110
1111pub struct CapturedJSStack<'a> {
1112    cx: *mut JSContext,
1113    stack: RootedGuard<'a, *mut JSObject>,
1114}
1115
1116impl<'a> CapturedJSStack<'a> {
1117    pub unsafe fn new(
1118        cx: *mut JSContext,
1119        mut guard: RootedGuard<'a, *mut JSObject>,
1120        max_frame_count: Option<u32>,
1121    ) -> Option<Self> {
1122        let ref mut stack_capture = MaybeUninit::uninit();
1123        match max_frame_count {
1124            None => JS_StackCapture_AllFrames(stack_capture.as_mut_ptr()),
1125            Some(count) => JS_StackCapture_MaxFrames(count, stack_capture.as_mut_ptr()),
1126        };
1127        let ref mut stack_capture = stack_capture.assume_init();
1128
1129        if !CaptureCurrentStack(
1130            cx,
1131            guard.handle_mut().raw(),
1132            stack_capture,
1133            HandleObject::null().into(),
1134        ) {
1135            None
1136        } else {
1137            Some(CapturedJSStack { cx, stack: guard })
1138        }
1139    }
1140
1141    pub fn as_string(&self, indent: Option<usize>, format: StackFormat) -> Option<String> {
1142        unsafe {
1143            let stack_handle = self.stack.handle();
1144            rooted!(in(self.cx) let mut js_string = ptr::null_mut::<JSString>());
1145            let mut string_handle = js_string.handle_mut();
1146
1147            if !BuildStackString(
1148                self.cx,
1149                ptr::null_mut(),
1150                stack_handle.into(),
1151                string_handle.raw(),
1152                indent.unwrap_or(0),
1153                format,
1154            ) {
1155                return None;
1156            }
1157
1158            Some(jsstr_to_string(self.cx, NonNull::new(string_handle.get())?))
1159        }
1160    }
1161
1162    /// Executes the provided closure for each frame on the js stack
1163    pub fn for_each_stack_frame<F>(&self, mut f: F)
1164    where
1165        F: FnMut(Handle<*mut JSObject>),
1166    {
1167        rooted!(in(self.cx) let mut current_element = self.stack.clone());
1168        rooted!(in(self.cx) let mut next_element = ptr::null_mut::<JSObject>());
1169
1170        loop {
1171            f(current_element.handle());
1172
1173            unsafe {
1174                let result = jsapi::GetSavedFrameParent(
1175                    self.cx,
1176                    ptr::null_mut(),
1177                    current_element.handle().into_handle(),
1178                    next_element.handle_mut().into_handle_mut(),
1179                    jsapi::SavedFrameSelfHosted::Include,
1180                );
1181
1182                if result != SavedFrameResult::Ok || next_element.is_null() {
1183                    return;
1184                }
1185            }
1186            current_element.set(next_element.get());
1187        }
1188    }
1189}
1190
1191#[macro_export]
1192macro_rules! capture_stack {
1193    (&in($cx:expr) $($t:tt)*) => {
1194        capture_stack!(in(unsafe {$cx.raw_cx_no_gc()}) $($t)*);
1195    };
1196    (in($cx:expr) let $name:ident = with max depth($max_frame_count:expr)) => {
1197        rooted!(in($cx) let mut __obj = ::std::ptr::null_mut());
1198        let $name = $crate::rust::CapturedJSStack::new($cx, __obj, Some($max_frame_count));
1199    };
1200    (in($cx:expr) let $name:ident ) => {
1201        rooted!(in($cx) let mut __obj = ::std::ptr::null_mut());
1202        let $name = $crate::rust::CapturedJSStack::new($cx, __obj, None);
1203    }
1204}
1205
1206pub struct EnvironmentChain {
1207    chain: *mut crate::jsapi::JS::EnvironmentChain,
1208}
1209
1210impl EnvironmentChain {
1211    pub fn new(
1212        cx: *mut JSContext,
1213        support_unscopeables: crate::jsapi::JS::SupportUnscopables,
1214    ) -> Self {
1215        unsafe {
1216            Self {
1217                chain: crate::jsapi::glue::NewEnvironmentChain(cx, support_unscopeables),
1218            }
1219        }
1220    }
1221
1222    pub fn append(&self, obj: *mut JSObject) {
1223        unsafe {
1224            assert!(crate::jsapi::glue::AppendToEnvironmentChain(
1225                self.chain, obj
1226            ));
1227        }
1228    }
1229
1230    pub fn get(&self) -> *mut crate::jsapi::JS::EnvironmentChain {
1231        self.chain
1232    }
1233}
1234
1235impl Drop for EnvironmentChain {
1236    fn drop(&mut self) {
1237        unsafe {
1238            crate::jsapi::glue::DeleteEnvironmentChain(self.chain);
1239        }
1240    }
1241}
1242
1243impl<'a> Handle<'a, StackGCVector<JSVal, js::TempAllocPolicy>> {
1244    pub fn at(&'a self, index: u32) -> Option<Handle<'a, JSVal>> {
1245        if index >= self.len() {
1246            return None;
1247        }
1248        let handle =
1249            unsafe { Handle::from_marked_location(StackGCVectorValueAtIndex(*self, index)) };
1250        Some(handle)
1251    }
1252
1253    pub fn len(&self) -> u32 {
1254        unsafe { StackGCVectorValueLength(*self) }
1255    }
1256}
1257
1258impl<'a> Handle<'a, StackGCVector<*mut JSString, js::TempAllocPolicy>> {
1259    pub fn at(&'a self, index: u32) -> Option<Handle<'a, *mut JSString>> {
1260        if index >= self.len() {
1261            return None;
1262        }
1263        let handle =
1264            unsafe { Handle::from_marked_location(StackGCVectorStringAtIndex(*self, index)) };
1265        Some(handle)
1266    }
1267
1268    pub fn len(&self) -> u32 {
1269        unsafe { StackGCVectorStringLength(*self) }
1270    }
1271}
1272
1273#[derive(Clone, Copy, Debug)]
1274pub enum ForOfIterationFailure<OtherError> {
1275    ValueIsNotIterable,
1276    /// There is a pending exception
1277    JSFailed,
1278    Other(OtherError),
1279}
1280
1281impl<OtherError> From<OtherError> for ForOfIterationFailure<OtherError> {
1282    fn from(value: OtherError) -> Self {
1283        Self::Other(value)
1284    }
1285}
1286
1287/// Helper for running `for .. of` iteration from rust.
1288///
1289/// If `Ok()` is returned then the iteration completed without unexpected failures.
1290///
1291/// The callback returns `Err()` to indicate a pending exception or `Ok()` containing a boolean
1292/// value that is `true` if the iterator should continue iterating.
1293pub fn for_of<Callback, OtherError>(
1294    cx: *mut JSContext,
1295    iterable: HandleValue<'_>,
1296    mut callback: Callback,
1297) -> Result<(), ForOfIterationFailure<OtherError>>
1298where
1299    Callback: FnMut(HandleValue<'_>) -> Result<ControlFlow<()>, ForOfIterationFailure<OtherError>>,
1300{
1301    // Depending on the version of LLVM in use, bindgen can end up including
1302    // a padding field in the ForOfIterator. To support multiple versions of
1303    // LLVM that may not have the same fields as a result, we create an empty
1304    // iterator instance and initialize a non-empty instance using the empty
1305    // instance as a base value.
1306    #[allow(unused_variables)]
1307    let zero = unsafe { mem::zeroed() };
1308    let mut iterator = jsapi::ForOfIterator {
1309        cx_: cx,
1310        iterator: RootedObject::new_unrooted(ptr::null_mut()),
1311        nextMethod: RootedValue::new_unrooted(JSVal { asBits_: 0 }),
1312        index: ::std::u32::MAX, // NOT_ARRAY
1313        ..zero
1314    };
1315
1316    // This code would benefit from https://github.com/rust-lang/rust/issues/144426
1317    struct IteratorRootGuard<'a> {
1318        inner: &'a mut jsapi::ForOfIterator,
1319    }
1320
1321    impl<'a> Drop for IteratorRootGuard<'a> {
1322        fn drop(&mut self) {
1323            // SAFETY: These values won't be used anymore
1324            unsafe {
1325                self.inner.iterator.remove_from_root_stack();
1326                self.inner.nextMethod.remove_from_root_stack();
1327            }
1328        }
1329    }
1330    let guard = IteratorRootGuard {
1331        inner: &mut iterator,
1332    };
1333    let iterator = &mut *guard.inner;
1334
1335    unsafe {
1336        RootedObject::add_to_root_stack(&raw mut iterator.iterator, cx);
1337        RootedValue::add_to_root_stack(&raw mut iterator.nextMethod, cx);
1338    }
1339
1340    let success = unsafe {
1341        iterator.init(
1342            iterable.into_handle(),
1343            jsapi::ForOfIterator_NonIterableBehavior::AllowNonIterable,
1344        )
1345    };
1346    if !success {
1347        return Err(ForOfIterationFailure::JSFailed);
1348    }
1349    if !iterator.is_iterable() {
1350        return Err(ForOfIterationFailure::ValueIsNotIterable);
1351    }
1352
1353    let mut done = false;
1354    rooted!(in(cx) let mut value = UndefinedValue());
1355    loop {
1356        if !unsafe { iterator.next(value.handle_mut().into(), &mut done) } {
1357            return Err(ForOfIterationFailure::JSFailed);
1358        }
1359
1360        if done {
1361            break;
1362        }
1363
1364        if callback(value.handle())?.is_break() {
1365            break;
1366        }
1367    }
1368
1369    Ok(())
1370}
1371
1372/// Wrappers for JSAPI methods that accept lifetimed Handle and MutableHandle arguments
1373pub mod wrappers {
1374    macro_rules! wrap {
1375        // The invocation of @inner has the following form:
1376        // @inner (input args) <> (accumulator) <> unparsed tokens
1377        // when `unparsed tokens == \eps`, accumulator contains the final result
1378
1379        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: Handle<$gentype:ty>, $($rest:tt)*) => {
1380            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1381        };
1382        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandle<$gentype:ty>, $($rest:tt)*) => {
1383            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1384        };
1385        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: Handle, $($rest:tt)*) => {
1386            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1387        };
1388        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandle, $($rest:tt)*) => {
1389            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1390        };
1391        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleFunction , $($rest:tt)*) => {
1392            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1393        };
1394        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleId , $($rest:tt)*) => {
1395            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1396        };
1397        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleObject , $($rest:tt)*) => {
1398            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1399        };
1400        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleScript , $($rest:tt)*) => {
1401            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1402        };
1403        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleString , $($rest:tt)*) => {
1404            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1405        };
1406        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleSymbol , $($rest:tt)*) => {
1407            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1408        };
1409        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: HandleValue , $($rest:tt)*) => {
1410            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1411        };
1412        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleFunction , $($rest:tt)*) => {
1413            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1414        };
1415        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleId , $($rest:tt)*) => {
1416            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1417        };
1418        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleObject , $($rest:tt)*) => {
1419            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1420        };
1421        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleScript , $($rest:tt)*) => {
1422            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1423        };
1424        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleString , $($rest:tt)*) => {
1425            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1426        };
1427        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleSymbol , $($rest:tt)*) => {
1428            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1429        };
1430        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: MutableHandleValue , $($rest:tt)*) => {
1431            wrap!(@inner $saved <> ($($acc,)* $arg.into(),) <> $($rest)*);
1432        };
1433        (@inner $saved:tt <> ($($acc:expr,)*) <> $arg:ident: $type:ty, $($rest:tt)*) => {
1434            wrap!(@inner $saved <> ($($acc,)* $arg,) <> $($rest)*);
1435        };
1436        (@inner ($module:tt: $func_name:ident ($($args:tt)*) -> $outtype:ty) <> ($($argexprs:expr,)*) <> ) => {
1437            #[inline]
1438            pub unsafe fn $func_name($($args)*) -> $outtype {
1439                $module::$func_name($($argexprs),*)
1440            }
1441        };
1442        ($module:tt: pub fn $func_name:ident($($args:tt)*) -> $outtype:ty) => {
1443            wrap!(@inner ($module: $func_name ($($args)*) -> $outtype) <> () <> $($args)* ,);
1444        };
1445        ($module:tt: pub fn $func_name:ident($($args:tt)*)) => {
1446            wrap!($module: pub fn $func_name($($args)*) -> ());
1447        }
1448    }
1449
1450    use super::*;
1451    use crate::glue;
1452    use crate::glue::EncodedStringCallback;
1453    use crate::glue::StringCallback;
1454    use crate::jsapi;
1455    use crate::jsapi::js::TempAllocPolicy;
1456    use crate::jsapi::jsid;
1457    use crate::jsapi::mozilla::Utf8Unit;
1458    use crate::jsapi::BigInt;
1459    use crate::jsapi::CallArgs;
1460    use crate::jsapi::CloneDataPolicy;
1461    use crate::jsapi::ColumnNumberOneOrigin;
1462    use crate::jsapi::CompartmentTransplantCallback;
1463    use crate::jsapi::EnvironmentChain;
1464    use crate::jsapi::JSONParseHandler;
1465    use crate::jsapi::Latin1Char;
1466    use crate::jsapi::PropertyKey;
1467    use crate::jsapi::TaggedColumnNumberOneOrigin;
1468    //use jsapi::DynamicImportStatus;
1469    use crate::jsapi::ESClass;
1470    use crate::jsapi::ExceptionStackBehavior;
1471    use crate::jsapi::ForOfIterator;
1472    use crate::jsapi::ForOfIterator_NonIterableBehavior;
1473    use crate::jsapi::HandleObjectVector;
1474    use crate::jsapi::InstantiateOptions;
1475    use crate::jsapi::JSClass;
1476    use crate::jsapi::JSErrorReport;
1477    use crate::jsapi::JSExnType;
1478    use crate::jsapi::JSFunctionSpecWithHelp;
1479    use crate::jsapi::JSJitInfo;
1480    use crate::jsapi::JSONWriteCallback;
1481    use crate::jsapi::JSPrincipals;
1482    use crate::jsapi::JSPropertySpec;
1483    use crate::jsapi::JSPropertySpec_Name;
1484    use crate::jsapi::JSProtoKey;
1485    use crate::jsapi::JSScript;
1486    use crate::jsapi::JSStructuredCloneData;
1487    use crate::jsapi::JSType;
1488    use crate::jsapi::ModuleErrorBehaviour;
1489    use crate::jsapi::ModuleType;
1490    use crate::jsapi::MutableHandleIdVector;
1491    use crate::jsapi::PromiseState;
1492    use crate::jsapi::PromiseUserInputEventHandlingState;
1493    use crate::jsapi::ReadOnlyCompileOptions;
1494    use crate::jsapi::Realm;
1495    use crate::jsapi::RefPtr;
1496    use crate::jsapi::RegExpFlags;
1497    use crate::jsapi::ScriptEnvironmentPreparer_Closure;
1498    use crate::jsapi::SourceText;
1499    use crate::jsapi::StackCapture;
1500    use crate::jsapi::Stencil;
1501    use crate::jsapi::StructuredCloneScope;
1502    use crate::jsapi::Symbol;
1503    use crate::jsapi::SymbolCode;
1504    use crate::jsapi::TranscodeBuffer;
1505    use crate::jsapi::TwoByteChars;
1506    use crate::jsapi::UniqueChars;
1507    use crate::jsapi::Value;
1508    use crate::jsapi::WasmModule;
1509    use crate::jsapi::{ElementAdder, IsArrayAnswer, PropertyDescriptor};
1510    use crate::jsapi::{JSContext, JSFunction, JSNative, JSObject, JSString};
1511    use crate::jsapi::{
1512        JSStructuredCloneCallbacks, JSStructuredCloneReader, JSStructuredCloneWriter,
1513    };
1514    use crate::jsapi::{MallocSizeOf, ObjectOpResult, ObjectPrivateVisitor, TabSizes};
1515    use crate::jsapi::{SavedFrameResult, SavedFrameSelfHosted};
1516    include!("jsapi_wrappers.in.rs");
1517    include!("glue_wrappers.in.rs");
1518}
1519
1520/// Wrappers for JSAPI/glue methods that accept lifetimed [crate::rust::Handle] and [crate::rust::MutableHandle] arguments and [crate::context::JSContext]
1521pub mod wrappers2 {
1522    macro_rules! wrap {
1523        // The invocation of @inner has the following form:
1524        // @inner (input args) <> (arg signture accumulator) <> (arg expr accumulator) <> unparsed tokens
1525        // when `unparsed tokens == \eps`, accumulator contains the final result
1526        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: Handle<$gentype:ty>, $($rest:tt)*) => {
1527            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: Handle<$gentype>) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1528        };
1529        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandle<$gentype:ty>, $($rest:tt)*) => {
1530            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandle<$gentype>) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1531        };
1532        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: Handle, $($rest:tt)*) => {
1533            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: Handle) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1534        };
1535        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandle, $($rest:tt)*) => {
1536            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandle) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1537        };
1538        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleFunction , $($rest:tt)*) => {
1539            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleFunction) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1540        };
1541        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleId , $($rest:tt)*) => {
1542            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleId) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1543        };
1544        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleObject , $($rest:tt)*) => {
1545            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleObject) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1546        };
1547        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleScript , $($rest:tt)*) => {
1548            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleScript) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1549        };
1550        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleString , $($rest:tt)*) => {
1551            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleString) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1552        };
1553        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleSymbol , $($rest:tt)*) => {
1554            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleSymbol) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1555        };
1556        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: HandleValue , $($rest:tt)*) => {
1557            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: HandleValue) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1558        };
1559        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleFunction , $($rest:tt)*) => {
1560            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleFunction) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1561        };
1562        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleId , $($rest:tt)*) => {
1563            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleId) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1564        };
1565        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleObject , $($rest:tt)*) => {
1566            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleObject) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1567        };
1568        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleScript , $($rest:tt)*) => {
1569            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleScript) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1570        };
1571        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleString , $($rest:tt)*) => {
1572            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleString) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1573        };
1574        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleSymbol , $($rest:tt)*) => {
1575            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleSymbol) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1576        };
1577        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: MutableHandleValue , $($rest:tt)*) => {
1578            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: MutableHandleValue) <> ($($arg_expr_acc,)* $arg.into(),) <> $($rest)*);
1579        };
1580        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: &mut JSContext , $($rest:tt)*) => {
1581            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: &mut JSContext) <> ($($arg_expr_acc,)* $arg.raw_cx(),) <> $($rest)*);
1582        };
1583        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: &JSContext , $($rest:tt)*) => {
1584            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: &JSContext) <> ($($arg_expr_acc,)* $arg.raw_cx_no_gc(),) <> $($rest)*);
1585        };
1586        // functions that take *const AutoRequireNoGC already have &JSContext, so we can remove this mareker argument
1587        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: *const AutoRequireNoGC , $($rest:tt)*) => {
1588            wrap!(@inner $saved <> ($($arg_sig_acc)*) <> ($($arg_expr_acc,)* ::std::ptr::null(),) <> $($rest)*);
1589        };
1590        (@inner $saved:tt <> ($($arg_sig_acc:tt)*) <> ($($arg_expr_acc:expr,)*) <> $arg:ident: $type:ty, $($rest:tt)*) => {
1591            wrap!(@inner $saved <> ($($arg_sig_acc)* , $arg: $type) <> ($($arg_expr_acc,)* $arg,) <> $($rest)*);
1592        };
1593        (@inner ($module:tt: $func_name:ident -> $outtype:ty) <> (, $($args:tt)*) <> ($($argexprs:expr,)*) <> ) => {
1594            #[inline]
1595            pub unsafe fn $func_name($($args)*) -> $outtype {
1596                $module::$func_name($($argexprs),*)
1597            }
1598        };
1599        ($module:tt: pub fn $func_name:ident($($args:tt)*) -> $outtype:ty) => {
1600            wrap!(@inner ($module: $func_name -> $outtype) <> () <> () <> $($args)* ,);
1601        };
1602        ($module:tt: pub fn $func_name:ident($($args:tt)*)) => {
1603            wrap!($module: pub fn $func_name($($args)*) -> ());
1604        }
1605    }
1606
1607    use super::*;
1608    use super::{
1609        Handle, HandleFunction, HandleId, HandleObject, HandleScript, HandleString, HandleValue,
1610        HandleValueArray, MutableHandle, MutableHandleId, MutableHandleObject, MutableHandleString,
1611        MutableHandleValue, StackGCVector,
1612    };
1613    use crate::context::JSContext;
1614    use crate::glue;
1615    use crate::glue::*;
1616    use crate::jsapi;
1617    use crate::jsapi::js::TempAllocPolicy;
1618    use crate::jsapi::mozilla::Utf8Unit;
1619    use crate::jsapi::mozilla::*;
1620    use crate::jsapi::BigInt;
1621    use crate::jsapi::CallArgs;
1622    use crate::jsapi::CloneDataPolicy;
1623    use crate::jsapi::ColumnNumberOneOrigin;
1624    use crate::jsapi::CompartmentTransplantCallback;
1625    use crate::jsapi::ESClass;
1626    use crate::jsapi::EnvironmentChain;
1627    use crate::jsapi::ExceptionStackBehavior;
1628    use crate::jsapi::ForOfIterator;
1629    use crate::jsapi::ForOfIterator_NonIterableBehavior;
1630    use crate::jsapi::HandleObjectVector;
1631    use crate::jsapi::InstantiateOptions;
1632    use crate::jsapi::JSClass;
1633    use crate::jsapi::JSErrorReport;
1634    use crate::jsapi::JSExnType;
1635    use crate::jsapi::JSFunctionSpecWithHelp;
1636    use crate::jsapi::JSJitInfo;
1637    use crate::jsapi::JSONParseHandler;
1638    use crate::jsapi::JSONWriteCallback;
1639    use crate::jsapi::JSPrincipals;
1640    use crate::jsapi::JSPropertySpec;
1641    use crate::jsapi::JSPropertySpec_Name;
1642    use crate::jsapi::JSProtoKey;
1643    use crate::jsapi::JSScript;
1644    use crate::jsapi::JSStructuredCloneData;
1645    use crate::jsapi::JSType;
1646    use crate::jsapi::Latin1Char;
1647    use crate::jsapi::ModuleErrorBehaviour;
1648    use crate::jsapi::ModuleType;
1649    use crate::jsapi::MutableHandleIdVector;
1650    use crate::jsapi::PromiseState;
1651    use crate::jsapi::PromiseUserInputEventHandlingState;
1652    use crate::jsapi::PropertyKey;
1653    use crate::jsapi::ReadOnlyCompileOptions;
1654    use crate::jsapi::Realm;
1655    use crate::jsapi::RealmOptions;
1656    use crate::jsapi::RefPtr;
1657    use crate::jsapi::RegExpFlags;
1658    use crate::jsapi::ScriptEnvironmentPreparer_Closure;
1659    use crate::jsapi::SourceText;
1660    use crate::jsapi::StackCapture;
1661    use crate::jsapi::Stencil;
1662    use crate::jsapi::StructuredCloneScope;
1663    use crate::jsapi::Symbol;
1664    use crate::jsapi::SymbolCode;
1665    use crate::jsapi::TaggedColumnNumberOneOrigin;
1666    use crate::jsapi::TranscodeBuffer;
1667    use crate::jsapi::TwoByteChars;
1668    use crate::jsapi::UniqueChars;
1669    use crate::jsapi::Value;
1670    use crate::jsapi::WasmModule;
1671    use crate::jsapi::*;
1672    use crate::jsapi::{ElementAdder, IsArrayAnswer, PropertyDescriptor};
1673    use crate::jsapi::{JSFunction, JSNative, JSObject, JSString};
1674    use crate::jsapi::{
1675        JSStructuredCloneCallbacks, JSStructuredCloneReader, JSStructuredCloneWriter,
1676    };
1677    use crate::jsapi::{MallocSizeOf, ObjectOpResult, ObjectPrivateVisitor, TabSizes};
1678    use crate::jsapi::{SavedFrameResult, SavedFrameSelfHosted};
1679    include!("jsapi2_wrappers.in.rs");
1680    include!("glue2_wrappers.in.rs");
1681
1682    #[inline]
1683    pub unsafe fn SetPropertyIgnoringNamedGetter(
1684        cx: &mut JSContext,
1685        obj: HandleObject,
1686        id: HandleId,
1687        v: HandleValue,
1688        receiver: HandleValue,
1689        ownDesc: Option<Handle<PropertyDescriptor>>,
1690        result: *mut ObjectOpResult,
1691    ) -> bool {
1692        if let Some(ownDesc) = ownDesc {
1693            let ownDesc = ownDesc.into();
1694            jsapi::SetPropertyIgnoringNamedGetter(
1695                cx.raw_cx(),
1696                obj.into(),
1697                id.into(),
1698                v.into(),
1699                receiver.into(),
1700                &raw const ownDesc,
1701                result,
1702            )
1703        } else {
1704            jsapi::SetPropertyIgnoringNamedGetter(
1705                cx.raw_cx(),
1706                obj.into(),
1707                id.into(),
1708                v.into(),
1709                receiver.into(),
1710                ptr::null(),
1711                result,
1712            )
1713        }
1714    }
1715}