1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::jsapi::glue::JS_ForOfIteratorInit;
use crate::jsapi::glue::JS_ForOfIteratorNext;
use crate::jsapi::jsid;
use crate::jsapi::mozilla;
use crate::jsapi::JSAutoRealm;
use crate::jsapi::JSContext;
use crate::jsapi::JSErrNum;
use crate::jsapi::JSFunctionSpec;
use crate::jsapi::JSJitGetterCallArgs;
use crate::jsapi::JSJitMethodCallArgs;
use crate::jsapi::JSJitSetterCallArgs;
use crate::jsapi::JSNativeWrapper;
use crate::jsapi::JSObject;
use crate::jsapi::JSPropertySpec;
use crate::jsapi::JSPropertySpec_Kind;
use crate::jsapi::JSPropertySpec_Name;
use crate::jsapi::JS;
use crate::jsapi::JS::Scalar::Type;
use crate::jsgc::{RootKind, Rooted, RootedBase, ValueArray};
use crate::jsid::VoidId;
use crate::jsval::{JSVal, UndefinedValue};

use std::marker::PhantomData;
use std::ops::Deref;
use std::ops::DerefMut;
use std::ptr;

impl<T> Deref for JS::Handle<T> {
    type Target = T;

    fn deref<'a>(&'a self) -> &'a T {
        unsafe { &*self.ptr }
    }
}

impl<T> Deref for JS::MutableHandle<T> {
    type Target = T;

    fn deref<'a>(&'a self) -> &'a T {
        unsafe { &*self.ptr }
    }
}

impl<T> DerefMut for JS::MutableHandle<T> {
    fn deref_mut<'a>(&'a mut self) -> &'a mut T {
        unsafe { &mut *self.ptr }
    }
}

impl Default for jsid {
    fn default() -> Self {
        VoidId()
    }
}

impl Default for JS::PropertyDescriptor {
    fn default() -> Self {
        JS::PropertyDescriptor {
            _bitfield_align_1: [],
            _bitfield_1: Default::default(),
            getter_: ptr::null_mut(),
            setter_: ptr::null_mut(),
            value_: UndefinedValue(),
        }
    }
}

impl Drop for JSAutoRealm {
    fn drop(&mut self) {
        unsafe {
            JS::LeaveRealm(self.cx_, self.oldRealm_);
        }
    }
}

impl<T> JS::Handle<T> {
    pub fn get(&self) -> T
    where
        T: Copy,
    {
        unsafe { *self.ptr }
    }

    pub unsafe fn from_marked_location(ptr: *const T) -> JS::Handle<T> {
        JS::Handle {
            ptr: ptr as *mut T,
            _phantom_0: PhantomData,
        }
    }
}

impl<T> JS::MutableHandle<T> {
    pub unsafe fn from_marked_location(ptr: *mut T) -> JS::MutableHandle<T> {
        JS::MutableHandle {
            ptr,
            _phantom_0: PhantomData,
        }
    }

    pub fn handle(&self) -> JS::Handle<T> {
        unsafe { JS::Handle::from_marked_location(self.ptr as *const _) }
    }

    pub fn get(&self) -> T
    where
        T: Copy,
    {
        unsafe { *self.ptr }
    }

    pub fn set(&self, v: T)
    where
        T: Copy,
    {
        unsafe { *self.ptr = v }
    }
}

impl JS::HandleValue {
    pub fn null() -> JS::HandleValue {
        unsafe { JS::NullHandleValue }
    }

    pub fn undefined() -> JS::HandleValue {
        unsafe { JS::UndefinedHandleValue }
    }
}

impl JS::HandleValueArray {
    pub fn empty() -> JS::HandleValueArray {
        JS::HandleValueArray {
            length_: 0,
            elements_: ptr::null(),
        }
    }
}

impl<const N: usize> From<&Rooted<ValueArray<N>>> for JS::HandleValueArray {
    fn from(array: &Rooted<ValueArray<N>>) -> JS::HandleValueArray {
        JS::HandleValueArray {
            length_: N,
            elements_: unsafe { array.ptr.get_ptr() },
        }
    }
}

impl From<&JS::CallArgs> for JS::HandleValueArray {
    fn from(args: &JS::CallArgs) -> JS::HandleValueArray {
        JS::HandleValueArray {
            length_: args.argc_ as usize,
            elements_: args.argv_ as *const _,
        }
    }
}

impl From<JS::Handle<JSVal>> for JS::HandleValueArray {
    fn from(handle: JS::Handle<JSVal>) -> JS::HandleValueArray {
        JS::HandleValueArray {
            length_: 1,
            elements_: handle.ptr,
        }
    }
}

const NULL_OBJECT: *mut JSObject = 0 as *mut JSObject;

