js-on-sx: JSON.stringify + JSON.parse

Recursive-descent parser/serializer in SX.

stringify: type-of dispatch for primitives, lists, dicts. Strings
escape \\ \" \n \r \t.

parse: {:s src :i idx} state dict threaded through helpers.
Handles primitives, strings (with escapes), numbers, arrays,
objects.

Wired into js-global.

391/393 unit (+10), 148/148 slice unchanged.
This commit is contained in:
2026-04-23 21:22:32 +00:00
parent 6f0b4fb476
commit ebaec1659e
3 changed files with 288 additions and 1 deletions

View File

@@ -993,6 +993,28 @@ cat > "$TMPFILE" << 'EPOCHS'
(epoch 1904)
(eval "(js-eval \"parseFloat('3.14')\")")
;; ── Phase 11.json: JSON.stringify / JSON.parse ────────────────
(epoch 2000)
(eval "(js-eval \"JSON.stringify(42)\")")
(epoch 2001)
(eval "(js-eval \"JSON.stringify('hi')\")")
(epoch 2002)
(eval "(js-eval \"JSON.stringify([1,2,3])\")")
(epoch 2003)
(eval "(js-eval \"JSON.stringify({a:1})\")")
(epoch 2004)
(eval "(js-eval \"JSON.stringify(true)\")")
(epoch 2005)
(eval "(js-eval \"JSON.parse('42')\")")
(epoch 2006)
(eval "(js-eval \"JSON.parse('true')\")")
(epoch 2007)
(eval "(js-eval \"JSON.parse('\\\"hello\\\"')\")")
(epoch 2008)
(eval "(js-eval \"JSON.parse('[1,2,3]').length\")")
(epoch 2009)
(eval "(js-eval \"JSON.parse('{\\\"a\\\":1}').a\")")
EPOCHS
@@ -1525,6 +1547,18 @@ check 1902 "parseInt('42')" '42'
check 1903 "parseInt(3.7)" '3'
check 1904 "parseFloat('3.14')" '3.14'
# ── Phase 11.json ──────────────────────────────────────────────
check 2000 "stringify(42)" '"42"'
check 2001 "stringify('hi')" '"\"hi\""'
check 2002 "stringify array" '"[1,2,3]"'
check 2003 "stringify object" '"{\"a\":1}"'
check 2004 "stringify true" '"true"'
check 2005 "parse 42" '42'
check 2006 "parse true" 'true'
check 2007 "parse string" '"hello"'
check 2008 "parse array length" '3'
check 2009 "parse object.a" '1'
TOTAL=$((PASS + FAIL))
if [ $FAIL -eq 0 ]; then
echo "$PASS/$TOTAL JS-on-SX tests passed"