biujs 0.1.0-alpha.0

A lightweight, embeddable JavaScript engine with ES6 support
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
use crate::RuntimeError;
use crate::vm::property::PropertyDescriptor;
use crate::vm::object::{JSObject, new_array_object_from_vec};
use crate::vm::property::PropertyKey;
use crate::vm::value::Value;
use std::cell::RefCell;
use std::rc::Rc;

use super::set_static_method;

pub fn object_constructor(args: &[Value]) -> Result<Value, RuntimeError> {
    match args.first() {
        // `Object()` / `Object(null)` / `Object(undefined)`: an empty ordinary
        // object. The prototype comes from the wrapper registry (a plain
        // `Object()` call reaches this builtin without the VM's `New` prologue,
        // which would otherwise leave the object prototype-less).
        None => {
            let proto = super::wrapper_prototype("Object");
            let mut obj = crate::vm::object::OrdinaryObject::new();
            obj.set_prototype(proto);
            Ok(Value::Object(Rc::new(RefCell::new(obj))))
        }
        Some(v) if matches!(v, Value::Undefined | Value::Null) => {
            let proto = super::wrapper_prototype("Object");
            let mut obj = crate::vm::object::OrdinaryObject::new();
            obj.set_prototype(proto);
            Ok(Value::Object(Rc::new(RefCell::new(obj))))
        }
        // `Object(value)`: ToObject — primitives are boxed in wrapper objects.
        Some(v) => super::to_object(v),
    }
}

/// Own enumerable string keys of a value, applying `ToObject` first.
///
/// Shared by `Object.keys` / `values` / `entries`: a string primitive exposes
/// its index keys, other primitives have none, and null/undefined throw.
fn to_object_string_keys(value: &Value) -> Result<Vec<PropertyKey>, RuntimeError> {
    match value {
        Value::Object(obj_ref) => Ok(obj_ref
            .borrow()
            .own_keys()
            .into_iter()
            .filter(|k| k.as_str().is_some())
            .filter(|k| {
                obj_ref
                    .borrow()
                    .property_get(k)
                    .is_some_and(|d| d.enumerable)
            })
            .collect()),
        Value::String(s) => Ok((0..s.chars().count())
            .map(|i| PropertyKey::from_str(&i.to_string()))
            .collect()),
        Value::Number(_) | Value::Bool(_) | Value::Symbol(_) => Ok(Vec::new()),
        Value::Undefined | Value::Null => Err(RuntimeError::TypeError(
            "Cannot convert undefined or null to object".to_string(),
        )),
        Value::Function(_) => Ok(Vec::new()),
    }
}

/// Read `key` from a value for `Object.values` / `Object.entries`, honouring
/// string index access on primitives.
fn to_object_get(value: &Value, key: &PropertyKey) -> Value {
    match value {
        Value::Object(obj_ref) => obj_ref
            .borrow()
            .property_get(key)
            .map(|d| d.value)
            .unwrap_or(Value::Undefined),
        Value::String(s) => key
            .as_str()
            .and_then(|k| k.parse::<usize>().ok())
            .and_then(|i| s.chars().nth(i))
            .map(|c| Value::string(&c.to_string()))
            .unwrap_or(Value::Undefined),
        _ => Value::Undefined,
    }
}

pub fn object_keys(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.keys requires at least 1 argument".to_string(),
        ));
    }
    let keys = to_object_string_keys(&args[0])?;
    let str_keys: Vec<Value> = keys
        .iter()
        .filter_map(|k| k.as_str().map(Value::string))
        .collect();
    Ok(new_array_object_from_vec(str_keys))
}

pub fn object_values(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.values requires at least 1 argument".to_string(),
        ));
    }
    let keys = to_object_string_keys(&args[0])?;
    let values: Vec<Value> = keys.iter().map(|k| to_object_get(&args[0], k)).collect();
    Ok(new_array_object_from_vec(values))
}

