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
// 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/.

use crate::{
    ecmascript::{
        Agent, BIGINT_DISCRIMINANT, BOOLEAN_DISCRIMINANT, BUILTIN_STRING_MEMORY, BigInt,
        FLOAT_DISCRIMINANT, HeapBigInt, HeapNumber, HeapString, INTEGER_DISCRIMINANT,
        InternalMethods, InternalSlots, JsResult, NUMBER_DISCRIMINANT, Number, Object,
        OrdinaryObject, Primitive, PropertyDescriptor, PropertyKey, ProtoIntrinsics,
        SMALL_BIGINT_DISCRIMINANT, SMALL_STRING_DISCRIMINANT, STRING_DISCRIMINANT,
        SYMBOL_DISCRIMINANT, SetResult, SmallBigInt, SmallF64, SmallInteger, String, Symbol,
        TryError, TryGetResult, TryHasResult, TryResult, Value, is_compatible_property_descriptor,
        js_result_into_try, object_handle, ordinary_define_own_property, ordinary_delete,
        ordinary_get, ordinary_get_own_property, ordinary_has_property, ordinary_set, unwrap_try,
    },
    engine::{Bindable, GcScope, NoGcScope, bindable_handle},
    heap::{
        ArenaAccess, ArenaAccessMut, BaseIndex, CompactionLists, CreateHeapData, Heap,
        HeapMarkAndSweep, HeapSweepWeakReference, IntrinsicPrimitiveObjectIndexes, WorkQueues,
        arena_vec_access,
    },
};
use small_string::SmallString;

use super::{
    ObjectShape, PropertyLookupCache, ordinary_own_property_keys, ordinary_try_get,
    ordinary_try_has_property, ordinary_try_set,
};

/// Primitive objects are special objects that hold a primitive value in their
/// internal data.
///
/// # Examples
///
/// ```javascript
/// new Number(0);
/// new String("");
/// new Boolean(true);
/// new Object(0);
/// new Object("");
/// new Object(true);
/// new Object(0n);
/// new Object(Symbol());
/// ```
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
pub struct PrimitiveObject<'a>(BaseIndex<'a, PrimitiveObjectRecord<'static>>);
object_handle!(PrimitiveObject);
arena_vec_access!(PrimitiveObject, 'a, PrimitiveObjectRecord, primitive_objects);

impl IntrinsicPrimitiveObjectIndexes {
    pub(crate) const fn get_primitive_object<'a>(
        self,
        base: BaseIndex<'a, PrimitiveObjectRecord<'static>>,
    ) -> PrimitiveObject<'a> {
        PrimitiveObject(BaseIndex::from_index_u32_const(
            self as u32 + base.get_index_u32_const() + Self::PRIMITIVE_OBJECT_INDEX_OFFSET,
        ))
    }
}

impl PrimitiveObject<'_> {
    /// Returns `true` if the primitive object is a BigInt object.
    ///
    /// ## Examples
    ///
    /// ```javascript
    /// new Object(0n);
    /// ```
    pub fn is_bigint_object(self, agent: &Agent) -> bool {
        matches!(
            self.get(agent).data,
            PrimitiveObjectData::BigInt(_) | PrimitiveObjectData::SmallBigInt(_)
        )
    }

    /// Returns `true` if the primitive object is a Number object.
    ///
    /// ## Examples
    ///
    /// ```javascript
    /// new Number(0);
    /// new Object(0);
    /// ```
    pub fn is_number_object(self, agent: &Agent) -> bool {
        matches!(
            self.get(agent).data,
            PrimitiveObjectData::SmallF64(_)
                | PrimitiveObjectData::Integer(_)
                | PrimitiveObjectData::Number(_)
        )
    }

    /// Returns `true` if the primitive object is a String object.
    ///
    /// ## Examples
    ///
    /// ```javascript
    /// new String("");
    /// new Object("");
    /// ```
    pub fn is_string_object(self, agent: &Agent) -> bool {
        matches!(
            self.get(agent).data,
            PrimitiveObjectData::String(_) | PrimitiveObjectData::SmallString(_)
        )
    }

    /// Returns `true` if the primitive object is a Symbol object.
    ///
    /// ## Examples
    ///
    /// ```javascript
    /// new Object(Symbol())
    /// ```
    pub fn is_symbol_object(self, agent: &Agent) -> bool {
        matches!(self.get(agent).data, PrimitiveObjectData::Symbol(_))
    }
}

