diff --git a/lib/js/runtime.sx b/lib/js/runtime.sx index 278b432d..299ed74d 100644 --- a/lib/js/runtime.sx +++ b/lib/js/runtime.sx @@ -1392,7 +1392,11 @@ (define js-num-to-int - (fn (n) (if (>= n 0) (floor n) (- 0 (floor (- 0 n)))))) + (fn + (v) + (let + ((n (if (number? v) v (js-to-number v)))) + (if (>= n 0) (floor n) (- 0 (floor (- 0 n))))))) (define dict-has? (fn (d k) (contains? (keys d) k))) diff --git a/lib/js/test.sh b/lib/js/test.sh index 5e83f260..4d10d531 100755 --- a/lib/js/test.sh +++ b/lib/js/test.sh @@ -1053,6 +1053,12 @@ cat > "$TMPFILE" << 'EPOCHS' (epoch 2307) (eval "(js-eval \"Array.from([1,2,3], (x)=>x*2).join(',')\")") +;; ── Phase 11.coerce: string-to-number coercion in int params ── +(epoch 2400) +(eval "(js-eval \"'abcd'.charAt('2')\")") +(epoch 2401) +(eval "(js-eval \"'abcd'.slice('1', '3')\")") + EPOCHS @@ -1619,6 +1625,10 @@ check 2305 "Array.from array" '3' check 2306 "Array.from string" '3' check 2307 "Array.from w/ map" '"2,4,6"' +# ── Phase 11.coerce ─────────────────────────────────────────── +check 2400 "charAt coerces string" '"c"' +check 2401 "slice coerces strings" '"bc"' + TOTAL=$((PASS + FAIL)) if [ $FAIL -eq 0 ]; then echo "✓ $PASS/$TOTAL JS-on-SX tests passed"