Skip to content

semantics: string indices mean octets on interp/wasm but UTF-16 units on Bun-ESM (owner decision) #831

Backlog

Description

@hyperpolymath

Needs an owner decision. This changes language semantics.

Evidence (main ee211d3, probed 2026-12-01)

Probe interp (eval) core wasm Bun-ESM
len("é") 2 2 1
len("🦜") 4 4 2
string_char_code_at("🦜", 0) 240 240 55358 (a lone surrogate)

Cause: lib/codegen_deno.ml:1240 lowers len to JS .length, which counts UTF-16 code units. string_get and string_sub (:1244-1245) use JS indices in the same way. The interpreter and wasm count UTF-8 octets.

Classification

This is a silent backend divergence (#560 class), not a type-soundness hole. The same well-typed program prints different values on different backends, and nothing fails.

Options

Ranked by elegance and correctness first, then by dependability.

  1. (Recommended: most elegant and correct) Use octet offsets everywhere, with boundary checks. len counts UTF-8 octets on every backend. string_get and string_sub fail loudly when an offset is not on a character boundary. Bun lowers these through a UTF-8 view of the string. Programs that need characters move to the feat(strings): backend-independent UTF-8 octet count and code-point view #829 API (code_points, code_point_slice). This is the Rust model.
  2. (Most dependable, type-led) Index strings through an opaque offset type instead of Int. Offsets would be a newtype such as Utf8Offset, produced only by find, by iteration, or by utf8_byte_length. Mixing units would then be a type error rather than a runtime value. Pairs with option 1.
  3. (Outside the box) Represent String on Bun as a Uint8Array of UTF-8. All backends would then share one representation. The cost is JS interop: every host boundary has to transcode.
  4. (Stop-gap) Refuse len, string_get and string_sub on Bun-ESM at compile time and point to the feat(strings): backend-independent UTF-8 octet count and code-point view #829 API. This is a loud failure, but it breaks existing Bun code.

Scope

This is wider than this repo. Estate code compiled to Bun-ESM, such as the DEED printer thread, counts in UTF-16 units today.

The audit is in docs/reports/AUDIT-2026-12-01-backend-divergences.adoc (PR for #830).


Generated by Claude Code

Activity

  1. added
    bindings
    decision
    migration
    on Dec 1, 2026
  2. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: On main ee211d3, len, string_get and string_sub count UTF-8 octets on interp and core wasm but UTF-16 code units on Bun-ESM. The cause is lib/codegen_deno.ml:1240-1245, which lowers them to JS .length and JS indices. The same well-typed program silently prints different values per backend; for example, len("🦜") is 4 vs 2, and string_char_code_at returns a lone surrogate on Bun.

    Expected: Identical string-index semantics on every backend.

    Missing: owner's choice among options 1–4, migration policy for estate Bun-ESM code (e.g. DEED printer)


    Clarity: 93%. Clear enough to work on.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

  3. coderabbitai commented on Dec 1, 2026

    @coderabbitai
    Contributor
    🔗 Related PRs

    #829 - feat(strings): backend-independent UTF-8 octet count and code-point view [open]


    📝 Issue Planner

    Check the box below or use the @coderabbitai plan command to generate an implementation plan and prompts that you can use with your favorite coding assistant.

    • Create Plan

    🧪 Issue enrichment is currently in open beta.

    You can configure auto-planning by selecting labels in the issue_enrichment configuration.

    To disable automatic issue enrichment, add the following to your .coderabbit.yaml:

    issue_enrichment:
      auto_enrich:
        enabled: false

    💬 Have feedback or questions? Drop into our discord!

  4. added
    architecture
    bug
    priority:p2
    scope:repo
    on Dec 1, 2026
  5. hyperpolymath commented on Dec 1, 2026

    @hyperpolymath
    OwnerAuthor

    Reopened: this issue was closed by accident, not by a decision.

    PR #844 merged a draft decision record and was meant to reference this issue without closing it. Its description contained the phrase "does not close #831", and GitHub read the words "close #831" in the squash commit message as a closing keyword. The issue was closed two seconds after the merge.

    The owner decision on string index units is still open. The draft record with the usage numbers, the Bun benchmark, the options and a recommendation is docs/decisions/0028-string-index-units.adoc on main.

    If the closure was in fact intended, please close it again.


    Generated by Claude Code

  6. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    The reopen fits what was already understood: the closure came from GitHub reading "close #831" inside "does not close #831" in PR #844's squash commit, not from an owner decision. The draft record docs/decisions/0028-string-index-units.adoc on main now holds the usage numbers, the Bun benchmark and a recommendation, but the choice among options 1–4 is still open.


    Clarity: 90%. Nothing further needed.

    ⌨️ Commands · 💬 Have feedback? Tell us · 🎯 New here? Add WhatProblem to your repo · 🔒 Privacy

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    architecturebindingsbugdecisionmigrationpriority:p2scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions