js2 icon indicating copy to clipboard operation
js2 copied to clipboard

Host mode: a number-initialised `var` assigned a string reads back as `NaN` (`var x = 1; x = "one"`)

Open trackoor opened this issue 3 weeks ago • 3 comments

Disclosure: found and reduced with LLM-assisted differential testing; I checked the reproduction by hand on current main.

A var/let initialised with a number and later assigned a string reads back as NaN in the default (host) target. The value is lost silently, with no diagnostic.

Version

  • main at d468fa1a3cb8d713f8a7fb37ef9e0cbf45906d6b (2026-11-05), built with pnpm install --frozen-lockfile --ignore-scripts && pnpm run build
  • also @loopdive/js2 0.71.0 from npm
  • Node.js 26.13.0, Linux x86_64

Reproduction

input.js:

var x = 1;
x = "one";
console.log(x);
var s = 0;
s += "ab";
console.log(s);
function f() { let y = 1; y = "one"; return y; }
console.log(f());

run.mjs:

import fs from "node:fs";
import { compile } from "./dist/index.js";
const r = await compile(fs.readFileSync("input.js", "utf8"),
  { fileName: "input.js", allowJs: true, skipSemanticDiagnostics: true });
const { instance } = await WebAssembly.instantiate(r.binary, r.importObject);
r.importObject.__setInstance?.(instance);

The test262 worker's path (deferTopLevelInit: true, buildImports(r.imports, undefined, r.stringPool), setInstance, __module_init()) gives the same result.

Actual

NaN
NaN
NaN

Expected (node input.js)

one
0ab
one

Without skipSemanticDiagnostics the file is rejected with TS2322 "Type 'string' is not assignable to type 'number'", although it is valid JavaScript.

Related variants on the same build: var z = 1; z = true; console.log(z) prints 1; var t = "a"; t = 5; console.log(t + 1) traps with RuntimeError: illegal cast; in a .ts file, var x: any = 1; x = "one"; console.log(x) prints [Object: null prototype] {}.

Spec

x = "one" stores a String in the binding (PutValue, https://tc39.es/ecma262/#sec-putvalue), and s += "ab" with s = 0 is string concatenation (ApplyStringOrNumericBinaryOperator, step 1.c, https://tc39.es/ecma262/#sec-applystringornumericbinaryoperator).

Notes

This looks like the host-target counterpart of plan/issues/4204-* and plan/issues/4495-* (standalone target, both marked done): the binding's Wasm slot appears to be chosen from the initializer, so the later string assignment goes through a numeric conversion. src/codegen/declarations/heterogeneous-scalar-var-widening.ts says the widening predicate is not gated on standalone, but these shapes still produce NaN here.

trackoor avatar Nov 05 '26 12:11 trackoor

@trackoor Thanks for the report. Note the JS host target is in process of been merged with the standalone Wasm GC target. Can you reproduce this with the standalone target as well?

ttraenkler avatar Nov 08 '26 18:11 ttraenkler

Yes, it reproduces with target: "standalone" too. On current main (a5debe6041, Node.js 26.13.0), I compiled the same program with { allowJs: true, target: "standalone", platform: "deno" } and read x, s and f() back through exported functions: all three are NaN in both the default target and the standalone target, instead of "one", "0ab" and "one".

trackoor avatar Nov 08 '26 18:11 trackoor

Yes, it reproduces with target: "standalone" too. On current main (a5debe6, Node.js 26.13.0), I compiled the same program with { allowJs: true, target: "standalone", platform: "deno" } and read x, s and f() back through exported functions: all three are NaN in both the default target and the standalone target, instead of "one", "0ab" and "one".

Good to know. The codebase is currently undergoing a rewrite adding an IR layer, so I would keep this open until that is complete and see if it still reproduces.

ttraenkler avatar Nov 13 '26 09:11 ttraenkler