Repository navigation
feat(codegen-bun): intrinsics are first-class values #864
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
hyperpolymath
merged 1 commit into
main
from
claude/gifted-lamport-k0mc5l-intrinsic-values
Dec 2, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| (* SPDX-License-Identifier: MPL-2.0 *) | ||
| (* Copyright (c) 2026 Jonathan D.A. Jewell <jonathan.jewell@open.ac.uk> *) | ||
| (** Intrinsics as values on Bun-ESM (owner ruling 2026-12-01: first-class | ||
| wrappers for every intrinsic). | ||
|
|
||
| An intrinsic such as [len] used as a value, not called, was emitted as a | ||
| bare name with no JS binding, so calling it failed with a ReferenceError. | ||
| Declared externs were already wrapped (#818); non-extern intrinsics now | ||
| are too, at the arity their type gives in the type checker's builtin | ||
| table ([Codegen_deno.builtin_arity]). | ||
|
|
||
| This suite pins coverage: every Bun builtin the type checker types gets | ||
| a wrapper, so a builtin added later cannot silently fall back to a bare | ||
| name. Behaviour is pinned by tests/codegen-bun/intrinsic_values. *) | ||
|
|
||
| open Affinescript | ||
|
|
||
| let every_typed_bun_builtin_is_wrapped () = | ||
| let arities = Lazy.force Codegen_deno.builtin_arity in | ||
| let unwrapped = ref [] and wrapped = ref 0 in | ||
| Hashtbl.iter (fun name lower -> | ||
| match Hashtbl.find_opt arities name with | ||
| | Some k when k > 0 -> | ||
| (match Codegen_deno.wrap_builtin_value lower k with | ||
| | Some js -> | ||
| incr wrapped; | ||
| if not (String.length js > 2 && String.sub js 0 2 = "((") then | ||
| unwrapped := (name ^ " (malformed: " ^ js ^ ")") :: !unwrapped | ||
| | None -> unwrapped := (name ^ " (awaits)") :: !unwrapped) | ||
| | _ -> ()) | ||
| Codegen_deno.bun_builtins; | ||
| if !unwrapped <> [] then | ||
| Alcotest.failf "typed Bun builtins with no value wrapper: %s" | ||
| (String.concat ", " (List.sort compare !unwrapped)); | ||
| Alcotest.(check bool) "at least the string/number intrinsics are wrapped" true (!wrapped >= 30) | ||
|
|
||
| let arity_comes_from_the_type () = | ||
| let arities = Lazy.force Codegen_deno.builtin_arity in | ||
| List.iter (fun (name, k) -> | ||
| Alcotest.(check (option int)) name (Some k) (Hashtbl.find_opt arities name)) | ||
| [ ("len", 1); ("string_sub", 3); ("string_find", 2); ("int_to_string", 1) ] | ||
|
|
||
| let wrapper_text () = | ||
| let lower = Hashtbl.find Codegen_deno.bun_builtins "string_sub" in | ||
| Alcotest.(check (option string)) "string_sub wrapper" | ||
| (Some "((__a0, __a1, __a2) => __as_strSub(__a0, __a1, __a2))") | ||
| (Codegen_deno.wrap_builtin_value lower 3) | ||
|
|
||
| let tests = [ | ||
| Alcotest.test_case "every typed Bun builtin is wrapped as a value" `Quick every_typed_bun_builtin_is_wrapped; | ||
| Alcotest.test_case "arity comes from the builtin's type" `Quick arity_comes_from_the_type; | ||
| Alcotest.test_case "wrapper text" `Quick wrapper_text; | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| // Intrinsics used as values (owner ruling 2026-12-01): each is wrapped in an | ||
| // arrow of its type's arity instead of being emitted as a bare name, which | ||
| // failed with a ReferenceError when called. | ||
| fn apply1(f: String -> Int, s: String) -> Int { | ||
| f(s) | ||
| } | ||
|
|
||
| fn apply_s(f: String -> String, s: String) -> String { | ||
| f(s) | ||
| } | ||
|
|
||
| pub fn length_via_value(s: String) -> Int { | ||
| apply1(len, s) | ||
| } | ||
|
|
||
| pub fn upper_via_value(s: String) -> String { | ||
| apply_s(to_uppercase, s) | ||
| } | ||
|
|
||
| pub fn trim_via_let(s: String) -> String { | ||
| let t = trim; | ||
| t(s) | ||
| } | ||
|
|
||
| pub fn sub_via_let(s: String) -> String { | ||
| let f = string_sub; | ||
| f(s, 1, 2) | ||
| } | ||
|
|
||
| pub fn find_via_let(s: String) -> Int { | ||
| let f = string_find; | ||
| f(s, "c") | ||
| } | ||
|
|
||
| pub fn show_int_via_let(n: Int) -> String { | ||
| let f = int_to_string; | ||
| f(n) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
| import * as subject from "./intrinsic_values.bun.js"; | ||
|
|
||
| const expect = (label, got, want) => { | ||
| if (got !== want) throw new Error(`${label}: got ${JSON.stringify(got)}, want ${JSON.stringify(want)}`); | ||
| }; | ||
| expect("len passed as a value", subject.length_via_value("abcd"), 4); | ||
| expect("to_uppercase passed as a value", subject.upper_via_value("ab"), "AB"); | ||
| expect("trim bound with let", subject.trim_via_let(" x "), "x"); | ||
| expect("string_sub bound with let", subject.sub_via_let("abcd"), "bc"); | ||
| expect("string_find bound with let", subject.find_via_let("abcd"), 2); | ||
| expect("int_to_string bound with let", subject.show_int_via_let(42), "42"); | ||
| console.log("intrinsic_values: ok"); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a multi-argument intrinsic value is partially applied, such as
let f = string_sub; let g = f("abcd"); g(1, 3), the type checker accepts the first call because function types are curried. This wrapper instead emits one JavaScript arrow taking every parameter, sof("abcd")immediately invokes__as_strSubwith twoundefinedarguments and returns a string rather than the remaining function; the subsequent call then fails or produces a wrong value. Emit staged curried wrappers, or otherwise preserve residual functions when fewer thanarityarguments are supplied.Useful? React with 👍 / 👎.