nova_vm 1.0.0

Nova Virtual Machine
Documentation
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
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
// 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 https://mozilla.org/MPL/2.0/.

mod intrinsics;

pub use intrinsics::*;

use super::{
    Agent, ExecutionContext, JsResult, environments::GlobalEnvironment, new_global_environment,
};
use crate::{
    ecmascript::{
        AbstractModule, BUILTIN_STRING_MEMORY, HostDefined, LoadedModules, ModuleRequest, Number,
        Object, OrdinaryObject, PropertyDescriptor, PropertyKey, Value, define_property_or_throw,
    },
    engine::{Bindable, GcScope, NoGcScope, Scopable, bindable_handle},
    heap::{
        ArenaAccess, ArenaAccessMut, BaseIndex, CompactionLists, CreateHeapData, Heap,
        HeapIndexHandle, HeapMarkAndSweep, WorkQueues, arena_vec_access, index_handle,
    },
};
use core::marker::PhantomData;

/// ## [9.3 Realms](https://tc39.es/ecma262/#sec-code-realms)
///
/// Before it is evaluated, all ECMAScript code must be associated with a
/// _realm_. Conceptually, a realm consists of a set of intrinsic objects, an
/// ECMAScript global environment, all of the ECMAScript code that is loaded
/// within the scope of that global environment, and other associated state and
/// resources.
///
/// [`Realm`]: Realm
/// [`RealmRoot`]: crate::ecmascript::RealmRoot
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[repr(transparent)]
pub struct Realm<'a>(BaseIndex<'a, RealmRecord<'static>>);
index_handle!(Realm);
arena_vec_access!(Realm, 'a, RealmRecord, realms);

impl core::fmt::Debug for Realm<'_> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "Realm({:?})", self.get_index_u32())
    }
}

impl<'r> Realm<'r> {
    /// ### \[\[\HostDefined]]
    pub fn host_defined(self, agent: &Agent) -> Option<HostDefined> {
        self.get(agent).host_defined.clone()
    }

    /// Initialize the \[\[HostDefined]] field to a value.
    ///
    /// ## Panics
    ///
    /// Panics if the \[\[HostDefined]] field is non-empty.
    pub fn initialize_host_defined(self, agent: &mut Agent, host_defined: HostDefined) {
        assert!(
            self.get(agent).host_defined.is_none(),
            "Attempted to replace Realm's [[HostDefined]] slot data."
        );
        self.get_mut(agent).host_defined.replace(host_defined);
    }

    /// ### \[\[GlobalObject]]
    pub fn global_object(self, agent: &mut Agent) -> Object<'r> {
        self.get(agent).global_object
    }

    /// ### \[\[GlobalEnv]]
    pub(crate) fn global_env<'gc>(
        self,
        agent: &mut Agent,
        gc: NoGcScope<'gc, '_>,
    ) -> Option<GlobalEnvironment<'gc>> {
        self.get(agent).global_env.bind(gc)
    }

    pub(crate) fn insert_loaded_module<'gc>(
        self,
        agent: &mut Agent,
        request: ModuleRequest<'gc>,
        module: AbstractModule<'gc>,
    ) {
        let requests = &agent.heap.module_request_records;
        self.get_mut(&mut agent.heap.realms)
            .loaded_modules
            .insert_loaded_module(requests, request, module);
    }
}

impl HeapMarkAndSweep for Realm<'static> {
    fn mark_values(&self, queues: &mut WorkQueues) {
        queues.realms.push(*self);
    }

    fn sweep_values(&mut self, compactions: &CompactionLists) {
        compactions.realms.shift_index(&mut self.0);
    }
}

impl<'a> CreateHeapData<RealmRecord<'a>, Realm<'a>> for Heap {
    fn create(&mut self, data: RealmRecord<'a>) -> Realm<'a> {
        self.realms.push(data.unbind());
        self.alloc_counter += core::mem::size_of::<RealmRecord<'static>>();
        Realm(BaseIndex::last(&self.realms))
    }
}

