Files
scriptc/packages/compiler/test/tuple-forof.test.ts
Chris Tate c08ab955ae Enforce linting and consistent source formatting
- Enforce oxlint and oxfmt for owned JavaScript and TypeScript with independent CI checks.
- Preserve compiler import boundaries and protected fixtures while cleaning up lint findings.
- Standardize source formatting and validate import, fixture, and embedded-source protections.
2026-10-05 17:51:24 -05:00

34 lines
1.3 KiB
TypeScript

import { mkdtemp, readFile, rm, writeFile } from "node:fs/promises";
import { tmpdir } from "node:os";
import { join } from "node:path";
import { expect, test } from "vitest";
import { compile, deserializeModule, validateModule } from "../src/index.js";
import { emitLlvmModule } from "../src/backend/llvm/emitter.js";
test("tuple iteration retains one computed receiver without allocating an array snapshot", async () => {
const dir = await mkdtemp(join(tmpdir(), "scriptc-tuple-forof-"));
try {
const entry = join(dir, "main.ts");
const outPath = join(dir, "main.ir.json");
await writeFile(
entry,
`
let calls = 0;
function pair(): [number, number] { calls++; return [2, 3]; }
let total = 0;
for (const n of pair()) total += n;
console.log(total, calls);
`,
);
const result = await compile(entry, { outDir: dir, outPath, outputKind: "ir", dynamic: false });
if (!result.ok) throw new Error(result.diagnostics.map((d) => d.message).join("\n"));
const mod = deserializeModule(await readFile(outPath, "utf8"));
expect(validateModule(mod)).toEqual([]);
const llvm = emitLlvmModule(mod);
expect(llvm.match(/call ptr @sc_f_pair\(/g)).toHaveLength(1);
expect(llvm).not.toContain("@scr_arr_new");
} finally {
await rm(dir, { recursive: true, force: true });
}
});