impl JS::HandleObject {
    pub fn null() -> JS::HandleObject {
        unsafe { JS::HandleObject::from_marked_location(&NULL_OBJECT) }
    }
}

// ___________________________________________________________________________
// Implementations for various things in jsapi.rs

impl JSAutoRealm {
    pub fn new(cx: *mut JSContext, target: *mut JSObject) -> JSAutoRealm {
        JSAutoRealm {
            cx_: cx,
            oldRealm_: unsafe { JS::EnterRealm(cx, target) },
        }
    }
}

impl JS::AutoGCRooter {
    pub fn new_unrooted(kind: JS::AutoGCRooterKind) -> JS::AutoGCRooter {
        JS::AutoGCRooter {
            down: ptr::null_mut(),
            kind_: kind,
            stackTop: ptr::null_mut(),
        }
    }

    pub unsafe fn add_to_root_stack(&mut self, cx: *mut JSContext) {
        #[allow(non_snake_case)]
        let autoGCRooters: *mut _ = {
            let rooting_cx = cx as *mut JS::RootingContext;
            &mut (*rooting_cx).autoGCRooters_.0[self.kind_ as usize]
        };
        self.stackTop = autoGCRooters as *mut *mut _;
        self.down = *autoGCRooters as *mut _;

        assert!(*self.stackTop != self);
        *autoGCRooters = self as *mut _ as _;
    }

    pub unsafe fn remove_from_root_stack(&mut self) {
        assert!(*self.stackTop == self);
        *self.stackTop = self.down;
    }
}

impl JSJitMethodCallArgs {
    #[inline]
    pub fn get(&self, i: u32) -> JS::HandleValue {
        unsafe {
            if i < self.argc_ {
                JS::HandleValue::from_marked_location(self.argv_.offset(i as isize))
            } else {
                JS::UndefinedHandleValue
            }
        }
    }

    #[inline]
    pub fn index(&self, i: u32) -> JS::HandleValue {
        assert!(i < self.argc_);
        unsafe { JS::HandleValue::from_marked_location(self.argv_.offset(i as isize)) }
    }

    #[inline]
    pub fn index_mut(&self, i: u32) -> JS::MutableHandleValue {
        assert!(i < self.argc_);
        unsafe { JS::MutableHandleValue::from_marked_location(self.argv_.offset(i as isize)) }
    }

    #[inline]
    pub fn rval(&self) -> JS::MutableHandleValue {
        unsafe { JS::MutableHandleValue::from_marked_location(self.argv_.offset(-2)) }
    }
}

impl JSJitGetterCallArgs {
    #[inline]
    pub fn rval(&self) -> JS::MutableHandleValue {
        self._base
    }
}

// XXX need to hack up bindgen to convert this better so we don't have
//     to duplicate so much code here
impl JS::CallArgs {
    #[inline]
    pub unsafe fn from_vp(vp: *mut JS::Value, argc: u32) -> JS::CallArgs {
        // For some reason, with debugmozjs, calling
        // JS_CallArgsFromVp(argc, vp)
        // produces a SEGV caused by the vp being overwritten by the argc.
        // TODO: debug this!
        JS::CallArgs {
            _bitfield_align_1: Default::default(),
            _bitfield_1: JS::CallArgs::new_bitfield_1((*vp.offset(1)).is_magic(), false),
            argc_: argc,
            argv_: vp.offset(2),
            #[cfg(not(feature = "debugmozjs"))]
            __bindgen_padding_0: [0, 0, 0],
            #[cfg(feature = "debugmozjs")]
            wantUsedRval_: JS::detail::IncludeUsedRval { usedRval_: false },
        }
    }

    #[inline]
    pub fn index(&self, i: u32) -> JS::HandleValue {
        assert!(i < self.argc_);
        unsafe { JS::HandleValue::from_marked_location(self.argv_.offset(i as isize)) }
    }

    #[inline]
    pub fn index_mut(&self, i: u32) -> JS::MutableHandleValue {
        assert!(i < self.argc_);
        unsafe { JS::MutableHandleValue::from_marked_location(self.argv_.offset(i as isize)) }
    }

    #[inline]
    pub fn get(&self, i: u32) -> JS::HandleValue {
        unsafe {
            if i < self.argc_ {
                JS::HandleValue::from_marked_location(self.argv_.offset(i as isize))
            } else {
                JS::UndefinedHandleValue
            }
        }
    }

    #[inline]
    pub fn rval(&self) -> JS::MutableHandleValue {
        unsafe { JS::MutableHandleValue::from_marked_location(self.argv_.offset(-2)) }
    }

    #[inline]
    pub fn thisv(&self) -> JS::HandleValue {
        unsafe { JS::HandleValue::from_marked_location(self.argv_.offset(-1)) }
    }

    #[inline]
    pub fn calleev(&self) -> JS::HandleValue {
        unsafe { JS::HandleValue::from_marked_location(self.argv_.offset(-2)) }
    }

