Repository navigation
Commit ff3f08d
feat(strings): backend-independent UTF-8 octet count and code-point view (#829)
## For the DEED printer thread (metadatastician/standards)
Switch to these names. They mean the same thing on every backend:
| Name | Kind | Meaning |
|---|---|---|
| `utf8_byte_length(s: String) -> Int` | intrinsic | octets of the UTF-8
encoding of `s` |
| `code_points(s: String) -> [Int]` | intrinsic | the Unicode scalar
values of `s` |
| `string_from_code_points(cps: [Int]) -> String` | intrinsic | inverse
of `code_points` |
| `utf8_width(cp: Int) -> Int` | `use utf8::{…}` | 1/2/3/4 octets for
one scalar value |
| `code_point_count(s: String) -> Int` | `use utf8::{…}` | number of
scalar values |
| `code_point_slice(s, start, end) -> String` | `use utf8::{…}` |
`[start, end)` in code points, clamped; never splits a character |
| `code_points_within_octets(s, max_octets) -> Int` | `use utf8::{…}` |
how many leading code points fit in `max_octets`; Bun-ESM only (the
interpreter can't run self-recursion, see Findings) |
Do **not** use `len`, `string_get`, `string_sub`, `string_length`,
`char_at` or `substring` to count octets. On Bun-ESM they count UTF-16
code units.
## What and why
Ground truth is recorded in `docs/UNICODE-STRINGS.adoc`. It was measured
by compiling and running probes on `main` `ee211d3`:
| Operation | interp | Bun ESM | plain JS | core wasm |
|---|---|---|---|---|
| `len` of `a`/`é`/`漢`/`🦜` | 1/2/3/4 (octets) | 1/1/1/2 (UTF-16 units) |
`ReferenceError` | 1/2/3/4 |
| `string_get("🦜",0)` → `char_to_int` | 240 | 55358 (lone surrogate) |
`ReferenceError` | refused at compile time |
| `string_char_code_at("🦜",0/1)` | 240/159 | 55358/56732 |
`ReferenceError` | 240/159 |
| `string_sub("🦜x",0,1)` | ill-formed octet | lone surrogate |
`ReferenceError` | ill-formed octet |
| char literal `'é'` | lexer rejects it on every backend (`Char` is one
octet) | | | |
Deno-ESM is retired (`--deno-esm` is refused), so the Bun-ESM column
covers it.
This PR is **additive**: `len`, `string_get` and `string_sub` keep their
current meaning. The three intrinsics are lowered on the interpreter,
Bun-ESM, plain JS and core wasm.
Each is defined only on well-formed input and fails loudly otherwise: a
run-time error on the interpreter, a JS throw, or a wasm trap.
Ill-formed input means a string cut by `string_sub`, a lone surrogate,
overlong or truncated UTF-8, or a non-scalar `Int`. None of them ever
substitutes U+FFFD.
## Assumptions a later proof can rely on (tested, not proved)
1. A well-formed `String` denotes a sequence of Unicode scalar values.
2. `string_from_code_points ∘ code_points = id` on well-formed strings,
and `code_points ∘ string_from_code_points = id` on arrays of scalar
values.
3. `utf8_byte_length(s) = Σ utf8_width(cp)` over `code_points(s)`, using
RFC 3629 shortest-form widths.
4. On Bun, writing a well-formed string as UTF-8 emits exactly
`utf8_byte_length(s)` octets.
5. There is no Unicode normalisation.
6. Integers are exact: wasm i32 and JS Number.
7. The trusted base is OCaml's `String.get_utf_8_uchar`/`Uchar`, JS
`charCodeAt`/`fromCodePoint`, and the wasm decode and encode loops in
`lib/codegen.ml`.
## Tests
`test/test_unicode_octets.ml` is part of `dune runtest`. It runs
fixtures through the real CLI on interp, Bun-ESM, plain JS (node) and
core wasm (`bun run`, Bun's WASI). No hand-written JS is involved. Each
backend is checked against RFC 3629 / Unicode values, not against
another backend.
The suite was mutation-checked:
- a wrong 2-octet width in the Bun helper fails the Bun-ESM checks;
- a missing surrogate check in the wasm encoder fails the wasm
loud-failure check.
```
just check → exit 0
test_main: Test Successful, 691 tests run (incl. 28 new Unicode cases)
res-to-affine walker: Test Successful, 32 tests run
OK: no extension.ts / doc-truthing / DEED gates / soundness ledger (5 properties) / capability anchors
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)
```
Notes on the test run:
- `lint` is a no-op: the repo has no `.ocamlformat`, so ocamlformat
disables itself. That predates this PR.
- In CI's coverage job (no Bun), the Bun and wasm cases print `SKIP` and
the interp cases still run. The `build` job has Bun and Node and runs
all four backends.
## Soundness-ledger stamp and gate fix (owner decision: "thread bumps +
fixes check")
This PR touches `lib/{interp,codegen,codegen_deno,typecheck}.ml`, which
are ledger soundness paths.
- `66a314e` advances `:ground-truth-sha:` to `ee211d3` and
`:ground-truth-date:` to `2026-12-01`. That is current `main`, verified
together with this branch's changes.
- `34f5ecc` and `f2ef469` fix ledger **property 4**, in their own
commits.
- `b70e9df` advances the stamp again, to `c2a5493`. The owner merged
`main` into this branch (`809c42d`), bringing in #840, which had itself
set the stamp to `ee211d3`, so property 4 correctly required a fresh
bump against the new base. I re-verified on the merged head: `dune
runtest --force` 701 + 32 + 3 tests pass; the codegen corpora (48 + 42 +
8 harnesses) pass; all 5 guards pass.
**The bug.** Property 4 diffed the tree against the stamp commit `S` and
passed if that diff showed a stamp-line change. The commit that sets the
stamp to `S` always comes after `S`, so the diff always contained one,
and the property passed for every later change. At least ten unbumped
soundness-path PRs merged after #802 that way.
**The new rule (`f2ef469`).** Property 4 judges *the change*:
`merge-base(HEAD, origin/main)` to the working tree, which is what gets
squash-merged. If that change touches a soundness path, it must also
change `:ground-truth-sha:`. Without `origin/main`, the fallback is a
history rule: nothing on a soundness path may change after the last
commit that changed the stamp line. That rule is merge-aware, so a bump
made in a merge commit is found.
I first used the history rule as the primary check (`34f5ecc`). It could
never pass a PR that re-verifies against an unchanged `main`, because
re-stamping to the same SHA changes no line, and it turned every PR red
while `main` was stale. `f2ef469` replaces it.
Planted cases, run in an isolated clone:
| Case | Old | New (with `origin/main`, as in CI) | New (fallback) |
|---|---|---|---|
| A pristine `main` | pass | pass | FAIL (stale `main`) |
| B unbumped soundness edit | pass | **FAIL** | **FAIL** |
| C edit + bump in one change | pass | pass | pass |
| D bump, then a later edit in the same branch | pass | pass (same
change) | FAIL |
| E edit commit, then bump commit | pass | pass | pass |
| F bump made in a merge commit | pass | pass | pass |
| G merge of an unbumped soundness change | pass | **FAIL** | **FAIL** |
| H unrelated docs-only change | pass | pass | FAIL (stale `main`) |
**Effect on other open PRs.** None, unless the PR touches a soundness
path; then it must advance the stamp in that PR. A stale `main` no
longer turns unrelated PRs red. The existing "main moved a soundness
path under this branch; rebase" check is unchanged.
## Review follow-ups (CodeRabbit)
- **User definitions now shadow the three intrinsics on every backend
(`4f5eb58`).**
- Core wasm used to emit the intrinsic anyway and print a heap pointer;
its three arms are now guarded by `user_binds`.
- The plain-JS backend refused such declarations, a regression from this
PR. Its helpers are now internal `__as_*` names, bound to the source
names only when the program doesn't declare them.
- The JS int-division classifier no longer treats a declared fn named
like an Int intrinsic as returning Int.
- New tests cover all four backends plus two int-division cases. Each
was mutation-checked: it fails with its fix reverted.
- **The `stdlib/utf8.affine` header** now says
`code_points_within_octets` runs on Bun-ESM only.
- **Not changed here: using an intrinsic as a value on Bun-ESM** (for
example `let f = code_points; f(s)`). It emits a bare name for every
intrinsic, `len` included, and that predates this PR. Direct calls work
on all four backends, and on plain JS the three new names now also work
as values.
- **Fixed on `main` by #840: a wasm parameter now shadows a same-named
top-level fn.** I reported this case with a repro in review, and it now
prints 9 on the merged head, matching interp and Bun-ESM.
- **Also not changed: shadowing for the older wasm intrinsics.** A user
`string_sub` still prints a heap pointer on wasm, on `main` today. Some
of those arms deliberately intercept stdlib externs, so it needs its own
audit; it is recorded in `docs/UNICODE-STRINGS.adoc`.
Fresh results on `f2ef469`'s code:
- `dune runtest --force`: 697 + 32 + 3 tests pass;
- the 3 codegen corpora: 48 + 42 + 8 harnesses pass;
- all 5 guards: OK.
## Owner decision still open
- **Commit signatures.** The commits are signed by the session's own
Claude key. You planned to re-sign them before merge.
## Findings (all reproduce on `main` without this PR; only 8 is fixed
here)
1. **The string builtins disagree between backends** (the table above).
This is a semantic divergence in the #560 class, not type unsoundness.
Proposed separate fix:
- make string indices octet offsets everywhere (the Rust model);
- make `string_sub`/`string_get` fail loudly off a character boundary;
- point code that needs characters at this API.
2. **Core-wasm heap overwrites string literals.** This is the most
serious finding. The heap starts at a fixed `1024`, but literals are
laid out from `2048`. Any program that allocates more than about 1 KiB
corrupts its own literals. Repro: 60 × `println("label_number_N" ++ "
ok")` compiled to wasm prints 21 lines, then `label_number_22abe`, then
traps with `Out of bounds memory access`. The interpreter prints all 60.
Proposed fix: start the heap at `align(next_string_offset, 8)`. The new
suite keeps its success path allocation-free so it tests the primitives,
not this.
3. **`affinescript eval` can't run a self-recursive top-level fn.**
`TopFn` builds the closure with `cl_env = env` before binding its own
name. `fn g(x) { … g(x - 1) … }` fails with `Unbound variable: g`; Bun
gives the right answer.
4. **`affinescript eval` rejects all mutable locals.** `let mut k = 0;
while k < 3 { k = k + 1; }` fails with `Type mismatch: Expected mutable
reference`.
5. **`string::char_at` fails under `eval`** with `Unbound variable:
Some`. The root cause is not established.
6. **The plain-JS backend lowers no string builtins** (`len`,
`int_to_string`, …). Each is a run-time `ReferenceError`, not a
compile-time refusal.
7. **Interp `println` prints a String as an escaped OCaml literal**,
quotes included.
8. **Soundness-ledger property 4** could not detect a missing stamp
bump. This one *is* fixed here, in `34f5ecc` and `f2ef469`, by owner
decision (see above).
## Coordination
The new Bun-ESM entries sit in the shared `deno_builtins`
string-primitive block next to `string_sub`, plus helpers next to
`__as_strFind`. The `bun_builtins` table is untouched, so this should
not collide with the typed-Bun-host-I/O thread.
https://claude.ai/code/session_01J4QvDfgDTxx9Bumfa2XXcQ
---
_Generated by [Claude Code](https://claude.ai/code)_
---------
Signed-off-by: Jonathan D.A. Jewell <6759885+hyperpolymath@users.noreply.github.com>
Co-authored-by: Claude <noreply@anthropic.com>1 parent 2f1526e commit ff3f08d
18 files changed
Lines changed: 1110 additions & 15 deletions
File tree
- docs
- lib
- stdlib
- test
- tools
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
7 | | - | |
| 7 | + | |
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| |||
| 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 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
| 201 | + | |
| 202 | + | |
| 203 | + | |
| 204 | + | |
| 205 | + | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
| 209 | + | |
| 210 | + | |
| 211 | + | |
| 212 | + | |
| 213 | + | |
| 214 | + | |
| 215 | + | |
| 216 | + | |
| 217 | + | |
| 218 | + | |
| 219 | + | |
| 220 | + | |
| 221 | + | |
| 222 | + | |
| 223 | + | |
| 224 | + | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
| 253 | + | |
| 254 | + | |
| 255 | + | |
| 256 | + | |
| 257 | + | |
| 258 | + | |
| 259 | + | |
| 260 | + | |
| 261 | + | |
0 commit comments