impl<'a> InternalSlots<'a> for PrimitiveObject<'a> {
    #[inline(always)]
    fn get_backing_object(self, agent: &Agent) -> Option<OrdinaryObject<'static>> {
        self.get(agent).object_index.unbind()
    }

    fn set_backing_object(self, agent: &mut Agent, backing_object: OrdinaryObject<'static>) {
        assert!(
            self.get_mut(agent)
                .object_index
                .replace(backing_object.unbind())
                .is_none()
        );
    }

    fn object_shape(self, agent: &mut Agent) -> ObjectShape<'static> {
        if let Some(bo) = self.get_backing_object(agent) {
            bo.object_shape(agent)
        } else {
            let primitive: Primitive = self.get(agent).data.into();
            primitive.object_shape(agent).unwrap()
        }
    }

    fn internal_prototype(self, agent: &Agent) -> Option<Object<'static>> {
        match self.get_backing_object(agent) {
            Some(obj) => obj.internal_prototype(agent),
            None => {
                let intrinsic_default_proto = match self.get(agent).data {
                    PrimitiveObjectData::Boolean(_) => ProtoIntrinsics::Boolean,
                    PrimitiveObjectData::String(_) | PrimitiveObjectData::SmallString(_) => {
                        ProtoIntrinsics::String
                    }
                    PrimitiveObjectData::Symbol(_) => ProtoIntrinsics::Symbol,
                    PrimitiveObjectData::Number(_)
                    | PrimitiveObjectData::Integer(_)
                    | PrimitiveObjectData::SmallF64(_) => ProtoIntrinsics::Number,
                    PrimitiveObjectData::BigInt(_) | PrimitiveObjectData::SmallBigInt(_) => {
                        ProtoIntrinsics::BigInt
                    }
                };
                // TODO: Should take realm from "backing object"'s Realm/None
                // variant
                Some(
                    agent
                        .current_realm_record()
                        .intrinsics()
                        .get_intrinsic_default_proto(intrinsic_default_proto),
                )
            }
        }
    }
}