pub fn object_entries(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.entries requires at least 1 argument".to_string(),
        ));
    }
    let keys = to_object_string_keys(&args[0])?;
    let entries: Vec<Value> = keys
        .iter()
        .map(|k| {
            new_array_object_from_vec(vec![
                Value::string(&k.display()),
                to_object_get(&args[0], k),
            ])
        })
        .collect();
    Ok(new_array_object_from_vec(entries))
}

pub fn object_define_property(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() < 3 {
        return Err(RuntimeError::TypeError(
            "Object.defineProperty requires at least 3 arguments".to_string(),
        ));
    }
    let obj = match &args[0] {
        Value::Object(obj_ref) => obj_ref,
        _ => {
            return Err(RuntimeError::TypeError(
                "Object.defineProperty: first argument must be an object".to_string(),
            ));
        }
    };
    // `ToPropertyKey`: strings pass through, symbols stay symbols, anything
    // else is stringified. Computed class members routinely use symbols.
    let key = match &args[1] {
        Value::String(s) => PropertyKey::from_str(s),
        Value::Symbol(sym) => PropertyKey::Symbol(sym.id),
        other => PropertyKey::from_str(&other.to_js_string()),
    };
    let desc = &args[2];

    // Honour the full descriptor (value/writable/enumerable/configurable and
    // get/set) instead of only copying the value. An *empty* descriptor is
    // valid: it creates the property with every attribute `false`.
    if let Value::Object(_) = desc {
        let desc_val = desc.clone();
        // Every object kind keeps its own property table, so the descriptor
        // survives on arrays, functions and prototype objects too — a class
        // constructor is a `FunctionObject`, and `static get x()` is defined
        // through exactly this path.
        let mut obj_mut = obj.borrow_mut();
        apply_property_descriptor(&mut *obj_mut, &key, &desc_val)?;
    } else {
        return Err(RuntimeError::TypeError(
            "Property description must be an object".to_string(),
        ));
    }

    Ok(Value::Object(Rc::clone(obj)))
}

pub fn object_get_own_property_descriptor(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() < 2 {
        return Err(RuntimeError::TypeError(
            "Object.getOwnPropertyDescriptor requires at least 2 arguments".to_string(),
        ));
    }
    // ES 19.1.2.6: ToObject(O) — string primitives expose their index keys
    // and `length`; only undefined/null raise TypeError.
    let obj_val = super::to_object(&args[0])?;
    let Value::Object(obj) = &obj_val else {
        return Err(RuntimeError::TypeError(
            "Object.getOwnPropertyDescriptor: first argument must be an object".to_string(),
        ));
    };
    let key = match &args[1] {
        Value::String(s) => PropertyKey::from_str(s),
        Value::Symbol(sym) => PropertyKey::Symbol(sym.id),
        other => PropertyKey::from_str(&other.to_js_string()),
    };

    let borrowed = obj.borrow();
    if let Some(desc) = borrowed.property_get(&key) {
        // Create a descriptor object
        let mut desc_obj = crate::vm::object::OrdinaryObject::new();
        if desc.is_accessor_descriptor() {
            // Accessor descriptor: report get/set, never value/writable.
            let undefined = Value::Undefined;
            desc_obj
                .property_set(
                    PropertyKey::from_str("get"),
                    desc.getter.clone().unwrap_or(undefined.clone()),
                )
                .map_err(|e| RuntimeError::TypeError(e))?;
            desc_obj
                .property_set(
                    PropertyKey::from_str("set"),
                    desc.setter.clone().unwrap_or(undefined),
                )
                .map_err(|e| RuntimeError::TypeError(e))?;
        } else {
            desc_obj
                .property_set(PropertyKey::from_str("value"), desc.value.clone())
                .map_err(|e| RuntimeError::TypeError(e))?;
            desc_obj
                .property_set(
                    PropertyKey::from_str("writable"),
                    Value::Bool(desc.writable),
                )
                .map_err(|e| RuntimeError::TypeError(e))?;
        }
        desc_obj
            .property_set(
                PropertyKey::from_str("enumerable"),
                Value::Bool(desc.enumerable),
            )
            .map_err(|e| RuntimeError::TypeError(e))?;
        desc_obj
            .property_set(
                PropertyKey::from_str("configurable"),
                Value::Bool(desc.configurable),
            )
            .map_err(|e| RuntimeError::TypeError(e))?;
        Ok(Value::Object(Rc::new(RefCell::new(desc_obj))))
    } else {
        Ok(Value::Undefined)
    }
}

