Repository navigation
Commit 70180a1
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.
- The string index builtins (`len`, `string_get`, `string_sub`,
`string_char_code_at`) have no agreed meaning on a JS string until #831
is decided. Lowering them here would add another meaning to the ones
#829 measured.
- The unambiguous ones (`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_deno` depends on `Js_codegen`, so sharing the
table needs the table moved to its own module first.
## 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 #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 #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](https://claude.ai/code)_
Co-authored-by: Claude <noreply@anthropic.com>
1 parent 84eca60 commit 70180a1
3 files changed
Lines changed: 256 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
611 | 611 | | |
612 | 612 | | |
613 | 613 | | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
| 677 | + | |
| 678 | + | |
| 679 | + | |
| 680 | + | |
| 681 | + | |
| 682 | + | |
| 683 | + | |
| 684 | + | |
| 685 | + | |
| 686 | + | |
| 687 | + | |
| 688 | + | |
| 689 | + | |
| 690 | + | |
| 691 | + | |
614 | 692 | | |
615 | 693 | | |
616 | 694 | | |
| |||
661 | 739 | | |
662 | 740 | | |
663 | 741 | | |
| 742 | + | |
| 743 | + | |
| 744 | + | |
| 745 | + | |
664 | 746 | | |
665 | 747 | | |
666 | 748 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| 5 | + | |
| 6 | + | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
| 10 | + | |
| 11 | + | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
| 71 | + | |
| 72 | + | |
| 73 | + | |
| 74 | + | |
| 75 | + | |
| 76 | + | |
| 77 | + | |
| 78 | + | |
| 79 | + | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
34 | 34 | | |
35 | 35 | | |
36 | 36 | | |
| 37 | + | |
37 | 38 | | |
38 | 39 | | |
39 | 40 | | |
| |||
0 commit comments