impl<'a> InternalMethods<'a> for PrimitiveObject<'a> {
    fn try_get_own_property<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        cache: Option<PropertyLookupCache>,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, Option<PropertyDescriptor<'gc>>> {
        let o = self.bind(gc);
        // For non-string primitive objects:
        // 1. Return OrdinaryGetOwnProperty(O, P).
        // For string exotic objects:
        // 1. Let desc be OrdinaryGetOwnProperty(S, P).
        // 2. If desc is not undefined, return desc.
        if let Some(backing_object) = o.get_backing_object(agent)
            && let Some(property_descriptor) =
                ordinary_get_own_property(agent, o.into(), backing_object, property_key, cache, gc)
        {
            return TryResult::Continue(Some(property_descriptor));
        }

        if let Ok(string) = String::try_from(self.get(agent).data) {
            // 3. Return StringGetOwnProperty(S, P).
            TryResult::Continue(string.get_property_descriptor(agent, property_key))
        } else {
            TryResult::Continue(None)
        }
    }

    fn try_define_own_property<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        property_descriptor: PropertyDescriptor,
        cache: Option<PropertyLookupCache>,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, bool> {
        if let Ok(string) = String::try_from(self.get(agent).data) {
            // For string exotic objects:
            // 1. Let stringDesc be StringGetOwnProperty(S, P).
            // 2. If stringDesc is not undefined, then
            if let Some(string_desc) = string.get_property_descriptor(agent, property_key) {
                // a. Let extensible be S.[[Extensible]].
                // b. Return IsCompatiblePropertyDescriptor(extensible, Desc, stringDesc).
                return match is_compatible_property_descriptor(
                    agent,
                    self.internal_extensible(agent),
                    property_descriptor,
                    Some(string_desc),
                    gc,
                ) {
                    Ok(b) => TryResult::Continue(b),
                    Err(_) => TryError::GcError.into(),
                };
            }
            // 3. Return ! OrdinaryDefineOwnProperty(S, P, Desc).
        }

        let backing_object = self
            .get_backing_object(agent)
            .unwrap_or_else(|| self.create_backing_object(agent));
        js_result_into_try(ordinary_define_own_property(
            agent,
            self.into(),
            backing_object,
            property_key,
            property_descriptor,
            cache,
            gc,
        ))
    }

    fn try_has_property<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        cache: Option<PropertyLookupCache>,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, TryHasResult<'gc>> {
        if let Ok(string) = String::try_from(self.get(agent).data)
            && string.get_property_value(agent, property_key).is_some()
        {
            return TryHasResult::Custom(0, self.bind(gc).into()).into();
        }

        // 1. Return ? OrdinaryHasProperty(O, P).
        ordinary_try_has_property(
            agent,
            self.into(),
            self.get_backing_object(agent),
            property_key,
            cache,
            gc,
        )
    }

    fn internal_has_property<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, bool> {
        let property_key = property_key.bind(gc.nogc());
        if let Ok(string) = String::try_from(self.get(agent).data)
            && string.get_property_value(agent, property_key).is_some()
        {
            return Ok(true);
        }

        // 1. Return ? OrdinaryHasProperty(O, P).
        match self.get_backing_object(agent) {
            Some(backing_object) => ordinary_has_property(
                agent,
                self.into(),
                backing_object,
                property_key.unbind(),
                gc,
            ),
            None => {
                // 3. Let parent be ? O.[[GetPrototypeOf]]().
                // Note: Primitive objects never call into JS from GetPrototypeOf.
                let parent = unwrap_try(self.try_get_prototype_of(agent, gc.nogc()));

                // 4. If parent is not null, then
                if let Some(parent) = parent {
                    // a. Return ? parent.[[HasProperty]](P).
                    parent
                        .unbind()
                        .internal_has_property(agent, property_key.unbind(), gc)
                } else {
                    // 5. Return false.
                    Ok(false)
                }
            }
        }
    }

    fn try_get<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        receiver: Value,
        cache: Option<PropertyLookupCache>,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, TryGetResult<'gc>> {
        if let Ok(string) = String::try_from(self.get(agent).data)
            && let Some(value) = string.get_property_value(agent, property_key)
        {
            return TryGetResult::Value(value.bind(gc)).into();
        }
        // 1. Return ? OrdinaryGet(O, P, Receiver).
        ordinary_try_get(
            agent,
            self.into(),
            self.get_backing_object(agent),
            property_key,
            receiver,
            cache,
            gc,
        )
    }

    fn internal_get<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        receiver: Value,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let property_key = property_key.bind(gc.nogc());
        if let Ok(string) = String::try_from(self.get(agent).data)
            && let Some(value) = string.get_property_value(agent, property_key)
        {
            return Ok(value.bind(gc.into_nogc()));
        }

        // 1. Return ? OrdinaryGet(O, P, Receiver).
        match self.get_backing_object(agent) {
            Some(backing_object) => {
                ordinary_get(agent, backing_object, property_key.unbind(), receiver, gc)
            }
            None => {
                // a. Let parent be ? O.[[GetPrototypeOf]]().
                let Some(parent) = unwrap_try(self.try_get_prototype_of(agent, gc.nogc())) else {
                    // b. If parent is null, return undefined.
                    return Ok(Value::Undefined);
                };

                // c. Return ? parent.[[Get]](P, Receiver).
                parent
                    .unbind()
                    .internal_get(agent, property_key.unbind(), receiver, gc)
            }
        }
    }

    fn try_set<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        value: Value,
        receiver: Value,
        cache: Option<PropertyLookupCache>,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, SetResult<'gc>> {
        if let Ok(string) = String::try_from(self.get(agent).data)
            && string.get_property_value(agent, property_key).is_some()
        {
            return SetResult::Unwritable.into();
        }

        // 1. Return ? OrdinarySet(O, P, V, Receiver).
        ordinary_try_set(agent, self, property_key, value, receiver, cache, gc)
    }

    fn internal_set<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        value: Value,
        receiver: Value,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, bool> {
        let property_key = property_key.bind(gc.nogc());
        if let Ok(string) = String::try_from(self.get(agent).data)
            && string.get_property_value(agent, property_key).is_some()
        {
            return Ok(false);
        }

        // 1. Return ? OrdinarySet(O, P, V, Receiver).
        ordinary_set(
            agent,
            self.into(),
            property_key.unbind(),
            value,
            receiver,
            gc,
        )
    }

    fn try_delete<'gc>(
        self,
        agent: &mut Agent,
        property_key: PropertyKey,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, bool> {
        if let Ok(string) = String::try_from(self.get(agent).data) {
            // A String will return unconfigurable descriptors for length and
            // all valid string indexes, making delete return false.
            if property_key == BUILTIN_STRING_MEMORY.length.into() {
                return TryResult::Continue(false);
            } else if let PropertyKey::Integer(index) = property_key {
                let index = index.into_i64();
                if index >= 0 && (index as usize) < string.utf16_len_(agent) {
                    return TryResult::Continue(false);
                }
            }
        }

        // 1. Return ! OrdinaryDelete(O, P).
        match self.get_backing_object(agent) {
            Some(backing_object) => TryResult::Continue(ordinary_delete(
                agent,
                self.into(),
                backing_object,
                property_key,
                gc,
            )),
            None => TryResult::Continue(true),
        }
    }

    fn try_own_property_keys<'gc>(
        self,
        agent: &mut Agent,
        gc: NoGcScope<'gc, '_>,
    ) -> TryResult<'gc, Vec<PropertyKey<'gc>>> {
        if let Ok(string) = String::try_from(self.get(agent).data) {
            let len = string.utf16_len_(agent);
            let mut keys = Vec::with_capacity(len + 1);

            // Insert keys for every index into the string.
            keys.extend(
                (0..len)
                    .map(|idx| PropertyKey::Integer(SmallInteger::try_from(idx as u64).unwrap())),
            );

            let backing_object_keys;
            let (integer_keys, other_keys) = match self.get_backing_object(agent) {
                Some(backing_object) => {
                    backing_object_keys = ordinary_own_property_keys(agent, backing_object, gc);
                    if let Some(PropertyKey::Integer(smi)) = backing_object_keys.first() {
                        debug_assert!(smi.into_i64() >= len as i64);
                    }
                    let split_idx = backing_object_keys
                        .iter()
                        .position(|pk| !pk.is_array_index());
                    if let Some(idx) = split_idx {
                        backing_object_keys.split_at(idx)
                    } else {
                        (&backing_object_keys[..], &[][..])
                    }
                }
                None => (&[][..], &[][..]),
            };

            // Insert the `length` key as the first non-array index key.
            keys.extend(integer_keys);
            keys.push(BUILTIN_STRING_MEMORY.length.into());
            keys.extend(other_keys);

            return TryResult::Continue(keys);
        }

        // 1. Return OrdinaryOwnPropertyKeys(O).
        match self.get_backing_object(agent) {
            Some(backing_object) => {
                TryResult::Continue(ordinary_own_property_keys(agent, backing_object, gc))
            }
            None => TryResult::Continue(vec![]),
        }
    }
}