pub fn object_get_own_property_names(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.getOwnPropertyNames requires at least 1 argument".to_string(),
        ));
    }
    // ES 19.1.2.7: ToObject(O) — string primitives report index keys and
    // `length`; undefined/null raise TypeError.
    let obj_val = super::to_object(&args[0])?;
    let Value::Object(obj_ref) = &obj_val else {
        return Ok(new_array_object_from_vec(vec![]));
    };
    let borrowed = obj_ref.borrow();
    // String-keyed own properties only (symbols are reported by
    // `Object.getOwnPropertySymbols`).
    let str_keys: Vec<Value> = borrowed
        .own_keys()
        .into_iter()
        .filter_map(|k| k.as_str().map(|s| Value::string(s)))
        .collect();
    Ok(new_array_object_from_vec(str_keys))
}

/// `Object.getOwnPropertySymbols(obj)` — own symbol-keyed properties, in
/// creation order.
pub fn object_get_own_property_symbols(args: &[Value]) -> Result<Value, RuntimeError> {
    let Some(Value::Object(obj_ref)) = args.first() else {
        return Err(RuntimeError::TypeError(
            "Object.getOwnPropertySymbols called on non-object".to_string(),
        ));
    };
    let symbols: Vec<Value> = obj_ref
        .borrow()
        .own_keys()
        .into_iter()
        .filter_map(|k| match k {
            PropertyKey::Symbol(id) => Some(crate::builtins::symbol_value_by_id(id)),
            _ => None,
        })
        .collect();
    Ok(new_array_object_from_vec(symbols))
}

pub fn object_get_prototype_of(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.getPrototypeOf requires at least 1 argument".to_string(),
        ));
    }
    match &args[0] {
        Value::Object(obj_ref) => {
            let borrowed = obj_ref.borrow();
            if let Some(proto) = borrowed.get_prototype() {
                Ok(Value::Object(Rc::clone(&proto)))
            } else {
                Ok(Value::Null)
            }
        }
        _ => Ok(Value::Null),
    }
}

pub fn object_set_prototype_of(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() < 2 {
        return Err(RuntimeError::TypeError(
            "Object.setPrototypeOf requires at least 2 arguments".to_string(),
        ));
    }
    let obj = match &args[0] {
        Value::Object(obj_ref) => obj_ref,
        _ => {
            return Err(RuntimeError::TypeError(
                "Object.setPrototypeOf: first argument must be an object".to_string(),
            ));
        }
    };
    let proto = match &args[1] {
        Value::Object(proto_ref) => Some(Rc::clone(proto_ref)),
        Value::Null => None,
        _ => {
            return Err(RuntimeError::TypeError(
                "Object.setPrototypeOf: prototype must be an object or null".to_string(),
            ));
        }
    };
    obj.borrow_mut().set_prototype(proto);
    Ok(Value::Object(Rc::clone(obj)))
}

pub fn object_create(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.is_empty() {
        return Err(RuntimeError::TypeError(
            "Object.create requires at least 1 argument".to_string(),
        ));
    }
    let proto = match &args[0] {
        Value::Object(proto_ref) => Some(Rc::clone(proto_ref)),
        Value::Null => None,
        _ => {
            return Err(RuntimeError::TypeError(
                "Object.create: prototype must be an object or null".to_string(),
            ));
        }
    };
    let mut new_obj = crate::vm::object::OrdinaryObject::new();
    new_obj.set_prototype(proto);

    // Second argument: property descriptors, each applied in order.
    // `undefined` means "none"; anything else must be an object (ES 19.1.2.2).
    if let Some(props) = args.get(1) {
        if !props.is_undefined() {
            let props_ref = match props {
                Value::Object(o) => Rc::clone(o),
                _ => {
                    return Err(RuntimeError::TypeError(
                        "Object.create: properties must be an object".to_string(),
                    ));
                }
            };
            let keys: Vec<PropertyKey> = props_ref
                .borrow()
                .own_keys()
                .into_iter()
                .filter(|k| {
                    props_ref
                        .borrow()
                        .property_get(k)
                        .is_some_and(|d| d.enumerable)
                })
                .collect();
            for key in keys {
                let desc = props_ref
                    .borrow()
                    .property_get(&key)
                    .map(|d| d.value)
                    .unwrap_or(Value::Undefined);
                apply_property_descriptor(&mut new_obj, &key, &desc)?;
            }
        }
    }

    Ok(Value::Object(Rc::new(RefCell::new(new_obj))))
}

