Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions docs/specs/zig-ffi-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -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).

Expand Down
22 changes: 22 additions & 0 deletions lib/codegen_deno.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down
66 changes: 52 additions & 14 deletions stdlib/json.affine
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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
Expand All @@ -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;
Expand All @@ -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<HpmJsonValue>;
/// 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<String>;
pub extern fn hpm_json_array_len(val: HpmJsonValue) -> Int;
pub extern fn hpm_json_array_get(val: HpmJsonValue, idx: Int) -> Option<HpmJsonValue>;
pub extern fn hpm_json_escape_string(src: String) -> String;
Expand All @@ -256,11 +266,8 @@ pub fn parse(src: String) -> Option<HpmJsonValue> {
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
Expand Down Expand Up @@ -305,8 +312,39 @@ pub fn to_json(val: HpmJsonValue) -> Option<Json> {
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
}
Expand Down
2 changes: 1 addition & 1 deletion tests/codegen-bun/json_escapes.harness.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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]"), "<none>");
expect("finite exponent", subject.roundtrip("[1e3]"), "[1000]");
expect("objects need key enumeration, not on this branch", subject.roundtrip('{"a":1}'), "<none>");
expect("objects convert", subject.roundtrip('{"a":1}'), '{"a":1}');
console.log("json_escapes: ok");
42 changes: 42 additions & 0 deletions tests/codegen-bun/json_object_keys.affine
Original file line number Diff line number Diff line change
@@ -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>" },
None => "<parse-fail>"
}
}

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>" },
None => "<parse-fail>"
}
}

pub fn len_of(src: String) -> Int {
match parse(src) { Some(h) => hpm_json_object_len(h), None => -2 }
}
37 changes: 37 additions & 0 deletions tests/codegen-bun/json_object_keys.harness.mjs
Original file line number Diff line number Diff line change
@@ -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}');

Check warning on line 12 in tests/codegen-bun/json_object_keys.harness.mjs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

`String.raw` should be used to avoid escaping `\`.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_affinescript&issues=AaHvm0XByiFRUiQP7bgb&open=AaHvm0XByiFRUiQP7bgb&pullRequest=799

Check warning on line 12 in tests/codegen-bun/json_object_keys.harness.mjs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

`String.raw` should be used to avoid escaping `\`.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_affinescript&issues=AaHvm0XByiFRUiQP7bga&open=AaHvm0XByiFRUiQP7bga&pullRequest=799
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), "<none>");
expect("negative index", subject.key_at('{"x":1}', -1), "<none>");
expect("key of non-object", subject.key_at("[1]", 0), "<none>");
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", ""]) {

Check warning on line 22 in tests/codegen-bun/json_object_keys.harness.mjs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

`String.raw` should be used to avoid escaping `\`.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_affinescript&issues=AaHvm0XByiFRUiQP7bgc&open=AaHvm0XByiFRUiQP7bgc&pullRequest=799
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;

Check warning on line 30 in tests/codegen-bun/json_object_keys.harness.mjs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Object prototype is read only, properties should not be added.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_affinescript&issues=AaHvm0XByiFRUiQP7bgd&open=AaHvm0XByiFRUiQP7bgd&pullRequest=799
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}'), "<none>");
expect("non-finite element", subject.roundtrip("[1e400]"), "<none>");
expect("finite exponent", subject.roundtrip("[1e3]"), "[1000]");
console.log("json_object_keys: ok");
Loading