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
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
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
use std::cell::RefCell;
use std::rc::Rc;

use crate::RuntimeError;
use crate::vm::object::{ArrayObject, JSObject};
use crate::vm::property::PropertyKey;
use crate::vm::value::Value;

// ─────────────────────────────────────────────────────────
// Prototype registration
// ─────────────────────────────────────────────────────────

pub fn register_array_prototype(proto: &Rc<RefCell<dyn JSObject>>) {
    use super::{mark_prototype_method, set_prototype_method};

    set_prototype_method(proto, "push", |this, args| array_push(this, args));
    set_prototype_method(proto, "pop", |this, _args| array_pop(this));
    set_prototype_method(proto, "shift", |this, _args| array_shift(this));
    set_prototype_method(proto, "unshift", |this, args| array_unshift(this, args));
    set_prototype_method(proto, "indexOf", |this, args| array_index_of(this, args));
    set_prototype_method(proto, "includes", |this, args| array_includes(this, args));
    set_prototype_method(proto, "join", |this, args| array_join(this, args));
    set_prototype_method(proto, "slice", |this, args| array_slice(this, args));
    set_prototype_method(proto, "concat", |this, args| array_concat(this, args));
    set_prototype_method(proto, "splice", |this, args| array_splice(this, args));
    set_prototype_method(proto, "reverse", |this, _args| array_reverse(this));
    set_prototype_method(proto, "lastIndexOf", |this, args| array_last_index_of(this, args));
    set_prototype_method(proto, "fill", |this, args| array_fill(this, args));
    set_prototype_method(proto, "toString", |this, _args| array_to_string(this));
    set_prototype_method(proto, "at", |this, args| array_at(this, args));
    set_prototype_method(proto, "copyWithin", |this, args| {
        array_copy_within(this, args)
    });

    // Callback-driven methods: the VM runs the algorithm so it can call the
    // user-supplied function for each element.
    for name in [
        "map",
        "filter",
        "forEach",
        "some",
        "every",
        "reduce",
        "reduceRight",
        "find",
        "findIndex",
        "sort",
    ] {
        mark_prototype_method(proto, name);
    }

    // `entries` / `keys` / `values` return iterators, which only the VM can
    // build: they are dispatched by `call_native_by_name` through the iterator
    // registry.
    for name in ["entries", "keys", "values"] {
        mark_prototype_method(proto, name);
    }
}

/// `Array.from(arrayLike)` — copies array-like values / iterables we support.
///
/// A `mapFn` would have to call back into JavaScript, which the builtin layer
/// cannot do; such calls are handled by the VM before reaching here.
pub fn array_from(args: &[Value]) -> Result<Value, RuntimeError> {
    let Some(source) = args.first() else {
        return Err(RuntimeError::TypeError(
            "Array.from requires an array-like object".to_string(),
        ));
    };
    let mut out: Vec<Value> = Vec::new();
    match source {
        Value::Object(obj_ref) => {
            let borrowed = obj_ref.borrow();
            if let Some(arr) = borrowed.as_any().downcast_ref::<ArrayObject>() {
                for i in 0..arr.len() {
                    out.push(arr.get(i).cloned().unwrap_or(Value::Undefined));
                }
            } else {
                // Generic array-like: read `length` and then each index.
                let len = borrowed
                    .property_get(&PropertyKey::from_str("length"))
                    .map(|d| d.value.to_number())
                    .unwrap_or(0.0);
                let len = len.max(0.0).min((1u32 << 20) as f64) as usize;
                for i in 0..len {
                    let key = PropertyKey::from_str(&i.to_string());
                    out.push(
                        borrowed
                            .property_get(&key)
                            .map(|d| d.value)
                            .unwrap_or(Value::Undefined),
                    );
                }
            }
        }
        Value::String(s) => {
            out.extend(s.chars().map(|c| Value::string(&c.to_string())));
        }
        _ => {
            return Err(RuntimeError::TypeError(format!(
                "Array.from: {} is not iterable",
                source.type_of()
            )));
        }
    }
    Ok(Value::Object(Rc::new(RefCell::new(ArrayObject::from_vec(
        out,
    )))))
}

/// Register `Array` constructor statics.
pub fn register_array_statics(array_fn: &Value) {
    use super::set_static_method;

    set_static_method(array_fn, "isArray", |args| {
        Ok(Value::Bool(matches!(
            args.first(),
            Some(Value::Object(o)) if o.borrow().kind() == crate::vm::ObjectKind::Array
        )))
    });
    set_static_method(array_fn, "of", |args| array_constructor(args));
    set_static_method(array_fn, "from", |args| array_from(args));
    register_species_accessor(array_fn);
}

/// Native name of the `get Array[Symbol.species]` accessor.
pub const ARRAY_SPECIES_NATIVE: &str = "__array_species__";

/// ES 22.1.2.5: `Array[Symbol.species]` is an accessor whose getter returns
/// its receiver (`get [Symbol.species]() { return this; }`).
///
/// It is what makes `ArraySpeciesCreate` (VM side) able to tell "no override"
/// from "inherited default": without it every subclass instance would look like
/// it had no species and would silently get an `Array` back.
fn register_species_accessor(array_fn: &Value) {
    let Value::Object(obj) = array_fn else {
        return;
    };
    let getter = Value::Object(Rc::new(RefCell::new(
        crate::vm::object::NativeFunctionObject::new(ARRAY_SPECIES_NATIVE),
    )));
    let desc = crate::vm::property::PropertyDescriptor {
        value: Value::Undefined,
        writable: false,
        enumerable: false,
        configurable: true,
        getter: Some(getter),
        setter: None,
    };
    let _ = obj
        .borrow_mut()
        .define_property(super::species_symbol_key(), desc);
}

/// `Array.prototype.toString()`
pub fn array_to_string(obj: &Value) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let arr = obj_ref.borrow();
        if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
            return Ok(Value::string(&array_obj.join_elements()));
        }
    }
    Ok(Value::string("[object Object]"))
}

// ─────────────────────────────────────────────────────────
// Array constructor
// ─────────────────────────────────────────────────────────

/// Largest length `Array(len)` will actually materialize. Anything above this
/// raises `RangeError` instead of trying to allocate gigabytes up front.
const MAX_MATERIALIZED_LEN: usize = 1 << 20;

pub fn array_constructor(args: &[Value]) -> Result<Value, RuntimeError> {
    if args.len() == 1 {
        if let Value::Number(n) = args[0] {
            // ES `Array(len)`: `intLen = ToUint32(len)` and `len` must equal it.
            // This rejects NaN, ±Infinity, negatives, non-integers and anything
            // >= 2**32 (previously `new Array(2**32)` tried to allocate 96 GB).
            let int_len = to_uint32(n);
            if n != int_len as f64 {
                return Err(RuntimeError::RangeError("Invalid array length".to_string()));
            }
            let len = int_len as usize;
            if len > MAX_MATERIALIZED_LEN {
                return Err(RuntimeError::RangeError(
                    "Invalid array length".to_string(),
                ));
            }
            let mut arr = ArrayObject::with_capacity(len);
            for _ in 0..len {
                arr.push(Value::Undefined);
            }
            return Ok(Value::Object(Rc::new(RefCell::new(arr))));
        }
    }
    let mut arr = ArrayObject::new();
    for arg in args {
        arr.push(arg.clone());
    }
    Ok(Value::Object(Rc::new(RefCell::new(arr))))
}

/// Validate an array `length` assignment, returning the materialized length or
/// `Err` when the spec requires a `RangeError`.
pub fn validate_array_length(n: f64) -> Result<usize, RuntimeError> {
    let int_len = to_uint32(n) as usize;
    if n != int_len as f64 || int_len > MAX_MATERIALIZED_LEN {
        return Err(RuntimeError::RangeError(
            "Invalid array length".to_string(),
        ));
    }
    Ok(int_len)
}

/// ES `ToUint32` (modulo 2**32, saturating NaN to 0).
fn to_uint32(n: f64) -> u32 {
    if n.is_nan() || n.is_infinite() || n == 0.0 {
        return 0;
    }
    let truncated = n.trunc();
    let rem = truncated % 4_294_967_296.0; // 2**32
    (rem as i64 % 4_294_967_296i64) as u32
}

// ─────────────────────────────────────────────────────────
// Generic (array-like) helpers
// ─────────────────────────────────────────────────────────
//
// The spec defines every Array.prototype method in terms of `length`,
// `HasProperty` and index `[[Get]]`/`[[Set]]`/`[[Delete]]`, so each method below
// has a fast path for real arrays and a generic fallback. Two properties of the
// fallback matter for the conformance suite:
//
// * It must not walk `length` indices one by one when the receiver is sparse
//   with a `length` near 2^53 (`slice`/`splice`/`reverse` all have such cases),
//   hence `own_index_keys` / `generic_window_move`.
// * It cannot run accessors: `[[Get]]` here reads the property table directly,
//   so an index whose value is defined by a getter is seen as `undefined`.

/// Largest valid array `length` (2^53 - 1).
const MAX_ARRAY_LENGTH: u64 = (1u64 << 53) - 1;

/// Upper bound on how many elements a *generic* (array-like) path will
/// materialize or touch one by one.
///
/// A receiver may legally claim `length = 2^53 - 1` — test262 exercises several
/// such cases — but an operation whose result is built element by element
/// cannot honour that: this engine's arrays are `Vec`-backed and top out at
/// `MAX_MATERIALIZED_LEN` entries. Those paths therefore raise `RangeError`
/// instead of attempting a gigantic allocation (the sparse-aware paths —
/// `shift`/`unshift`/`reverse`/`splice`'s window move — are unaffected, they
/// only touch indices that exist).
const MAX_GENERIC_ELEMENTS: u64 = 1 << 22;

/// Upper bound for the string `join` is allowed to build (see `array_join`).
const MAX_JOIN_LENGTH: usize = 1 << 20;

/// `ToObject(this)` for the generic Array.prototype paths.
fn to_object(obj: &Value) -> Result<Rc<RefCell<dyn JSObject>>, RuntimeError> {
    match super::to_object(obj) {
        Ok(Value::Object(o)) => Ok(o),
        Ok(_) => Err(RuntimeError::TypeError(
            "Array.prototype method called on a non-object".to_string(),
        )),
        Err(err) => Err(err),
    }
}

/// `ToLength(Get(O, "length"))` — saturating, so a huge `length` cannot make
/// the caller allocate or loop forever.
fn generic_length(obj: &Value) -> u64 {
    let raw = match obj {
        Value::Object(o) => o
            .borrow()
            .property_get(&PropertyKey::from_str("length"))
            .map(|d| d.value.to_number()),
        _ => None,
    };
    let n = raw.unwrap_or(f64::NAN);
    if n.is_nan() || n <= 0.0 {
        0
    } else if n.is_infinite() {
        MAX_ARRAY_LENGTH
    } else {
        n.trunc().min(MAX_ARRAY_LENGTH as f64) as u64
    }
}

fn generic_get(obj: &Value, index: u64) -> Value {
    match obj {
        Value::Object(o) => o
            .borrow()
            .property_get(&PropertyKey::from_str(&index.to_string()))
            .map(|d| d.value)
            .unwrap_or(Value::Undefined),
        _ => Value::Undefined,
    }
}

fn generic_set(obj: &Value, index: u64, value: Value) -> Result<(), RuntimeError> {
    match obj {
        Value::Object(o) => o
            .borrow_mut()
            .property_set(PropertyKey::from_str(&index.to_string()), value)
            .map(|_| ())
            .map_err(RuntimeError::TypeError),
        _ => Ok(()),
    }
}

/// `HasProperty(O, key)` — walks the prototype chain, as the spec requires.
fn generic_has(obj: &Value, index: u64) -> bool {
    match obj {
        Value::Object(o) => crate::vm::prototype::internal_has_property(
            Rc::clone(o),
            &PropertyKey::from_str(&index.to_string()),
        )
        .unwrap_or(false),
        _ => false,
    }
}

/// `[[Delete]](O, key)`.
fn generic_delete(obj: &Value, index: u64) -> Result<bool, RuntimeError> {
    match obj {
        Value::Object(o) => crate::vm::prototype::internal_delete(
            Rc::clone(o),
            &PropertyKey::from_str(&index.to_string()),
        )
        .map_err(RuntimeError::TypeError),
        _ => Ok(true),
    }
}

/// `Set(O, "length", len, true)`.
fn generic_set_length(obj: &Value, len: u64) -> Result<(), RuntimeError> {
    match obj {
        Value::Object(o) => o
            .borrow_mut()
            .property_set(PropertyKey::from_str("length"), Value::Number(len as f64))
            .map(|_| ())
            .map_err(RuntimeError::TypeError),
        _ => Ok(()),
    }
}

/// Own index keys of `obj` below `limit`, ascending.
fn own_index_keys_below(obj: &Value, limit: u64) -> Vec<u64> {
    let Value::Object(o) = obj else {
        return Vec::new();
    };
    let keys = o.borrow().own_keys();
    let mut indices: Vec<u64> = keys
        .iter()
        .filter_map(|key| key.as_str().and_then(|s| s.parse::<u64>().ok()))
        .filter(|index| *index < limit)
        .collect();
    indices.sort_unstable();
    indices.dedup();
    indices
}

/// Move the window `[from, to)` by `delta` indices (`delta > 0` moves towards
/// higher indices), touching only the indices that exist.
///
/// Mirrors "for each k: if HasProperty(from + k) then Set(to + k, Get(...))
/// else Delete(to + k)" without paying O(length) on a sparse receiver: the
/// destinations that do not receive a value are deleted, the rest are written
/// from a snapshot taken before anything is modified.
fn generic_window_move(obj: &Value, from: u64, to: u64, delta: i64, len: u64) -> Result<(), RuntimeError> {
    if to <= from || delta == 0 {
        return Ok(());
    }
    let present = own_index_keys_below(obj, len);
    let shift = |k: u64| -> u64 {
        if delta > 0 {
            k + delta as u64
        } else {
            k - (-delta) as u64
        }
    };

    let mut moves: Vec<(u64, Value)> = Vec::new();
    for k in &present {
        if *k >= from && *k < to {
            let value = generic_get(obj, *k);
            moves.push((shift(*k), value));
        }
    }
    let destinations: std::collections::HashSet<u64> = moves.iter().map(|(d, _)| *d).collect();

    let (dest_from, dest_to) = (shift(from), shift(to - 1) + 1);
    for k in &present {
        if *k >= dest_from && *k < dest_to && !destinations.contains(k) {
            generic_delete(obj, *k)?;
        }
    }
    for (dest, value) in moves {
        generic_set(obj, dest, value)?;
    }
    Ok(())
}

/// Delete every *present* index in `[from, to)`.
fn generic_delete_range(obj: &Value, from: u64, to: u64, len: u64) -> Result<(), RuntimeError> {
    if to <= from {
        return Ok(());
    }
    for k in own_index_keys_below(obj, len) {
        if k >= from && k < to {
            generic_delete(obj, k)?;
        }
    }
    Ok(())
}

/// `ToIntegerOrInfinity(arg)` resolved against a length: negatives count from
/// the end and the result is clamped into `[0, len]` (ES 23.1.3.3 steps 5-9).
///
/// `ToNumber`'s abrupt completion is propagated, so a `Symbol` argument raises
/// a TypeError instead of silently reading as `0`.
fn relative_index(arg: Option<&Value>, len: u64) -> Result<u64, RuntimeError> {
    let n = super::to_integer_or_infinity_throwing(arg)?;
    let len_i = len.min(i64::MAX as u64) as i64;
    let index = if n < 0 { n.saturating_add(len_i) } else { n };
    Ok(index.clamp(0, len_i) as u64)
}

/// `ToNumber(arg)` clamped into `[0, len]` with negatives counting from the end —
/// the bound shared by `slice` and the `start` of `splice`/`indexOf`.
fn clamped_index(arg: Option<&Value>, len: usize) -> Result<usize, RuntimeError> {
    let n = arg.map_or(Ok(f64::NAN), super::to_number_throwing)?;
    if n.is_nan() {
        return Ok(0);
    }
    let n = if n.is_infinite() {
        if n.is_sign_positive() {
            len as i64
        } else {
            0
        }
    } else {
        n.trunc() as i64
    };
    Ok(if n < 0 {
        ((len as i64) + n).max(0) as usize
    } else {
        (n as usize).min(len)
    })
}

/// `IsConcatSpreadable(value)` (ES 23.1.3.1.1): an Array is spread unless
/// `Symbol.isConcatSpreadable` says otherwise; a non-Array only when that
/// property is truthy.
fn is_concat_spreadable(value: &Value) -> bool {
    let Value::Object(obj_ref) = value else {
        return false;
    };
    let is_array = obj_ref.borrow().kind() == crate::vm::ObjectKind::Array;
    let flag = crate::vm::prototype::find_descriptor(
        Rc::clone(obj_ref),
        &super::is_concat_spreadable_symbol_key(),
    )
    .ok()
    .flatten()
    .map(|(_, desc)| desc.value);
    match flag {
        Some(Value::Undefined) | None => is_array,
        Some(v) => v.to_boolean(),
    }
}

// ─────────────────────────────────────────────────────────
// Prototype methods
// ─────────────────────────────────────────────────────────

