Files
scriptc/packages/cli/test/output-options.test.ts
T
Malte Ubl 6a9475e931 Free the address space after process.exit off the critical path (--detached-exit)
`scriptc build|run --detached-exit` opts an executable into handing the address-space teardown after process.exit to a helper process on Linux. The helper shares the address space (CLONE_VM), blocks signals, closes every descriptor but the read end of a pipe and waits for EOF on it. The kernel releases an exiting process's memory (exit_mm) before its files (exit_files), so EOF means the helper holds the last reference; it then exits and the kernel frees the pages there instead of before the parent sees the exit (30-40 ms per GB of 4 KiB pages; tsc-ts 20-110 ms). The same latency mold hides with --fork.

It is a build-time choice, not a run-time knob, because the helper keeps the executable mapped (overwriting the binary right after it exits can fail with ETXTBSY) and the memory charged to the cgroup for up to ~100 ms, and the teardown CPU time leaves the parent's rusage. The program object defines `scr_detached_exit = 1`, overriding the runtime's weak default of 0, so no runtime flavor or linker flag is added. The option is part of the early executable cache and route keys; executables only, no effect on other platforms.
2026-10-11 07:05:29 -07:00

116 lines
3.9 KiB
TypeScript

import { describe, expect, test } from "vitest";
import { resolveOutputOptions, type OutputOptionValues } from "../src/output-options.js";
const BASE: OutputOptionValues = {
emitIr: false,
keepLlvm: true,
sanitize: false,
};
describe("output option compatibility", () => {
test.each([
[undefined, undefined, "exe", "llvm"],
["exe", "llvm", "exe", "llvm"],
["ir", undefined, "ir", undefined],
["llvm", undefined, "llvm", "llvm"],
["llvm", "llvm", "llvm", "llvm"],
["asm", undefined, "asm", "llvm"],
["obj", "llvm", "obj", "llvm"],
] as const)("accepts --emit=%s --backend=%s", (emit, backend, outputKind, expectedBackend) => {
const result = resolveOutputOptions("build", {
...BASE,
...(emit === undefined ? {} : { emit }),
...(backend === undefined ? {} : { backend }),
});
expect(result).toMatchObject({
ok: true,
outputKind,
...(expectedBackend === undefined ? {} : { backend: expectedBackend }),
});
});
test.each([
[{ emit: "wat" }, /unknown emit kind/],
[{ emit: "asm", backend: "c" }, /unknown backend/],
[{ emit: "obj", backend: "c" }, /unknown backend/],
[{ emit: "c" }, /unknown emit kind/],
[{ emit: "llvm", backend: "c" }, /unknown backend/],
[{ emit: "ir", keepLlvm: false }, /no-keep-llvm/],
[{ emit: "llvm", sanitize: true }, /sanitize/],
[{ emit: "llvm", optimization: "dev" }, /optimization/],
[{ emit: "ir", backend: "llvm" }, /before backend selection/],
[{ emit: "llvm", emitIr: true }, /emit-ir/],
[{ emit: "ir", emitIr: true }, /same output/],
[{ backend: "wat" }, /unknown backend/],
[{ emit: "obj", windowsSubsystem: "gui" }, /windows-subsystem/],
[{ emit: "llvm", windowsSubsystem: "gui" }, /windows-subsystem/],
[{ emit: "obj", strip: true }, /strip/],
[{ emit: "llvm", strip: true }, /strip/],
[{ emit: "obj", detachedExit: true }, /detached-exit/],
[{ emit: "llvm", detachedExit: true }, /detached-exit/],
] as const)("rejects %j", (override, message) => {
const result = resolveOutputOptions("build", { ...BASE, ...override });
expect(result).toEqual({ ok: false, message: expect.stringMatching(message) });
});
test.each(["ir", "llvm"])("run rejects --emit=%s", (emit) => {
expect(resolveOutputOptions("run", { ...BASE, emit })).toEqual({
ok: false,
message: "scriptc run requires --emit=exe",
});
});
test.each(["asm", "obj"])("run rejects --emit=%s", (emit) => {
expect(resolveOutputOptions("run", { ...BASE, emit })).toEqual({
ok: false,
message: "scriptc run requires --emit=exe",
});
});
test.each(["asm", "obj"])(
"native outputs accept optimization and sanitizer for compiler-level validation",
(emit) => {
expect(
resolveOutputOptions("build", {
...BASE,
emit,
optimization: "dev",
sanitize: true,
}),
).toMatchObject({ ok: true, outputKind: emit, backend: "llvm" });
},
);
test("the deprecated alias remains additive for executable builds", () => {
expect(resolveOutputOptions("build", { ...BASE, emitIr: true })).toMatchObject({
ok: true,
outputKind: "exe",
emitIr: true,
deprecateEmitIr: true,
});
});
test("source outputs retain frontend-affecting FFI manifests", () => {
expect(
resolveOutputOptions("build", { ...BASE, emit: "llvm", ffi: "native.json" }),
).toMatchObject({
ok: true,
outputKind: "llvm",
});
});
test("executable outputs accept a Windows subsystem selection", () => {
expect(resolveOutputOptions("build", { ...BASE, windowsSubsystem: "gui" })).toMatchObject({
ok: true,
outputKind: "exe",
});
});
test("executable outputs accept stripping", () => {
expect(resolveOutputOptions("build", { ...BASE, strip: true })).toMatchObject({
ok: true,
outputKind: "exe",
});
});
});