Repository navigation
JSON.stringify writes an object literal or a tuple - #95
Merged
Merged
Conversation
`JSON.stringify({x: 1})` threw a TypeError ("cannot read the fields of an object literal or a
tuple"), which, uncaught, ended the program with 0xC0000409 and no output. The compiler now
makes `for...in` over an object literal or a tuple for each field of its type, with the key a
literal, so `value[key]` is that field with its own type; toValue walks the fields with it.
A tuple of unnamed fields is an array, as in JavaScript; a function or an undefined field is
left out of an object. A BigInt is still a TypeError.
Needs the compiler change in TypeScriptCompiler (for...in over fields, and a generic's
instance making other instances: `JSON.stringify([[1], [2]])` failed to compile in a program
that had not stringified a number first).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
JSON.stringify({x: 1})threwTypeError("JSON.stringify cannot read the fields of an object literal or a tuple"). Uncaught, that ended a program with 0xC0000409 and no output. Now it writes{"x":1}.Needs ASDAlexander77/TypeScriptCompiler#832 merged first. That compiler PR makes
for...inover an object literal or a tuple once for each field of its type, with the key as a literal. It also fixes a generic instance making other instances: on main,JSON.stringify([1, 2])does not compile when it is a program's first stringify. The release workflow clones this repo's main, so merging this PR first would failjson.tswith the current compiler.Change
toValuesends a tuple, or an"object"(an object literal with methods), to a newfieldsToValue.fieldsToValuewalks the fields withfor (const key in value).value[key]is then that field with its own type."0" in value) becomes a JSON array, as in JavaScript.null.Tests
json.tsgets a newobjects(), run first, so the writer's inner instances are made from inside it.get_-named value fieldsspaceVerified locally (Windows x64, built with #832)
tests.ps1passes for gc, rc, none and own, in release and debug, compiled and JIT.Known limits
{ "0": … }is written as an array, and a named tuple as an object. The compiler does not tell the two apart.-mm=own, a literal holding a class instance ({ when: new Date(0) }) is rejected by the compiler's existing own rule.🤖 Generated with Claude Code