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
20 changes: 20 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ kept so existing scripts fail loudly (Refs #56)

==== Added

* 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 +111,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
291 changes: 254 additions & 37 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>;
19 changes: 11 additions & 8 deletions stdlib/json.affine
Original file line number Diff line number Diff line change
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 Down Expand Up @@ -274,7 +274,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
1 change: 1 addition & 0 deletions test/test_main.ml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ 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);
("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
59 changes: 59 additions & 0 deletions test/test_runtime_prune.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
(* SPDX-License-Identifier: MPL-2.0 *)
(* SPDX-FileCopyrightText: 2026 hyperpolymath *)

(** Runtime pruning ([Codegen_deno.prune_runtime]) relies on the runtime's
shape: a sequence of top-level [const] declarations whose continuation
lines are indented or close a bracket, with no helper reached by a
computed name. These tests hold the runtime to that shape, so a helper
written another way fails here instead of being pruned wrongly. *)

open Affinescript

let runtime = Codegen_deno.bun_host_prelude ^ Codegen_deno.common_prelude

let test_shape () =
String.split_on_char '\n' runtime
|> List.iteri (fun i line ->
let t = String.trim line in
let ok =
t = ""
|| (String.length t >= 2 && String.sub t 0 2 = "//")
|| Codegen_deno.declared_name line <> None
|| line.[0] = ' ' || line.[0] = '\t'
|| line.[0] = '}' || line.[0] = ')' || line.[0] = ']'
in
if not ok then
Alcotest.failf "runtime line %d is neither a const declaration nor a continuation: %s" (i + 1) line)

let test_no_computed_access () =
List.iter (fun bad ->
let n = String.length bad in
let rec find i =
i + n <= String.length runtime && (String.sub runtime i n = bad || find (i + 1)) in
if find 0 then Alcotest.failf "runtime reaches something by computed name: %s" bad)
[ "eval("; "new Function"; "globalThis[\"__as"; "globalThis['__as"; "\"__as_\" +"; "'__as_' +" ]

let kept body name =
let out = Codegen_deno.prune_runtime runtime ~body in
List.mem name (List.filter_map Codegen_deno.declared_name (String.split_on_char '\n' out))

let test_reachability () =
Alcotest.(check bool) "direct use kept" true (kept "x = Some(1);" "Some");
Alcotest.(check bool) "transitive use kept" true (kept "__as_hpmJsonParse(s)" "None");
Alcotest.(check bool) "unused DOM sink dropped" false (kept "x = Some(1);" "__as_bodySetInnerHTML");
Alcotest.(check bool) "used DOM sink kept" true (kept "__as_bodySetInnerHTML(h)" "__as_bodySetInnerHTML")

let test_order_preserved () =
let names s = List.filter_map Codegen_deno.declared_name (String.split_on_char '\n' s) in
let all = names runtime in
let pruned = names (Codegen_deno.prune_runtime runtime ~body:"__as_hpmJsonParse(s); __as_urlParse(u)") in
let rec subseq xs ys = match xs, ys with
| [], _ -> true | _, [] -> false
| x :: xs', y :: ys' -> if x = y then subseq xs' ys' else subseq xs ys' in
Alcotest.(check bool) "kept helpers keep their order" true (subseq pruned all)

let tests =
[ ("runtime is const declarations and continuations", `Quick, test_shape);
("runtime reaches no helper by computed name", `Quick, test_no_computed_access);
("pruning keeps what the body reaches, transitively", `Quick, test_reachability);
("pruning preserves declaration order", `Quick, test_order_preserved) ]
3 changes: 3 additions & 0 deletions tests/codegen-bun/collision/a.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: MPL-2.0
module a;
pub fn pick(x: Int) -> Int { x + 1 }
3 changes: 3 additions & 0 deletions tests/codegen-bun/collision/b.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// SPDX-License-Identifier: MPL-2.0
module b;
pub fn pick(x: Int) -> Int { x * 100 }
9 changes: 9 additions & 0 deletions tests/codegen-bun/collision/main.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
// Regression: two modules' different public `pick` reach one flattened program
// transitively (main -> usea -> a::pick, main -> useb -> b::pick). Last-wins
// used to rebind usea's call to b::pick silently (viaA(1) returned 100, not
// 2). It must fail to compile; tools/run_codegen_bun_tests.sh checks that.
module main;
use usea::{ viaA };
use useb::{ viaB };
pub fn both() -> [Int] { [viaA(1), viaB(1)] }
4 changes: 4 additions & 0 deletions tests/codegen-bun/collision/usea.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MPL-2.0
module usea;
use a::{ pick };
pub fn viaA(x: Int) -> Int { pick(x) }
4 changes: 4 additions & 0 deletions tests/codegen-bun/collision/useb.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// SPDX-License-Identifier: MPL-2.0
module useb;
use b::{ pick };
pub fn viaB(x: Int) -> Int { pick(x) }
9 changes: 9 additions & 0 deletions tests/codegen-bun/diamond_internalised.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
// Regression: one declaration reaching a program by two paths is not a
// collision, even when the copies differ. This module imports `grade`
// without `is_big`, so its copy of `grade` calls an internalised name,
// while the copy carried through diamond_j calls `is_big`. The flattener
// must accept both as the same declaration (same source span).
use diamond_p::{ grade };
use diamond_j::{ twice };
pub fn both(x: Int) -> Int { grade(x) * 10 + twice(x) }
9 changes: 9 additions & 0 deletions tests/codegen-bun/diamond_internalised.harness.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// SPDX-License-Identifier: MPL-2.0
import * as subject from "./diamond_internalised.bun.js";

const expect = (label, got, want) => {
if (got !== want) throw new Error(`${label}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`);
};
expect("small", subject.both(3), 12);
expect("big", subject.both(30), 24);
console.log("diamond_internalised: ok");
6 changes: 6 additions & 0 deletions tests/codegen-bun/diamond_j.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// SPDX-License-Identifier: MPL-2.0
// Helper module for diamond_internalised.affine: binds is_big under its own
// name, so the copy of `grade` it carries calls `is_big`.
module diamond_j;
use diamond_p::{ is_big, grade };
pub fn twice(x: Int) -> Int { grade(x) * 2 }
5 changes: 5 additions & 0 deletions tests/codegen-bun/diamond_p.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// SPDX-License-Identifier: MPL-2.0
// Helper module for diamond_internalised.affine.
module diamond_p;
pub fn is_big(x: Int) -> Bool { x > 10 }
pub fn grade(x: Int) -> Int { if is_big(x) { 2 } else { 1 } }
16 changes: 16 additions & 0 deletions tests/codegen-bun/json_escapes.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MPL-2.0
// json::stringify must emit valid JSON (escape_char used to return the bare
// quote, backslash and control characters), and to_json must reject numbers
// JSON cannot represent.

use prelude::{ Option, Some, None };
use json::{ Json, JArray, JString, parse, to_json, stringify };

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 quote(s: String) -> String { stringify(JArray([JString(s)])) }
18 changes: 18 additions & 0 deletions tests/codegen-bun/json_escapes.harness.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// SPDX-License-Identifier: MPL-2.0
import * as subject from "./json_escapes.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}`);
};
for (const v of ["q\"uote", "back\\slash", "nl\nx", "cr\rtab\t", "bs\bff\f", "c\u0001\u001f", "é 
", ""]) {

Check warning on line 8 in tests/codegen-bun/json_escapes.harness.mjs

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Multiline support is limited to browsers supporting ES5 only.

See more on https://sonarcloud.io/project/issues?id=hyperpolymath_affinescript&issues=AaHvmvRPQX2Spxye-Klo&open=AaHvmvRPQX2Spxye-Klo&pullRequest=798

Check warning on line 8 in tests/codegen-bun/json_escapes.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=AaHvmvRPQX2Spxye-Kln&open=AaHvmvRPQX2Spxye-Kln&pullRequest=798
const out = subject.quote(v);
let back;
try { back = JSON.parse(out); } catch (e) { throw new Error(`invalid JSON for ${JSON.stringify(v)}: ${out}`); }
expect(`escapes ${JSON.stringify(v)}`, back, [v]);
expect(`array round-trip ${JSON.stringify(v)}`, JSON.parse(subject.roundtrip(JSON.stringify([v, [v]]))), [v, [v]]);
}
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>");
console.log("json_escapes: ok");
Loading
Loading