/// `Array.prototype.at(index)` — negative indices count from the end
/// (ES 23.1.3.1).
pub fn array_at(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        if let Some(arr) = obj_ref.borrow().as_any().downcast_ref::<ArrayObject>() {
            let len = arr.len() as i64;
            let n = super::to_integer_or_infinity_throwing(args.first())?;
            let index = if n < 0 { n + len } else { n };
            if index < 0 || index >= len {
                return Ok(Value::Undefined);
            }
            return Ok(arr.get(index as usize).cloned().unwrap_or(Value::Undefined));
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    let n = super::to_integer_or_infinity_throwing(args.first())?;
    let len_i = len.min(i64::MAX as u64) as i64;
    let index = if n < 0 { n.saturating_add(len_i) } else { n };
    if index < 0 || index >= len_i {
        return Ok(Value::Undefined);
    }
    Ok(generic_get(obj, index as u64))
}

/// `Array.prototype.copyWithin(target, start, end)`.
///
/// The range is read into a buffer first, which makes overlapping source and
/// destination ranges behave as the spec requires (the algorithm is defined in
/// terms of a snapshot of the values, not of the live elements).
pub fn array_copy_within(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    let len = if let Value::Object(obj_ref) = obj {
        if let Some(arr) = obj_ref.borrow().as_any().downcast_ref::<ArrayObject>() {
            arr.len() as u64
        } else {
            generic_length(obj)
        }
    } else {
        generic_length(obj)
    };

    let to = relative_index(args.first(), len)?;
    let from = relative_index(args.get(1), len)?;
    let end = match args.get(2) {
        Some(v) if !v.is_undefined() => relative_index(Some(v), len)?,
        _ => len,
    };
    // ToLength(2**53) must not be looped over.
    let count = end.saturating_sub(from).min(len.saturating_sub(to));
    if count > MAX_GENERIC_ELEMENTS {
        return Err(RuntimeError::RangeError(
            "Invalid array length".to_string(),
        ));
    }

    let mut buffer: Vec<Value> = Vec::with_capacity(count as usize);
    for k in 0..count {
        buffer.push(generic_get(obj, from + k));
    }
    for (k, value) in buffer.into_iter().enumerate() {
        generic_set(obj, to + k as u64, value)?;
    }
    Ok(obj.clone())
}

/// ES `Set(O, "length", …, true)` (`Throw` = true) for the real-array fast
/// paths, which mutate the dense store directly and so skip `[[Set]]`.
///
/// `push`/`pop`/`shift`/`unshift` all perform this Set — `pop` and `shift` even
/// when the array is empty — hence `Object.freeze(a); a.push()` is a TypeError
/// rather than a silent no-op.
fn set_length_throwing(array_obj: &ArrayObject) -> Result<(), RuntimeError> {
    if !array_obj.length_is_writable() {
        return Err(RuntimeError::TypeError(
            "Cannot assign to read-only property 'length'".to_string(),
        ));
    }
    Ok(())
}

/// `Array.prototype.push(...items)` (ES 23.1.3.20).
pub fn array_push(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        if let Some(array_obj) = obj_ref.borrow_mut().as_any_mut().downcast_mut::<ArrayObject>() {
            set_length_throwing(array_obj)?;
            for arg in args {
                array_obj.push(arg.clone());
            }
            return Ok(Value::Number(array_obj.len() as f64));
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    let count = args.len() as u64;
    let new_len = len.saturating_add(count);
    if new_len > MAX_ARRAY_LENGTH {
        return Err(RuntimeError::TypeError("Invalid array length".to_string()));
    }
    for (i, arg) in args.iter().enumerate() {
        generic_set(obj, len + i as u64, arg.clone())?;
    }
    generic_set_length(obj, new_len)?;
    Ok(Value::Number(new_len as f64))
}

/// `Array.prototype.pop()` (ES 23.1.3.19).
pub fn array_pop(obj: &Value) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        if let Some(array_obj) = obj_ref.borrow_mut().as_any_mut().downcast_mut::<ArrayObject>() {
            // Step 3 performs `Set(O, "length", 0, true)` even for an empty
            // array, so a non-writable length aborts before anything changes.
            set_length_throwing(array_obj)?;
            return Ok(array_obj.pop());
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    if len == 0 {
        generic_set_length(obj, 0)?;
        return Ok(Value::Undefined);
    }
    let index = len - 1;
    let element = generic_get(obj, index);
    if !generic_delete(obj, index)? {
        return Err(RuntimeError::TypeError("Cannot delete property".to_string()));
    }
    generic_set_length(obj, index)?;
    Ok(element)
}

/// `Array.prototype.shift()` (ES 23.1.3.29).
pub fn array_shift(obj: &Value) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        if let Some(array_obj) = obj_ref.borrow_mut().as_any_mut().downcast_mut::<ArrayObject>() {
            set_length_throwing(array_obj)?;
            return Ok(array_obj.shift());
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    if len == 0 {
        generic_set_length(obj, 0)?;
        return Ok(Value::Undefined);
    }
    let first = generic_get(obj, 0);
    generic_window_move(obj, 1, len, -1, len)?;
    generic_delete_range(obj, len - 1, len, len)?;
    generic_set_length(obj, len - 1)?;
    Ok(first)
}

/// `Array.prototype.unshift(...items)` (ES 23.1.3.32).
pub fn array_unshift(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        if let Some(array_obj) = obj_ref.borrow_mut().as_any_mut().downcast_mut::<ArrayObject>() {
            set_length_throwing(array_obj)?;
            for arg in args.iter().rev() {
                array_obj.unshift(arg.clone());
            }
            return Ok(Value::Number(array_obj.len() as f64));
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    let count = args.len() as u64;
    let new_len = len.saturating_add(count);
    if new_len > MAX_ARRAY_LENGTH {
        return Err(RuntimeError::TypeError("Invalid array length".to_string()));
    }
    if count > 0 {
        generic_window_move(obj, 0, len, count as i64, len)?;
        for (i, arg) in args.iter().enumerate() {
            generic_set(obj, i as u64, arg.clone())?;
        }
    }
    generic_set_length(obj, new_len)?;
    Ok(Value::Number(new_len as f64))
}

/// `Array.prototype.reverse()` (ES 23.1.3.24).
///
/// Implemented over the present indices: a pair with exactly one side present
/// moves that value to the other side, a pair with both present swaps them.
/// That is what the spec's `lower`/`upper` loop produces, without iterating
/// `length / 2` times on a sparse receiver.
pub fn array_reverse(obj: &Value) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let mut borrowed = obj_ref.borrow_mut();
        if let Some(array_obj) = borrowed.as_any_mut().downcast_mut::<ArrayObject>() {
            array_obj.reverse();
            return Ok(obj.clone());
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    if len < 2 {
        return Ok(obj.clone());
    }
    let present = own_index_keys_below(obj, len);
    let present_set: std::collections::HashSet<u64> = present.iter().copied().collect();

    let mut swaps: Vec<(u64, u64)> = Vec::new();
    let mut moves: Vec<(u64, Value)> = Vec::new();
    let mut deletes: Vec<u64> = Vec::new();
    for k in &present {
        let other = len - 1 - *k;
        if other == *k {
            // Middle element of an odd-length window: nothing to do.
            continue;
        }
        if present_set.contains(&other) {
            if *k < other {
                swaps.push((*k, other));
            }
        } else {
            moves.push((other, generic_get(obj, *k)));
            deletes.push(*k);
        }
    }

    for (lo, hi) in swaps {
        let (low_value, high_value) = (generic_get(obj, lo), generic_get(obj, hi));
        generic_set(obj, lo, high_value)?;
        generic_set(obj, hi, low_value)?;
    }
    for (dest, value) in moves {
        generic_set(obj, dest, value)?;
    }
    for k in deletes {
        generic_delete(obj, k)?;
    }
    Ok(obj.clone())
}

pub fn array_index_of(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    match obj {
        Value::Object(obj_ref) => {
            let arr = obj_ref.borrow();
            if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
                if args.is_empty() {
                    return Ok(Value::Number(-1.0));
                }
                let from = if args.len() < 2 {
                    0
                } else {
                    clamped_index(Some(&args[1]), array_obj.len())?
                };
                Ok(Value::Number(array_obj.index_of(&args[0], from) as f64))
            } else {
                Err(RuntimeError::TypeError("not an array".to_string()))
            }
        }
        _ => Err(RuntimeError::TypeError("not an array".to_string())),
    }
}

pub fn array_includes(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    match obj {
        Value::Object(obj_ref) => {
            let arr = obj_ref.borrow();
            if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
                if args.is_empty() {
                    return Ok(Value::Bool(false));
                }
                Ok(Value::Bool(array_obj.includes(&args[0])))
            } else {
                Err(RuntimeError::TypeError("not an array".to_string()))
            }
        }
        _ => Err(RuntimeError::TypeError("not an array".to_string())),
    }
}

/// `Array.prototype.lastIndexOf(searchElement[, fromIndex])`.
pub fn array_last_index_of(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let arr = obj_ref.borrow();
        if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
            let needle = args.first().cloned().unwrap_or(Value::Undefined);
            let len = array_obj.len();
            if len == 0 {
                return Ok(Value::Number(-1.0));
            }
            let from = match args.get(1) {
                Some(v) => {
                    let n = super::to_number_throwing(v)?;
                    if n.is_nan() {
                        0
                    } else {
                        n as i64
                    }
                }
                None => len as i64 - 1,
            };
            let from = if from < 0 {
                (len as i64 + from).max(0)
            } else {
                from.min(len as i64 - 1)
            };
            for i in (0..=from).rev() {
                if array_obj.get(i as usize).is_some_and(|v| v.strict_eq(&needle)) {
                    return Ok(Value::Number(i as f64));
                }
            }
            return Ok(Value::Number(-1.0));
        }
    }
    Err(RuntimeError::TypeError("not an array".to_string()))
}

