diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index f18885be..9c7b2bca 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -23,6 +23,10 @@ kept so existing scripts fail loudly (Refs #56) ==== Added +* json: `hpm_json_object_len` / `hpm_json_object_key` enumerate object +members, so `to_json` materialises objects instead of returning `None`. +Lowered on Bun-ESM; the matching hpm-json-rsr Zig exports are not merged +yet, so native targets cannot link them until they are. * Url: new `stdlib/Url.affine` with `url_resolve` and `url_parse`, WHATWG semantics via the host `URL` constructor. Bun-ESM only; no native lowering. * PixiUI: rename extern type `List` → `UiList` so `use PixiUI` typechecks diff --git a/docs/specs/zig-ffi-patterns.adoc b/docs/specs/zig-ffi-patterns.adoc index b7a22e86..dbb6e447 100644 --- a/docs/specs/zig-ffi-patterns.adoc +++ b/docs/specs/zig-ffi-patterns.adoc @@ -213,6 +213,19 @@ pub extern fn hpm_json_free(val: HpmJsonValue) -> Int; // ownership: caller fr * `parse` returns `Option` because parsing can fail. * `free` discharges the host allocation; every successful `parse` is balanced by exactly one `free`. + +*Wasm host import set (changed in json v0.4).* A Wasm module that brings in +`json`'s externs, for example with `use json::*`, imports every +`hpm_json_*` extern whether or not it calls one. v0.4 adds +`hpm_json_object_len` and `hpm_json_object_key`, so the set grows from 11 +to 13. Compiled with `use json::*`, the imports appear under the `json` +module (`json.hpm_json_object_len`, `json.hpm_json_object_key`), not under +`env` as the generic description above says. A host that supplies only the +v0.3 eleven fails to instantiate the module with a `LinkError`, before any +code runs. Until `hpm-json-rsr` exports the two functions, such a host must +stub them (`hpm_json_object_len` returning 0, `hpm_json_object_key` +returning `None`), which makes `to_json` reject objects, or import `json` +selectively (`use json::{encode_int}` imports only what it names). * No effect row: parsing a string is pure with respect to the affine effect lattice (the allocation is an implementation detail of the host). diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 2203ee71..472d06b4 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -630,6 +630,26 @@ const __as_urlParse = (s) => { password: u.password, host: u.host, hostname: u.hostname, port: u.port, pathname: u.pathname, search: u.search, hash: u.hash }); }; +// Object-key enumeration mirrors the planned hpm-json-rsr Zig exports +// `hpm_json_object_len` / `hpm_json_object_key`: own enumerable string keys +// in insertion order (std.json's ObjectMap is an ordered ArrayHashMap). +// JavaScript lists integer-like keys first and the native backend will not, +// so callers must not rely on the relative order of such keys. +const __as_hpmJsonObjectKeys = (v) => + (v === null || typeof v !== "object" || Array.isArray(v)) ? [] : Object.keys(v); +const __as_hpmJsonObjectLen = (v) => __as_hpmJsonObjectKeys(v).length; +// A walk asks for keys by index, so index 0 takes a snapshot of the object's +// keys and later indices read it: one Object.keys per walk, not per key. +// Each walk starts at 0, so a snapshot never outlives the walk that made it. +const __as_hpmJsonKeySnapshots = new WeakMap(); +const __as_hpmJsonObjectKey = (v, i) => { + if (v === null || typeof v !== "object" || Array.isArray(v)) return None; + const idx = Number(i); + if (!Number.isInteger(idx) || idx < 0) return None; + let ks = idx === 0 ? undefined : __as_hpmJsonKeySnapshots.get(v); + if (ks === undefined) { ks = Object.keys(v); __as_hpmJsonKeySnapshots.set(v, ks); } + return idx < ks.length ? Some(ks[idx]) : None; +}; const __as_hpmJsonArrayLen = (v) => (Array.isArray(v) ? v.length : 0); const __as_hpmJsonArrayGet = (v, i) => { if (!Array.isArray(v)) return None; @@ -1215,6 +1235,8 @@ let () = b "hpm_json_object_get" (fun a -> Printf.sprintf "__as_hpmJsonObjectGet(%s, %s)" (arg 0 a) (arg 1 a)); b "url_resolve" (fun a -> Printf.sprintf "__as_urlResolve(%s, %s)" (arg 0 a) (arg 1 a)); b "url_parse" (fun a -> Printf.sprintf "__as_urlParse(%s)" (arg 0 a)); + b "hpm_json_object_len" (fun a -> Printf.sprintf "__as_hpmJsonObjectLen(%s)" (arg 0 a)); + b "hpm_json_object_key" (fun a -> Printf.sprintf "__as_hpmJsonObjectKey(%s, %s)" (arg 0 a) (arg 1 a)); b "hpm_json_array_len" (fun a -> Printf.sprintf "__as_hpmJsonArrayLen(%s)" (arg 0 a)); b "hpm_json_array_get" (fun a -> Printf.sprintf "__as_hpmJsonArrayGet(%s, %s)" (arg 0 a) (arg 1 a)); b "hpm_json_escape_string" (fun a -> Printf.sprintf "__as_hpmJsonEscapeString(%s)" (arg 0 a)); diff --git a/stdlib/json.affine b/stdlib/json.affine index c24a7fdd..1f7179ae 100644 --- a/stdlib/json.affine +++ b/stdlib/json.affine @@ -23,10 +23,10 @@ // The wrapper does NOT hand-roll a JSON parser: `hpm_json_parse` // owns parsing + arena allocation, and AffineScript-side functions // tree-walk the resulting opaque `HpmJsonValue` handle into the AS -// `Json` sum type for leaves + arrays. Object-key enumeration is -// not yet exposed by the Zig FFI — see `to_json` for the gap, and -// prefer the lazy `hpm_json_object_get` + leaf-extract pattern for -// object payloads (e.g. GitHub webhook JSON). +// `Json` sum type. Object members are enumerated through +// `hpm_json_object_len` / `hpm_json_object_key` (v0.4). Those two are +// lowered on the ESM backend; the matching hpm-json-rsr Zig exports are +// not merged yet, so native targets cannot link them until they are. module json; @@ -212,7 +212,7 @@ fn escape_string(s: String) -> String { // ============================================================================ // RSR rewire (v0.3) — hpm-json-rsr Zig FFI bindings // -// The 11 `hpm_json_*` externs faithfully mirror the Zig exports at +// Eleven of the 13 `hpm_json_*` externs mirror the Zig exports at // `hyperpolymath/hpm-json-rsr/ffi/zig/src/main.zig`. They lower on the // Deno-ESM backend (lib/codegen_deno.ml) to `JSON.parse` + JS-native // walks (a handle is just the underlying JS value); on native targets @@ -229,9 +229,12 @@ fn escape_string(s: String) -> String { // hpm_json_int -> INT64_MIN on type mismatch // hpm_json_float -> NaN on type mismatch // -// Object-key enumeration is NOT yet a Zig export; consequently `to_json` -// returns None for HpmJsonValue roots whose type tag is 6 (object). For -// object payloads, descend lazily via `hpm_json_object_get`. +// hpm_json_object_len -> 0 on type mismatch +// hpm_json_object_key -> None on type mismatch or index out of range +// +// `hpm_json_object_len` and `hpm_json_object_key` are the v0.4 additions. +// Until hpm-json-rsr ships the matching Zig exports they exist on the ESM +// backend only. // ============================================================================ pub extern type HpmJsonValue; @@ -244,6 +247,13 @@ pub extern fn hpm_json_int(val: HpmJsonValue) -> Int; pub extern fn hpm_json_float(val: HpmJsonValue) -> Float; pub extern fn hpm_json_string(val: HpmJsonValue) -> String; pub extern fn hpm_json_object_get(val: HpmJsonValue, key: String) -> Option; +/// Number of members of an object value; 0 on type mismatch. +pub extern fn hpm_json_object_len(val: HpmJsonValue) -> Int; +/// Key of the `idx`-th member of an object value, in document order; `None` +/// on type mismatch or out-of-range index. Integer-like keys may be ordered +/// differently by the JavaScript host (it lists them first), so callers must +/// not depend on their relative order. +pub extern fn hpm_json_object_key(val: HpmJsonValue, idx: Int) -> Option; pub extern fn hpm_json_array_len(val: HpmJsonValue) -> Int; pub extern fn hpm_json_array_get(val: HpmJsonValue, idx: Int) -> Option; pub extern fn hpm_json_escape_string(src: String) -> String; @@ -256,11 +266,8 @@ pub fn parse(src: String) -> Option { hpm_json_parse(src) } -/// Tree-walk a non-object `HpmJsonValue` into the AS `Json` sum. -/// -/// Returns `None` if the value's root is an object (tag 6) — see the -/// module preamble: object-key enumeration is not yet exported by the -/// Zig FFI. Leaves + arrays of leaves/arrays materialise fully. +/// Tree-walk an `HpmJsonValue` into the AS `Json` sum. Objects keep their +/// members in the order `hpm_json_object_key` reports them. /// /// Recursively `hpm_json_free`s every child handle the walk allocates; /// the root handle is the caller's responsibility (matches the parse @@ -305,8 +312,39 @@ pub fn to_json(val: HpmJsonValue) -> Option { i = i + 1; } Some(JArray(acc)) + } else if t == 6 { + let n = hpm_json_object_len(val); + let mut fields = []; + let mut i = 0; + while i < n { + match hpm_json_object_key(val, i) { + Some(k) => { + match hpm_json_object_get(val, k) { + Some(child) => { + match to_json(child) { + Some(j) => { + fields = fields ++ [(k, j)]; + hpm_json_free(child); + }, + None => { + hpm_json_free(child); + return None; + } + } + }, + None => { + return None; + } + } + }, + None => { + return None; + } + }; + i = i + 1; + } + Some(JObject(fields)) } else { - // tag 6 (object) — no key-enumeration in the Zig FFI yet; // tag -1 (null val) — defensive: shouldn't reach here. None } diff --git a/tests/codegen-bun/json_escapes.harness.mjs b/tests/codegen-bun/json_escapes.harness.mjs index 6d2e7905..cb0793ab 100644 --- a/tests/codegen-bun/json_escapes.harness.mjs +++ b/tests/codegen-bun/json_escapes.harness.mjs @@ -14,5 +14,5 @@ for (const v of ["q\"uote", "back\\slash", "nl\nx", "cr\rtab\t", "bs\bff\f", "c\ } expect("non-finite element", subject.roundtrip("[1e400]"), ""); expect("finite exponent", subject.roundtrip("[1e3]"), "[1000]"); -expect("objects need key enumeration, not on this branch", subject.roundtrip('{"a":1}'), ""); +expect("objects convert", subject.roundtrip('{"a":1}'), '{"a":1}'); console.log("json_escapes: ok"); diff --git a/tests/codegen-bun/json_object_keys.affine b/tests/codegen-bun/json_object_keys.affine new file mode 100644 index 00000000..c055e81a --- /dev/null +++ b/tests/codegen-bun/json_object_keys.affine @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: MPL-2.0 +// hpm_json_object_len / hpm_json_object_key (json.affine v0.4): object +// members are enumerable, so `to_json` materialises objects instead of +// returning None, and `stringify` round-trips them in member order. + +use prelude::{ Option, Some, None }; +use json::{ Json, parse, to_json, stringify, hpm_json_object_len, + hpm_json_object_key, HpmJsonValue }; + +pub fn roundtrip(src: String) -> String { + match parse(src) { + Some(h) => match to_json(h) { Some(j) => stringify(j), None => "" }, + None => "" + } +} + +pub fn keys(src: String) -> [String] { + let mut out = []; + match parse(src) { + Some(h) => { + let n = hpm_json_object_len(h); + let mut i = 0; + while i < n { + match hpm_json_object_key(h, i) { Some(k) => { out = out ++ [k]; }, None => {} }; + i = i + 1; + } + }, + None => {} + }; + out +} + +pub fn key_at(src: String, i: Int) -> String { + match parse(src) { + Some(h) => match hpm_json_object_key(h, i) { Some(k) => k, None => "" }, + None => "" + } +} + +pub fn len_of(src: String) -> Int { + match parse(src) { Some(h) => hpm_json_object_len(h), None => -2 } +} diff --git a/tests/codegen-bun/json_object_keys.harness.mjs b/tests/codegen-bun/json_object_keys.harness.mjs new file mode 100644 index 00000000..f7f338d3 --- /dev/null +++ b/tests/codegen-bun/json_object_keys.harness.mjs @@ -0,0 +1,37 @@ +// SPDX-License-Identifier: MPL-2.0 +import * as subject from "./json_object_keys.bun.js"; + +const expect = (label, got, want) => { + const g = JSON.stringify(got), w = JSON.stringify(want); + if (g !== w) throw new Error(`${label}: got ${g}, want ${w}`); +}; +expect("flat object keys in order", subject.keys('{"b":1,"a":2,"c":3}'), ["b", "a", "c"]); +expect("nested object round-trip", subject.roundtrip('{"p":{"q":[1,{"r":null}],"s":"t"},"u":true}'), + '{"p":{"q":[1,{"r":null}],"s":"t"},"u":true}'); +expect("empty object", subject.roundtrip("{}"), "{}"); +expect("escaped key", subject.roundtrip('{"a\\"b":1}'), '{"a\\"b":1}'); +expect("length of object", subject.len_of('{"x":1,"y":2}'), 2); +expect("length of array is 0", subject.len_of("[1,2]"), 0); +expect("length of scalar is 0", subject.len_of("5"), 0); +expect("key out of range", subject.key_at('{"x":1}', 1), ""); +expect("negative index", subject.key_at('{"x":1}', -1), ""); +expect("key of non-object", subject.key_at("[1]", 0), ""); +expect("own keys only", subject.keys('{"__proto__":1,"constructor":2}'), ["__proto__", "constructor"]); +// stringify must emit valid JSON that reparses to the same value (escape_char +// used to return bare quote, backslash and control characters). +for (const v of ["q\"uote", "back\\slash", "nl\nx", "cr\rtab\t", "bs\bff\f", "c\u0001\u001f", "\u00e9 \u2028", ""]) { + const src = JSON.stringify({ k: v, [v]: [v] }); + const out = subject.roundtrip(src); + let back; + try { back = JSON.parse(out); } catch (e) { throw new Error(`invalid JSON for ${JSON.stringify(v)}: ${out}`); } + expect(`string escapes ${JSON.stringify(v)}`, back, JSON.parse(src)); +} +// Inherited enumerable keys are not members. +Object.prototype.inheritedProbe = 1; +try { expect("inherited key excluded", subject.keys('{"a":1}'), ["a"]); } +finally { delete Object.prototype.inheritedProbe; } +// JSON has no Infinity: a number that parsed to one does not convert. +expect("non-finite member", subject.roundtrip('{"n":1e400}'), ""); +expect("non-finite element", subject.roundtrip("[1e400]"), ""); +expect("finite exponent", subject.roundtrip("[1e3]"), "[1000]"); +console.log("json_object_keys: ok");