/// Apply a property descriptor object (as consumed by `Object.defineProperty`
/// and the `properties` argument of `Object.create`) to any object kind.
///
/// Only the attributes the descriptor actually mentions are changed; the
/// others keep the value the existing property already has, and default to
/// `false` for a brand-new property — the ES `[[DefineOwnProperty]]` rule for
/// a partial descriptor.
pub fn apply_property_descriptor(
    obj: &mut dyn JSObject,
    key: &PropertyKey,
    desc: &Value,
) -> Result<bool, RuntimeError> {
    let desc_ref = match desc {
        Value::Object(o) => o,
        _ => {
            return Err(RuntimeError::TypeError(
                "Property description must be an object".to_string(),
            ));
        }
    };
    let read = |name: &str| -> Option<Value> {
        desc_ref
            .borrow()
            .property_get(&PropertyKey::from_str(name))
            .map(|d| d.value)
    };

    // Attributes the descriptor does not mention keep the value they already
    // have; for a brand-new property they default to `false` (ES `[[DefineOwnProperty]]`
    // with a partial descriptor).
    let existing = obj.property_get(key);
    let current = |pick: fn(&PropertyDescriptor) -> bool| -> bool {
        existing.as_ref().is_some_and(pick)
    };

    let getter = read("get");
    let setter = read("set");
    if getter.is_some() || setter.is_some() {
        // Accessor descriptor. Omitting `get`/`set` preserves the half that is
        // already installed, so `defineProperty(o, k, {set})` keeps an existing
        // getter instead of dropping it.
        let existing_is_accessor = existing
            .as_ref()
            .is_some_and(|d| d.getter.is_some() || d.setter.is_some());
        let mut current_get = if existing_is_accessor {
            existing.as_ref().and_then(|d| d.getter.clone())
        } else {
            None
        };
        let mut current_set = if existing_is_accessor {
            existing.as_ref().and_then(|d| d.setter.clone())
        } else {
            None
        };
        if let Some(v) = getter {
            current_get = Some(v);
        }
        if let Some(v) = setter {
            current_set = Some(v);
        }

        let mut descriptor = PropertyDescriptor::accessor_descriptor(current_get, current_set);
        descriptor.enumerable = read("enumerable")
            .map(|v| v.to_boolean())
            .unwrap_or_else(|| current(|d| d.enumerable));
        descriptor.configurable = read("configurable")
            .map(|v| v.to_boolean())
            .unwrap_or_else(|| current(|d| d.configurable));
        return obj
            .define_property(key.clone(), descriptor)
            .map_err(RuntimeError::TypeError);
    }

    let value = match read("value") {
        Some(v) => v,
        None => existing.as_ref().map(|d| d.value.clone()).unwrap_or(Value::Undefined),
    };
    let mut descriptor = PropertyDescriptor::data_descriptor(value);
    descriptor.writable = read("writable")
        .map(|v| v.to_boolean())
        .unwrap_or_else(|| current(|d| d.writable));
    descriptor.enumerable = read("enumerable")
        .map(|v| v.to_boolean())
        .unwrap_or_else(|| current(|d| d.enumerable));
    descriptor.configurable = read("configurable")
        .map(|v| v.to_boolean())
        .unwrap_or_else(|| current(|d| d.configurable));
    obj.define_property(key.clone(), descriptor)
        .map_err(RuntimeError::TypeError)
}

pub fn object_has_own(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() < 2 {
        return Err(RuntimeError::TypeError(
            "Object.hasOwn requires at least 2 arguments".to_string(),
        ));
    }
    // ES 20.1.2.13 step 2: `? ToObject(O)` — primitives do not answer `false`.
    let target = super::to_object(&args[0])?;
    match &target {
        Value::Object(obj_ref) => {
            let key = match &args[1] {
                Value::String(s) => PropertyKey::from_str(s),
                Value::Symbol(sym) => PropertyKey::Symbol(sym.id),
                _ => PropertyKey::from_str(&args[1].to_js_string()),
            };
            let borrowed = obj_ref.borrow();
            // Check if property exists directly on this object (not in prototype chain)
            // by checking if the property is in own_keys
            let own_keys = borrowed.own_keys();
            Ok(Value::Bool(own_keys.iter().any(|k| k == &key)))
        }
        _ => Ok(Value::Bool(false)),
    }
}

pub fn object_is(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() < 2 {
        return Err(RuntimeError::TypeError(
            "Object.is requires at least 2 arguments".to_string(),
        ));
    }
    // SameValue algorithm: NaN === NaN, +0 !== -0
    let result = match (&args[0], &args[1]) {
        (Value::Number(a), Value::Number(b)) => {
            if a.is_nan() && b.is_nan() {
                true
            } else if *a == 0.0 && *b == 0.0 {
                // Check signs: +0 !== -0
                a.is_sign_positive() == b.is_sign_positive()
            } else {
                a == b
            }
        }
        (a, b) => a == b,
    };
    Ok(Value::Bool(result))
}

/// `Object.assign(target, ...sources)` — copies own enumerable properties
/// (string and symbol keys). Values of *own accessor* properties cannot be
/// invoked at this layer, so the VM wraps the whole builtin (see
/// `VM::object_assign`) when getters need to run; this function is the
/// data-property fallback used for direct dispatch.
pub fn object_assign(args: &[Value]) -> Result<Value, RuntimeError> {
    let Some(target) = args.first() else {
        return Err(RuntimeError::TypeError(
            "Object.assign requires at least 1 argument".to_string(),
        ));
    };
    // ES 19.1.2.1: ToObject(target) — primitive targets are boxed.
    let target = super::to_object(target)?;
    for source in args.iter().skip(1) {
        if matches!(source, Value::Undefined | Value::Null) {
            continue;
        }
        let source = super::to_object(source)?;
        let Value::Object(source_ref) = &source else {
            continue;
        };
        let entries: Vec<(PropertyKey, Value)> = {
            let borrowed = source_ref.borrow();
            borrowed
                .own_keys()
                .into_iter()
                .filter_map(|k| {
                    borrowed
                        .property_get(&k)
                        .filter(|d| d.enumerable && d.is_data_descriptor())
                        .map(|d| (k, d.value))
                })
                .collect()
        };
        if let Value::Object(target_ref) = &target {
            for (key, value) in entries {
                let _ = target_ref.borrow_mut().property_set(key, value);
            }
        }
    }
    Ok(target)
}

/// `Object.defineProperties(obj, props)`
pub fn object_define_properties(args: &[Value]) -> Result<Value, RuntimeError> {
    let (Some(target), Some(props)) = (args.first(), args.get(1)) else {
        return Err(RuntimeError::TypeError(
            "Object.defineProperties requires 2 arguments".to_string(),
        ));
    };
    let Value::Object(target_ref) = target else {
        return Err(RuntimeError::TypeError(
            "Object.defineProperties: target is not an object".to_string(),
        ));
    };
    // `Properties` must be an object (ES 19.1.2.3 step 2); null/undefined and
    // primitives are a TypeError.
    let Value::Object(props_ref) = props else {
        return Err(RuntimeError::TypeError(
            "Object.defineProperties: properties must be an object".to_string(),
        ));
    };
    // Every descriptor is converted *before* any property is defined, so a bad
    // descriptor leaves the target untouched. Only own *enumerable* keys count.
    let descriptors: Vec<(PropertyKey, Value)> = {
        let borrowed = props_ref.borrow();
        borrowed
            .own_keys()
            .into_iter()
            .filter(|k| borrowed.property_get(k).is_some_and(|d| d.enumerable))
            .filter_map(|k| borrowed.property_get(&k).map(|d| (k, d.value)))
            .collect()
    };
    for (key, descriptor) in descriptors {
        let key_value = match &key {
            PropertyKey::Str(s) => Value::string(s),
            PropertyKey::Symbol(id) => crate::builtins::symbol_value_by_id(*id),
        };
        object_define_property(&[target.clone(), key_value, descriptor])?;
    }
    Ok(target.clone())
}

