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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,5 @@ tools/vendor/
# affinescript-tea browser test deps
/affinescript-tea/e2e/node_modules/
/affinescript-router/e2e/node_modules/
/tests/codegen-bun/collision/*.log
/tests/codegen-bun/collision/*.bun.js
26 changes: 26 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ 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
against prelude `List<T>` (kind mismatch). Harness actually imports
`smokeIdaptikReach` / Assets mock. Smokes compile + run (Refs #760)
Expand Down Expand Up @@ -109,6 +115,26 @@ pure-extern semantics (Closes #332) (#338)

==== Fixed

* Bun-ESM: every module carried the whole runtime (~300 host helpers),
including a `document.body.innerHTML` setter that add-on linters flag. Only
the helpers a module reaches are emitted now, in their original order
(`test/test_runtime_prune.ml`).
* json: `stringify` emitted invalid JSON for strings containing `"`, `\`
or a control character: `escape_char` returned the bare character instead
of its two-character escape. Now round-trips (`tests/codegen-bun/json_escapes`).
* Bun-ESM: a local binding named like a host intrinsic (`get`, `len`,
`trim`, `show`, ...) now shadows it. A parameter `get` compiled to the
ref-cell builtin and crashed at run time. Parameters, `let`, `for`, lambda,
match-arm and `catch` binders are scoped, a block's `let`s end with the block,
a source block keeps its braces, and a call to a local is classed as an `Int`
division operand only by its own declared type (`len: fn(String) -> Int`),
never by the intrinsic it shadows (`tests/codegen-bun/local_shadows_builtin`,
`test/test_int_scope_bun.ml`).
* Bun-ESM: an `Int` assigned inside a nested block to an enclosing variable
was forgotten at the block's end, so a later `n / 2` did not truncate.
* json (Bun-ESM): `hpm_json_object_key` cached an object's keys for the
length of a walk, so after the host deleted a member it still returned the
deleted name. It now reads the live object (`tests/codegen-bun/json_object_keys`).
* fix(modules,codegen): same-named functions in two modules no longer
collapse to one in a flattened program (Bun-ESM, JS and the other
non-Wasm backends). Every function and constant of an imported module is
Expand Down
16 changes: 16 additions & 0 deletions docs/specs/zig-ffi-patterns.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,22 @@ 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. Stubs are safe only for imports that are never called;
alternatively, import `json` selectively (`use json::{encode_int}` imports
only what it names). A length stub returning 0 makes `to_json` convert
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
even a non-empty object to `Some(JObject([]))` without calling
`hpm_json_object_key`, so a key stub returning `None` does not reject it.
Object conversion requires working host implementations of both accessors
or an explicit rejection path before object conversion proceeds.
* 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
398 changes: 356 additions & 42 deletions lib/codegen_deno.ml

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions stdlib/Url.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 hyperpolymath
//
// Url.affine — URL parsing and reference resolution.
//
// Semantics are the WHATWG URL Standard's, because the hosts these programs
// run in (browsers, extensions, Bun, Deno) resolve URLs that way, and code
// that compares or rewrites URLs must agree with them. WHATWG differs from
// RFC 3986 in places (backslashes, default ports, host normalisation), so a
// hand-rolled RFC 3986 resolver would silently disagree with the host.
//
// Portability: both externs are lowered by the ESM backend
// (lib/codegen_deno.ml) to the host's `URL` constructor. There is no native
// lowering yet; a native backend must supply a WHATWG-conformant parser
// before these can be used there.

module Url;

use prelude::{Option, Some, None};

/// The components of a parsed absolute URL, as WHATWG's URL API reports
/// them: `protocol` keeps its trailing ":", `search` its leading "?" and
/// `hash` its leading "#"; absent parts are "".
pub type UrlParts = {
href: String,
protocol: String,
username: String,
password: String,
host: String,
hostname: String,
port: String,
pathname: String,
search: String,
hash: String
}

/// Resolve `href` against `base`. `base` "" means no base, so `href` must
/// itself be absolute. `None` when the result is not a valid URL.
pub extern fn url_resolve(href: String, base: String) -> Option<String>;

/// Parse an absolute URL. `None` when `s` is not one.
pub extern fn url_parse(s: String) -> Option<UrlParts>;
85 changes: 63 additions & 22 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 @@ -175,19 +175,19 @@ fn hex_digit(n: Int) -> String {
fn escape_char(s: String, i: Int) -> String {
let code = char_to_int(string_get(s, i));
if code == 34 {
"\""
"\\\""
} else if code == 92 {
"\\"
"\\\\"
} else if code == 8 {
"\b"
"\\b"
} else if code == 12 {
"\f"
"\\f"
} else if code == 10 {
"\n"
"\\n"
} else if code == 13 {
"\r"
"\\r"
} else if code == 9 {
"\t"
"\\t"
} else if code < 32 {
let hi = code / 16;
let lo = code - hi * 16;
Expand All @@ -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 All @@ -274,7 +281,10 @@ pub fn to_json(val: HpmJsonValue) -> Option<Json> {
} else if t == 2 {
Some(JInt(hpm_json_int(val)))
} else if t == 3 {
Some(JFloat(hpm_json_float(val)))
// JSON has no Infinity or NaN: a value that parsed to one (`1e400`)
// has no JSON form, so it does not convert.
let f = hpm_json_float(val);
if f - f == 0.0 { Some(JFloat(f)) } else { None }
} else if t == 4 {
Some(JString(hpm_json_string(val)))
} else if t == 5 {
Expand Down Expand Up @@ -302,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
74 changes: 74 additions & 0 deletions test/test_int_scope_bun.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
(* SPDX-License-Identifier: MPL-2.0 *)
open Affinescript

(* Exercise emission directly: the frontend currently rejects nested
statement bindings that shadow an enclosing binding with another type. *)
let count_truncs src =
let prog = Parse_driver.parse_string ~file:"<int_scope>" src in
let loader = Module_loader.create (Module_loader.default_config ()) in
let ctx = match Resolve.resolve_program_with_loader prog loader with
| Ok (ctx, _) -> ctx
| Error (e, _) -> Alcotest.fail (Resolve.show_resolve_error e)
in
match Codegen_deno.codegen_bun prog ctx.symbols with
| Error e -> Alcotest.fail e
| Ok js -> Test_int_div_js.count_occurrences ~needle:"Math.trunc(" js

let expect_truncs what n src =
Alcotest.(check int) what n (count_truncs src)

let check_divisions = expect_truncs "only the Int division truncates" 1

let tests = [
Alcotest.test_case "inner Int preserves outer Float" `Quick (fun () ->
check_divisions {|pub fn f() -> Float {
let x = 7.0; let y = 2.0;
{ let x = 9; let y = 2; x / y; };
x / y
}|});
Alcotest.test_case "inner Float preserves outer Int" `Quick (fun () ->
check_divisions {|pub fn f() -> Int {
let x = 7;
{ let x = 9.0; x / 2.0; };
x / 2
}|});
(* An assignment in a nested block changes the enclosing variable, so the
Int it records outlives the block. *)
Alcotest.test_case "Int assigned in a block to an enclosing var" `Quick (fun () ->
expect_truncs "the division after the block truncates" 1
{|pub fn f(flag: Bool) -> Int {
let mut n = match flag { true => 5, false => 7 };
{ n = 5; };
n / 2
}|});
(* ... but not when the block declared its own binding of that name. *)
Alcotest.test_case "Int assigned to a block's own binding" `Quick (fun () ->
expect_truncs "the outer Float division does not truncate" 0
{|pub fn f() -> Float {
let mut n = 7.0;
{ let mut n = 0.0; n = 5; };
n / 2.0
}|});
(* A lambda parameter is scoped to the lambda: an Int assigned to it does
not classify a same-named outer Float. *)
Alcotest.test_case "Int assigned to a lambda parameter" `Quick (fun () ->
expect_truncs "the outer Float division does not truncate" 0
{|pub fn f() -> Float {
let k = 7.0;
let g = |k| { k = 4; 0 };
k / 2.0
}|});
(* A callable local is classified by its own declared return type. *)
Alcotest.test_case "Int-returning callable parameter" `Quick (fun () ->
expect_truncs "len(s) / 2 truncates" 1
{|pub fn f(len: fn(String) -> Int) -> Int { len("abcde") / 2 }|});
Alcotest.test_case "Float-returning callable shadows intrinsic" `Quick (fun () ->
expect_truncs "len(s) / 2.0 does not truncate" 0
{|pub fn f(len: fn(String) -> Float) -> Float { len("abcde") / 2.0 }|});
Alcotest.test_case "Int-returning callable let" `Quick (fun () ->
expect_truncs "w(s) / 2 truncates" 1
{|pub fn f() -> Int {
let w: fn(String) -> Int = |s| 5;
w("abcde") / 2
}|});
]
2 changes: 2 additions & 0 deletions test/test_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ let () =
("Module mut (#548)", Test_module_mut.tests);
("Int-div on JS-text backend (#478)", Test_int_div_js.tests);
("Deno builtins ↔ stdlib decls consistency", Test_deno_builtins_consistency.tests);
("Bun integer block scope", Test_int_scope_bun.tests);
("ESM runtime pruning", Test_runtime_prune.tests);
("Stdlib algebraic laws (batch 1)", Test_stdlib_laws.tests);
("Solo CESK (VM M1, ADR-0025)", Test_solo_cesk.tests);
("Polonius solver (ADR-022 M3)", Test_borrow_polonius.tests);
Expand Down
Loading
Loading