js-on-sx: 15 new Array.prototype methods (at, flatMap, findLast, reduceRight, toString, toReversed, toSorted, ...)

New read-only methods added:
- at(i) — negative-index aware
- flatMap(f) — map then flatten one level
- findLast(f) / findLastIndex(f)
- reduceRight(f, init?)
- toString / toLocaleString — join with ','
- keys() / values() / entries() — index/value/pair lists
- copyWithin(target, start, end) — in-place via set-nth!
- toReversed() / toSorted() — non-mutating variants

Mutating methods (unshift, splice) are stubs that return correct lengths
but don't mutate — we don't have a pop-first!/clear! primitive to rebuild
the list in place. Tracked as a runtime limitation.

10 new unit tests, 479/481 total. Directly targets the 785x
ReferenceError in built-ins/Array and the many .toString() crashes.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-24 07:27:00 +00:00
parent d74344ffbd
commit 30ef085844
2 changed files with 216 additions and 0 deletions

View File

@@ -1191,6 +1191,28 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 3609)
(eval "(js-eval \"(true).valueOf()\")")
;; ── Phase 11.arrmore: more Array.prototype methods ──────────
(epoch 3700)
(eval "(js-eval \"[1,2,3].at(0)\")")
(epoch 3701)
(eval "(js-eval \"[1,2,3].at(-1)\")")
(epoch 3702)
(eval "(js-eval \"[1,2,3,4].flatMap(x=>[x,x*2]).join(',')\")")
(epoch 3703)
(eval "(js-eval \"[1,5,2,7,3].findLast(x=>x<5)\")")
(epoch 3704)
(eval "(js-eval \"[1,5,2,7,3].findLastIndex(x=>x<5)\")")
(epoch 3705)
(eval "(js-eval \"[1,2,3,4].reduceRight((acc,x)=>acc+','+x)\")")
(epoch 3706)
(eval "(js-eval \"[1,2,3].toString()\")")
(epoch 3707)
(eval "(js-eval \"[3,1,2].toReversed().join(',')\")")
(epoch 3708)
(eval "(js-eval \"[3,1,4,1,5].toSorted((a,b)=>a-b).join(',')\")")
(epoch 3709)
(eval "(js-eval \"var a=[1,2,3]; a.keys().join(',')\")")
;; ── Phase 11.arrlike: Array.prototype.* on {length, 0:..., 1:...} ──
(epoch 3500)
(eval "(js-eval \"var a = {length: 3, 0: 41, 1: 42, 2: 43}; Array.prototype.slice.call(a).length\")")
@@ -1853,6 +1875,18 @@ check 3607 "true.toString()" '"true"'
check 3608 "false.toString()" '"false"'
check 3609 "(true).valueOf()" 'true'
# ── Phase 11.arrmore: more Array.prototype methods ────────────
check 3700 "arr.at(0)" '1'
check 3701 "arr.at(-1)" '3'
check 3702 "arr.flatMap" '"1,2,2,4,3,6,4,8"'
check 3703 "arr.findLast" '3'
check 3704 "arr.findLastIndex" '4'
check 3705 "arr.reduceRight" '"4,3,2,1"'
check 3706 "arr.toString" '"1,2,3"'
check 3707 "arr.toReversed" '"2,1,3"'
check 3708 "arr.toSorted" '"1,1,3,4,5"'
check 3709 "arr.keys" '"0,1,2"'
# ── Phase 11.arrlike: array-like receivers on Array.prototype ─
check 3500 "slice.call arrLike length" '3'
check 3501 "slice.call arrLike join" '"41,42,43"'