fix(json): JSON.parse<T> with 'any'-typed field stores raw JSON substring#555
Merged
fix(json): JSON.parse<T> with 'any'-typed field stores raw JSON substring#555
Conversation
…ring
previously `any`-typed field in a typed-parse interface emitted
`call %any* @parse_json_any(i8* ...)` — broken IR ('use of undefined
type named any').
fix: parse writes the field's JSON source text back to a string via
csyyjson_val_write and stores it in the struct slot as i8*. users can
then re-parse the substring with JSON.parse<ConcreteShape>(field) once
they know the shape from a sibling discriminant ('command', 'method',
event type, etc.).
concretely enables the envelope pattern dapweb was forced to implement
manually via an extractField walker:
interface Envelope { command: string; body: any; }
const e = JSON.parse<Envelope>(rawWire);
switch (e.command) {
case "launch":
const a = JSON.parse<LaunchArgs>(e.body);
...
}
closes dapweb NOTES #12.
Contributor
Benchmark Results (Linux x86-64)
CLI Tool Benchmarks
|
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.
Summary
Closes dapweb NOTES #12.
any-typed fields inside a JSON.parse interface previously emitted@parse_json_any— broken IR with 'use of undefined type named any'. Now the parser serializes the JSON value's source text back to a string viacsyyjson_val_writeand stores it in the struct slot asi8*.Why this is useful, not a hack
This is exactly the semantics for polymorphic payload dispatch (DAP, JSON-RPC, webhooks):
dapweb was doing this manually with an
extractField()walker — now it's native, zero boilerplate.Scope note
anyremains rejected as a parameter type (#5 behavior unchanged). This PR only addresses the interface-field case, where the field type maps toi8*anyway — the parser was just missing the generation branch.Test plan
tests/fixtures/builtins/json-parse-any-field.tscovers full envelope-dispatch pattern