#[derive(Debug, Clone, Copy, PartialEq)]
#[repr(u8)]
pub(crate) enum PrimitiveObjectData<'a> {
    Boolean(bool) = BOOLEAN_DISCRIMINANT,
    String(HeapString<'a>) = STRING_DISCRIMINANT,
    SmallString(SmallString) = SMALL_STRING_DISCRIMINANT,
    Symbol(Symbol<'a>) = SYMBOL_DISCRIMINANT,
    Number(HeapNumber<'a>) = NUMBER_DISCRIMINANT,
    Integer(SmallInteger) = INTEGER_DISCRIMINANT,
    SmallF64(SmallF64) = FLOAT_DISCRIMINANT,
    BigInt(HeapBigInt<'a>) = BIGINT_DISCRIMINANT,
    SmallBigInt(SmallBigInt) = SMALL_BIGINT_DISCRIMINANT,
}
bindable_handle!(PrimitiveObjectData);

impl<'a> TryFrom<PrimitiveObjectData<'a>> for BigInt<'a> {
    type Error = ();

    fn try_from(value: PrimitiveObjectData<'a>) -> Result<Self, Self::Error> {
        match value {
            PrimitiveObjectData::BigInt(data) => Ok(BigInt::BigInt(data)),
            PrimitiveObjectData::SmallBigInt(data) => Ok(BigInt::SmallBigInt(data)),
            _ => Err(()),
        }
    }
}

impl<'a> TryFrom<PrimitiveObjectData<'a>> for Number<'a> {
    type Error = ();

    fn try_from(value: PrimitiveObjectData<'a>) -> Result<Self, Self::Error> {
        match value {
            PrimitiveObjectData::Number(data) => Ok(Number::Number(data)),
            PrimitiveObjectData::Integer(data) => Ok(Number::Integer(data)),
            PrimitiveObjectData::SmallF64(data) => Ok(Number::SmallF64(data)),
            _ => Err(()),
        }
    }
}

impl<'a> TryFrom<PrimitiveObjectData<'a>> for String<'a> {
    type Error = ();

    fn try_from(value: PrimitiveObjectData<'a>) -> Result<Self, Self::Error> {
        match value {
            PrimitiveObjectData::String(data) => Ok(String::String(data)),
            PrimitiveObjectData::SmallString(data) => Ok(String::SmallString(data)),
            _ => Err(()),
        }
    }
}