/// `Array.prototype.fill(value[, start[, end]])` (ES 23.1.3.6).
pub fn array_fill(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let mut arr = obj_ref.borrow_mut();
        if let Some(array_obj) = arr.as_any_mut().downcast_mut::<ArrayObject>() {
            let value = args.first().cloned().unwrap_or(Value::Undefined);
            let len = array_obj.len();
            let resolve = |arg: Option<&Value>, default: usize| -> Result<usize, RuntimeError> {
                match arg {
                    Some(v) => {
                        let n = super::to_number_throwing(v)?;
                        Ok(if n < 0.0 {
                            ((len as f64) + n).max(0.0) as usize
                        } else {
                            (n as usize).min(len)
                        })
                    }
                    None => Ok(default),
                }
            };
            let start = resolve(args.get(1), 0)?;
            let end = resolve(args.get(2), len)?;
            for i in start..end {
                if let Some(slot) = array_obj.get_mut(i) {
                    *slot = value.clone();
                }
            }
            return Ok(obj.clone());
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    let value = args.first().cloned().unwrap_or(Value::Undefined);
    let start = relative_index(args.get(1), len)?;
    let end = match args.get(2) {
        Some(v) if !v.is_undefined() => relative_index(Some(v), len)?,
        _ => len,
    };
    if end.saturating_sub(start) > MAX_GENERIC_ELEMENTS {
        return Err(RuntimeError::RangeError(
            "Invalid array length".to_string(),
        ));
    }
    for k in start..end {
        generic_set(obj, k, value.clone())?;
    }
    Ok(obj.clone())
}

/// `Array.prototype.join(separator)` (ES 23.1.3.15).
pub fn array_join(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let arr = obj_ref.borrow();
        if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
            let sep = if args.is_empty() {
                ","
            } else {
                &args[0].to_js_string()
            };
            return Ok(Value::string(&array_obj.join(sep)));
        }
    }
    to_object(obj)?;
    let separator = args
        .first()
        .map(|v| v.to_js_string())
        .unwrap_or_else(|| ",".to_string());
    let len = generic_length(obj);
    if len == 0 {
        return Ok(Value::string(""));
    }
    // Every index past the first contributes a separator, present or not; a
    // `length` near 2^53 must therefore fail fast instead of building a string
    // that cannot exist anyway.
    let separator_bytes = separator.len().max(1) as u64;
    if len.saturating_sub(1).saturating_mul(separator_bytes) > MAX_JOIN_LENGTH as u64 {
        // The reference engines raise a `TypeError` here (`join` has no
        // specified error for "the result cannot exist"); match them.
        return Err(RuntimeError::TypeError("Invalid string length".to_string()));
    }
    // A separator precedes every index but the first, whether or not that index
    // exists, so `written` counts the separators emitted so far (= the last
    // index written) and index `k` needs `k - written` more.
    let mut out = String::new();
    let mut written = 0u64;
    for k in own_index_keys_below(obj, len) {
        for _ in written..k {
            out.push_str(&separator);
        }
        written = k;
        let value = generic_get(obj, k);
        if !matches!(value, Value::Undefined | Value::Null) {
            out.push_str(&value.to_js_string());
        }
    }
    let separators_after = len - 1;
    for _ in written..separators_after {
        out.push_str(&separator);
    }
    Ok(Value::string(&out))
}

/// `Array.prototype.slice(start, end)` (ES 23.1.3.25).
pub fn array_slice(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let arr = obj_ref.borrow();
        if let Some(array_obj) = arr.as_any().downcast_ref::<ArrayObject>() {
            let len = array_obj.len();
            let start = clamped_index(args.first(), len)?;
            let end = if args.len() < 2 {
                len
            } else {
                clamped_index(args.get(1), len)?
            };
            let sliced = array_obj.slice(start, end);
            return Ok(Value::Object(Rc::new(RefCell::new(sliced))));
        }
    }
    to_object(obj)?;
    let len = generic_length(obj);
    let start = relative_index(args.first(), len)?;
    let end = match args.get(1) {
        Some(v) if !v.is_undefined() => relative_index(Some(v), len)?,
        _ => len,
    };
    let count = end.saturating_sub(start);
    if count > MAX_MATERIALIZED_LEN as u64 {
        return Err(RuntimeError::RangeError("Invalid array length".to_string()));
    }
    let mut result = ArrayObject::new();
    for k in 0..count {
        let index = start + k;
        if generic_has(obj, index) {
            result.push(generic_get(obj, index));
        } else {
            result.push(Value::Undefined);
            result.mark_hole(k as usize);
        }
    }
    Ok(Value::Object(Rc::new(RefCell::new(result))))
}

