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
// 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, ArgumentsList, BUILTIN_STRING_MEMORY, Behaviour, Builtin, ExceptionType,
        InternalMethods, JsResult, Object, PropertyDescriptor, Realm, String, Value,
        builders::OrdinaryObjectBuilder, call_function, construct, create_array_from_list,
        create_list_from_array_like, is_callable, is_constructor, to_property_key_complex,
        to_property_key_simple, try_result_into_js,
    },
    engine::{Bindable, GcScope, Scopable},
    heap::WellKnownSymbols,
};

pub(crate) struct ReflectObject;

struct ReflectObjectApply;
impl Builtin for ReflectObjectApply {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.apply;

    const LENGTH: u8 = 3;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::apply);
}

struct ReflectObjectConstruct;
impl Builtin for ReflectObjectConstruct {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.construct;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::construct);
}
struct ReflectObjectDefineProperty;
impl Builtin for ReflectObjectDefineProperty {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.defineProperty;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::define_property);
}
struct ReflectObjectDeleteProperty;
impl Builtin for ReflectObjectDeleteProperty {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.deleteProperty;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::delete_property);
}
struct ReflectObjectGet;
impl Builtin for ReflectObjectGet {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.get;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::get);
}
struct ReflectObjectGetOwnPropertyDescriptor;
impl Builtin for ReflectObjectGetOwnPropertyDescriptor {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.getOwnPropertyDescriptor;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::get_own_property_descriptor);
}
struct ReflectObjectGetPrototypeOf;
impl Builtin for ReflectObjectGetPrototypeOf {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.getPrototypeOf;

    const LENGTH: u8 = 1;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::get_prototype_of);
}

struct ReflectObjectHas;
impl Builtin for ReflectObjectHas {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.has;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::has);
}
struct ReflectObjectIsExtensible;
impl Builtin for ReflectObjectIsExtensible {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.isExtensible;

    const LENGTH: u8 = 1;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::is_extensible);
}
struct ReflectObjectOwnKeys;
impl Builtin for ReflectObjectOwnKeys {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.ownKeys;

    const LENGTH: u8 = 1;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::own_keys);
}
struct ReflectObjectPreventExtensions;
impl Builtin for ReflectObjectPreventExtensions {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.preventExtensions;

    const LENGTH: u8 = 1;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::prevent_extensions);
}
struct ReflectObjectSet;
impl Builtin for ReflectObjectSet {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.set;

    const LENGTH: u8 = 3;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::set);
}
struct ReflectObjectSetPrototypeOf;
impl Builtin for ReflectObjectSetPrototypeOf {
    const NAME: String<'static> = BUILTIN_STRING_MEMORY.setPrototypeOf;

    const LENGTH: u8 = 2;

    const BEHAVIOUR: Behaviour = Behaviour::Regular(ReflectObject::set_prototype_of);
}

impl ReflectObject {
    /// ### [28.1.1 Reflect.apply ( target, thisArgument, argumentsList )](https://tc39.es/ecma262/#sec-reflect.apply)
    fn apply<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let this_argument = arguments.get(1).bind(nogc);
        let arguments_list = arguments.get(2).bind(nogc);

