use temporary file for wat2wasm

This commit is contained in:
Remko Tronçon 2022-04-15 15:28:48 +02:00
parent 7e2cf5dad5
commit 9d94df431f
2 changed files with 34 additions and 23 deletions

View file

@ -102,32 +102,35 @@ async function handleBuildFinished(result) {
testIndex = testIndex.replace(`/${sourcefile}`, `/${outfile}`); testIndex = testIndex.replace(`/${sourcefile}`, `/${outfile}`);
benchmarksIndex = benchmarksIndex.replace(`/${sourcefile}`, `/${outfile}`); benchmarksIndex = benchmarksIndex.replace(`/${sourcefile}`, `/${outfile}`);
} }
fs.writeFileSync("public/waforth/index.html", index); await fs.promises.writeFile("public/waforth/index.html", index);
fs.mkdirSync("public/waforth/tests", { recursive: true }); await fs.promises.mkdir("public/waforth/tests", { recursive: true });
fs.writeFileSync("public/waforth/tests/index.html", testIndex); await fs.promises.writeFile("public/waforth/tests/index.html", testIndex);
fs.mkdirSync("public/waforth/benchmarks", { recursive: true }); await fs.promises.mkdir("public/waforth/benchmarks", { recursive: true });
fs.writeFileSync("public/waforth/benchmarks/index.html", benchmarksIndex); await fs.promises.writeFile(
"public/waforth/benchmarks/index.html",
benchmarksIndex
);
} }
if (watch) { if (watch) {
// Simple static file server // Simple static file server
createServer(function (req, res) { createServer(async function (req, res) {
let f = path.join(__dirname, "public", req.url); let f = path.join(__dirname, "public", req.url);
if (fs.lstatSync(f).isDirectory()) { if ((await fs.promises.lstat(f)).isDirectory()) {
f = path.join(f, "index.html"); f = path.join(f, "index.html");
} }
fs.readFile(f, function (err, data) { try {
if (err) { const data = await fs.promises.readFile(f);
res.writeHead(404);
res.end(JSON.stringify(err));
return;
}
res.writeHead(200); res.writeHead(200);
res.end(data); res.end(data);
}); } catch (err) {
res.writeHead(404);
res.end(JSON.stringify(err));
}
}).listen(8080); }).listen(8080);
console.log("listening on port 8080"); console.log("listening on port 8080");
buildConfig = withWatcher(buildConfig, handleBuildFinished, 8081); buildConfig = withWatcher(buildConfig, handleBuildFinished, 8081);
} }
esbuild.build(buildConfig).then(handleBuildFinished, () => process.exit(1)); esbuild.build(buildConfig).then(handleBuildFinished, () => process.exit(1));

View file

@ -24,7 +24,10 @@ function wasmTextPlugin() {
}); });
build.onLoad({ filter: /.*/, namespace: "wasm-text" }, async (args) => { build.onLoad({ filter: /.*/, namespace: "wasm-text" }, async (args) => {
// Would be handy if we could get output from stdout without going through file // Would be handy if we could get output from stdout without going through file
const out = args.path.replace(".wat", ".wasm"); const out = `wat2wasm.${Math.floor(
Math.random() * 1000000000
)}.tmp.wasm`;
try {
const flags = ""; const flags = "";
// flags = --debug-names // flags = --debug-names
// console.log("wat: compiling %s", args.path); // console.log("wat: compiling %s", args.path);
@ -33,6 +36,11 @@ function wasmTextPlugin() {
contents: await fs.promises.readFile(out), contents: await fs.promises.readFile(out),
loader: "binary", loader: "binary",
}; };
} finally {
if ((await fs.promises.lstat(out)).isFile()) {
await fs.promises.unlink(out);
}
}
}); });
}, },
}; };