/// `Array.prototype.concat(...items)` (ES 23.1.3.1).
///
/// Items are spread when `IsConcatSpreadable` says so, which is what
/// `Symbol.isConcatSpreadable` is for.
pub fn array_concat(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    let mut result = ArrayObject::new();
    let mut append = |item: &Value,
                      result: &mut ArrayObject|
     -> Result<(), RuntimeError> {
        if is_concat_spreadable(item) {
            let len = generic_length(item);
            if len > MAX_MATERIALIZED_LEN as u64 {
                return Err(RuntimeError::RangeError("Invalid array length".to_string()));
            }
            for k in 0..len {
                result.push(generic_get(item, k));
            }
        } else {
            result.push(item.clone());
        }
        Ok(())
    };

    append(obj, &mut result)?;
    for arg in args {
        append(arg, &mut result)?;
    }
    Ok(Value::Object(Rc::new(RefCell::new(result))))
}

/// `Array.prototype.splice(start, deleteCount, ...items)` (ES 23.1.3.28).
pub fn array_splice(obj: &Value, args: &[Value]) -> Result<Value, RuntimeError> {
    if let Value::Object(obj_ref) = obj {
        let mut arr = obj_ref.borrow_mut();
        if let Some(array_obj) = arr.as_any_mut().downcast_mut::<ArrayObject>() {
            let len = array_obj.len();
            let start = clamped_index(args.first(), len)?;
            // Step 5 of ES 23.1.3.28: with no arguments at all `start` is not
            // present, so nothing is deleted (only `splice(start)` deletes the
            // range `len - start`).
            let delete_count = if args.is_empty() {
                0
            } else if args.len() < 2 || args[1].is_undefined() {
                len - start
            } else {
                let n = super::to_number_throwing(&args[1])?;
                let n = if n.is_nan() || n < 0.0 {
                    0usize
                } else if n.is_infinite() {
                    len - start
                } else {
                    n.trunc() as usize
                };
                n.min(len - start)
            };
            let insert_items: Vec<Value> = if args.len() > 2 {
                args[2..].to_vec()
            } else {
                vec![]
            };
            let removed = array_obj.splice(start, delete_count, &insert_items);
            return Ok(Value::Object(Rc::new(RefCell::new(ArrayObject::from_vec(
                removed,
            )))));
        }
    }

    to_object(obj)?;
    let len = generic_length(obj);
    let start = relative_index(args.first(), len)?;
    let delete_count = if args.is_empty() {
        // `splice()` with no arguments: `start` is not present, so nothing is
        // removed (ES 23.1.3.28 step 5).
        0
    } else {
        match args.get(1) {
            None | Some(Value::Undefined) => len - start,
            Some(v) => {
                let n = super::to_integer_or_infinity_throwing(Some(v))?;
                n.max(0).min((len - start) as i64) as u64
            }
        }
    };
    let items: &[Value] = if args.len() > 2 { &args[2..] } else { &[] };
    let item_count = items.len() as u64;
    let new_len = len - delete_count + item_count;
    if new_len > MAX_ARRAY_LENGTH {
        return Err(RuntimeError::TypeError("Invalid array length".to_string()));
    }

    // 1. Snapshot the removed range (holes stay holes in the result). The
    //    removed array's length is `delete_count`, which an array-like may have
    //    set far beyond what can be materialized.
    if delete_count > MAX_GENERIC_ELEMENTS {
        return Err(RuntimeError::RangeError(
            "Invalid array length".to_string(),
        ));
    }
    let mut removed = ArrayObject::new();
    for k in 0..delete_count {
        let index = start + k;
        if generic_has(obj, index) {
            removed.push(generic_get(obj, index));
        } else {
            removed.push(Value::Undefined);
            removed.mark_hole(k as usize);
        }
    }

    // 2. Move the tail, then write the inserted items.
    if item_count < delete_count {
        generic_window_move(obj, start + delete_count, len, -((delete_count - item_count) as i64), len)?;
        // Everything past the new length is dropped.
        generic_delete_range(obj, new_len, len, len)?;
    } else if item_count > delete_count {
        generic_window_move(obj, start + delete_count, len, (item_count - delete_count) as i64, len)?;
    }
    for (i, item) in items.iter().enumerate() {
        generic_set(obj, start + i as u64, item.clone())?;
    }
    generic_set_length(obj, new_len)?;
    Ok(Value::Object(Rc::new(RefCell::new(removed))))
}