Parent: #459
Problem
JSON.parse collapses every syntax fault into one string. JavaScriptCore names the
fault. A 40-case differential against a real JSC backend matches 1/40:
| input | zig-js | JavaScriptCore |
|---|---|---|
'' | JSON.parse: invalid JSON | JSON Parse error: Unexpected EOF |
'{' | JSON.parse: invalid JSON | JSON Parse error: Expected '}' |
'{"a"}' | JSON.parse: invalid JSON | JSON Parse error: Expected ':' before value in object property definition |
"{'a':1}" | JSON.parse: invalid JSON | JSON Parse error: Single quotes (') are not allowed in JSON |
'[1,]' | JSON.parse: invalid JSON | JSON Parse error: Unexpected comma at the end of array expression |
'{"a":1,}' | JSON.parse: invalid JSON | JSON Parse error: Property name must be a string literal |
'NaN' | JSON.parse: invalid JSON | JSON Parse error: Unexpected identifier "NaN" |
'"abc' | JSON.parse: invalid JSON | JSON Parse error: Unterminated string |
'1 2' | JSON.parse: trailing characters | JSON Parse error: Unable to parse JSON string |
JSON.stringify on a cycle is also wrong, and interestingly matches V8 rather than
JSC: zig-js says Converting circular structure to JSON, JSC says
JSON.stringify cannot serialize cyclic structures.
The complete JavaScriptCore vocabulary
Every message is prefixed JSON Parse error: . Measured, not guessed:
Unexpected EOF
Unable to parse JSON string (trailing content after a complete value)
Expected '}' / Expected ']'
Expected ':' before value in object property definition
Property name must be a string literal
Single quotes (') are not allowed in JSON
Unexpected comma at the end of array expression
Unexpected token '<c>' (parameterized: } ] , . …)
Unrecognized token '<c>' (e.g. '+')
Unexpected identifier "<word>" (NaN, Infinity, undefined, tru, True, nul)
Unterminated string
Invalid escape character <c> (unquoted)
\u must be followed by 4 hex digits
Invalid number
Invalid digits after decimal point
Exponent symbols should be followed by an optional '+' or '-' and then by at least one numberNote '01' and '00' are Unable to parse JSON string, not a number error — JSC parses
the leading 0 as a complete value and then reports trailing content.
Required implementation
The parser in src/builtins.zig (~3574-3900) returns a bare error.Invalid from roughly
twenty sites, and jsonParse (3444) maps that one error to one message. Carry the reason:
- add a
reasonfield to the parser struct (an enum plus the one-char / one-word payload the parameterized forms need), set it at eachreturn error.Invalidsite; - map reason to JSC's exact string at the
jsonParse/jsonRawJsonboundary; - keep the
JSON.rawJSONwording aligned with whatever JSC does there — probe it, do not assume it sharesJSON.parse's prefix.
Verification
scratchpad/sweep/probe.sh drives both engines and diffs; the 40-case set used above is
sweep/jsonvocab.js. Drive the probe from inside a function — zig-js --eval is script
code, so an indirect eval in a case shares global scope with the harness and a case
declaring var i will clobber the loop counter and silently truncate the run.