Files
scriptc/tests/corpus/dynamic-module-let-numbers.mjs
T
Chris Tate 2c7b67c5aa Keep module-level lets unboxed in the dynamic tier and return receivers from dispatch helpers
- The jsval pass also promotes dyn module globals to jsval globals when
  only dynamic-tier functions read and write them and the boxing their
  writes save outweighs the boxing their reads add, the measure locals
  use. `checksum += f(x)` in a top-level loop no longer allocates a
  number cell per update.
- A class dispatch helper that calls a method returning `this` on every
  path calls a copy of the method that returns nothing and answers its
  own receiver, the capsule of the same instance at its exact class, so
  the call boxes nothing.
2026-10-28 08:35:25 -05:00

54 lines
1.4 KiB
JavaScript

// Module-level `let` bindings that untyped code updates in loops: numbers,
// then values of other kinds, read by functions declared before and after.
function readTotal() {
return total;
}
let total = 0;
let label = "n";
let mixed = 1;
let big = 10n;
let counter = 0;
var hoisted;
function bump(n) {
return n * 2 + 1;
}
for (let i = 0; i < 1000; i++) {
total += bump(i);
label += i % 10 === 0 ? i : "";
mixed = mixed * 1.5 + i;
big += BigInt(i);
counter++;
}
console.log(total, readTotal(), label.length, mixed, big, counter);
total = total / 0;
console.log(total, readTotal());
total = -0;
console.log(Object.is(total, -0), 1 / total);
total = NaN;
console.log(total === total, String(total));
mixed = "text";
mixed += 5;
console.log(mixed, typeof mixed);
mixed = { value: 3 };
mixed.value += 4;
console.log(mixed.value, JSON.stringify(mixed));
mixed = undefined;
console.log(mixed, typeof mixed, mixed ?? "fallback");
mixed = null;
console.log(mixed === null, `${mixed}`);
hoisted = 0;
for (let i = 0; i < 10; i++) hoisted += i * 0.1;
console.log(hoisted);
const add = (n) => {
total = (Number.isNaN(total) ? 0 : total) + n;
return total;
};
for (let i = 0; i < 5; i++) add(i);
console.log(total, readTotal());
let product = 1;
for (let i = 1; i <= 25; i++) product *= i;
console.log(product, product > Number.MAX_SAFE_INTEGER);
let text = 0;
for (let i = 0; i < 3; i++) text = text + "" + i;
console.log(text, typeof text);