Repository navigation
fix(js-backend): refuse an unprovided builtin at compile time, by name (#832) - #842
Merged
Merged
Conversation
#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
|
Review these changes at https://app.gitnotebooks.com/hyperpolymath/affinescript/pull/842 |
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
Contributor
|
Warning Review limit reachedYou'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. View limit details
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. |
|
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.



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 onlyprint,printlnandread_line. A program that named any other builtin compiled, and the script then stopped when the call was reached:What changes
Js_codegen.generateruns a closed-world check before it emits any declaration. A free name is refused when all three hold:Resolve.seed_builtins);let, match arm, lambda orforbinds it;The refusal names every such builtin:
Refuse, not lower: why
I chose refusal for every unprovided builtin and lowered none.
len,string_get,string_sub,string_char_code_at) have no agreed meaning on a JS string until semantics: string indices mean octets on interp/wasm but UTF-16 units on Bun-ESM (owner decision) #831 is decided. Lowering them here would add another meaning to the ones feat(strings): backend-independent UTF-8 octet count and code-point view #829 measured.int_to_string,to_uppercase,trim, the math functions) could be lowered later by sharing the Bun-ESM table (option 2 in the issue). That is a feature, and it is not needed to close the bug.Codegen_denodepends onJs_codegen, so sharing the table needs the table moved to its own module first.Design notes
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.top_namestable thatgeneratealready 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).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
mainand does not need #829's API. I mergedorigin/claude/unicode-octets(161eaaf) into this branch locally to check the two together:main, 27 from feat(strings): backend-independent UTF-8 octet count and code-point view #829, 8 new here).utf8_byte_length,code_pointsandstring_from_code_pointscompiles to.jsand prints the right values undernode;lenandint_to_stringin the same build are refused.So either merge order works, with no follow-up edit.
In that merged worktree the separate
res-to-affinewalker 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)
implmethods and effect operations are erased by this backend (// impl block (erased)), so a call to one is still a run-timeReferenceError. 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_lineis typedResult[String, String], but this backend's prelude returns a bare string.Test evidence
Run on this branch at
main4248912.New tests,
test/test_js_builtin_refusal.ml:Run by hand: a program that shadows
len,int_to_stringandtrimwith its own bindings compiles to.jsand printsshadow ok: 12under bothnodeandbun.lintinjust checkprinted "ocamlformat not installed; skipping format check" in this container, so formatting was not checked here.Soundness ledger
Only
lib/js_codegen.mland tests change. No soundness path is touched, sodocs/SOUNDNESS.adocis not advanced.Generated by Claude Code