Skip to content

JSON.stringify Runtime Error Prevention#80

Merged
cs01 merged 2 commits intomainfrom
worktree-json-stringify-fix
Mar 2, 2026
Merged

JSON.stringify Runtime Error Prevention#80
cs01 merged 2 commits intomainfrom
worktree-json-stringify-fix

Conversation

@cs01
Copy link
Owner

@cs01 cs01 commented Mar 2, 2026

JSON.stringify Runtime Error Prevention

Summary

Fixed JSON.stringify to properly handle string[] and number[] 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 (like string[], 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:

  • Added dispatch cases for string[] and number[] that iterate and call new C bridge functions
  • Replaced silent fallback with compile-time error detection for truly unsupported types
  • Added new C bridge functions csyyjson_arr_add_str and csyyjson_arr_add_num for array serialization

Test Plan

  • All 259 existing tests pass
  • Two new fixtures added:
    • json-stringify-string-array.ts — verifies string[] serialization
    • json-stringify-number-array.ts — verifies number[] serialization
  • Existing mixed-type tests continue to pass

Implementation Details

Files Changed

  1. c_bridges/yyjson-bridge.c

    • Added csyyjson_arr_add_str(doc, arr, val) — appends string to JSON array
    • Added csyyjson_arr_add_num(doc, arr, val) — appends number to JSON array
  2. src/codegen/runtime/runtime.ts

    • Declared two new extern functions in generateJSONRuntime()
  3. src/codegen/stdlib/json.ts

    • Enhanced generateStringifyArg() to detect and dispatch:
      • string[] via symbolTable.isStringArray() + isStringArrayExpression()
      • number[] via symbolTable.isNumberArray()
    • Added stringifyStringArray() method with loop-based array iteration
    • Added stringifyNumberArray() method with loop-based array iteration
    • Replaced silent number fallback with ctx.emitError() for unsupported types
    • Now correctly rejects unsupported types at compile time with clear error messages
  4. tests/fixtures/builtins/json-stringify-string-array.ts

    • Tests JSON.stringify(["alpha", "beta", "gamma"])
  5. tests/fixtures/builtins/json-stringify-number-array.ts

    • Tests JSON.stringify([1, 2, 3])

How It Works

String Array Serialization

  • Load %StringArray struct (pointer to strings, length, capacity)
  • Create yyjson array document
  • Loop over elements calling csyyjson_arr_add_str per string
  • Stringify the result

Number Array Serialization

  • Load %Array struct (pointer to doubles, length, capacity)
  • Create yyjson array document
  • Loop over elements calling csyyjson_arr_add_num per number
  • Stringify the result

Compile Error for Unsupported Types

Before returning a value, generateStringifyArg now checks if the expression is a number/boolean literal or variable:

  • If arg.type === "number" or "boolean" → serialize as number
  • If variable is number/boolean per symbol table → serialize as number
  • Otherwise → emit compile error with clear message listing supported types

Verification

npm test  # All 259 tests pass

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.

@cs01 cs01 changed the title json.stringify: add string[]/number[] support, error on unsupported t… JSON.stringify Runtime Error Prevention Mar 2, 2026
tests verify compile-time errors when trying to serialize map/set
@cs01 cs01 merged commit 8e8a8b0 into main Mar 2, 2026
12 checks passed
@cs01 cs01 deleted the worktree-json-stringify-fix branch March 2, 2026 19:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant