Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 22 additions & 7 deletions lib/codegen_deno.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -1737,6 +1737,21 @@ 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
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
Expand Down Expand Up @@ -1858,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
Expand Down Expand Up @@ -2565,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)
Expand Down Expand Up @@ -2776,10 +2791,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
Expand Down Expand Up @@ -2827,7 +2842,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)
Expand Down
18 changes: 18 additions & 0 deletions tests/codegen-deno/extern_as_value.affine
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// 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);

// Shorthand record field holding an extern value.
pub fn digest_via_record(b: [Int]) -> [Int] {
let r = #{sha256};
apply(r.sha256, b)
}
14 changes: 14 additions & 0 deletions tests/codegen-deno/extern_as_value.harness.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// 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, 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);

console.log("extern_as_value: ok");
Loading