    #[inline]
    pub fn callee(&self) -> *mut JSObject {
        self.calleev().to_object()
    }

    #[inline]
    pub fn new_target(&self) -> JS::MutableHandleValue {
        assert!(self.constructing_());
        unsafe {
            JS::MutableHandleValue::from_marked_location(self.argv_.offset(self.argc_ as isize))
        }
    }

    #[inline]
    pub fn is_constructing(&self) -> bool {
        unsafe { (*self.argv_.offset(-1)).is_magic() }
    }
}

impl JSJitSetterCallArgs {
    #[inline]
    pub fn get(&self, i: u32) -> JS::HandleValue {
        assert!(i == 0);
        self._base.handle()
    }
}

impl JSFunctionSpec {
    pub const ZERO: Self = JSFunctionSpec {
        name: JSPropertySpec_Name {
            string_: ptr::null(),
        },
        selfHostedName: 0 as *const _,
        flags: 0,
        nargs: 0,
        call: JSNativeWrapper::ZERO,
    };

    pub fn is_zeroed(&self) -> bool {
        (unsafe { self.name.string_.is_null() })
            && self.selfHostedName.is_null()
            && self.flags == 0
            && self.nargs == 0
            && self.call.is_zeroed()
    }
}

impl JSPropertySpec {
    pub const ZERO: Self = JSPropertySpec {
        name: JSPropertySpec_Name {
            string_: ptr::null(),
        },
        attributes_: 0,
        kind_: JSPropertySpec_Kind::NativeAccessor,
        u: crate::jsapi::JSPropertySpec_AccessorsOrValue {
            accessors: crate::jsapi::JSPropertySpec_AccessorsOrValue_Accessors {
                getter: crate::jsapi::JSPropertySpec_Accessor {
                    native: JSNativeWrapper::ZERO,
                },
                setter: crate::jsapi::JSPropertySpec_Accessor {
                    native: JSNativeWrapper::ZERO,
                },
            },
        },
    };

    /// https://searchfox.org/mozilla-central/rev/2bdaa395cb841b28f8ef74882a61df5efeedb42b/js/public/PropertySpec.h#305-307
    pub fn is_accessor(&self) -> bool {
        self.kind_ == JSPropertySpec_Kind::NativeAccessor
            || self.kind_ == JSPropertySpec_Kind::SelfHostedAccessor
    }

    pub fn is_zeroed(&self) -> bool {
        (unsafe { self.name.string_.is_null() })
            && self.attributes_ == 0
            && self.is_accessor()
            && unsafe { self.u.accessors.getter.native.is_zeroed() }
            && unsafe { self.u.accessors.setter.native.is_zeroed() }
    }
}

impl JSNativeWrapper {
    pub const ZERO: Self = JSNativeWrapper {
        info: 0 as *const _,
        op: None,
    };

    pub fn is_zeroed(&self) -> bool {
        self.op.is_none() && self.info.is_null()
    }
}

impl RootedBase {
    unsafe fn add_to_root_stack(&mut self, cx: *mut JSContext, kind: JS::RootKind) {
        let stack = Self::get_root_stack(cx, kind);
        self.stack = stack;
        self.prev = *stack;

        *stack = self as *mut _ as usize as _;
    }

    unsafe fn remove_from_root_stack(&mut self) {
        assert!(*self.stack == self as *mut _ as usize as _);
        *self.stack = self.prev;
    }

    unsafe fn get_root_stack(cx: *mut JSContext, kind: JS::RootKind) -> *mut *mut RootedBase {
        let kind = kind as usize;
        let rooting_cx = Self::get_rooting_context(cx);
        &mut (*rooting_cx).stackRoots_.0[kind] as *mut _ as *mut _
    }

    unsafe fn get_rooting_context(cx: *mut JSContext) -> *mut JS::RootingContext {
        cx as *mut JS::RootingContext
    }
}

impl<T: RootKind> JS::Rooted<T> {
    pub fn new_unrooted() -> JS::Rooted<T> {
        JS::Rooted {
            vtable: T::VTABLE,
            base: RootedBase {
                stack: ptr::null_mut(),
                prev: ptr::null_mut(),
            },
            ptr: unsafe { std::mem::zeroed() },
        }
    }

    pub unsafe fn add_to_root_stack(&mut self, cx: *mut JSContext) {
        self.base.add_to_root_stack(cx, T::KIND)
    }

    pub unsafe fn remove_from_root_stack(&mut self) {
        self.base.remove_from_root_stack()
    }
}

impl JS::ObjectOpResult {
    pub fn ok(&self) -> bool {
        assert_ne!(
            self.code_,
            JS::ObjectOpResult_SpecialCodes::Uninitialized as usize
        );
        self.code_ == JS::ObjectOpResult_SpecialCodes::OkCode as usize
    }