/// ## [9.3 Realms](https://tc39.es/ecma262/#sec-code-realms)
///
/// Before it is evaluated, all ECMAScript code must be associated with a
/// realm. Conceptually, a realm consists of a set of intrinsic objects, an
/// ECMAScript global environment, all of the ECMAScript code that is loaded
/// within the scope of that global environment, and other associated state and
/// resources.
#[derive(Debug)]
pub(crate) struct RealmRecord<'a> {
    /// ### \[\[AgentSignifier]]
    ///
    /// The agent that owns this realm
    agent_signifier: PhantomData<Agent>,

    /// ### \[\[Intrinsics]]
    ///
    /// The intrinsic values used by code associated with this realm.
    intrinsics: Intrinsics,

    /// ### \[\[GlobalObject]]
    ///
    /// The global object for this realm.
    pub(crate) global_object: Object<'a>,

    /// ### \[\[GlobalEnv]]
    ///
    /// The global environment for this realm.
    pub(crate) global_env: Option<GlobalEnvironment<'a>>,

    /// ### \[\[TemplateMap]]
    ///
    /// Template objects are canonicalized separately for each realm using its
    /// Realm Record's \[\[TemplateMap]]. Each \[\[Site]] value is a Parse Node
    /// that is a TemplateLiteral. The associated \[\[Array]] value is the
    /// corresponding template object that is passed to a tag function.
    /// NOTE: The template data is included in the AST.
    #[expect(dead_code)]
    template_map: (),

    /// ### \[\[LoadedModules]]
    ///
    /// A map from the specifier strings imported by this realm to the resolved
    /// Module Record. The list does not contain two different Records with the
    /// same \[\[Specifier]].
    loaded_modules: LoadedModules<'a>,

    /// ### \[\[HostDefined]]
    ///
    /// Field reserved for use by hosts that need to associate additional
    /// information with a Realm Record.
    pub(crate) host_defined: Option<HostDefined>,
}

unsafe impl Send for RealmRecord<'_> {}

impl RealmRecord<'_> {
    pub(crate) fn intrinsics(&self) -> &Intrinsics {
        &self.intrinsics
    }
}

bindable_handle!(RealmRecord);

impl HeapMarkAndSweep for RealmRecord<'static> {
    fn mark_values(&self, queues: &mut WorkQueues) {
        let Self {
            agent_signifier: _,
            intrinsics,
            global_object,
            global_env,
            template_map: _,
            loaded_modules,
            host_defined: _,
        } = self;
        intrinsics.mark_values(queues);
        global_env.mark_values(queues);
        global_object.mark_values(queues);
        loaded_modules.mark_values(queues);
    }

    fn sweep_values(&mut self, compactions: &CompactionLists) {
        let Self {
            agent_signifier: _,
            intrinsics,
            global_object,
            global_env,
            template_map: _,
            loaded_modules,
            host_defined: _,
        } = self;
        intrinsics.sweep_values(compactions);
        global_env.sweep_values(compactions);
        global_object.sweep_values(compactions);
        loaded_modules.sweep_values(compactions);
    }
}

/// ### [9.3.1 CreateRealm ( )](https://tc39.es/ecma262/#sec-createrealm)
///
/// The abstract operation CreateRealm takes no arguments and returns a Realm
/// Record.
pub(crate) fn create_realm<'gc>(agent: &mut Agent, gc: NoGcScope<'gc, '_>) -> Realm<'gc> {
    // 1. Let realmRec be a new Realm Record.
    let realm_rec = RealmRecord {
        // 2. Perform CreateIntrinsics(realmRec).
        intrinsics: create_intrinsics(agent),

        // 3. Set realmRec.[[AgentSignifier]] to AgentSignifier().
        agent_signifier: PhantomData,

        // 4. Set realmRec.[[GlobalObject]] to undefined.
        global_object: Object::Object(OrdinaryObject::_DEF),

        // 5. Set realmRec.[[GlobalEnv]] to undefined.
        global_env: None,

        // 6. Set realmRec.[[TemplateMap]] to a new empty List.
        template_map: (),

        // NOTE: These fields are implicitly empty.
        host_defined: None,
        loaded_modules: Default::default(),
    };

    // 7. Return realmRec.
    let realm = agent.heap.create(realm_rec).bind(gc);
    Intrinsics::create_intrinsics(agent, realm.unbind(), gc);
    realm
}