        // 1. If IsCallable(target) is false, throw a TypeError exception.
        let Some(target) = is_callable(target, nogc) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not callable",
                gc.into_nogc(),
            ));
        };
        let target = target.scope(agent, nogc);
        let this_argument = this_argument.scope(agent, nogc);
        // 2. Let args be ? CreateListFromArrayLike(argumentsList).
        let args = create_list_from_array_like(agent, arguments_list.unbind(), gc.reborrow())
            .unbind()?
            .bind(gc.nogc());
        // TODO: 3. Perform PrepareForTailCall().
        // 4. Return ? Call(target, thisArgument, args)
        call_function(
            agent,
            target.get(agent),
            this_argument.get(agent),
            Some(ArgumentsList::from_mut_slice(&mut args.unbind())),
            gc,
        )
    }

    /// [28.1.2 Reflect.construct ( target, argumentsList \[ , newTarget \] )](https://tc39.es/ecma262/#sec-reflect.construct)
    fn construct<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let arguments_list = arguments.get(1).bind(nogc);

        // 1. If IsConstructor(target) is false, throw a TypeError exception.
        let Some(target) = is_constructor(agent, target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not a constructor",
                gc.into_nogc(),
            ));
        };

        // 2. If newTarget is not present, set newTarget to target.
        // 3. Else if IsConstructor(newTarget) is false, throw a TypeError exception.
        let new_target = if arguments.len() > 2 {
            let new_target = arguments.get(2).bind(nogc);
            let Some(new_target) = is_constructor(agent, new_target) else {
                return Err(agent.throw_exception_with_static_message(
                    ExceptionType::TypeError,
                    "Value is not a constructor",
                    gc.into_nogc(),
                ));
            };
            new_target
        } else {
            target
        };

        let target = target.scope(agent, nogc);
        let new_target = new_target.scope(agent, nogc);
        // 4. Let args be ? CreateListFromArrayLike(argumentsList).
        let args = create_list_from_array_like(agent, arguments_list.unbind(), gc.reborrow())
            .unbind()?
            .bind(gc.nogc());
        // 5. Return ? Construct(target, args, newTarget)
        construct(
            agent,
            target.get(agent),
            Some(ArgumentsList::from_mut_slice(&mut args.unbind())),
            Some(new_target.get(agent)),
            gc,
        )
        .map(|o| o.into())
    }

    /// ### [28.1.3 Reflect.defineProperty ( target, propertyKey, attributes )](https://tc39.es/ecma262/#sec-reflect.defineproperty)
    fn define_property<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);
        let mut attributes = arguments.get(2).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };
        let mut target = target.bind(nogc);

        let mut scoped_target = None;

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let mut key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            scoped_target = Some(target.scope(agent, nogc));
            let scoped_attributes = attributes.scope(agent, nogc);
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.as_ref().unwrap().get(agent);
            attributes = scoped_attributes.get(agent);
            key
        };

        // 3. Let desc be ? ToPropertyDescriptor(attributes).
        let desc = if let Some(desc) = try_result_into_js(
            PropertyDescriptor::try_to_property_descriptor(agent, attributes, gc.nogc()),
        )
        .unbind()?
        .bind(gc.nogc())
        {
            desc
        } else {
            if scoped_target.is_none() {
                scoped_target = Some(target.scope(agent, gc.nogc()));
            }
            let scoped_key = key.scope(agent, gc.nogc());
            let desc = PropertyDescriptor::to_property_descriptor(
                agent,
                attributes.unbind(),
                gc.reborrow(),
            )
            .unbind()?
            .bind(gc.nogc());
            key = scoped_key.get(agent).bind(gc.nogc());
            target = scoped_target.unwrap().get(agent).bind(gc.nogc());
            desc
        };
        // 4. Return ? target.[[DefineOwnProperty]](key, desc).
        let ret =
            target
                .unbind()
                .internal_define_own_property(agent, key.unbind(), desc.unbind(), gc)?;

        Ok(ret.into())
    }

    /// ### [28.1.4 Reflect.deleteProperty ( target, propertyKey )](https://tc39.es/ecma262/#sec-reflect.deleteproperty)
    fn delete_property<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(mut target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            let scoped_target = target.scope(agent, nogc);
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.get(agent);
            key
        };
        // 3. Return ? target.[[Delete]](key).
        let ret = target
            .unbind()
            .internal_delete(agent, key.unbind(), gc.reborrow())
            .unbind()?;

        Ok(ret.into())
    }

    /// [28.1.5 Reflect.get ( target, propertyKey \[ , receiver \] )](https://tc39.es/ecma262/#sec-reflect.get)
    fn get<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);
        let mut receiver = if arguments.len() > 2 {
            Some(arguments.get(2).bind(nogc))
        } else {
            None
        };

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };
        let mut target = target.bind(nogc);

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            let scoped_target = target.scope(agent, nogc);
            let scoped_receiver = receiver.map(|receiver| receiver.scope(agent, nogc));
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.get(agent).bind(gc.nogc());
            receiver = scoped_receiver.map(|scoped_receiver| scoped_receiver.get(agent));
            key
        };
        // 3. If receiver is not present, then
        //   a. Set receiver to target.
        let receiver = receiver.unwrap_or(target.into());
        // 4. Return ? target.[[Get]](key, receiver).
        target
            .unbind()
            .internal_get(agent, key.unbind(), receiver.unbind(), gc)
    }

    /// ### [28.1.6 Reflect.getOwnPropertyDescriptor ( target, propertyKey )](https://tc39.es/ecma262/#sec-reflect.getownpropertydescriptor)
    fn get_own_property_descriptor<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };
        let mut target = target.bind(nogc);

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            let scoped_target = target.scope(agent, nogc);
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.get(agent).bind(gc.nogc());
            key
        };
        // 3. Let desc be ? target.[[GetOwnProperty]](key).
        let desc = target
            .unbind()
            .internal_get_own_property(agent, key.unbind(), gc.reborrow())
            .unbind()?
            .bind(gc.nogc());
        // 4. Return FromPropertyDescriptor(desc).
        match PropertyDescriptor::from_property_descriptor(desc.unbind(), agent, gc.into_nogc()) {
            Some(ret) => Ok(ret.into()),
            None => Ok(Value::Undefined),
        }
    }

    /// ### [28.1.7 Reflect.getPrototypeOf ( target )](https://tc39.es/ecma262/#sec-reflect.getprototypeof)
    fn get_prototype_of<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Return ? target.[[GetPrototypeOf]]().
        match target.unbind().internal_get_prototype_of(agent, gc)? {
            Some(ret) => Ok(ret.into()),
            None => Ok(Value::Null),
        }
    }

    /// ### [28.1.8 Reflect.has ( target, propertyKey )](https://tc39.es/ecma262/#sec-reflect.has)
    fn has<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };
        let mut target = target.bind(nogc);

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            let scoped_target = target.scope(agent, nogc);
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.get(agent).bind(gc.nogc());
            key
        };
        // 3. Return ? target.[[HasProperty]](key).
        let ret = target
            .unbind()
            .internal_has_property(agent, key.unbind(), gc)?;
        Ok(ret.into())
    }

    /// ### [28.1.9 Reflect.isExtensible ( target )](https://tc39.es/ecma262/#sec-reflect.isextensible)
    fn is_extensible<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Return ? target.[[IsExtensible]]().
        let ret = target.unbind().internal_is_extensible(agent, gc)?;
        Ok(ret.into())
    }

    /// ### [28.1.10 Reflect.ownKeys ( target )](https://tc39.es/ecma262/#sec-reflect.ownkeys)
    fn own_keys<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Let keys be ? target.[[OwnPropertyKeys]]().
        let keys: Vec<Value> = target
            .unbind()
            .internal_own_property_keys(agent, gc.reborrow())
            .unbind()?
            .into_iter()
            .map(|key| key.convert_to_value(agent, gc.nogc()).into())
            .collect();
        // 3. Return CreateArrayFromList(keys).
        Ok(create_array_from_list(agent, &keys.unbind(), gc.into_nogc()).into())
    }

    /// ### [28.1.11 Reflect.preventExtensions ( target )](https://tc39.es/ecma262/#sec-reflect.preventextensions)
    fn prevent_extensions<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Return ? target.[[PreventExtensions]]().
        let ret = target.unbind().internal_prevent_extensions(agent, gc)?;
        Ok(ret.into())
    }

    /// [28.1.12 Reflect.set ( target, propertyKey, V \[ , receiver \] )](https://tc39.es/ecma262/#sec-reflect.set)
    fn set<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        mut gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let property_key = arguments.get(1).bind(nogc);
        let mut v = arguments.get(2).bind(nogc);
        let mut receiver = if arguments.len() > 3 {
            Some(arguments.get(3).bind(nogc))
        } else {
            None
        };

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(mut target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. Let key be ? ToPropertyKey(propertyKey).
        let key = if let Some(key) = to_property_key_simple(agent, property_key, nogc) {
            key
        } else {
            let scoped_target = target.scope(agent, nogc);
            let scoped_v = v.scope(agent, nogc);
            let scoped_receiver = receiver.map(|receiver| receiver.scope(agent, nogc));
            let key = to_property_key_complex(agent, property_key.unbind(), gc.reborrow())
                .unbind()?
                .bind(gc.nogc());
            target = scoped_target.get(agent);
            v = scoped_v.get(agent);
            receiver = scoped_receiver.map(|scoped_receiver| scoped_receiver.get(agent));
            key
        };

        // 3. If receiver is not present, then
        //   a. Set receiver to target.
        let receiver = receiver.unwrap_or(target.into());

        // 4. Return ? target.[[Set]](key, V, receiver).
        let ret =
            target
                .unbind()
                .internal_set(agent, key.unbind(), v.unbind(), receiver.unbind(), gc)?;
        Ok(ret.into())
    }

    /// ### [28.1.13 Reflect.setPrototypeOf ( target, proto )](https://tc39.es/ecma262/#sec-reflect.setprototypeof)
    fn set_prototype_of<'gc>(
        agent: &mut Agent,
        _this_value: Value,
        arguments: ArgumentsList,
        gc: GcScope<'gc, '_>,
    ) -> JsResult<'gc, Value<'gc>> {
        let nogc = gc.nogc();
        let target = arguments.get(0).bind(nogc);
        let proto = arguments.get(1).bind(nogc);

        // 1. If target is not an Object, throw a TypeError exception.
        let Ok(target) = Object::try_from(target) else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Value is not an object",
                gc.into_nogc(),
            ));
        };

        // 2. If proto is not an Object and proto is not null, throw a TypeError exception.
        let proto = if let Ok(proto) = Object::try_from(proto) {
            Some(proto)
        } else if proto.is_null() {
            None
        } else {
            return Err(agent.throw_exception_with_static_message(
                ExceptionType::TypeError,
                "Prototype must be an object or null",
                gc.into_nogc(),
            ));
        };

        // 3. Return ? target.[[SetPrototypeOf]](proto).
        let ret = target
            .unbind()
            .internal_set_prototype_of(agent, proto.unbind(), gc)?;
        Ok(ret.into())
    }

    pub(crate) fn create_intrinsic(agent: &mut Agent, realm: Realm<'static>) {
        let intrinsics = agent.get_realm_record_by_id(realm).intrinsics();
        let object_prototype = intrinsics.object_prototype();
        let this = intrinsics.reflect();

        OrdinaryObjectBuilder::new_intrinsic_object(agent, realm, this)
            .with_property_capacity(14)
            .with_prototype(object_prototype)
            .with_builtin_function_property::<ReflectObjectApply>()
            .with_builtin_function_property::<ReflectObjectConstruct>()
            .with_builtin_function_property::<ReflectObjectDefineProperty>()
            .with_builtin_function_property::<ReflectObjectDeleteProperty>()
            .with_builtin_function_property::<ReflectObjectGet>()
            .with_builtin_function_property::<ReflectObjectGetOwnPropertyDescriptor>()
            .with_builtin_function_property::<ReflectObjectGetPrototypeOf>()
            .with_builtin_function_property::<ReflectObjectHas>()
            .with_builtin_function_property::<ReflectObjectIsExtensible>()
            .with_builtin_function_property::<ReflectObjectOwnKeys>()
            .with_builtin_function_property::<ReflectObjectPreventExtensions>()
            .with_builtin_function_property::<ReflectObjectSet>()
            .with_builtin_function_property::<ReflectObjectSetPrototypeOf>()
            .with_property(|builder| {
                builder
                    .with_key(WellKnownSymbols::ToStringTag.into())
                    .with_value_readonly(BUILTIN_STRING_MEMORY.Reflect.into())
                    .with_enumerable(false)
                    .with_configurable(true)
                    .build()
            })
            .build();
    }
}