    /// Set this ObjectOpResult to true and return true.
    pub fn succeed(&mut self) -> bool {
        self.code_ = JS::ObjectOpResult_SpecialCodes::OkCode as usize;
        true
    }

    pub fn fail(&mut self, code: JSErrNum) -> bool {
        assert_ne!(
            code as usize,
            JS::ObjectOpResult_SpecialCodes::OkCode as usize
        );
        self.code_ = code as usize;
        true
    }

    pub fn fail_cant_redefine_prop(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_REDEFINE_PROP)
    }

    pub fn fail_read_only(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_READ_ONLY)
    }

    pub fn fail_getter_only(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_GETTER_ONLY)
    }

    pub fn fail_cant_delete(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DELETE)
    }

    pub fn fail_cant_set_interposed(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_SET_INTERPOSED)
    }

    pub fn fail_cant_define_window_element(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DEFINE_WINDOW_ELEMENT)
    }

    pub fn fail_cant_delete_window_element(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DELETE_WINDOW_ELEMENT)
    }

    pub fn fail_cant_define_window_named_property(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DEFINE_WINDOW_NAMED_PROPERTY)
    }

    pub fn fail_cant_delete_window_named_property(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DELETE_WINDOW_NAMED_PROPERTY)
    }

    pub fn fail_cant_define_window_non_configurable(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_DEFINE_WINDOW_NC)
    }

    pub fn fail_cant_prevent_extensions(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_PREVENT_EXTENSIONS)
    }

    pub fn fail_cant_set_proto(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_CANT_SET_PROTO)
    }

    pub fn fail_no_named_setter(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_NO_NAMED_SETTER)
    }

    pub fn fail_no_indexed_setter(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_NO_INDEXED_SETTER)
    }

    pub fn fail_not_data_descriptor(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_NOT_DATA_DESCRIPTOR)
    }

    pub fn fail_invalid_descriptor(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_INVALID_DESCRIPTOR)
    }

    pub fn fail_bad_array_length(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_BAD_ARRAY_LENGTH)
    }

    pub fn fail_bad_index(&mut self) -> bool {
        self.fail(JSErrNum::JSMSG_BAD_INDEX)
    }

    pub fn failure_code(&self) -> u32 {
        assert!(!self.ok());
        self.code_ as u32
    }

    #[deprecated]
    #[allow(non_snake_case)]
    pub fn failNoNamedSetter(&mut self) -> bool {
        self.fail_no_named_setter()
    }
}

impl Default for JS::ObjectOpResult {
    fn default() -> JS::ObjectOpResult {
        JS::ObjectOpResult {
            code_: JS::ObjectOpResult_SpecialCodes::Uninitialized as usize,
        }
    }
}

impl JS::ForOfIterator {
    pub unsafe fn init(
        &mut self,
        iterable: JS::HandleValue,
        non_iterable_behavior: JS::ForOfIterator_NonIterableBehavior,
    ) -> bool {
        JS_ForOfIteratorInit(self, iterable, non_iterable_behavior)
    }

    pub unsafe fn next(&mut self, val: JS::MutableHandleValue, done: *mut bool) -> bool {
        JS_ForOfIteratorNext(self, val, done)
    }
}

impl<T> mozilla::Range<T> {
    pub fn new(start: &mut T, end: &mut T) -> mozilla::Range<T> {
        mozilla::Range {
            mStart: mozilla::RangedPtr {
                mPtr: start,
                #[cfg(feature = "debugmozjs")]
                mRangeStart: start,
                #[cfg(feature = "debugmozjs")]
                mRangeEnd: end,
                _phantom_0: PhantomData,
            },
            mEnd: mozilla::RangedPtr {
                mPtr: end,
                #[cfg(feature = "debugmozjs")]
                mRangeStart: start,
                #[cfg(feature = "debugmozjs")]
                mRangeEnd: end,
                _phantom_0: PhantomData,
            },
            _phantom_0: PhantomData,
        }
    }
}

impl Type {
    /// Returns byte size of Type (if possible to determine)
    ///
    /// <https://searchfox.org/mozilla-central/rev/396a6123691f7ab3ffb449dcbe95304af6f9df3c/js/public/ScalarType.h#66>
    pub const fn byte_size(&self) -> Option<usize> {
        match self {
            Type::Int8 | Type::Uint8 | Type::Uint8Clamped => Some(1),
            Type::Int16 | Type::Uint16 | Type::Float16 => Some(2),
            Type::Int32 | Type::Uint32 | Type::Float32 => Some(4),
            Type::Int64 | Type::Float64 | Type::BigInt64 | Type::BigUint64 => Some(8),
            Type::Simd128 => Some(16),
            Type::MaxTypedArrayViewType => None,
        }
    }
}