Skip to content

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

Done

Description

@hyperpolymath

Evidence (main ee211d3)

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

lib/js_codegen.ml:171 emits ExprVar as a bare mangled name. Nothing checks it against the builtins the JS-text backend actually provides. The same happens for len, string_get, string_sub, int_to_string and the other string builtins.

Classification

This is a divergence, not a soundness hole. It fails loudly, but at run time rather than compile time. The Bun-ESM backend lowers all of these builtins.

Options

Ranked by elegance and correctness first, then by dependability.

  1. (Recommended: most elegant and correct) Do a closed-world check at codegen. Each backend declares which builtins it can lower. Any free identifier that is neither bound by the program nor in that table makes codegen return Error ("int_to_string is not available on the JS backend; use Bun-ESM"). The bug class becomes a compile-time refusal on every text backend, not only on JS.
  2. (Most dependable) Share the Bun-ESM deno_builtins lowering table with js_codegen. There would be one source of truth, as feat(strings): backend-independent UTF-8 octet count and code-point view #829 already did for its new intrinsics. Do this together with option 1.
  3. (Outside the box) Retire the plain-JS backend in favour of Bun-ESM, as was done for Deno-ESM. .js would become an alias for Bun-ESM, or be refused.

This does not change language semantics: a refusal only states what the backend can do.


Generated by Claude Code

Activity

  1. added
    bug
    on Dec 1, 2026
  2. whatproblem-ai commented on Dec 1, 2026

    @whatproblem-ai

    Problem: On main ee211d3, the plain-JS backend compiles programs that use string builtins (int_to_string, len, string_get, string_sub, and others) without error, then crashes in Node with ReferenceError. lib/js_codegen.ml:171 emits ExprVar as a bare mangled name and never checks it against the builtins that backend provides. Bun-ESM lowers these builtins correctly.

    Expected: Compilation of idx.affine to .js should fail with an Error naming the unsupported builtin, rather than succeeding.

    Missing: source of idx.affine


    Clarity: 92%. Clear enough to work on.

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

  3. added
    architecture
    enhancement
    priority:p2
    scope:repo
    on Dec 1, 2026
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

    architecturebugenhancementpriority:p2scope:repo

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions