Merged
Conversation
tests verify compile-time errors when trying to serialize map/set
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 Runtime Error Prevention
Summary
Fixed JSON.stringify to properly handle
string[]andnumber[]arrays, and now emits compile-time errors for unsupported types instead of silently producing garbage output.Problem: When calling
JSON.stringify()with types that weren't explicitly handled (likestring[],number[], or arbitrary interfaces), the codegen would fall through to a number handler that treated pointer values as doubles, causing LLVM type errors or silent garbage output.Solution:
string[]andnumber[]that iterate and call new C bridge functionscsyyjson_arr_add_strandcsyyjson_arr_add_numfor array serializationTest Plan
json-stringify-string-array.ts— verifiesstring[]serializationjson-stringify-number-array.ts— verifiesnumber[]serializationImplementation Details
Files Changed
c_bridges/yyjson-bridge.ccsyyjson_arr_add_str(doc, arr, val)— appends string to JSON arraycsyyjson_arr_add_num(doc, arr, val)— appends number to JSON arraysrc/codegen/runtime/runtime.tsgenerateJSONRuntime()src/codegen/stdlib/json.tsgenerateStringifyArg()to detect and dispatch:string[]viasymbolTable.isStringArray()+isStringArrayExpression()number[]viasymbolTable.isNumberArray()stringifyStringArray()method with loop-based array iterationstringifyNumberArray()method with loop-based array iterationctx.emitError()for unsupported typestests/fixtures/builtins/json-stringify-string-array.tsJSON.stringify(["alpha", "beta", "gamma"])tests/fixtures/builtins/json-stringify-number-array.tsJSON.stringify([1, 2, 3])How It Works
String Array Serialization
%StringArraystruct (pointer to strings, length, capacity)csyyjson_arr_add_strper stringNumber Array Serialization
%Arraystruct (pointer to doubles, length, capacity)csyyjson_arr_add_numper numberCompile Error for Unsupported Types
Before returning a value,
generateStringifyArgnow checks if the expression is a number/boolean literal or variable:arg.type === "number"or"boolean"→ serialize as numberVerification
The implementation preserves existing behavior for supported types (objects, interfaces, ObjectArray, strings, numbers, booleans) while extending support to common array types and preventing silent failures.