Skip to content

fix(js-backend): refuse an unprovided builtin at compile time, by name (#832) - #842

Merged
hyperpolymath merged 3 commits into
mainfrom
claude/js-backend-builtin-refusal-832
Dec 1, 2026
Merged

hyperpolymath merged 3 commits into
mainfrom
claude/js-backend-builtin-refusal-832

Conversation

@hyperpolymath

Copy link
Copy Markdown
Owner

Closes #832

What was wrong

The JS-text backend (lib/js_codegen.ml, output *.js) emits a variable as its bare name. Its runtime prelude defines only print, println and read_line. A program that named any other builtin compiled, and the script then stopped when the call was reached:

$ affinescript compile idx.affine -o idx.js      # succeeded
$ node idx.js
ReferenceError: int_to_string is not defined

What changes

Js_codegen.generate runs a closed-world check before it emits any declaration. A free name is refused when all three hold:

  • the resolver seeds it as a builtin (Resolve.seed_builtins);
  • the program does not bind it at top level, and no enclosing parameter, let, match arm, lambda or for binds it;
  • the emitted runtime does not bind it.

The refusal names every such builtin:

$ affinescript compile idx.affine -o idx.js
JS codegen error: builtins `int_to_string`, `len` are not provided by the JS-text backend:
the emitted script would stop with a ReferenceError when the reference is reached (#832).
The Bun-ESM backend (`--bun-esm`, output `.bun.js`) and the interpreter (`eval`) provide more of the builtins

Refuse, not lower: why

I chose refusal for every unprovided builtin and lowered none.

Design notes

  • One source for "is a builtin". The set is read from Resolve.seed_builtins. A builtin added to the resolver is refused here until this backend provides it. A test walks the whole set and checks each name is either bound by the emitted script or refused.
  • One source for "the runtime provides it". The check reads the top_names table that generate already fills for the prelude. An alias registered there is accepted without a second list. This is what keeps feat(strings): backend-independent UTF-8 octet count and code-point view #829 working (below).
  • Scoping comes from Ast.free_names, the walker import flattening already uses, so shadowing follows the same rules as the rest of the compiler.

Interaction with #829 (open, not merged)

This branch is based on main and does not need #829's API. I merged origin/claude/unicode-octets (161eaaf) into this branch locally to check the two together:

  • The merge is clean, with no conflicts.
  • The main suite passes: 715 tests (680 on main, 27 from feat(strings): backend-independent UTF-8 octet count and code-point view #829, 8 new here).
  • On the merged build, a program using utf8_byte_length, code_points and string_from_code_points compiles to .js and prints the right values under node; len and int_to_string in the same build are refused.

So either merge order works, with no follow-up edit.

In that merged worktree the separate res-to-affine walker suite failed 32 tests with "tree-sitter grammar not built". That is the untracked vendored grammar being absent from a fresh worktree; the same suite passes in the main checkout.

Behaviour change to be aware of

A program that names an unprovided builtin only on a path it never executes used to compile and run. It is now refused. That is the intended closed-world behaviour, but it is a change.

Not fixed here (found while testing)

  • impl methods and effect operations are erased by this backend (// impl block (erased)), so a call to one is still a run-time ReferenceError. They are not resolver builtins, so this check does not cover them. The issue's option 1 in full (any unbound free identifier) would.
  • read_line is typed Result[String, String], but this backend's prelude returns a bare string.

Test evidence

Run on this branch at main 4248912.

$ dune runtest --force
Test Successful in 0.608s. 680 tests run.        # main suite, 8 new
Test Successful in 2.113s. 32 tests run.         # res-to-affine walker
exit=0

$ just check
OK: capability anchors intact — Test-anchors section present + every named test exists.
exit=0

$ tools/run_codegen_wasm_tests.sh
All codegen WASM tests passed (48 JS harnesses).
$ tools/run_codegen_deno_tests.sh
All codegen Bun-ESM (legacy codegen-deno corpus) tests passed (42 harnesses).
$ tools/run_codegen_bun_tests.sh
All native Bun-ESM tests passed (8 harnesses).

New tests, test/test_js_builtin_refusal.ml:

[OK]  JS-text backend refuses unprovided builtins (#832)  0  issue #832 example is refused by name.
[OK]  JS-text backend refuses unprovided builtins (#832)  1  each string builtin is refused by its own name.
[OK]  JS-text backend refuses unprovided builtins (#832)  2  every resolver builtin is bound or refused.
[OK]  JS-text backend refuses unprovided builtins (#832)  3  prelude builtins still compile.
[OK]  JS-text backend refuses unprovided builtins (#832)  4  a program binding shadows the builtin.
[OK]  JS-text backend refuses unprovided builtins (#832)  5  shadowing is scoped.
[OK]  JS-text backend refuses unprovided builtins (#832)  6  nested references are refused.
[OK]  JS-text backend refuses unprovided builtins (#832)  7  program without builtins is unaffected.

Run by hand: a program that shadows len, int_to_string and trim with its own bindings compiles to .js and prints shadow ok: 12 under both node and bun.

lint in just check printed "ocamlformat not installed; skipping format check" in this container, so formatting was not checked here.

Soundness ledger

Only lib/js_codegen.ml and tests change. No soundness path is touched, so docs/SOUNDNESS.adoc is not advanced.


Generated by Claude Code

#832)

The JS-text backend emits a variable as its bare name, and its runtime
prelude defines only print, println and read_line.  A program that named
any other builtin (len, string_sub, int_to_string, ...) compiled and then
stopped with a ReferenceError when the call was reached.

Js_codegen.generate now runs a closed-world check before emitting any
declaration: a free name that the resolver seeds as a builtin, that the
program does not bind itself and that the emitted runtime does not bind,
is refused with a message naming every such builtin.

- The builtin set is read from Resolve.seed_builtins, so a builtin added
  to the resolver is refused here until this backend provides it.
- 'The runtime binds it' is read from the same top_names table that
  generate fills for the prelude, so an alias added there (the Unicode
  primitives of #829) is accepted without a second list.
- Free names come from Ast.free_names, so a parameter, let, match-arm,
  lambda or for binder of the same name hides the builtin, as it does in
  the emitted script.

Chosen over lowering: the string index builtins have no agreed meaning
on a JS string until #831 is decided, so lowering them here would add a
fourth meaning to the ones measured in #829.

Closes #832

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDhJog6g9kprphCAadr
@gitnotebooks

gitnotebooks Bot commented Dec 1, 2026

Copy link
Copy Markdown

@hyperpolymath
hyperpolymath marked this pull request as ready for review December 1, 2026 18:31
Conflict in test/test_main.ml: both sides added a suite to the list
(#832 here, #827 on main). Both kept.

Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01J4QvDhJog6g9kprphCAadr
@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 44 minutes.

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: 12283078-16df-469b-90f8-054fcf2e7bb5
Commits

Reviewing files that changed from the base of the PR and between 84eca60 and 5c288b1.

Files selected for processing: 3

  • lib/js_codegen.ml
  • test/test_js_builtin_refusal.ml
  • test/test_main.ml
  • 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

@hyperpolymath
hyperpolymath merged commit 70180a1 into main Dec 1, 2026
18 checks passed
@hyperpolymath
hyperpolymath deleted the claude/js-backend-builtin-refusal-832 branch December 1, 2026 18:52
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.

js backend: string builtins are not lowered and crash at run time (ReferenceError) instead of being refused at compile time

2 participants