Skip to content
Open
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
24 changes: 24 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,24 @@ 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`).
* Module flattening: when two modules' different public declarations of one name
reached a program transitively (`M` imports `U` and `V`, which import `A::f`
and `B::f`), last-wins kept one and silently rebound the other module's calls
to it. Now an `ImportCollision` error, as private collisions already were
(`tests/codegen-bun/collision`).
* 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 never classed as an
`Int` division operand (`tests/codegen-bun/local_shadows_builtin`).
* fix(vscode-smoke): SKIP cleanly when @hyperpolymath/affine-vscode is
unpublished (#381)
* fix(governance): rename CLAUDE.md TypeScript exemptions heading to
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
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
321 changes: 281 additions & 40 deletions lib/codegen_deno.ml

Large diffs are not rendered by default.

40 changes: 27 additions & 13 deletions lib/module_loader.ml
Original file line number Diff line number Diff line change
Expand Up @@ -514,34 +514,48 @@ let rec flatten_imports_from
let imported_by_name = Hashtbl.create 32 in
let imported_origin = Hashtbl.create 32 in
let imported_order = ref [] in
let is_private = function
| `Fn fd -> fd.fd_vis = Private
| `Const (TopConst { tc_vis; _ }) -> tc_vis = Private
| `Type (TopType td) -> td.td_vis = Private
| `Const _ | `Type _ -> false
in
(* The same declaration reaches here more than once in a diamond (`D`
imports `P` and `J`, and `J` imports `P`): flattening is recursive, so
`P`'s helpers arrive both directly and inside `J`. That is one helper,
not a collision. *)
let same_decl a b = match a, b with
(* Copies of one source declaration can differ: an importer that does not
bind a carried public helper under its own name internalises it (renames
it and rewrites the calls to it), so `strMember` arrives once calling
`isStr` and once calling `__priv_5jsonv__isStr`. Both are the same
declaration, and each copy's callees are carried with it. Renaming keeps
the name's span, so a shared real source span identifies the
declaration. *)
let decl_span = function
| `Fn fd -> Some fd.fd_name.span
| `Const (TopConst { tc_name; _ }) -> Some tc_name.span
| `Type (TopType td) -> Some td.td_name.span
| `Const _ | `Type _ -> None
in
let same_source a b =
match decl_span a, decl_span b with
| Some s, Some t -> s.Span.file <> Span.dummy.file && Span.equal s t
| _ -> false
in
let same_decl a b = (match a, b with
| `Fn x, `Fn y -> x == y || x = y
| `Const x, `Const y | `Type x, `Type y -> x == y || x = y
| _ -> false
| _ -> false) || same_source a b
in
let add_imported origin name decl_kind =
if not (Hashtbl.mem local_names name) then begin
(match Hashtbl.find_opt imported_origin name,
Hashtbl.find_opt imported_by_name name with
| Some other, Some prev
when other <> origin
&& (is_private prev || is_private decl_kind)
&& not (same_decl prev decl_kind) ->
(* Two modules each carry a private helper of this name: one
(* Two modules each carry a different declaration of this name: one
flattened program can hold only one, and keeping the later would
make the other module call the wrong function. (Public
collisions are rejected by the resolver; #743's last-wins rule
still applies to them.) *)
make the other module call the wrong function. This holds for
public declarations too. The resolver rejects a collision only
within one module's imports; one that arrives transitively (`M`
imports `U` and `V`, which import `A::f` and `B::f`) reaches
here, and the old last-wins rule silently rebound `U`'s calls
to `B::f`. Fail loudly instead; the fix is to rename one. *)
raise (Flatten_error (ImportCollision (name, other, origin)))
| _ -> ());
if not (Hashtbl.mem imported_by_name name) then
Expand Down
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
32 changes: 32 additions & 0 deletions test/test_int_scope_bun.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
(* 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 check_divisions 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 ->
Alcotest.(check int) "only the Int division truncates" 1
(Test_int_div_js.count_occurrences ~needle:"Math.trunc(" js)

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
}|});
]
2 changes: 2 additions & 0 deletions test/test_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,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