/// ### [9.3.2 CreateIntrinsics ( realmRec )](https://tc39.es/ecma262/#sec-createintrinsics)
///
/// The abstract operation CreateIntrinsics takes argument realmRec (a Realm
/// Record) and returns UNUSED.
pub(crate) fn create_intrinsics(agent: &mut Agent) -> Intrinsics {
    // TODO: Follow the specification.
    // 1. Set realmRec.[[Intrinsics]] to a new Record.
    // 2. Set fields of realmRec.[[Intrinsics]] with the values listed in
    //    Table 6. The field names are the names listed in column one of the
    //    table. The value of each field is a new object value fully and
    //    recursively populated with property values as defined by the
    //    specification of each object in clauses 19 through 28. All object
    //    property values are newly created object values. All values that are
    //    built-in function objects are created by performing
    //    CreateBuiltinFunction(steps, length, name, slots, realmRec, prototype)
    //    where steps is the definition of that function provided by this
    //    specification, name is the initial value of the function's "name"
    //    property, length is the initial value of the function's "length"
    //    property, slots is a list of the names, if any, of the function's
    //    specified internal slots, and prototype is the specified value of the
    //    function's [[Prototype]] internal slot. The creation of the intrinsics
    //    and their properties must be ordered to avoid any dependencies upon
    //    objects that have not yet been created.
    // 3. Perform AddRestrictedFunctionProperties(realmRec.[[Intrinsics]].[[%Function.prototype%]], realmRec).

    // 4. Return UNUSED.
    // NOTE: We divert from the specification to allow us to call
    //       CreateIntrinsics when we create the Realm.

    Intrinsics::new(agent)
}

/// ### [9.3.3 SetRealmGlobalObject ( realmRec, globalObj, thisValue )](https://tc39.es/ecma262/#sec-setrealmglobalobject)
pub(crate) fn set_realm_global_object(
    agent: &mut Agent,
    realm_id: Realm,
    global_object: Option<Object>,
    this_value: Option<Object>,
    gc: NoGcScope,
) {
    // 1. If globalObj is undefined, then
    let global_object = global_object.unwrap_or_else(|| {
        // a. Let intrinsics be realmRec.[[Intrinsics]].
        let intrinsics = &agent.get_realm_record_by_id(realm_id).intrinsics;
        // b. Set globalObj to OrdinaryObjectCreate(intrinsics.[[%Object.prototype%]]).
        Object::Object(
            OrdinaryObject::create_intrinsic_object(
                agent,
                Some(intrinsics.object_prototype().into()),
                &[],
            )
            .expect("Should perform GC here"),
        )
    });

    // 2. Assert: globalObj is an Object.
    // No-op

    // 3. If thisValue is undefined, set thisValue to globalObj.
    let this_value = this_value.unwrap_or(global_object);

    // 4. Set realmRec.[[GlobalObject]] to globalObj.
    realm_id.get_mut(agent).global_object = global_object.unbind();

    // 5. Let newGlobalEnv be NewGlobalEnvironment(globalObj, thisValue).
    let new_global_env = new_global_environment(agent, global_object, this_value, gc);

    // 6. Set realmRec.[[GlobalEnv]] to newGlobalEnv.
    realm_id.get_mut(agent).global_env = Some(new_global_env.unbind());

    // 7. Return UNUSED.
}

/// ### [9.3.4 SetDefaultGlobalBindings ( realmRec )](https://tc39.es/ecma262/#sec-setdefaultglobalbindings)
///
/// The abstract operation SetDefaultGlobalBindings takes argument realmRec (a
/// Realm Record) and returns either a normal completion containing an Object
/// or a throw completion.
pub(crate) fn set_default_global_bindings<'a>(
    agent: &mut Agent,
    realm_id: Realm,
    mut gc: GcScope<'a, '_>,
) -> JsResult<'a, Object<'a>> {
    // 1. Let global be realmRec.[[GlobalObject]].
    let global = realm_id.get(agent).global_object.scope(agent, gc.nogc());

    // 2. For each property of the Global Object specified in clause 19, do
    macro_rules! define_property {
        (intrinsic $name:ident, $value:ident) => {
            // most of the properties have this configuration
            let value = agent
                .get_realm_record_by_id(realm_id)
                .intrinsics()
                .$value()
                .into();
            define_property!($name, value, Some(true), Some(false), Some(true));
        };
        ($name:ident, $value:ident, $writable:expr, $enumerable:expr, $configurable:expr) => {
            // a. Let name be the String value of the property name.
            let name = PropertyKey::from(BUILTIN_STRING_MEMORY.$name);
            let value = $value;

            // b. Let desc be the fully populated data Property Descriptor for the
            //    property, containing the specified attributes for the property. For
            //    properties listed in 19.2, 19.3, or 19.4 the value of the [[Value]]
            //    attribute is the corresponding intrinsic object from realmRec.
            let desc = PropertyDescriptor {
                value: Some(value),
                writable: $writable,
                enumerable: $enumerable,
                configurable: $configurable,
                ..Default::default()
            };

            // c. Perform ? DefinePropertyOrThrow(global, name, desc).
            define_property_or_throw(agent, global.get(agent), name, desc, gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
        };
    }

    // 19.1 Value Properties of the Global Object
    {
        // 19.1.1 globalThis
        let global_env = realm_id.get(agent).global_env.bind(gc.nogc());
        let value = global_env.unwrap().get_this_binding(agent).unbind().into();
        define_property!(globalThis, value, Some(true), Some(false), Some(true));

        // 19.1.2 Infinity
        let value = Number::from_f64(agent, f64::INFINITY, gc.nogc())
            .unbind()
            .into();
        define_property!(Infinity, value, Some(false), Some(false), Some(false));

        // 19.1.3 NaN
        let value = Number::from_f64(agent, f64::NAN, gc.nogc()).unbind().into();
        define_property!(NaN, value, Some(false), Some(false), Some(false));

        // 19.1.4 undefined
        let value = Value::Undefined;
        define_property!(undefined, value, Some(false), Some(false), Some(false));
    }

    // 19.2 Function Properties of the Global Object
    {
        // 19.2.1 eval ( x )
        define_property!(intrinsic eval, eval);

        // 19.2.2 isFinite ( number )
        define_property!(intrinsic isFinite, is_finite);

        // 19.2.3 isNaN ( number )
        define_property!(intrinsic isNaN, is_nan);

        // 19.2.4 parseFloat ( string )
        define_property!(intrinsic parseFloat, parse_float);

        // 19.2.5 parseInt ( string, radix )
        define_property!(intrinsic parseInt, parse_int);

        // 19.2.6.1 decodeURI ( . . . )
        define_property!(intrinsic decodeURI, decode_uri);

        // 19.2.6.2 decodeURIComponent ( . . . )
        define_property!(intrinsic decodeURIComponent, decode_uri_component);

        // 19.2.6.3 encodeURI ( . . . )
        define_property!(intrinsic encodeURI, encode_uri);

        // 19.2.6.4 encodeURIComponent ( . . . )
        define_property!(intrinsic encodeURIComponent, encode_uri_component);

        #[cfg(feature = "annex-b-global")]
        {
            // B.2.1.1 escape ( string )
            define_property!(intrinsic escape, escape);
            // B.2.1.2 unescape ( string )
            define_property!(intrinsic unescape, unescape);
        }
    }

    // 19.3 Constructor Properties of the Global Object
    {
        // 19.3.1 AggregateError ( . . . )
        define_property!(intrinsic AggregateError, aggregate_error);

        // 19.3.2 Array ( . . . )
        define_property!(intrinsic Array, array);

        // 19.3.3 ArrayBuffer ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic ArrayBuffer, array_buffer);

        // 19.3.4 BigInt ( . . . )
        define_property!(intrinsic BigInt, big_int);

        // 19.3.5 BigInt64Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic BigInt64Array, big_int64_array);

        // 19.3.6 BigUint64Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic BigUint64Array, big_uint64_array);

        // 19.3.7 Boolean ( . . . )
        define_property!(intrinsic Boolean, boolean);

        // 19.3.8 DataView ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic DataView, data_view);

        // 19.3.9 Date ( . . . )
        #[cfg(feature = "date")]
        define_property!(intrinsic Date, date);

        // 19.3.10 Error ( . . . )
        define_property!(intrinsic Error, error);

        // 19.3.11 EvalError ( . . . )
        define_property!(intrinsic EvalError, eval_error);

        // 19.3.12 FinalizationRegistry ( . . . )
        define_property!(intrinsic FinalizationRegistry, finalization_registry);

        // 19.3.13 Float16Array ( . . . )
        #[cfg(feature = "proposal-float16array")]
        define_property!(intrinsic Float16Array, float16_array);

        // 19.3.14 Float32Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Float32Array, float32_array);

        // 19.3.15 Float64Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Float64Array, float64_array);

        // 19.3.16 Function ( . . . )
        define_property!(intrinsic Function, function);

        // 19.3.17 Int8Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Int8Array, int8_array);

        // 19.3.18 Int16Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Int16Array, int16_array);

        // 19.3.19 Int32Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Int32Array, int32_array);

        // 19.3.20 Iterator ( . . . )
        define_property!(intrinsic Iterator, iterator);

        // 19.3.21 Map ( . . . )
        define_property!(intrinsic Map, map);

        // 19.3.22 Number ( . . . )
        define_property!(intrinsic Number, number);

        // 19.3.23 Object ( . . . )
        define_property!(intrinsic Object, object);

        // 19.3.24 Promise ( . . . )
        define_property!(intrinsic Promise, promise);

        // 19.3.25 Proxy ( . . . )
        define_property!(intrinsic Proxy, proxy);

        // 19.3.26 RangeError ( . . . )
        define_property!(intrinsic RangeError, range_error);

        // 19.3.27 ReferenceError ( . . . )
        define_property!(intrinsic ReferenceError, reference_error);

        // 19.3.28 RegExp ( . . . )
        #[cfg(feature = "regexp")]
        define_property!(intrinsic RegExp, reg_exp);

        // 19.3.29 Set ( . . . )
        #[cfg(feature = "set")]
        define_property!(intrinsic Set, set);

        // 19.3.30 SharedArrayBuffer ( . . . )
        #[cfg(feature = "shared-array-buffer")]
        define_property!(intrinsic SharedArrayBuffer, shared_array_buffer);

        // 19.3.31 String ( . . . )
        define_property!(intrinsic String, string);

        // 19.3.32 Symbol ( . . . )
        define_property!(intrinsic Symbol, symbol);

        // 19.3.33 SyntaxError ( . . . )
        define_property!(intrinsic SyntaxError, syntax_error);

        // 19.3.34 TypeError ( . . . )
        define_property!(intrinsic TypeError, type_error);

        // 19.3.35 Uint8Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Uint8Array, uint8_array);

        // 19.3.36 Uint8ClampedArray ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Uint8ClampedArray, uint8_clamped_array);

        // 19.3.37 Uint16Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Uint16Array, uint16_array);

        // 19.3.38 Uint32Array ( . . . )
        #[cfg(feature = "array-buffer")]
        define_property!(intrinsic Uint32Array, uint32_array);

        // 19.3.39 URIError ( . . . )
        define_property!(intrinsic URIError, uri_error);

        // 19.3.40 WeakMap ( . . . )
        #[cfg(feature = "weak-refs")]
        define_property!(intrinsic WeakMap, weak_map);

        // 19.3.41 WeakRef ( . . . )
        #[cfg(feature = "weak-refs")]
        define_property!(intrinsic WeakRef, weak_ref);

        // 19.3.42 WeakSet ( . . . )
        #[cfg(feature = "weak-refs")]
        define_property!(intrinsic WeakSet, weak_set);
    }

    // 19.4 Other Properties of the Global Object
    {
        // 19.4.1 Atomics
        #[cfg(feature = "atomics")]
        define_property!(intrinsic Atomics, atomics);

        // 19.4.2 JSON
        #[cfg(feature = "json")]
        define_property!(intrinsic JSON, json);

        // 19.4.3 Math
        #[cfg(feature = "math")]
        define_property!(intrinsic Math, math);

        // 19.4.4 Reflect
        define_property!(intrinsic Reflect, reflect);

        #[cfg(feature = "temporal")]
        define_property!(intrinsic Temporal, temporal);
    }

    // 3. Return global.
    Ok(global.get(agent).bind(gc.into_nogc()))
}

/// ## [9.6 InitializeHostDefinedRealm ( )](https://tc39.es/ecma262/#sec-initializehostdefinedrealm)
pub(crate) fn initialize_host_defined_realm(
    agent: &mut Agent,
    create_global_object: Option<impl for<'a> FnOnce(&mut Agent, GcScope<'a, '_>) -> Object<'a>>,
    create_global_this_value: Option<
        impl for<'a> FnOnce(&mut Agent, GcScope<'a, '_>) -> Object<'a>,
    >,
    initialize_global_object: Option<impl FnOnce(&mut Agent, Object, GcScope)>,
    mut gc: GcScope,
) {
    // 1. Let realm be CreateRealm().
    let realm = create_realm(agent, gc.nogc());

    // 2. Let newContext be a new execution context.
    let new_context = ExecutionContext {
        // NOTE: This property is assumed to be null until the specification
        //       assigns it.
        ecmascript_code: None,

        // 3. Set the Function of newContext to null.
        function: None,

        // 4. Set the Realm of newContext to realm.
        realm: realm.unbind(),

        // 5. Set the ScriptOrModule of newContext to null.
        script_or_module: None,
    };

    // 6. Push newContext onto the execution context stack; newContext is now the running execution context.
    agent.push_execution_context(new_context);

    // 7. If the host requires use of an exotic object to serve as realm's global object,
    // let global be such an object created in a host-defined manner.
    // Otherwise, let global be undefined, indicating that an ordinary object should be created as the global object.
    let global = create_global_this_value.map(|create_global_this_value| {
        create_global_this_value(agent, gc.reborrow())
            .unbind()
            .scope(agent, gc.nogc())
    });

    // 8. If the host requires that the this binding in realm's global scope return an object other than the global object,
    // let thisValue be such an object created in a host-defined manner.
    // Otherwise, let thisValue be undefined, indicating that realm's global this binding should be the global object.
    let this_value =
        create_global_object.map(|create_global_object| create_global_object(agent, gc.reborrow()));

    // 9. Perform SetRealmGlobalObject(realm, global, thisValue).
    set_realm_global_object(
        agent,
        agent.current_realm_id_internal(),
        global.map(|g| g.get(agent)),
        this_value.unbind(),
        gc.nogc(),
    );

    // 10. Let globalObj be ? SetDefaultGlobalBindings(realm).
    let global_object =
        set_default_global_bindings(agent, agent.current_realm_id_internal(), gc.reborrow())
            .unbind()
            .map_err(|err| {
                err.value()
                    .string_repr(agent, gc.reborrow())
                    .to_string_lossy_(agent)
                    .to_string()
            })
            .unwrap()
            .unbind()
            .bind(gc.nogc());

    // 11. Create any host-defined global object properties on globalObj.
    if let Some(initialize_global_object) = initialize_global_object {
        initialize_global_object(agent, global_object.unbind(), gc.reborrow());
    };

    // 12. Return UNUSED.
}

pub(crate) fn initialize_default_realm(agent: &mut Agent, gc: GcScope) {
    let create_global_object: Option<for<'a> fn(&mut Agent, GcScope<'a, '_>) -> Object<'a>> = None;
    let create_global_this_value: Option<for<'a> fn(&mut Agent, GcScope<'a, '_>) -> Object<'a>> =
        None;
    let initialize_global_object: Option<fn(&mut Agent, Object, GcScope)> = None;
    initialize_host_defined_realm(
        agent,
        create_global_object,
        create_global_this_value,
        initialize_global_object,
        gc,
    );
}

#[cfg(test)]
mod test {
    use crate::heap::HeapIndexHandle;
    #[allow(unused_imports)]
    use crate::{
        ecmascript::types::BuiltinFunctionHeapData,
        engine::{Bindable, GcScope},
        heap::{
            IntrinsicConstructorIndexes, IntrinsicFunctionIndexes, IntrinsicObjectIndexes,
            LAST_INTRINSIC_CONSTRUCTOR_INDEX, LAST_INTRINSIC_FUNCTION_INDEX,
            LAST_INTRINSIC_OBJECT_INDEX, LAST_WELL_KNOWN_SYMBOL_INDEX,
        },
    };
    fn panic_builtin_function_missing(index: usize) {
        let index = index as u32;
        let mut changed_index = index;
        if changed_index <= LAST_INTRINSIC_CONSTRUCTOR_INDEX as u32 {
            // Safety: Tested to be within limits.
            panic!(
                "Found a missing BuiltinFunction at constructor index {:?}",
                unsafe { core::mem::transmute::<u32, IntrinsicConstructorIndexes>(changed_index) }
            );
        }
        changed_index -= LAST_INTRINSIC_CONSTRUCTOR_INDEX as u32 + 1;
        if changed_index <= LAST_INTRINSIC_FUNCTION_INDEX as u32 {
            // Safety: Tested to be within limits.
            panic!(
                "Found a missing BuiltinFunction at function index {:?}",
                unsafe { core::mem::transmute::<u32, IntrinsicFunctionIndexes>(changed_index) }
            );
        }
        panic!("Found a missing BuiltinFunction at index {index:?}");
    }

    fn panic_blank_object(index: usize) {
        let index = index as u32;
        let mut changed_index = index;
        if changed_index <= LAST_INTRINSIC_OBJECT_INDEX as u32 {
            // Safety: Tested to be within limits.
            panic!("Found a blank Object at object index {:?}", unsafe {
                core::mem::transmute::<u32, IntrinsicObjectIndexes>(changed_index)
            });
        }
        changed_index -= LAST_INTRINSIC_OBJECT_INDEX as u32 + 1;
        if changed_index <= LAST_INTRINSIC_CONSTRUCTOR_INDEX as u32 {
            // Safety: Tested to be within limits.
            panic!(
                "Found a blank BuiltinFunction at constructor index {:?}",
                unsafe { core::mem::transmute::<u32, IntrinsicConstructorIndexes>(changed_index) }
            );
        }
        panic!("Found a blank object at index {index:?}");
    }

    #[test]
    fn test_default_realm_sanity() {
        use super::initialize_default_realm;
        use crate::ecmascript::{Agent, AgentOptions, DefaultHostHooks, ObjectRecord};

        let mut agent = Agent::new(AgentOptions::default(), &DefaultHostHooks);
        let (mut gc, mut scope) = unsafe { GcScope::create_root() };
        let gc = GcScope::new(&mut gc, &mut scope);
        initialize_default_realm(&mut agent, gc);
        assert_eq!(
            agent
                .current_realm_record()
                .intrinsics()
                .object_prototype()
                .get_index(),
            0
        );
        let object_constructor = agent.current_realm_record().intrinsics().object();
        assert_eq!(object_constructor.get_index(), 0);
        #[cfg(feature = "array-buffer")]
        assert!(agent.heap.array_buffers.is_empty());
        // Array prototype is itself an Array :/
        assert_eq!(agent.heap.arrays.len(), 1);
        assert!(agent.heap.bigints.is_empty());
        assert!(agent.heap.bound_functions.is_empty());
        let missing_builtin = agent
            .heap
            .builtin_functions
            .iter()
            .enumerate()
            .find(|(_, item)| *item == &BuiltinFunctionHeapData::BLANK);
        if let Some((missing_builtin_index, _)) = missing_builtin {
            panic_builtin_function_missing(missing_builtin_index);
        }
        #[cfg(feature = "date")]
        assert!(agent.heap.dates.is_empty());
        assert!(agent.heap.ecmascript_functions.is_empty());
        assert_eq!(agent.heap.environments.declarative.len(), 1);
        assert!(agent.heap.environments.function.is_empty());
        assert_eq!(agent.heap.environments.global.len(), 1);
        assert_eq!(agent.heap.environments.object.len(), 1);
        assert!(agent.heap.errors.is_empty());
        assert!(agent.heap.globals.borrow().is_empty());
        assert!(agent.heap.modules.is_empty());
        let blank_object = agent
            .heap
            .objects
            .iter()
            .enumerate()
            .find(|(_, item)| *item == &ObjectRecord::BLANK);
        if let Some((blank_object_index, _)) = blank_object {
            panic_blank_object(blank_object_index);
        }
        assert_eq!(agent.heap.realms.len(), 1);
        assert!(agent.heap.scripts.is_empty());
        assert_eq!(
            agent.heap.symbols.len() - 1,
            LAST_WELL_KNOWN_SYMBOL_INDEX as usize
        );
        #[cfg(feature = "regexp")]
        assert!(agent.heap.regexps.is_empty());
    }
}