34 lines
1.3 KiB
JavaScript
34 lines
1.3 KiB
JavaScript
import { strict as assert } from "node:assert";
|
|||
|
|
import { existsSync } from "node:fs";
|
||
|
|
import { fileURLToPath, pathToFileURL } from "node:url";
|
||
|
|
import { dirname, resolve } from "node:path";
|
||
|
|
|
||
|
|
const here = dirname(fileURLToPath(import.meta.url));
|
||
|
|
const repoRoot = resolve(here, "..");
|
||
|
|
|
||
|
|
const helloPath =
|
||
|
|
process.env.HELLO_MJS ?? resolve(repoRoot, "out/build/wasm/bin/hello.mjs");
|
||
|
|
|
||
|
|
if (!existsSync(helloPath)) {
|
||
|
|
console.error(
|
||
|
|
`[wasm-smoke] hello.mjs not found at: ${helloPath}\n` +
|
||
|
|
`Did you run \`cmake --build --preset wasm\` first?\n` +
|
||
|
|
`Override with HELLO_MJS=/path/to/hello.mjs if your build dir differs.`,
|
||
|
|
);
|
||
|
|
process.exit(1);
|
||
|
|
}
|
||
|
|
|
||
|
|
const { default: createModule } = await import(pathToFileURL(helloPath).href);
|
||
|
|
const Module = await createModule();
|
||
|
|
|
||
|
|
const addResult = Module.ccall("add", "number", ["number", "number"], [2, 3]);
|
||
|
|
assert.equal(addResult, 5, `add(2, 3) returned ${addResult}, expected 5`);
|
||
|
|
|
||
|
|
const version = Module.ccall("hello_version", "number", [], []);
|
||
|
|
assert.equal(version, 1, `hello_version() returned ${version}, expected 1`);
|
||
|
|
|
||
|
|
const addWrapped = Module.cwrap("add", "number", ["number", "number"]);
|
||
|
|
assert.equal(addWrapped(40, 2), 42);
|
||
|
|
|
||
|
|
console.log("[wasm-smoke] OK — add(2,3)=5, hello_version()=1, cwrap add(40,2)=42");
|