From 902d3c5cd90f6d4bd6dc06f442e32a5548fd9015 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:33:55 +0000 Subject: [PATCH 1/7] feat(stdlib): JSON object keys and Url; fix stringify escapes and local shadowing json: add hpm_json_object_len / hpm_json_object_key, 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). fix(json): stringify emitted invalid JSON for strings containing a quote, backslash or control character; escape_char returned the bare character. fix(codegen): a local binding named like a host intrinsic (get, len, trim, show, ...) now shadows it. Parameters, let, for, lambda, match-arm and catch binders are tracked with lexical scope; a let's initializer still sees the intrinsic. Tests: tests/codegen-bun/{json_object_keys,local_shadows_builtin,url_module}. Each was checked against a planted fault. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- CHANGELOG.adoc | 13 ++ lib/codegen_deno.ml | 137 +++++++++++++++--- stdlib/Url.affine | 42 ++++++ stdlib/json.affine | 80 +++++++--- tests/codegen-bun/json_object_keys.affine | 42 ++++++ .../codegen-bun/json_object_keys.harness.mjs | 29 ++++ .../codegen-bun/local_shadows_builtin.affine | 36 +++++ .../local_shadows_builtin.harness.mjs | 14 ++ tests/codegen-bun/url_module.affine | 13 ++ tests/codegen-bun/url_module.harness.mjs | 14 ++ 10 files changed, 377 insertions(+), 43 deletions(-) create mode 100644 stdlib/Url.affine create mode 100644 tests/codegen-bun/json_object_keys.affine create mode 100644 tests/codegen-bun/json_object_keys.harness.mjs create mode 100644 tests/codegen-bun/local_shadows_builtin.affine create mode 100644 tests/codegen-bun/local_shadows_builtin.harness.mjs create mode 100644 tests/codegen-bun/url_module.affine create mode 100644 tests/codegen-bun/url_module.harness.mjs diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 414748a2..6e48f3b1 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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` (kind mismatch). Harness actually imports `smokeIdaptikReach` / Assets mock. Smokes compile + run (Refs #760) @@ -109,6 +115,13 @@ pure-extern semantics (Closes #332) (#338) ==== Fixed +* 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_object_keys`). +* 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 (`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 diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 42613c7b..b4edcc29 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -95,6 +95,12 @@ type codegen_ctx = { integer operand, so divisions over them truncate (#478). Reset per function alongside {!int_vars}. *) int_array_vars : (string, unit) Hashtbl.t; + (* Value names bound locally at the current emission point: parameters, + `let`, `for`, lambda and match-arm binders. A local shadows a host + intrinsic of the same name, so `fn f(get: ...) { get(x) }` calls the + parameter instead of the ref-cell builtin `get`. Reset per function; + scoped constructs restore it on exit (see {!with_locals}). *) + locals : (string, unit) Hashtbl.t; (* True while emitting a synthesised `async` method body. The expression-position IIFE wrappers (block/try/match/let/return) must then be `async` and awaited, because they may contain an awaited @@ -119,6 +125,7 @@ let create_ctx host symbols = { ctors = Hashtbl.create 32; int_vars = Hashtbl.create 16; int_array_vars = Hashtbl.create 16; + locals = Hashtbl.create 16; in_async = false; } @@ -608,6 +615,32 @@ const __as_hpmJsonObjectGet = (v, k) => { return Object.prototype.hasOwnProperty.call(v, String(k)) ? Some(v[String(k)]) : None; }; +// ---- Url (stdlib/Url.affine): WHATWG URL via the host's URL constructor ---- +const __as_urlResolve = (href, base) => { + try { + return Some(new URL(String(href), base === "" ? undefined : String(base)).href); + } catch (_e) { return None; } +}; +const __as_urlParse = (s) => { + let u; + try { u = new URL(String(s)); } catch (_e) { return None; } + return Some({ href: u.href, protocol: u.protocol, username: u.username, + 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; +const __as_hpmJsonObjectKey = (v, i) => { + const ks = __as_hpmJsonObjectKeys(v); + const idx = Number(i); + return (Number.isInteger(idx) && idx >= 0 && 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; @@ -1191,6 +1224,10 @@ let () = b "hpm_json_float" (fun a -> Printf.sprintf "__as_hpmJsonFloat(%s)" (arg 0 a)); b "hpm_json_string" (fun a -> Printf.sprintf "__as_hpmJsonString(%s)" (arg 0 a)); 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)); @@ -1436,7 +1473,35 @@ let enter_fn_scope ctx (params : param list) : codegen_ctx = else if type_is_int_array p.p_ty then Hashtbl.replace arr_tbl p.p_name.name ()) params; - { ctx with int_vars = tbl; int_array_vars = arr_tbl } + let locals = Hashtbl.create 16 in + List.iter (fun (p : param) -> Hashtbl.replace locals p.p_name.name ()) params; + { ctx with int_vars = tbl; int_array_vars = arr_tbl; locals } + +(* Run [f] with [names] bound as locals, then restore the previous bindings, + so a scoped binder (let body, lambda, match arm, loop) cannot shadow an + intrinsic outside its scope. *) +let with_locals : 'a. codegen_ctx -> string list -> (unit -> 'a) -> 'a = + fun ctx names f -> + let had = List.map (fun n -> (n, Hashtbl.mem ctx.locals n)) names in + List.iter (fun n -> Hashtbl.replace ctx.locals n ()) names; + let restore () = + List.iter (fun (n, h) -> if not h then Hashtbl.remove ctx.locals n) had + in + match f () with + | v -> restore (); v + | exception e -> restore (); raise e + +(* Snapshot [ctx.locals] around [f]: statement `let`s inside a nested block + stay local to it. *) +let with_locals_scope : 'a. codegen_ctx -> (unit -> 'a) -> 'a = fun ctx f -> + let saved = Hashtbl.copy ctx.locals in + let restore () = + Hashtbl.reset ctx.locals; + Hashtbl.iter (Hashtbl.replace ctx.locals) saved + in + match f () with + | v -> restore (); v + | exception e -> restore (); raise e (* ============================================================================ Expression code generation @@ -1484,7 +1549,8 @@ let rec gen_expr ctx (expr : expr) : string = "(await " ^ recv ^ "." ^ m ^ "(" ^ String.concat ", " rest ^ "))" | ExprVar id when Hashtbl.mem (builtins_for ctx) id.name - && not (Hashtbl.mem ctx.local_fns id.name) -> + && not (Hashtbl.mem ctx.local_fns id.name) + && not (Hashtbl.mem ctx.locals id.name) -> (* Honest host/runtime intrinsic (FS/JSON/Date/Wasm extern or a string/number primitive underpinning stdlib/string.affine). Applied to ANY matching call head, not only declared externs, @@ -1492,7 +1558,9 @@ let rec gen_expr ctx (expr : expr) : string = same-named user definition shadows it (e.g. a user `len`). *) (Hashtbl.find (builtins_for ctx) id.name) (List.map (gen_expr ctx) args) - | ExprVar id when Hashtbl.mem ctx.externs id.name -> + | ExprVar id + when Hashtbl.mem ctx.externs id.name + && not (Hashtbl.mem ctx.locals id.name) -> if ctx.host = Bun && String.starts_with ~prefix:"bun_" id.name then failwith (Printf.sprintf @@ -1564,7 +1632,9 @@ let rec gen_expr ctx (expr : expr) : string = else Hashtbl.remove ctx.int_vars n | None -> fun () -> () in - let b = gen_expr ctx body in + let b = + with_locals ctx (pattern_binders el_pat) (fun () -> gen_expr ctx body) + in restore (); kw ^ " " ^ pat_str ^ " = " ^ val_str ^ "; return " ^ b ^ ";" | None -> kw ^ " " ^ pat_str ^ " = " ^ val_str ^ "; return Unit;" @@ -1609,14 +1679,17 @@ let rec gen_expr ctx (expr : expr) : string = `(x) => (await ...)` is a SyntaxError in browsers — Bun's parser happens to accept it, which hid the bug. *) let lctx = { ctx with in_async = false } in - if lowers_to_statements elam_body && expr_contains_return elam_body then - (* A `return` in the body returns from the lambda: emit a block - body so it is not swallowed by an IIFE (see {!gen_match}). *) - "((" ^ String.concat ", " ps ^ ") => { " - ^ gen_return_tail lctx elam_body ^ " })" - else - "((" ^ String.concat ", " ps ^ ") => " - ^ gen_expr lctx elam_body ^ ")" + let names = List.map (fun (p : param) -> p.p_name.name) elam_params in + with_locals lctx names (fun () -> + with_locals_scope lctx (fun () -> + if lowers_to_statements elam_body && expr_contains_return elam_body then + (* A `return` in the body returns from the lambda: emit a block + body so it is not swallowed by an IIFE (see {!gen_match}). *) + "((" ^ String.concat ", " ps ^ ") => { " + ^ gen_return_tail lctx elam_body ^ " })" + else + "((" ^ String.concat ", " ps ^ ") => " + ^ gen_expr lctx elam_body ^ ")")) | ExprTry { et_body; et_catch; et_finally } -> gen_try ctx et_body et_catch et_finally | ExprVariant (ty, ctor) -> @@ -1701,6 +1774,7 @@ and gen_match ctx scrutinee arms = | arm :: rest -> let cond = gen_pattern_test scrut_var arm.ma_pat in let bindings = gen_pattern_bindings scrut_var arm.ma_pat in + with_locals ctx (pattern_binders arm.ma_pat) @@ fun () -> let body = gen_expr ctx arm.ma_body in let prefix = if bindings = "" then @@ -1798,6 +1872,7 @@ and gen_pattern_bindings scrut pat = Buffer.contents buf and gen_block_expr ctx block = + with_locals_scope ctx @@ fun () -> let body = Buffer.create 64 in List.iter (fun s -> Buffer.add_string body (gen_stmt ctx s); @@ -1817,7 +1892,9 @@ and gen_try ctx body catch finally = | PatVar id -> "const " ^ mangle id.name ^ " = __e; " | _ -> "" in - "catch (__e) { " ^ bind ^ "return " ^ gen_expr ctx arm.ma_body ^ "; }" + "catch (__e) { " ^ bind ^ "return " + ^ with_locals ctx (pattern_binders arm.ma_pat) (fun () -> gen_expr ctx arm.ma_body) + ^ "; }" in let finally_str = match finally with | None -> "" @@ -1880,10 +1957,14 @@ and gen_match_stmt ctx scrut arms = | arm :: rest -> let cond = gen_pattern_test sv arm.ma_pat in let binds = gen_pattern_bindings sv arm.ma_pat in - let guard = match arm.ma_guard with - | Some g -> " && (" ^ gen_expr ctx g ^ ")" | None -> "" in - "if (" ^ cond ^ guard ^ ") { " ^ binds - ^ gen_branch ctx arm.ma_body ^ " } else " ^ arms_js rest + let arm_js = + with_locals ctx (pattern_binders arm.ma_pat) (fun () -> + let guard = match arm.ma_guard with + | Some g -> " && (" ^ gen_expr ctx g ^ ")" | None -> "" in + "if (" ^ cond ^ guard ^ ") { " ^ binds + ^ gen_branch ctx arm.ma_body ^ " } else ") + in + arm_js ^ arms_js rest in "{ const " ^ sv ^ " = " ^ gen_expr ctx scrut ^ "; " ^ arms_js arms ^ " }" @@ -1938,6 +2019,7 @@ and gen_tail_to ctx (sink : string -> string) e = let arm_js arm = let cond = gen_pattern_test sv arm.ma_pat in let binds = gen_pattern_bindings sv arm.ma_pat in + with_locals ctx (pattern_binders arm.ma_pat) @@ fun () -> (* The guard is emitted before the body, against the integer tracking the guard actually runs under. *) let guard = Option.map (gen_expr ctx) arm.ma_guard in @@ -1969,7 +2051,7 @@ and with_int_scope : 'a. codegen_ctx -> (unit -> 'a) -> 'a = fun ctx f -> Hashtbl.reset ctx.int_vars; Hashtbl.iter (Hashtbl.replace ctx.int_vars) saved in - match f () with + match with_locals_scope ctx f with | v -> restore (); v | exception e -> restore (); raise e @@ -1998,7 +2080,9 @@ and gen_try_stmt ctx body catch finally = let bind = match arm.ma_pat with | PatVar id -> "const " ^ mangle id.name ^ " = __e; " | _ -> "" in - "catch (__e) { " ^ bind ^ gen_branch ctx arm.ma_body ^ " }" + "catch (__e) { " ^ bind + ^ with_locals ctx (pattern_binders arm.ma_pat) (fun () -> gen_branch ctx arm.ma_body) + ^ " }" in let f = match finally with | None -> "" | Some blk -> " finally { " ^ gen_stmt_seq ctx blk ^ " }" @@ -2047,6 +2131,9 @@ and gen_stmt ctx (stmt : stmt) : string = (match pat_var_name sl_pat with | Some n -> track_int_binding ctx n sl_value | None -> ()); + (* Bound after its initializer is emitted: `let len = len(s);` still + calls the intrinsic. Lasts to the end of the enclosing scope. *) + List.iter (fun n -> Hashtbl.replace ctx.locals n ()) (pattern_binders sl_pat); js | StmtExpr e -> gen_stmt_expr ctx e | StmtAssign (lhs, op, rhs) -> @@ -2082,9 +2169,13 @@ and gen_stmt ctx (stmt : stmt) : string = before the `let`s they depend on, and `(lo + hi) / 2` lost its truncation. *) let cond_js = gen_expr ctx cond in - let stmts_js = String.concat " " (List.map (gen_stmt ctx) body.blk_stmts) in - let tail_js = match body.blk_expr with - | Some e -> " " ^ gen_stmt_expr ctx e | None -> "" in + let stmts_js, tail_js = + with_locals_scope ctx (fun () -> + let s = String.concat " " (List.map (gen_stmt ctx) body.blk_stmts) in + let t = match body.blk_expr with + | Some e -> " " ^ gen_stmt_expr ctx e | None -> "" in + (s, t)) + in "while (" ^ cond_js ^ ") { " ^ stmts_js ^ tail_js ^ " }" | StmtFor (pat, iter, body) -> (* The iterable is evaluated in the outer scope, so emit it first. *) @@ -2105,6 +2196,8 @@ and gen_stmt ctx (stmt : stmt) : string = | _ -> fun () -> () in let body_js = + with_locals ctx (pattern_binders pat) @@ fun () -> + with_locals_scope ctx @@ fun () -> (* Source order (see StmtWhile): statements before the tail. *) let stmts_js = String.concat " " (List.map (gen_stmt ctx) body.blk_stmts) in let tail_js = match body.blk_expr with diff --git a/stdlib/Url.affine b/stdlib/Url.affine new file mode 100644 index 00000000..34e34142 --- /dev/null +++ b/stdlib/Url.affine @@ -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; + +/// Parse an absolute URL. `None` when `s` is not one. +pub extern fn url_parse(s: String) -> Option; diff --git a/stdlib/json.affine b/stdlib/json.affine index 640f54fa..fdd1d5c2 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; @@ -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; @@ -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 +// 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 @@ -302,8 +309,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_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..86d042f9 --- /dev/null +++ b/tests/codegen-bun/json_object_keys.harness.mjs @@ -0,0 +1,29 @@ +// 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)); +} +console.log("json_object_keys: ok"); diff --git a/tests/codegen-bun/local_shadows_builtin.affine b/tests/codegen-bun/local_shadows_builtin.affine new file mode 100644 index 00000000..c611d0bb --- /dev/null +++ b/tests/codegen-bun/local_shadows_builtin.affine @@ -0,0 +1,36 @@ +// SPDX-License-Identifier: MPL-2.0 +// Regression: a local binding named like a host intrinsic (`get`, `len`, +// `trim`, `show`, ...) must shadow it. A parameter `get` used to compile to +// the ref-cell builtin, `("x").__cell`, and crash at run time (found while +// porting metadatastician/consent-aware-inspector). + +use prelude::{ Option, Some, None }; + +pub fn call_param(get: String -> Option) -> String { + match get("x") { Some(v) => v, None => "none" } +} + +// The initializer still calls the intrinsic; the body sees the local. +pub fn let_after_init(s: String) -> Int { + let len = len(s); + len + 1 +} + +pub fn lambda_param(s: String) -> String { + let f = fn(trim: String -> String) => trim(s); + f(fn(x: String) => x ++ "!") +} + +// A binder's scope ends with its arm, loop or lambda: the intrinsic is +// visible again afterwards in the same function. +pub fn scope_ends(s: String, xs: [String -> String]) -> String { + let mut out = ""; + for trim in xs { out = out ++ trim(s); } + let h = match Some(fn(z: String) => "[" ++ z ++ "]") { Some(trim) => trim(s), None => "" }; + out ++ h ++ trim(" t ") +} + +pub fn let_scope_ends(s: String) -> String { + let a = { let trim = fn(z: String) => z ++ "?"; trim(s) }; + a ++ trim(" u ") +} diff --git a/tests/codegen-bun/local_shadows_builtin.harness.mjs b/tests/codegen-bun/local_shadows_builtin.harness.mjs new file mode 100644 index 00000000..153f1aa6 --- /dev/null +++ b/tests/codegen-bun/local_shadows_builtin.harness.mjs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MPL-2.0 +import * as subject from "./local_shadows_builtin.bun.js"; + +const expect = (label, got, want) => { + if (got !== want) throw new Error(`${label}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`); +}; +expect("parameter shadows `get`", + subject.call_param((k) => ({ tag: "Some", value: "got " + k })), "got x"); +expect("`let len = len(s)` calls the intrinsic, then the local", subject.let_after_init("abc"), 4); +expect("lambda parameter shadows `trim`", subject.lambda_param(" a "), " a !"); +expect("loop and arm binders end with their scope", + subject.scope_ends(" s ", [(x) => x + "1", (x) => x + "2"]), " s 1 s 2[ s ]t"); +expect("block `let` ends with its block", subject.let_scope_ends("v"), "v?u"); +console.log("local_shadows_builtin: ok"); diff --git a/tests/codegen-bun/url_module.affine b/tests/codegen-bun/url_module.affine new file mode 100644 index 00000000..bd5cc704 --- /dev/null +++ b/tests/codegen-bun/url_module.affine @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MPL-2.0 +// stdlib/Url.affine on the ESM backend: WHATWG resolution and parsing. + +use prelude::{ Option, Some, None }; +use Url::{ url_resolve, url_parse, UrlParts }; + +pub fn resolve(h: String, b: String) -> String { + match url_resolve(h, b) { Some(u) => u, None => "" } +} + +pub fn path_of(s: String) -> String { + match url_parse(s) { Some(p) => p.protocol ++ "|" ++ p.host ++ "|" ++ p.pathname ++ "|" ++ p.search ++ "|" ++ p.hash, None => "" } +} diff --git a/tests/codegen-bun/url_module.harness.mjs b/tests/codegen-bun/url_module.harness.mjs new file mode 100644 index 00000000..adf1e243 --- /dev/null +++ b/tests/codegen-bun/url_module.harness.mjs @@ -0,0 +1,14 @@ +// SPDX-License-Identifier: MPL-2.0 +import * as subject from "./url_module.bun.js"; + +const expect = (label, got, want) => { + if (got !== want) throw new Error(`${label}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`); +}; +expect("relative against base", subject.resolve("../x?q#f", "https://a.example/b/c/d"), "https://a.example/b/x?q#f"); +expect("no base, absolute", subject.resolve("HTTPS://A.Example:443/p", ""), "https://a.example/p"); +expect("no base, relative fails", subject.resolve("/p", ""), ""); +expect("invalid base fails", subject.resolve("p", "not a url"), ""); +expect("WHATWG backslash", subject.resolve("\\x", "https://a.example/b/"), "https://a.example/x"); +expect("parts", subject.path_of("http://h.example:8080/a/%7e?x=1#y"), "http:|h.example:8080|/a/%7e|?x=1|#y"); +expect("parse rejects relative", subject.path_of("/a"), ""); +console.log("url_module: ok"); From ad46652645897002f96d24eadd7bb7d34ba7fe3f Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:46:52 +0000 Subject: [PATCH 2/7] fix(modules): reject transitive public name collisions when flattening When two modules' different public declarations of one name reached a program transitively (main imports usea and useb, which import a::pick and b::pick), flattening kept the last and silently rebound usea's calls to b::pick: viaA(1) returned 100 instead of 2, with every module type-checking. The resolver only rejects a collision inside one module's imports. Raise ImportCollision for public declarations too, as for private ones. Regression: tests/codegen-bun/collision, checked by tools/run_codegen_bun_tests.sh (fails on the previous compiler). Found porting metadatastician/consent-aware-inspector, where vocab::member and a JSON helper named member collided. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- .gitignore | 2 ++ CHANGELOG.adoc | 5 +++++ lib/module_loader.ml | 18 +++++++----------- tests/codegen-bun/collision/a.affine | 3 +++ tests/codegen-bun/collision/b.affine | 3 +++ tests/codegen-bun/collision/main.affine | 9 +++++++++ tests/codegen-bun/collision/usea.affine | 4 ++++ tests/codegen-bun/collision/useb.affine | 4 ++++ tools/run_codegen_bun_tests.sh | 10 ++++++++++ 9 files changed, 47 insertions(+), 11 deletions(-) create mode 100644 tests/codegen-bun/collision/a.affine create mode 100644 tests/codegen-bun/collision/b.affine create mode 100644 tests/codegen-bun/collision/main.affine create mode 100644 tests/codegen-bun/collision/usea.affine create mode 100644 tests/codegen-bun/collision/useb.affine diff --git a/.gitignore b/.gitignore index bcbc7208..b89185b5 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 6e48f3b1..e439097d 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -115,6 +115,11 @@ pure-extern semantics (Closes #332) (#338) ==== Fixed +* 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_object_keys`). diff --git a/lib/module_loader.ml b/lib/module_loader.ml index 13282b9a..a990e468 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -514,12 +514,6 @@ 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, @@ -535,13 +529,15 @@ let rec flatten_imports_from 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 diff --git a/tests/codegen-bun/collision/a.affine b/tests/codegen-bun/collision/a.affine new file mode 100644 index 00000000..dd5362bb --- /dev/null +++ b/tests/codegen-bun/collision/a.affine @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: MPL-2.0 +module a; +pub fn pick(x: Int) -> Int { x + 1 } diff --git a/tests/codegen-bun/collision/b.affine b/tests/codegen-bun/collision/b.affine new file mode 100644 index 00000000..acc6c045 --- /dev/null +++ b/tests/codegen-bun/collision/b.affine @@ -0,0 +1,3 @@ +// SPDX-License-Identifier: MPL-2.0 +module b; +pub fn pick(x: Int) -> Int { x * 100 } diff --git a/tests/codegen-bun/collision/main.affine b/tests/codegen-bun/collision/main.affine new file mode 100644 index 00000000..e2a11fa4 --- /dev/null +++ b/tests/codegen-bun/collision/main.affine @@ -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)] } diff --git a/tests/codegen-bun/collision/usea.affine b/tests/codegen-bun/collision/usea.affine new file mode 100644 index 00000000..ae48808b --- /dev/null +++ b/tests/codegen-bun/collision/usea.affine @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: MPL-2.0 +module usea; +use a::{ pick }; +pub fn viaA(x: Int) -> Int { pick(x) } diff --git a/tests/codegen-bun/collision/useb.affine b/tests/codegen-bun/collision/useb.affine new file mode 100644 index 00000000..d47aac92 --- /dev/null +++ b/tests/codegen-bun/collision/useb.affine @@ -0,0 +1,4 @@ +// SPDX-License-Identifier: MPL-2.0 +module useb; +use b::{ pick }; +pub fn viaB(x: Int) -> Int { pick(x) } diff --git a/tools/run_codegen_bun_tests.sh b/tools/run_codegen_bun_tests.sh index 9ab77a13..c6ea5a67 100755 --- a/tools/run_codegen_bun_tests.sh +++ b/tools/run_codegen_bun_tests.sh @@ -105,6 +105,16 @@ if "${COMPILE_CMD[@]}" "$TEST_DIR/unsupported_host.affine" \ fail "unsupported Bun host operation compiled successfully" fi +# 8. A transitive collision between two modules' different public +# declarations must be rejected, not resolved by last-wins. +collision_log="$TEST_DIR/collision/collision.log" +if "${COMPILE_CMD[@]}" "$TEST_DIR/collision/main.affine" \ + -o "$TEST_DIR/collision/main.bun.js" --bun-esm >"$collision_log" 2>&1; then + fail "transitive public name collision compiled successfully" +elif ! grep -q 'cannot flatten imports: `pick` is defined in both' "$collision_log"; then + fail "transitive collision rejection lacks the ImportCollision diagnostic" +fi + if [ "${#failures[@]}" -gt 0 ]; then echo "" echo "✗ ${#failures[@]} native Bun-ESM check(s) failed:" From 04a5c1aeaa5eb5d18dc968324f975d16c98abdd7 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 15:49:38 +0000 Subject: [PATCH 3/7] fix(modules): one declaration reached by two paths is not a collision The previous commit compared copies structurally. An importer that does not bind a carried public helper under its own name internalises it and rewrites the calls, so one source declaration can arrive as two structurally different copies (one calling `is_big`, one an internal name), and was wrongly reported as a collision. Identify a declaration by its name's real source span, which renaming preserves. Regression: tests/codegen-bun/diamond_internalised (fails on the previous commit, passes here); the collision/ negative check still fails to compile. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- lib/module_loader.ml | 22 +++++++++++++++++-- tests/codegen-bun/diamond_internalised.affine | 9 ++++++++ .../diamond_internalised.harness.mjs | 9 ++++++++ tests/codegen-bun/diamond_j.affine | 6 +++++ tests/codegen-bun/diamond_p.affine | 5 +++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/codegen-bun/diamond_internalised.affine create mode 100644 tests/codegen-bun/diamond_internalised.harness.mjs create mode 100644 tests/codegen-bun/diamond_j.affine create mode 100644 tests/codegen-bun/diamond_p.affine diff --git a/lib/module_loader.ml b/lib/module_loader.ml index a990e468..64bd5cba 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -518,10 +518,28 @@ let rec flatten_imports_from 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 diff --git a/tests/codegen-bun/diamond_internalised.affine b/tests/codegen-bun/diamond_internalised.affine new file mode 100644 index 00000000..b1026678 --- /dev/null +++ b/tests/codegen-bun/diamond_internalised.affine @@ -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) } diff --git a/tests/codegen-bun/diamond_internalised.harness.mjs b/tests/codegen-bun/diamond_internalised.harness.mjs new file mode 100644 index 00000000..fce9816e --- /dev/null +++ b/tests/codegen-bun/diamond_internalised.harness.mjs @@ -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"); diff --git a/tests/codegen-bun/diamond_j.affine b/tests/codegen-bun/diamond_j.affine new file mode 100644 index 00000000..df42134c --- /dev/null +++ b/tests/codegen-bun/diamond_j.affine @@ -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 } diff --git a/tests/codegen-bun/diamond_p.affine b/tests/codegen-bun/diamond_p.affine new file mode 100644 index 00000000..9e373125 --- /dev/null +++ b/tests/codegen-bun/diamond_p.affine @@ -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 } } From 7ca7731a3c4a2edef19a2d463b46b0cf6f155413 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 16:54:23 +0000 Subject: [PATCH 4/7] fix(codegen): emit only the runtime helpers a Bun-ESM module reaches Every module carried the whole runtime: ~300 host helpers (DOM, Pixi, Sqlite, ...), among them a document.body.innerHTML setter that web-ext lint reports as UNSAFE_VAR_ASSIGNMENT in any extension built with AffineScript. The body is now generated first; the runtime is split into its top-level const declarations and only those the body reaches, transitively by name, are emitted, in their original order. A name occurring anywhere in reaching text keeps a helper, so pruning errs towards keeping. test/test_runtime_prune.ml holds the runtime to the shape this relies on (only const declarations and continuations; nothing reached by a computed name) and checks reachability and order. Dropping transitive reachability fails it and the http_fetch corpus harness. Effect on metadatastician/consent-aware-inspector's core: 295 helpers to 24, 153 KB to 121 KB, and web-ext lint warnings 1 to 0; its full gate (unit, servers, browser e2e, Tauri, Idris2 equivalence) stays green. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- CHANGELOG.adoc | 4 ++ lib/codegen_deno.ml | 119 ++++++++++++++++++++++++++++++++++++- test/test_main.ml | 1 + test/test_runtime_prune.ml | 59 ++++++++++++++++++ 4 files changed, 180 insertions(+), 3 deletions(-) create mode 100644 test/test_runtime_prune.ml diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index e439097d..abfb1833 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -115,6 +115,10 @@ 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 diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index b4edcc29..97cdc57e 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -2423,6 +2423,112 @@ let gen_type_decl ctx (td : type_decl) : unit = If a top-level function is named [main], the module awaits its invocation. Emission exceptions, including [Failure] from [gen_expr], propagate; [codegen_bun] and [codegen_deno] convert them to [Error] results. *) +(* ---- Runtime pruning ---------------------------------------------------- + + The runtime preludes hold every host helper any program might need (DOM, + Pixi, Sqlite, ...). Emitting all of them made every module carry ~300 + helpers it never calls, among them a `document.body.innerHTML` setter that + add-on linters rightly flag. [prune_runtime] keeps a helper only if the + module's own code, or a kept helper, names it. + + The runtime is a sequence of top-level `const` declarations (checked by + [test/test_runtime_prune.ml]); continuation lines are indented or close a + bracket, and comment lines attach to the declaration that follows them. + A helper is reachable when its name occurs as an identifier anywhere in + the reaching text, comments and strings included, so pruning can only err + towards keeping. No runtime helper is reached by a computed name. Kept + helpers stay in their original order, so evaluation order is unchanged. *) + +let is_ident_start c = + (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c = '_' || c = '$' + +let is_ident_char c = is_ident_start c || (c >= '0' && c <= '9') + +(** Every identifier-shaped word in [s]. *) +let identifiers (s : string) : string list = + let n = String.length s in + let rec go i acc = + if i >= n then acc + else if is_ident_start s.[i] && (i = 0 || not (is_ident_char s.[i - 1])) then begin + let j = ref i in + while !j < n && is_ident_char s.[!j] do incr j done; + go !j (String.sub s i (!j - i) :: acc) + end else go (i + 1) acc + in + go 0 [] + +(** The name a runtime line declares, if it starts a top-level `const`. *) +let declared_name (line : string) : string option = + let p = "const " in + let lp = String.length p in + if String.length line > lp && String.sub line 0 lp = p + && is_ident_start line.[lp] then begin + let j = ref lp in + while !j < String.length line && is_ident_char line.[!j] do incr j done; + Some (String.sub line lp (!j - lp)) + end else None + +let is_comment_or_blank (line : string) : bool = + let t = String.trim line in + t = "" || (String.length t >= 2 && String.sub t 0 2 = "//") + +(** Split [runtime] into chunks: [(Some name, text)] per declaration (with + the comment lines before it), and [(None, text)] for lines that belong + to no declaration, which are always kept. *) +let runtime_chunks (runtime : string) : (string option * string) list = + let lines = String.split_on_char '\n' runtime in + let chunks = ref [] in + let current = ref None in (* (name, lines rev) of the open declaration *) + let pending = ref [] in (* comment lines awaiting a declaration *) + let close () = + (match !current with + | Some (name, ls) -> chunks := (Some name, String.concat "\n" (List.rev ls)) :: !chunks + | None -> ()); + current := None + in + List.iter (fun line -> + match declared_name line with + | Some name -> + close (); + current := Some (name, line :: !pending); + pending := [] + | None -> + if is_comment_or_blank line then pending := line :: !pending + else begin + (* A continuation line: comments seen since belong to it too. *) + match !current with + | Some (name, ls) -> current := Some (name, line :: (!pending @ ls)); pending := [] + | None -> chunks := (None, String.concat "\n" (List.rev (line :: !pending))) :: !chunks; + pending := [] + end) lines; + close (); + if !pending <> [] then + chunks := (None, String.concat "\n" (List.rev !pending)) :: !chunks; + List.rev !chunks + +(** [runtime] without the declarations [body] cannot reach. *) +let prune_runtime (runtime : string) ~(body : string) : string = + let chunks = runtime_chunks runtime in + let by_name = Hashtbl.create 512 in + List.iter (fun (n, text) -> + match n with Some name -> Hashtbl.replace by_name name text | None -> ()) chunks; + let kept = Hashtbl.create 512 in + let rec visit = function + | [] -> () + | id :: rest -> + if Hashtbl.mem by_name id && not (Hashtbl.mem kept id) then begin + Hashtbl.replace kept id (); + visit (identifiers (Hashtbl.find by_name id) @ rest) + end else visit rest + in + visit (identifiers body); + List.iter (fun (n, text) -> if n = None then visit (identifiers text)) chunks; + let out = List.filter_map (fun (n, text) -> + match n with + | None -> Some text + | Some name -> if Hashtbl.mem kept name then Some text else None) chunks in + String.concat "\n" out ^ "\n" + let generate (host : host_profile) (program : program) (symbols : Symbol.t) : string = let ctx = create_ctx host symbols in (* Register extern names so calls lower via the builtin table, and @@ -2463,8 +2569,12 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st | Deno -> "// Generated by AffineScript compiler (Deno-ESM target, issue #122)" | Bun -> "// Generated by AffineScript compiler (Bun-ESM target, issue #734)"); emit_line ctx "// SPDX-License-Identifier: MPL-2.0"; - emit ctx (match host with Deno -> deno_host_prelude | Bun -> bun_host_prelude); - emit ctx common_prelude; + (* The runtime is spliced in after the body is generated, pruned to what + the body reaches (see [prune_runtime]). *) + let runtime_at = Buffer.length ctx.output in + let runtime = + (match host with Deno -> deno_host_prelude | Bun -> bun_host_prelude) + ^ common_prelude in (* Collect structs. AffineScript's grammar accepts neither inherent [impl Type {}] nor a [self] expression (SELF_KW has no expression @@ -2572,7 +2682,10 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st | TopFn fd -> fd.fd_name.name = "main" | _ -> false) program.prog_decls in if has_main then emit_line ctx "await main();"; - Buffer.contents ctx.output + let all = Buffer.contents ctx.output in + let header = String.sub all 0 runtime_at in + let body = String.sub all runtime_at (String.length all - runtime_at) in + header ^ prune_runtime runtime ~body ^ body (* Deno-ESM host profile is retired; the ESM emitter is Bun-only. The name is kept so existing library callers compile. *) diff --git a/test/test_main.ml b/test/test_main.ml index d9304a2e..7c166c0d 100644 --- a/test/test_main.ml +++ b/test/test_main.ml @@ -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); diff --git a/test/test_runtime_prune.ml b/test/test_runtime_prune.ml new file mode 100644 index 00000000..f8ba745f --- /dev/null +++ b/test/test_runtime_prune.ml @@ -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) ] From 11ee9ad1bc7f881b1d17f0801522dae8d4809253 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 16:59:09 +0000 Subject: [PATCH 5/7] fix(codegen,json): address review of #798 - gen_match: an arm's binders no longer scope the arms after it (a later arm calling `trim` after `Some(trim)` emitted a call to an unbound name). - Statement-position match: an arm binds its pattern before its guard runs and a false guard falls through, in a labelled block as the tail form does. The guard used to run in the pattern test, before its binders existed (pre-existing). - for: the iterable is evaluated into a temporary before the loop, so `for f in f()` no longer reads `f` in its temporal dead zone (pre-existing). - URL helpers use globalThis.URL, so a program's own `URL` (a variant) cannot shadow the host constructor. - hpm_json_object_key snapshots an object's keys at index 0 and reuses the snapshot for later indices: one Object.keys per walk, not per key. - to_json rejects non-finite numbers (`1e400` parses to Infinity, which has no JSON form; stringify would emit `Infinity`). - json.affine: 11 externs mirror Zig exports, 2 are ESM-only for now. Tests: local_shadows_builtin (lambda, later arm, guard, for), url_module (every UrlParts field; a variant named URL), json_object_keys (inherited enumerable key, non-finite values). Each fails on the previous commit. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- lib/codegen_deno.ml | 65 ++++++++++++------- stdlib/json.affine | 7 +- .../codegen-bun/json_object_keys.harness.mjs | 8 +++ .../codegen-bun/local_shadows_builtin.affine | 34 ++++++++++ .../local_shadows_builtin.harness.mjs | 7 ++ tests/codegen-bun/url_module.affine | 8 +++ tests/codegen-bun/url_module.harness.mjs | 6 ++ 7 files changed, 111 insertions(+), 24 deletions(-) diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 97cdc57e..9996b58f 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -616,14 +616,16 @@ const __as_hpmJsonObjectGet = (v, k) => { ? Some(v[String(k)]) : None; }; // ---- Url (stdlib/Url.affine): WHATWG URL via the host's URL constructor ---- +// `globalThis.URL`, because a program may declare its own `URL` (a variant, +// say), which would shadow the host constructor in this module. const __as_urlResolve = (href, base) => { try { - return Some(new URL(String(href), base === "" ? undefined : String(base)).href); + return Some(new globalThis.URL(String(href), base === "" ? undefined : String(base)).href); } catch (_e) { return None; } }; const __as_urlParse = (s) => { let u; - try { u = new URL(String(s)); } catch (_e) { return None; } + try { u = new globalThis.URL(String(s)); } catch (_e) { return None; } return Some({ href: u.href, protocol: u.protocol, username: u.username, password: u.password, host: u.host, hostname: u.hostname, port: u.port, pathname: u.pathname, search: u.search, hash: u.hash }); @@ -636,10 +638,17 @@ const __as_urlParse = (s) => { 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) => { - const ks = __as_hpmJsonObjectKeys(v); + if (v === null || typeof v !== "object" || Array.isArray(v)) return None; const idx = Number(i); - return (Number.isInteger(idx) && idx >= 0 && idx < ks.length) ? Some(ks[idx]) : None; + 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) => { @@ -1774,9 +1783,10 @@ and gen_match ctx scrutinee arms = | arm :: rest -> let cond = gen_pattern_test scrut_var arm.ma_pat in let bindings = gen_pattern_bindings scrut_var arm.ma_pat in - with_locals ctx (pattern_binders arm.ma_pat) @@ fun () -> + (* This arm's binders scope its guard and body only; the arms after + it are generated outside that scope. *) + let arm_js = with_locals ctx (pattern_binders arm.ma_pat) @@ fun () -> let body = gen_expr ctx arm.ma_body in - let prefix = if bindings = "" then let guard = match arm.ma_guard with | Some g -> " && (" ^ gen_expr ctx g ^ ")" | None -> "" @@ -1789,7 +1799,7 @@ and gen_match ctx scrutinee arms = ^ (match arm.ma_guard with Some g -> gen_expr ctx g | None -> "true") ^ ") { return " ^ body ^ "; } }" in - prefix ^ " " ^ gen_arms rest + arm_js ^ " " ^ gen_arms rest in if ctx.in_async then "(await (async (" ^ scrut_var ^ ") => { " ^ gen_arms arms ^ " })(" @@ -1952,21 +1962,27 @@ and gen_stmt_seq ctx blk = and gen_match_stmt ctx scrut arms = let sv = "__scrut" in - let rec arms_js = function - | [] -> "throw new Error(\"non-exhaustive match\");" - | arm :: rest -> - let cond = gen_pattern_test sv arm.ma_pat in - let binds = gen_pattern_bindings sv arm.ma_pat in - let arm_js = - with_locals ctx (pattern_binders arm.ma_pat) (fun () -> - let guard = match arm.ma_guard with - | Some g -> " && (" ^ gen_expr ctx g ^ ")" | None -> "" in - "if (" ^ cond ^ guard ^ ") { " ^ binds - ^ gen_branch ctx arm.ma_body ^ " } else ") - in - arm_js ^ arms_js rest + incr let_tmp_counter; + (* A labelled block, as in {!gen_tail_to}: an arm binds its pattern + variables before its guard reads them, and an arm whose guard fails + falls through to the next. A guard used to run in the pattern test, + before the bindings existed. `$` cannot occur in a source identifier. *) + let label = Printf.sprintf "$smatch_%d" !let_tmp_counter in + let arm_js arm = + let cond = gen_pattern_test sv arm.ma_pat in + let binds = gen_pattern_bindings sv arm.ma_pat in + with_locals ctx (pattern_binders arm.ma_pat) (fun () -> + let guard = Option.map (gen_expr ctx) arm.ma_guard in + let body = gen_branch ctx arm.ma_body ^ " break " ^ label ^ ";" in + let guarded = match guard with + | None -> body + | Some g -> "if (" ^ g ^ ") { " ^ body ^ " }" + in + "if (" ^ cond ^ ") { " ^ binds ^ guarded ^ " }") in - "{ const " ^ sv ^ " = " ^ gen_expr ctx scrut ^ "; " ^ arms_js arms ^ " }" + label ^ ": { const " ^ sv ^ " = " ^ gen_expr ctx scrut ^ "; " + ^ String.concat " " (List.map arm_js arms) + ^ " throw new Error(\"non-exhaustive match\"); }" (* Statements that evaluate [e] and hand its value to [sink], which turns a JS expression into a statement (`x = v;` or `return v;`). A `return` @@ -2205,7 +2221,12 @@ and gen_stmt ctx (stmt : stmt) : string = stmts_js ^ tail_js in restore (); - "for (const " ^ pat_str ^ " of " ^ iter_str ^ ") { " ^ body_js ^ " }" + (* In `for (const x of e)`, `e` runs with `x` already in its temporal + dead zone, so `for f in f()` threw. Evaluate `e` first. *) + incr let_tmp_counter; + let it = Printf.sprintf "$iter_%d" !let_tmp_counter in + "{ const " ^ it ^ " = " ^ iter_str ^ "; for (const " ^ pat_str ^ " of " + ^ it ^ ") { " ^ body_js ^ " } }" (* ============================================================================ Top-level declarations + class emission diff --git a/stdlib/json.affine b/stdlib/json.affine index fdd1d5c2..1f7179ae 100644 --- a/stdlib/json.affine +++ b/stdlib/json.affine @@ -212,7 +212,7 @@ fn escape_string(s: String) -> String { // ============================================================================ // RSR rewire (v0.3) — hpm-json-rsr Zig FFI bindings // -// The 13 `hpm_json_*` externs 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 @@ -281,7 +281,10 @@ pub fn to_json(val: HpmJsonValue) -> Option { } 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 { diff --git a/tests/codegen-bun/json_object_keys.harness.mjs b/tests/codegen-bun/json_object_keys.harness.mjs index 86d042f9..f7f338d3 100644 --- a/tests/codegen-bun/json_object_keys.harness.mjs +++ b/tests/codegen-bun/json_object_keys.harness.mjs @@ -26,4 +26,12 @@ for (const v of ["q\"uote", "back\\slash", "nl\nx", "cr\rtab\t", "bs\bff\f", "c\ 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"); diff --git a/tests/codegen-bun/local_shadows_builtin.affine b/tests/codegen-bun/local_shadows_builtin.affine index c611d0bb..9bfd6237 100644 --- a/tests/codegen-bun/local_shadows_builtin.affine +++ b/tests/codegen-bun/local_shadows_builtin.affine @@ -34,3 +34,37 @@ pub fn let_scope_ends(s: String) -> String { let a = { let trim = fn(z: String) => z ++ "?"; trim(s) }; a ++ trim(" u ") } + +// The intrinsic is visible again after a lambda that shadowed it. +pub fn lambda_then_intrinsic(s: String) -> String { + let f = fn(trim: String -> String) => trim(s); + f(fn(x: String) => x ++ "!") ++ trim(" t ") +} + +// A later arm sees the intrinsic an earlier arm's binder shadowed. +pub fn later_arm_intrinsic(o: Option String>) -> String { + match o { Some(trim) => trim("a"), None => trim(" b ") } +} + +fn positive(n: Int) -> Option { if n > 0 { Some(n) } else { None } } + +// A statement-position guard reads its arm's binder; a false guard falls +// through to the next arm. +pub fn stmt_guard(n: Int) -> String { + let mut out = ""; + match positive(n) { + Some(len) if len > 5 => { out = "big"; }, + Some(len) => { out = "small:" ++ int_to_string(len); }, + None => { out = "none"; } + }; + out +} + +fn letters() -> [String] { ["x", "y"] } + +// The iterable runs before the loop binder exists. +pub fn for_shadows_callee() -> String { + let mut out = ""; + for letters in letters() { out = out ++ letters; } + out +} diff --git a/tests/codegen-bun/local_shadows_builtin.harness.mjs b/tests/codegen-bun/local_shadows_builtin.harness.mjs index 153f1aa6..6eb3513b 100644 --- a/tests/codegen-bun/local_shadows_builtin.harness.mjs +++ b/tests/codegen-bun/local_shadows_builtin.harness.mjs @@ -11,4 +11,11 @@ expect("lambda parameter shadows `trim`", subject.lambda_param(" a "), " a !"); expect("loop and arm binders end with their scope", subject.scope_ends(" s ", [(x) => x + "1", (x) => x + "2"]), " s 1 s 2[ s ]t"); expect("block `let` ends with its block", subject.let_scope_ends("v"), "v?u"); +expect("intrinsic visible after the lambda", subject.lambda_then_intrinsic(" a "), " a !t"); +expect("later arm calls the intrinsic", subject.later_arm_intrinsic({ tag: "None" }), "b"); +expect("earlier arm binds", subject.later_arm_intrinsic({ tag: "Some", value: (x) => x + "!" }), "a!"); +expect("guard reads its binder", subject.stmt_guard(9), "big"); +expect("false guard falls through", subject.stmt_guard(2), "small:2"); +expect("no match", subject.stmt_guard(0), "none"); +expect("for evaluates its iterable first", subject.for_shadows_callee(), "xy"); console.log("local_shadows_builtin: ok"); diff --git a/tests/codegen-bun/url_module.affine b/tests/codegen-bun/url_module.affine index bd5cc704..616aa3c8 100644 --- a/tests/codegen-bun/url_module.affine +++ b/tests/codegen-bun/url_module.affine @@ -4,10 +4,18 @@ use prelude::{ Option, Some, None }; use Url::{ url_resolve, url_parse, UrlParts }; +// A variant named URL is emitted as a module-level `const URL`; the runtime +// must still reach the host constructor. +pub type Kind = URL | Path + +pub fn kind_of(s: String) -> Kind { match url_parse(s) { Some(_) => URL, None => Path } } + pub fn resolve(h: String, b: String) -> String { match url_resolve(h, b) { Some(u) => u, None => "" } } +pub fn parts(s: String) -> Option { url_parse(s) } + pub fn path_of(s: String) -> String { match url_parse(s) { Some(p) => p.protocol ++ "|" ++ p.host ++ "|" ++ p.pathname ++ "|" ++ p.search ++ "|" ++ p.hash, None => "" } } diff --git a/tests/codegen-bun/url_module.harness.mjs b/tests/codegen-bun/url_module.harness.mjs index adf1e243..29948138 100644 --- a/tests/codegen-bun/url_module.harness.mjs +++ b/tests/codegen-bun/url_module.harness.mjs @@ -11,4 +11,10 @@ expect("invalid base fails", subject.resolve("p", "not a url"), ""); expect("WHATWG backslash", subject.resolve("\\x", "https://a.example/b/"), "https://a.example/x"); expect("parts", subject.path_of("http://h.example:8080/a/%7e?x=1#y"), "http:|h.example:8080|/a/%7e|?x=1|#y"); expect("parse rejects relative", subject.path_of("/a"), ""); +const all = (u) => { const p = subject.parts(u); return p.tag === "Some" ? JSON.stringify(p.value) : "none"; }; +expect("every field, with credentials and a port", all("https://u:p@h.example:8443/a/b?q=1#f"), + JSON.stringify({ href: "https://u:p@h.example:8443/a/b?q=1#f", protocol: "https:", username: "u", password: "p", host: "h.example:8443", hostname: "h.example", port: "8443", pathname: "/a/b", search: "?q=1", hash: "#f" })); +expect("every field, nothing optional", all("http://h.example"), + JSON.stringify({ href: "http://h.example/", protocol: "http:", username: "", password: "", host: "h.example", hostname: "h.example", port: "", pathname: "/", search: "", hash: "" })); +expect("host URL despite a variant named URL", subject.kind_of("https://e.example/").tag, "URL"); console.log("url_module: ok"); From ce22b22bc34c3321aef5cf36e0361596d99fc131 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 17:00:08 +0000 Subject: [PATCH 6/7] refactor(json): move object-key enumeration to its own PR Per the owner's ruling on review, #798 is split. hpm_json_object_len / hpm_json_object_key and the to_json object branch move to a follow-up PR that waits for the matching hpm-json-rsr Zig exports, so no native build can meet an unresolved symbol. This branch keeps the stringify escaping and non-finite fixes, now covered by tests/codegen-bun/json_escapes (arrays only; objects still do not convert here). Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- CHANGELOG.adoc | 6 +- lib/codegen_deno.ml | 22 ------- stdlib/json.affine | 66 ++++--------------- tests/codegen-bun/json_escapes.affine | 16 +++++ tests/codegen-bun/json_escapes.harness.mjs | 18 +++++ tests/codegen-bun/json_object_keys.affine | 42 ------------ .../codegen-bun/json_object_keys.harness.mjs | 37 ----------- 7 files changed, 49 insertions(+), 158 deletions(-) create mode 100644 tests/codegen-bun/json_escapes.affine create mode 100644 tests/codegen-bun/json_escapes.harness.mjs delete mode 100644 tests/codegen-bun/json_object_keys.affine delete mode 100644 tests/codegen-bun/json_object_keys.harness.mjs diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index abfb1833..30320698 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -23,10 +23,6 @@ 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 @@ -126,7 +122,7 @@ 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_object_keys`). +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, diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 9996b58f..93e6ea98 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -630,26 +630,6 @@ 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; @@ -1235,8 +1215,6 @@ 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 1f7179ae..c24a7fdd 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. 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. +// `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). module json; @@ -212,7 +212,7 @@ fn escape_string(s: String) -> String { // ============================================================================ // RSR rewire (v0.3) — hpm-json-rsr Zig FFI bindings // -// Eleven of the 13 `hpm_json_*` externs mirror the Zig exports at +// The 11 `hpm_json_*` externs faithfully 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,12 +229,9 @@ fn escape_string(s: String) -> String { // hpm_json_int -> INT64_MIN on type mismatch // hpm_json_float -> NaN on type mismatch // -// 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. +// 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`. // ============================================================================ pub extern type HpmJsonValue; @@ -247,13 +244,6 @@ 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; @@ -266,8 +256,11 @@ pub fn parse(src: String) -> Option { hpm_json_parse(src) } -/// Tree-walk an `HpmJsonValue` into the AS `Json` sum. Objects keep their -/// members in the order `hpm_json_object_key` reports them. +/// 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. /// /// Recursively `hpm_json_free`s every child handle the walk allocates; /// the root handle is the caller's responsibility (matches the parse @@ -312,39 +305,8 @@ 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.affine b/tests/codegen-bun/json_escapes.affine new file mode 100644 index 00000000..249db0ae --- /dev/null +++ b/tests/codegen-bun/json_escapes.affine @@ -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 => "" + } +} + +pub fn quote(s: String) -> String { stringify(JArray([JString(s)])) } diff --git a/tests/codegen-bun/json_escapes.harness.mjs b/tests/codegen-bun/json_escapes.harness.mjs new file mode 100644 index 00000000..6d2e7905 --- /dev/null +++ b/tests/codegen-bun/json_escapes.harness.mjs @@ -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", "é 
", ""]) { + 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]"), ""); +expect("finite exponent", subject.roundtrip("[1e3]"), "[1000]"); +expect("objects need key enumeration, not on this branch", subject.roundtrip('{"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 deleted file mode 100644 index c055e81a..00000000 --- a/tests/codegen-bun/json_object_keys.affine +++ /dev/null @@ -1,42 +0,0 @@ -// 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 deleted file mode 100644 index f7f338d3..00000000 --- a/tests/codegen-bun/json_object_keys.harness.mjs +++ /dev/null @@ -1,37 +0,0 @@ -// 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"); From 50b7b969f45784e3ebda151aded00f7908d6ab34 Mon Sep 17 00:00:00 2001 From: Claude Date: Wed, 18 Nov 2026 17:07:38 +0000 Subject: [PATCH 7/7] fix(codegen): end block-local bindings with their block Addresses the outside-diff findings of the CodeRabbit review on #798. - gen_stmt_seq scopes ctx.locals to the block, so a `let len` inside an `if` block no longer turns a later builtin `len(...)` into a bare call to an out-of-scope const (ReferenceError). - A source block in statement position keeps its braces, so its declarations stay in a JS block scope. The tail-position block also scopes its locals. - expr_is_int no longer classes a call to a local (or local fn) named like an Int-returning builtin as Int, so `len("a") / len("b")` with a Float-returning local `len` divides instead of truncating. Each new case in tests/codegen-bun/local_shadows_builtin fails without this change. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK --- CHANGELOG.adoc | 4 +++- lib/codegen_deno.ml | 18 ++++++++++++--- .../codegen-bun/local_shadows_builtin.affine | 22 +++++++++++++++++++ .../local_shadows_builtin.harness.mjs | 4 ++++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.adoc b/CHANGELOG.adoc index 30320698..f18885be 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -126,7 +126,9 @@ 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 (`tests/codegen-bun/local_shadows_builtin`). +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 diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 93e6ea98..2203ee71 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -1424,8 +1424,13 @@ let rec expr_is_int ctx (e : expr) : bool = | ExprIf { ei_then; ei_else = Some e; _ } -> expr_is_int ctx ei_then && expr_is_int ctx e | ExprApp (ExprVar id, _) -> - Hashtbl.mem ctx.int_fns id.name - || List.mem id.name int_returning_builtins + (* A call to a local binding is not the intrinsic or top-level + function of the same name (see the intrinsic dispatch in + {!gen_expr}), so its result is not provably an [Int]. *) + not (Hashtbl.mem ctx.locals id.name) + && not (Hashtbl.mem ctx.local_fns id.name) + && (Hashtbl.mem ctx.int_fns id.name + || List.mem id.name int_returning_builtins) (* An element read from a provably-[Array] value is an [Int] (covers `xs[i] / 2` where xs: [Int]) — issue #478 finding 2. *) | ExprIndex (arr, _) -> expr_is_int_array ctx arr @@ -1914,7 +1919,9 @@ and gen_stmt_expr ctx (e : expr) : string = in "if (" ^ gen_expr ctx ei_cond ^ ") { " ^ gen_branch ctx ei_then ^ " }" ^ elseb - | ExprBlock blk -> gen_stmt_seq ctx blk + (* A source block is a scope: keep its braces, so its declarations do + not land in the enclosing JS scope. *) + | ExprBlock blk -> "{ " ^ gen_stmt_seq ctx blk ^ " }" | ExprMatch { em_scrutinee; em_arms } -> gen_match_stmt ctx em_scrutinee em_arms | ExprTry { et_body; et_catch; et_finally } -> @@ -1928,7 +1935,11 @@ and gen_branch ctx e = | ExprBlock blk -> gen_stmt_seq ctx blk | other -> gen_stmt_expr ctx other +(* The statements of a block whose braces the caller emits. Its `let`s are + local to it, so a binder declared inside cannot shadow an intrinsic + called after it. *) and gen_stmt_seq ctx blk = + with_locals_scope ctx @@ fun () -> let b = Buffer.create 64 in List.iter (fun s -> Buffer.add_string b (gen_stmt ctx s); @@ -1975,6 +1986,7 @@ and gen_tail_to ctx (sink : string -> string) e = (* The block is a JS block scope: its `let`s must not change the integer tracking of the code after it (a guard, a later arm). *) with_int_scope ctx (fun () -> + with_locals_scope ctx @@ fun () -> let b = Buffer.create 64 in List.iter (fun s -> Buffer.add_string b (gen_stmt ctx s); diff --git a/tests/codegen-bun/local_shadows_builtin.affine b/tests/codegen-bun/local_shadows_builtin.affine index 9bfd6237..a4a17b20 100644 --- a/tests/codegen-bun/local_shadows_builtin.affine +++ b/tests/codegen-bun/local_shadows_builtin.affine @@ -68,3 +68,25 @@ pub fn for_shadows_callee() -> String { for letters in letters() { out = out ++ letters; } out } + +// A `let` inside an `if` block ends with the block: the call after it is +// the intrinsic, whether or not the branch ran. +pub fn if_block_let(c: Bool) -> Int { + let mut n = 0; + if c { let len = fn(t: String) => 10; n = len("x"); } + n + len("ab") +} + +// A standalone block is a scope too. +pub fn bare_block_let() -> Int { + let mut n = 0; + { let len = fn(t: String) => 7; n = len("x"); }; + n + len("abc") +} + +// A call to a local named `len` is not provably an Int: `/` must not +// truncate it. +pub fn shadowed_len_div() -> Float { + let len = fn(s: String) => { if s == "a" { 3.0 } else { 2.0 } }; + len("a") / len("b") +} diff --git a/tests/codegen-bun/local_shadows_builtin.harness.mjs b/tests/codegen-bun/local_shadows_builtin.harness.mjs index 6eb3513b..be86853b 100644 --- a/tests/codegen-bun/local_shadows_builtin.harness.mjs +++ b/tests/codegen-bun/local_shadows_builtin.harness.mjs @@ -18,4 +18,8 @@ expect("guard reads its binder", subject.stmt_guard(9), "big"); expect("false guard falls through", subject.stmt_guard(2), "small:2"); expect("no match", subject.stmt_guard(0), "none"); expect("for evaluates its iterable first", subject.for_shadows_callee(), "xy"); +expect("if-block let ends with the block (taken)", subject.if_block_let(true), 12); +expect("if-block let ends with the block (not taken)", subject.if_block_let(false), 2); +expect("bare block let ends with the block", subject.bare_block_let(), 10); +expect("shadowed len is not integer division", subject.shadowed_len_div(), 1.5); console.log("local_shadows_builtin: ok");