mirror of
https://github.com/vercel-labs/scriptc.git
synced 2026-11-03 07:54:40 +08:00
84 lines
3.3 KiB
TypeScript
84 lines
3.3 KiB
TypeScript
import { expect, test } from "vitest";
|
|
import { createRuntimePackMatrix } from "../runtime-pack-matrix.mjs";
|
|
|
|
const matrix = createRuntimePackMatrix({
|
|
target: { object_format: "elf" },
|
|
compileFlags: [],
|
|
systemLibraries: [],
|
|
});
|
|
|
|
/** The runtime pack's variant selection: the matching variant with the
|
|
* most conditions, ties by id. */
|
|
function selected(
|
|
source: string,
|
|
features: Record<string, boolean>,
|
|
): { id: string; defines: string[] } {
|
|
const unit = matrix.runtime_units.find((u: { source: string }) => u.source === source);
|
|
const matches = unit.variants.filter((variant: { when: Record<string, boolean> }) =>
|
|
Object.entries(variant.when).every(([name, expected]) => features[name] === expected),
|
|
);
|
|
matches.sort(
|
|
(a: { when: object; id: string }, b: { when: object; id: string }) =>
|
|
Object.keys(b.when).length - Object.keys(a.when).length || a.id.localeCompare(b.id),
|
|
);
|
|
return matches[0];
|
|
}
|
|
|
|
test("the typed stdio write slot selects the events build that carries it", () => {
|
|
const plain = { stdioExtended: false, nodeIsland: false, workers: false };
|
|
// Alone, the write slot selects its own build.
|
|
expect(selected("scr_events.c", { ...plain, stdioWriteSlot: true }).defines).toEqual([
|
|
"SCR_STDIO_WRITE_SLOT",
|
|
]);
|
|
// Beside the other events features it keeps each one's defines.
|
|
expect(
|
|
selected("scr_events.c", { ...plain, stdioExtended: true, stdioWriteSlot: true }).defines,
|
|
).toEqual(["SCR_STDIO_EXTENDED", "SCR_STDIO_WRITE_SLOT"]);
|
|
expect(
|
|
selected("scr_events.c", { ...plain, nodeIsland: true, stdioWriteSlot: true }).defines,
|
|
).toEqual(["SCR_NODE_ISLANDS", "SCR_STDIO_WRITE_SLOT"]);
|
|
expect(
|
|
selected("scr_events.c", {
|
|
...plain,
|
|
stdioExtended: true,
|
|
nodeIsland: true,
|
|
stdioWriteSlot: true,
|
|
}).defines,
|
|
).toEqual(["SCR_STDIO_EXTENDED", "SCR_NODE_ISLANDS", "SCR_STDIO_WRITE_SLOT"]);
|
|
// A program that never reaches the write slot keeps the old object.
|
|
expect(
|
|
selected("scr_events.c", {
|
|
...plain,
|
|
stdioExtended: true,
|
|
nodeIsland: true,
|
|
stdioWriteSlot: false,
|
|
}).defines,
|
|
).toEqual(["SCR_STDIO_EXTENDED", "SCR_NODE_ISLANDS"]);
|
|
});
|
|
|
|
test("web streams and Node islands select an async build that defines both", () => {
|
|
const variant = selected("scr_async.c", { webStreams: true, nodeIsland: true, workers: false });
|
|
expect(variant.defines).toEqual(
|
|
expect.arrayContaining(["SCR_NATIVE_REACTIONS", "SCR_NODE_ISLANDS"]),
|
|
);
|
|
// Each feature alone still selects its own build, byte for byte.
|
|
expect(
|
|
selected("scr_async.c", { webStreams: true, nodeIsland: false, workers: false }).defines,
|
|
).toEqual(["SCR_NATIVE_REACTIONS"]);
|
|
expect(
|
|
selected("scr_async.c", { webStreams: false, nodeIsland: true, workers: false }).defines,
|
|
).toEqual(["SCR_NODE_ISLANDS"]);
|
|
expect(
|
|
selected("scr_async.c", { webStreams: false, nodeIsland: false, workers: false }).defines,
|
|
).toEqual([]);
|
|
});
|
|
|
|
test("the BigIntStats unit follows its own member-read gate, not bigint", () => {
|
|
const unit = matrix.runtime_units.find(
|
|
(u: { source: string }) => u.source === "scr_stats_bigint.c",
|
|
);
|
|
// Every bigint value turns the bigint feature on; only the member reads
|
|
// name bigstats, so a bigint program keeps this unit out of its binary.
|
|
expect(unit.predicate).toBe("bigstats");
|
|
});
|