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 414748a2..f18885be 100644 --- a/CHANGELOG.adoc +++ b/CHANGELOG.adoc @@ -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` (kind mismatch). Harness actually imports `smokeIdaptikReach` / Assets mock. Smokes compile + run (Refs #760) @@ -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 diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 42613c7b..2203ee71 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,21 @@ 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 ---- +// `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 globalThis.URL(String(href), base === "" ? undefined : String(base)).href); + } catch (_e) { return None; } +}; +const __as_urlParse = (s) => { + let u; + 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 }); +}; const __as_hpmJsonArrayLen = (v) => (Array.isArray(v) ? v.length : 0); const __as_hpmJsonArrayGet = (v, i) => { if (!Array.isArray(v)) return None; @@ -1191,6 +1213,8 @@ 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_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)); @@ -1400,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 @@ -1436,7 +1465,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 +1541,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 +1550,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 +1624,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 +1671,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,8 +1766,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 + (* 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 -> "" @@ -1715,7 +1782,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 ^ " })(" @@ -1798,6 +1865,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 +1885,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 -> "" @@ -1849,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 } -> @@ -1863,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); @@ -1875,17 +1951,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 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 + 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` @@ -1900,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); @@ -1938,6 +2025,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 +2057,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 +2086,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 +2137,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 +2175,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 +2202,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 @@ -2112,7 +2211,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 @@ -2330,6 +2434,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 @@ -2370,8 +2580,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 @@ -2479,7 +2693,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/lib/module_loader.ml b/lib/module_loader.ml index 13282b9a..64bd5cba 100644 --- a/lib/module_loader.ml +++ b/lib/module_loader.ml @@ -514,20 +514,32 @@ 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 @@ -535,13 +547,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/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..c24a7fdd 100644 --- a/stdlib/json.affine +++ b/stdlib/json.affine @@ -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; @@ -274,7 +274,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/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) ] 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/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 } } 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/local_shadows_builtin.affine b/tests/codegen-bun/local_shadows_builtin.affine new file mode 100644 index 00000000..a4a17b20 --- /dev/null +++ b/tests/codegen-bun/local_shadows_builtin.affine @@ -0,0 +1,92 @@ +// 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 ") +} + +// 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 +} + +// 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 new file mode 100644 index 00000000..be86853b --- /dev/null +++ b/tests/codegen-bun/local_shadows_builtin.harness.mjs @@ -0,0 +1,25 @@ +// 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"); +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"); +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"); diff --git a/tests/codegen-bun/url_module.affine b/tests/codegen-bun/url_module.affine new file mode 100644 index 00000000..616aa3c8 --- /dev/null +++ b/tests/codegen-bun/url_module.affine @@ -0,0 +1,21 @@ +// 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 }; + +// 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 new file mode 100644 index 00000000..29948138 --- /dev/null +++ b/tests/codegen-bun/url_module.harness.mjs @@ -0,0 +1,20 @@ +// 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"), ""); +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"); 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:"