/// `Object.prototype.toString.call(value)` — the spec's [[Class]] based form.
pub fn object_prototype_to_string(obj: &Value, _args: &[Value]) -> Result<Value, RuntimeError> {
    let class = match obj {
        Value::Object(o) => o.borrow().class_name().to_string(),
        Value::Undefined => "Undefined".to_string(),
        Value::Null => "Null".to_string(),
        Value::Bool(_) => "Boolean".to_string(),
        Value::Number(_) => "Number".to_string(),
        Value::String(_) => "String".to_string(),
        Value::Symbol(_) => "Symbol".to_string(),
        Value::Function(_) => "Function".to_string(),
    };
    Ok(Value::string(&format!("[object {class}]")))
}

/// `Object.prototype.hasOwnProperty(V)`
pub fn object_has_own_property(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    // ES 20.1.3.3: `ToObject(this)` first, so `hasOwnProperty.call(null, "x")`
    // is a TypeError rather than `false`.
    super::require_object_coercible(obj)?;
    let key = match args.first() {
        Some(Value::String(s)) => PropertyKey::from_str(s),
        Some(Value::Symbol(sym)) => PropertyKey::Symbol(sym.id),
        Some(other) => PropertyKey::from_str(&other.to_js_string()),
        None => return Ok(Value::Bool(false)),
    };
    match obj {
        Value::Object(obj_ref) => Ok(Value::Bool(obj_ref.borrow().has_property(&key))),
        Value::String(s) => {
            // String wrappers expose an own `length` and their index keys.
            let own = match key.as_str() {
                Some("length") => true,
                Some(k) => k
                    .parse::<usize>()
                    .ok()
                    .is_some_and(|i| i < s.chars().count()),
                None => false,
            };
            Ok(Value::Bool(own))
        }
        _ => Ok(Value::Bool(false)),
    }
}

/// `Object.prototype.isPrototypeOf(V)` — is `this` anywhere on `V`'s
/// prototype chain?
pub fn object_is_prototype_of(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    // ES 20.1.3.5: `ToObject(this)`, then walk V's chain — `null`/`undefined`
    // receivers raise a TypeError instead of answering `false`.
    super::require_object_coercible(obj)?;
    let Value::Object(target) = obj else {
        // A primitive receiver has no object identity, so no object can have it
        // on its prototype chain.
        return Ok(Value::Bool(false));
    };
    let Some(Value::Object(candidate)) = args.first() else {
        return Ok(Value::Bool(false));
    };
    let mut current = candidate.borrow().get_prototype();
    let mut depth = 0;
    while let Some(proto) = current {
        if depth > 1000 {
            break;
        }
        if Rc::ptr_eq(&proto, target) {
            return Ok(Value::Bool(true));
        }
        current = proto.borrow().get_prototype();
        depth += 1;
    }
    Ok(Value::Bool(false))
}

/// `Object.prototype.propertyIsEnumerable(V)`.
pub fn object_property_is_enumerable(
    obj: &Value,
    args: &[Value],
) -> Result<Value, RuntimeError> {
    // ES 20.1.3.4: `ToObject(this)` first.
    super::require_object_coercible(obj)?;
    let key = match args.first() {
        Some(Value::String(s)) => PropertyKey::from_str(s),
        Some(Value::Symbol(sym)) => PropertyKey::Symbol(sym.id),
        Some(other) => PropertyKey::from_str(&other.to_js_string()),
        None => return Ok(Value::Bool(false)),
    };
    match obj {
        Value::Object(obj_ref) => Ok(Value::Bool(
            obj_ref
                .borrow()
                .property_get(&key)
                .is_some_and(|d| d.enumerable),
        )),
        _ => Ok(Value::Bool(false)),
    }
}

