Skip to content

feat(codegen-bun): intrinsics are first-class values - #864

Draft
hyperpolymath wants to merge 1 commit into
mainfrom
claude/gifted-lamport-k0mc5l-intrinsic-values
Draft

hyperpolymath wants to merge 1 commit into
mainfrom
claude/gifted-lamport-k0mc5l-intrinsic-values

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Owner ruling 2026-12-01: first-class wrappers for intrinsics on Bun-ESM.

Problem

An intrinsic used as a value, rather than called, was emitted as a bare name with no JS binding:

let f = len;   f("abcd")         // ReferenceError: len is not defined
apply1(len, s)                    // same

Declared externs were already wrapped (#818). Non-extern intrinsics (len, string_sub, trim, int_to_string, …) were not.

Change

Coverage (measured)

  • All 40 Bun builtins the type checker types are wrapped. test_intrinsic_values fails if any typed builtin ever is not.
  • The other 345 Bun builtins are stdlib extern fns, which codegen(bun-esm): extern fn used as a value emits an unbound name #818 wraps when they are declared.
  • Pre-existing gap, not fixed here: acos and asin have a Bun lowering but are neither resolvable nor typed (undefined value: acos), so no program can use them. Fixing that needs types and interpreter support, so it should be its own change.

Tests

  • tests/codegen-bun/intrinsic_values:
    • len and to_uppercase are passed to higher-order functions;
    • trim, string_sub, string_find and int_to_string are bound with let.
    • It fails on main with ReferenceError: len is not defined and passes here.
  • test/test_intrinsic_values.ml: coverage, arity from type, and exact wrapper text.
  • dune runtest: 852 pass. run_codegen_bun_tests.sh: pass, including codegen(bun-esm): extern fn used as a value emits an unbound name #818's harnesses.
  • check-soundness-ledger.sh: 5/5. dune build @fmt: exit 0. Governance: PASS.

Ledger

main is red on property 3. This ports the ledger part of #856, the same port as #862 and #863. It becomes a no-op once #856 merges.

Relation to #862

#862 adds char_to_string. Once both are merged, char_to_string is wrapped like any other typed intrinsic, with no extra code.


Generated by Claude Code

Owner ruling 2026-12-01 ("First-class wrappers"). An intrinsic such as
`len`, `string_sub` or `trim` used as a value, not called, was emitted as
a bare name with no JS binding, so calling it threw a ReferenceError
(`let f = len; f(s)`; `apply(len, s)`). Declared externs were already
wrapped (#818). Non-extern intrinsics now are too.

- `builtin_arity`: each builtin's arity, read once from its type in
  `Typecheck.register_builtins`. One source of truth, no hand-kept list.
- `wrap_builtin_value`: the lowering wrapped in an arrow of that arity.
  It is shared with the #818 extern path, which now uses it, so there is
  one wrapping rule. An awaiting lowering keeps the bare name, as #818
  did.

Coverage:
- All 40 Bun builtins the type checker types are wrapped, and the new
  suite fails if a typed builtin ever is not.
- The remaining Bun builtins are stdlib `extern fn`s, covered by #818.
- Two names, `acos` and `asin`, have a Bun lowering but are neither
  resolvable nor typed, so no program can use them. That predates this
  change and is reported in the PR.

Tests:
- tests/codegen-bun/intrinsic_values: len and to_uppercase passed to a
  higher-order fn; trim, string_sub, string_find and int_to_string bound
  with `let`. It fails on main with `ReferenceError: len is not defined`.
- test/test_intrinsic_values.ml: coverage, arity from type, and wrapper
  text.
- dune runtest: 852 pass. Bun harnesses: pass. Ledger: ports #856's part
  (main is red on property 3) and advances the stamp to 4a55253.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDiosfvobXq67wbduuK
@gitnotebooks

gitnotebooks Bot commented Dec 1, 2026

Copy link
Copy Markdown

@coderabbitai

coderabbitai Bot commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Warning

Review limit reached

You've used all free OSS reviews for now. Wait for the free limit to reset to keep reviewing this public repository.

Next included review available in 1 minute.

Check out review usage here.

View limit details

Limit details: You’ve used the included review currently available.

Learn how review limits work.

Review configuration:

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 2e6e7a15-facc-44da-8a4c-31b1c7a0d04d
Commits

Reviewing files that changed from the base of the PR and between 4a55253 and 2e42667.

Files selected for processing: 7

  • docs/SOUNDNESS.adoc
  • lib/codegen_deno.ml
  • test/test_intrinsic_values.ml
  • test/test_main.ml
  • tests/codegen-bun/intrinsic_values.affine
  • tests/codegen-bun/intrinsic_values.harness.mjs
  • tools/soundness-anchors.sha256
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

@sonarqubecloud

sonarqubecloud Bot commented Dec 1, 2026

Copy link
Copy Markdown

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants