ParseJson throws on primitive JSON and breaks FormatJson→ParseJson round-trips (Cannot read properties of undefined reading 'source')
### Environment
- **brs-node** `1.9.9` and `1.10.0` (Node BrightScript bundle from this repo)
- **Node** (e.g. v20.x)
- Reproduced while running generated BrightScript that calls `ParseJson` / `FormatJson` via Hosanna’s `hs_bridge_core.brs` helpers.
### Summary
`ParseJson` sometimes throws a **JavaScript** error inside the engine:
`ParseJSON: Cannot read properties of undefined (reading 'source')`
This shows up in two situations:
1. **JSON document that is only a number** (valid JSON: a primitive at the root).
2. **`ParseJson(FormatJson(obj))`** style round-trips (e.g. `structuredClone` implemented as parse after stringify).
When `ParseJson` throws, BrightScript callers see **invalid** results and downstream code fails (e.g. `isNaN` checks on the parsed value, or property access on a clone that stayed `invalid`).
---
### Repro A — bare numeric JSON (minimal)
BrightScript (or any code path that ends up calling `ParseJson` with this string):
```brightscript
function main()
' Valid JSON text whose root value is a number — should parse to a number, not throw
s = "1751259600000"
x = ParseJson(s)
? x
end function
Expected: x is a numeric value; no runtime error.
Actual: Error logged, e.g.
ParseJSON: Cannot read properties of undefined (reading 'source')
JS equivalent for comparison:
JSON.parse("1751259600000") // => 1751259600000
Repro B — FormatJson → ParseJson round-trip
function main()
obj = { "a": 1, "b": { "c": 2 } }
txt = FormatJson(obj, 512)
clone = ParseJson(txt)
? clone.a
end function
Expected: clone is a valid associative array mirroring obj.
Actual: Same class of ParseJson error at the ParseJson line (observed at hs_structuredClone-style code: clone = parseJSON(formatJSON(obj))).
```
Notes
The failure is in the engine’s ParseJson implementation (stack originates from JS, not BrightScript syntax).
brs-engine 1.8.9 (bin/brs.node.js) does not exhibit this for our call sites; brs-node 1.9.9 / 1.10.0 do.
Impact
Any host that relies on ParseJson for arbitrary JSON text (including primitive roots) or on stringify→parse cloning is affected.
Thanks for looking at this.
关闭于 2026-04-30 1 条评论