/// Name of the `Object.prototype.toString` native.
///
/// It needs a dedicated name because `Object.prototype.toString.call(x)` must
/// report the [[Class]] of `x` even when `x` is an Array or Number, whereas the
/// generic `toString` dispatch delegates to the receiver's own type.
pub const OBJECT_TO_STRING_NATIVE: &str = "Object.prototype.toString";

/// Populate `Object.prototype` with the methods every object inherits.
pub fn register_object_prototype(proto: &Rc<RefCell<dyn JSObject>>, object_fn: &Value) {
    use super::set_prototype_method;

    // Dedicated native so that `.call(receiver)` keeps Array/Number semantics.
    let _ = proto.borrow_mut().define_property(
        PropertyKey::from_str("toString"),
        super::method_descriptor(Value::Object(Rc::new(RefCell::new(
            crate::vm::object::NativeFunctionObject::new(OBJECT_TO_STRING_NATIVE),
        )))),
    );
    set_prototype_method(proto, "valueOf", |this, _args| Ok(this.clone()));
    set_prototype_method(proto, "hasOwnProperty", |this, args| {
        object_has_own_property(this, args)
    });
    set_prototype_method(proto, "isPrototypeOf", |this, args| {
        object_is_prototype_of(this, args)
    });
    set_prototype_method(proto, "propertyIsEnumerable", |this, args| {
        object_property_is_enumerable(this, args)
    });
    set_prototype_method(proto, "toLocaleString", |this, args| {
        object_prototype_to_string(this, args)
    });
    let _ = proto
        .borrow_mut()
        .define_property(
            PropertyKey::from_str("constructor"),
            super::method_descriptor(object_fn.clone()),
        );
}

pub fn register_object_statics(object_fn: &Value, _builtins: &super::Builtins) {
    set_static_method(object_fn, "keys", |args| object_keys(args));
    set_static_method(object_fn, "values", |args| object_values(args));
    set_static_method(object_fn, "entries", |args| object_entries(args));
    set_static_method(object_fn, "assign", |args| object_assign(args));
    set_static_method(object_fn, "defineProperty", |args| {
        object_define_property(args)
    });
    set_static_method(object_fn, "defineProperties", |args| {
        object_define_properties(args)
    });
    set_static_method(object_fn, "getOwnPropertyDescriptor", |args| {
        object_get_own_property_descriptor(args)
    });
    set_static_method(object_fn, "getOwnPropertyNames", |args| {
        object_get_own_property_names(args)
    });
    set_static_method(object_fn, "getOwnPropertySymbols", |args| {
        object_get_own_property_symbols(args)
    });
    set_static_method(object_fn, "getPrototypeOf", |args| {
        object_get_prototype_of(args)
    });
    set_static_method(object_fn, "setPrototypeOf", |args| {
        object_set_prototype_of(args)
    });
    set_static_method(object_fn, "create", |args| object_create(args));
    set_static_method(object_fn, "hasOwn", |args| object_has_own(args));
    set_static_method(object_fn, "is", |args| object_is(args));
    set_static_method(object_fn, "isExtensible", |args| Ok(Value::Bool(false)));
    set_static_method(object_fn, "isFrozen", |args| Ok(Value::Bool(false)));
    set_static_method(object_fn, "isSealed", |args| Ok(Value::Bool(false)));
    set_static_method(object_fn, "preventExtensions", |args| {
        Ok(args.first().cloned().unwrap_or(Value::Undefined))
    });
    set_static_method(object_fn, "seal", |args| {
        Ok(args.first().cloned().unwrap_or(Value::Undefined))
    });
    set_static_method(object_fn, "freeze", |args| {
        Ok(args.first().cloned().unwrap_or(Value::Undefined))
    });
}