js-on-sx: JSON.parse raises SyntaxError, rejects trailing content + control chars
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 43s
This commit is contained in:
@@ -5321,12 +5321,21 @@
|
||||
(&rest args)
|
||||
(if
|
||||
(= (len args) 0)
|
||||
js-undefined
|
||||
(raise (js-new-call SyntaxError (js-args "Unexpected token undefined")))
|
||||
(let
|
||||
((st (dict)))
|
||||
(dict-set! st "s" (js-to-string (nth args 0)))
|
||||
(dict-set! st "i" 0)
|
||||
(js-json-parse-value st)))))
|
||||
(let
|
||||
((result (js-json-parse-value st)))
|
||||
(begin
|
||||
(js-json-skip-ws! st)
|
||||
(if
|
||||
(< (get st "i") (len (get st "s")))
|
||||
(raise
|
||||
(js-new-call SyntaxError
|
||||
(js-args (str "Unexpected token at position " (get st "i")))))
|
||||
result)))))))
|
||||
|
||||
(define
|
||||
js-json-skip-ws!
|
||||
@@ -5348,7 +5357,7 @@
|
||||
(let
|
||||
((s (get st "s")) (i (get st "i")))
|
||||
(cond
|
||||
((>= i (len s)) (error "JSON: unexpected end"))
|
||||
((>= i (len s)) (raise (js-new-call SyntaxError (js-args "JSON: unexpected end"))))
|
||||
((= (char-at s i) "\"") (js-json-parse-string st))
|
||||
((= (char-at s i) "[") (js-json-parse-array st))
|
||||
((= (char-at s i) "{") (js-json-parse-object st))
|
||||
@@ -5380,8 +5389,13 @@
|
||||
(let
|
||||
((i (get st "i")))
|
||||
(cond
|
||||
((>= i (len s)) nil)
|
||||
((>= i (len s))
|
||||
(raise (js-new-call SyntaxError (js-args "JSON: unterminated string"))))
|
||||
((= (char-at s i) "\"") nil)
|
||||
((< (char-code (char-at s i)) 32)
|
||||
(raise
|
||||
(js-new-call SyntaxError
|
||||
(js-args "JSON: control character in string"))))
|
||||
((= (char-at s i) "\\")
|
||||
(begin
|
||||
(when
|
||||
@@ -5457,7 +5471,7 @@
|
||||
(js-json-skip-ws! st)
|
||||
(js-json-parse-array-loop st result)))
|
||||
((= c "]") (dict-set! st "i" (+ (get st "i") 1)))
|
||||
(else (error "JSON: expected , or ]"))))))
|
||||
(else (raise (js-new-call SyntaxError (js-args "JSON: expected , or ]"))))))))
|
||||
|
||||
(define
|
||||
js-json-parse-object
|
||||
@@ -5482,7 +5496,7 @@
|
||||
(js-json-skip-ws! st)
|
||||
(when
|
||||
(not (= (char-at (get st "s") (get st "i")) ":"))
|
||||
(error "JSON: expected :"))
|
||||
(raise (js-new-call SyntaxError (js-args "JSON: expected :"))))
|
||||
(dict-set! st "i" (+ (get st "i") 1))
|
||||
(let ((v (js-json-parse-value st))) (dict-set! result k v))
|
||||
(js-json-skip-ws! st)
|
||||
@@ -5494,7 +5508,7 @@
|
||||
(dict-set! st "i" (+ (get st "i") 1))
|
||||
(js-json-parse-object-loop st result)))
|
||||
((= c "}") (dict-set! st "i" (+ (get st "i") 1)))
|
||||
(else (error "JSON: expected , or }")))))))
|
||||
(else (raise (js-new-call SyntaxError (js-args "JSON: expected , or }")))))))))
|
||||
|
||||
(define JSON {:stringify js-json-stringify :parse js-json-parse})
|
||||
|
||||
|
||||
@@ -158,6 +158,8 @@ Each item: implement → tests → update progress. Mark `[x]` when tests green.
|
||||
|
||||
Append-only record of completed iterations. Loop writes one line per iteration: date, what was done, test count delta.
|
||||
|
||||
- 2026-05-10 — **`JSON.parse` raises spec-correct `SyntaxError` instances and rejects malformed input.** Previously `JSON.parse("12 34")` silently returned `12` (no trailing-content check), `JSON.parse('" | ||||