impl<'a> TryFrom<PrimitiveObjectData<'a>> for Symbol<'a> {
    type Error = ();

    fn try_from(value: PrimitiveObjectData<'a>) -> Result<Self, Self::Error> {
        match value {
            PrimitiveObjectData::Symbol(data) => Ok(data),
            _ => Err(()),
        }
    }
}

impl<'a> From<PrimitiveObjectData<'a>> for Primitive<'a> {
    fn from(value: PrimitiveObjectData<'a>) -> Self {
        match value {
            PrimitiveObjectData::Boolean(d) => Self::Boolean(d),
            PrimitiveObjectData::String(d) => Self::String(d),
            PrimitiveObjectData::SmallString(d) => Self::SmallString(d),
            PrimitiveObjectData::Symbol(d) => Self::Symbol(d),
            PrimitiveObjectData::Number(d) => Self::Number(d),
            PrimitiveObjectData::Integer(d) => Self::Integer(d),
            PrimitiveObjectData::SmallF64(d) => Self::SmallF64(d),
            PrimitiveObjectData::BigInt(d) => Self::BigInt(d),
            PrimitiveObjectData::SmallBigInt(d) => Self::SmallBigInt(d),
        }
    }
}

#[derive(Debug, Clone, Copy)]
pub(crate) struct PrimitiveObjectRecord<'a> {
    pub(crate) object_index: Option<OrdinaryObject<'a>>,
    pub(crate) data: PrimitiveObjectData<'a>,
}
bindable_handle!(PrimitiveObjectRecord);

impl<'a> PrimitiveObjectRecord<'a> {
    pub(crate) const BLANK: Self = Self {
        object_index: None,
        data: PrimitiveObjectData::Boolean(false),
    };

    pub(crate) fn new_big_int_object(big_int: BigInt<'a>) -> Self {
        let data = match big_int {
            BigInt::BigInt(data) => PrimitiveObjectData::BigInt(data.unbind()),
            BigInt::SmallBigInt(data) => PrimitiveObjectData::SmallBigInt(data),
        };
        Self {
            object_index: None,
            data,
        }
    }

    pub(crate) fn new_boolean_object(boolean: bool) -> Self {
        Self {
            object_index: None,
            data: PrimitiveObjectData::Boolean(boolean),
        }
    }

    pub(crate) fn new_number_object(number: Number<'a>) -> Self {
        let data = match number {
            Number::Number(data) => PrimitiveObjectData::Number(data.unbind()),
            Number::Integer(data) => PrimitiveObjectData::Integer(data),
            Number::SmallF64(data) => PrimitiveObjectData::SmallF64(data),
        };
        Self {
            object_index: None,
            data,
        }
    }

    pub(crate) fn new_string_object(string: String<'a>) -> Self {
        let data = match string {
            String::String(data) => PrimitiveObjectData::String(data),
            String::SmallString(data) => PrimitiveObjectData::SmallString(data),
        };
        Self {
            object_index: None,
            data,
        }
    }

    pub(crate) fn new_symbol_object(symbol: Symbol<'a>) -> Self {
        Self {
            object_index: None,
            data: PrimitiveObjectData::Symbol(symbol.unbind()),
        }
    }
}

impl HeapMarkAndSweep for PrimitiveObjectRecord<'static> {
    fn mark_values(&self, queues: &mut WorkQueues) {
        let Self { object_index, data } = self;
        object_index.mark_values(queues);
        match data {
            PrimitiveObjectData::String(data) => data.mark_values(queues),
            PrimitiveObjectData::Symbol(data) => data.mark_values(queues),
            PrimitiveObjectData::Number(data) => data.mark_values(queues),
            PrimitiveObjectData::BigInt(data) => data.mark_values(queues),
            _ => {}
        }
    }

    fn sweep_values(&mut self, compactions: &CompactionLists) {
        let Self { object_index, data } = self;
        object_index.sweep_values(compactions);
        match data {
            PrimitiveObjectData::String(data) => data.sweep_values(compactions),
            PrimitiveObjectData::Symbol(data) => data.sweep_values(compactions),
            PrimitiveObjectData::Number(data) => data.sweep_values(compactions),
            PrimitiveObjectData::BigInt(data) => data.sweep_values(compactions),
            _ => {}
        }
    }
}

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

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

impl HeapSweepWeakReference for PrimitiveObject<'static> {
    fn sweep_weak_reference(self, compactions: &CompactionLists) -> Option<Self> {
        compactions
            .primitive_objects
            .shift_weak_index(self.0)
            .map(Self)
    }
}

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