From 55a48c37b61b2bfbbbe60557167b03bcb799c646 Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 00:48:07 +0000 Subject: [PATCH 1/2] fix(codegen): lower an extern used as a value to an arrow (#818) A bare extern name emitted an unbound identifier. The extern table now records arity, and value position wraps the builtin lowering in an arrow. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZjyGGiPhiEUxNGPwsr --- lib/codegen_deno.ml | 20 ++++++++++++++----- tests/codegen-deno/extern_as_value.affine | 12 +++++++++++ .../codegen-deno/extern_as_value.harness.mjs | 13 ++++++++++++ 3 files changed, 40 insertions(+), 5 deletions(-) create mode 100644 tests/codegen-deno/extern_as_value.affine create mode 100644 tests/codegen-deno/extern_as_value.harness.mjs diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index 86a0d476..b14430f4 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -52,7 +52,7 @@ type codegen_ctx = { (* Names declared as [extern fn] (after import flattening these arrive as [TopExternFn]). A call to one of these lowers via {!deno_builtin} rather than as an ordinary AffineScript function call. *) - externs : (string, unit) Hashtbl.t; + externs : (string, int) Hashtbl.t; (* name -> arity *) (* When emitting a method body, the receiver parameter is rewritten to [this]; this holds that parameter's source name (if any). *) self_name : string option; @@ -1737,6 +1737,16 @@ let rec lowers_to_statements = function let rec gen_expr ctx (expr : expr) : string = match expr with | ExprLit lit -> gen_literal lit + | ExprVar id + when Hashtbl.mem ctx.externs id.name + && Hashtbl.mem (builtins_for ctx) id.name + && not (Hashtbl.mem ctx.local_fns id.name) + && not (Hashtbl.mem ctx.locals id.name) -> + (* #818: an extern used as a value has no JS binding of its own name; + wrap its builtin lowering in an arrow of the declared arity. *) + let ps = List.init (Hashtbl.find ctx.externs id.name) (Printf.sprintf "__a%d") in + Printf.sprintf "((%s) => %s)" (String.concat ", " ps) + ((Hashtbl.find (builtins_for ctx) id.name) ps) | ExprVar name -> resolve_var ctx name.name | ExprApp (func, args) -> (match func with @@ -2776,10 +2786,10 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st (* Register extern names so calls lower via the builtin table, and user-defined top-level names so they shadow host intrinsics. *) List.iter (function - | TopExternFn { ef_name; _ } -> - Hashtbl.replace ctx.externs ef_name.name () + | TopExternFn { ef_name; ef_params; _ } -> + Hashtbl.replace ctx.externs ef_name.name (List.length ef_params) | TopFn fd when fd.fd_body = FnExtern -> - Hashtbl.replace ctx.externs fd.fd_name.name () + Hashtbl.replace ctx.externs fd.fd_name.name (List.length fd.fd_params) | TopFn fd -> Hashtbl.replace ctx.local_fns fd.fd_name.name (); if fd_is_async fd then @@ -2827,7 +2837,7 @@ let generate (host : host_profile) (program : program) (symbols : Symbol.t) : st which must then be the host symbol: when [mangle] escapes it (an extern `class` is called as `class_`), reserve that name, so a program binding emitted as `class_` is refused (#801) instead of receiving the call. *) - Hashtbl.iter (fun e () -> + Hashtbl.iter (fun e _ -> let js = mangle e in if js <> e && not (Hashtbl.mem (builtins_for ctx) e) && not (Hashtbl.mem ctx.local_fns e) diff --git a/tests/codegen-deno/extern_as_value.affine b/tests/codegen-deno/extern_as_value.affine new file mode 100644 index 00000000..833fec69 --- /dev/null +++ b/tests/codegen-deno/extern_as_value.affine @@ -0,0 +1,12 @@ +// SPDX-License-Identifier: MPL-2.0 +// #818: an extern fn passed as a value lowers to a callable arrow. + +use Crypto::{ sha256, base64url_encode }; + +fn apply(f: ([Int]) -> [Int], b: [Int]) -> [Int] = f(b); + +fn apply_s(f: ([Int]) -> String, b: [Int]) -> String = f(b); + +pub fn digest_via_value(b: [Int]) -> [Int] = apply(sha256, b); + +pub fn b64_via_value(b: [Int]) -> String = apply_s(base64url_encode, b); diff --git a/tests/codegen-deno/extern_as_value.harness.mjs b/tests/codegen-deno/extern_as_value.harness.mjs new file mode 100644 index 00000000..2c7fe5c8 --- /dev/null +++ b/tests/codegen-deno/extern_as_value.harness.mjs @@ -0,0 +1,13 @@ +// SPDX-License-Identifier: MPL-2.0 +// #818: Node-ESM harness for externs used as values. + +import assert from "node:assert/strict"; + +const { digest_via_value, b64_via_value } = await import("./extern_as_value.bun.js"); + +assert.equal(digest_via_value([]).length, 32); +assert.equal(b64_via_value([0xfb, 0xff, 0xfe]), "-__-"); +// Same byte validation as a direct call. +assert.throws(() => digest_via_value([256]), RangeError); + +console.log("extern_as_value: ok"); From fee86a7372d3c07994557d997ac6363478da9e8d Mon Sep 17 00:00:00 2001 From: Claude Date: Fri, 27 Nov 2026 00:55:25 +0000 Subject: [PATCH 2/2] fix(codegen): extern values in shorthand record fields; keep await lowerings bare (#818) A shorthand record field (`#{sha256}`) now goes through the same extern-value lowering as an explicit field. A builtin lowering that contains `await` is not wrapped. A sync arrow around `await` is a syntax error that stops the whole module from loading. These names keep the old behaviour. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_0136P7ZjyGGiPhiEUxNGPwsr --- lib/codegen_deno.ml | 13 +++++++++---- tests/codegen-deno/extern_as_value.affine | 6 ++++++ tests/codegen-deno/extern_as_value.harness.mjs | 3 ++- 3 files changed, 17 insertions(+), 5 deletions(-) diff --git a/lib/codegen_deno.ml b/lib/codegen_deno.ml index b14430f4..8d23be6c 100644 --- a/lib/codegen_deno.ml +++ b/lib/codegen_deno.ml @@ -1745,8 +1745,13 @@ let rec gen_expr ctx (expr : expr) : string = (* #818: an extern used as a value has no JS binding of its own name; wrap its builtin lowering in an arrow of the declared arity. *) let ps = List.init (Hashtbl.find ctx.externs id.name) (Printf.sprintf "__a%d") in - Printf.sprintf "((%s) => %s)" (String.concat ", " ps) - ((Hashtbl.find (builtins_for ctx) id.name) ps) + let body = (Hashtbl.find (builtins_for ctx) id.name) ps in + (* snip: an await-based lowering keeps the bare name (as before #818); + an async wrapper also needs awaited calls through the value. *) + if (try ignore (Str.search_forward (Str.regexp "\\bawait\\b") body 0); true + with Not_found -> false) + then resolve_var ctx id.name + else Printf.sprintf "((%s) => %s)" (String.concat ", " ps) body | ExprVar name -> resolve_var ctx name.name | ExprApp (func, args) -> (match func with @@ -1868,7 +1873,7 @@ let rec gen_expr ctx (expr : expr) : string = let field_strs = List.map (fun (name, e_opt) -> let v = match e_opt with | Some e -> gen_expr ctx e - | None -> resolve_var ctx name.name + | None -> gen_expr ctx (ExprVar name) in mangle name.name ^ ": " ^ v ) er_fields in @@ -2575,7 +2580,7 @@ let gen_constructor ctx (fd : fn_decl) : unit = List.iter (fun (name, e_opt) -> let v = match e_opt with | Some e -> gen_expr body_ctx e - | None -> resolve_var body_ctx name.name + | None -> gen_expr body_ctx (ExprVar name) in emit_line body_ctx (Printf.sprintf "this.%s = %s;" (mangle name.name) v) diff --git a/tests/codegen-deno/extern_as_value.affine b/tests/codegen-deno/extern_as_value.affine index 833fec69..17f36ce4 100644 --- a/tests/codegen-deno/extern_as_value.affine +++ b/tests/codegen-deno/extern_as_value.affine @@ -10,3 +10,9 @@ fn apply_s(f: ([Int]) -> String, b: [Int]) -> String = f(b); pub fn digest_via_value(b: [Int]) -> [Int] = apply(sha256, b); pub fn b64_via_value(b: [Int]) -> String = apply_s(base64url_encode, b); + +// Shorthand record field holding an extern value. +pub fn digest_via_record(b: [Int]) -> [Int] { + let r = #{sha256}; + apply(r.sha256, b) +} diff --git a/tests/codegen-deno/extern_as_value.harness.mjs b/tests/codegen-deno/extern_as_value.harness.mjs index 2c7fe5c8..e01ca00b 100644 --- a/tests/codegen-deno/extern_as_value.harness.mjs +++ b/tests/codegen-deno/extern_as_value.harness.mjs @@ -3,10 +3,11 @@ import assert from "node:assert/strict"; -const { digest_via_value, b64_via_value } = await import("./extern_as_value.bun.js"); +const { digest_via_value, b64_via_value, digest_via_record } = await import("./extern_as_value.bun.js"); assert.equal(digest_via_value([]).length, 32); assert.equal(b64_via_value([0xfb, 0xff, 0xfe]), "-__-"); +assert.deepEqual(digest_via_record([1, 2]), digest_via_value([1, 2])); // Same byte validation as a direct call. assert.throws(() => digest_via_value([256]), RangeError);