From 9d94df431f3f2d315ecd4264e0f816ca5de1f357 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Fri, 15 Apr 2022 15:28:48 +0200 Subject: [PATCH] use temporary file for wat2wasm --- build.js | 31 +++++++++++++++++-------------- scripts/esbuild/wasm-text.js | 26 +++++++++++++++++--------- 2 files changed, 34 insertions(+), 23 deletions(-) diff --git a/build.js b/build.js index a471d23..46648dc 100755 --- a/build.js +++ b/build.js @@ -102,32 +102,35 @@ async function handleBuildFinished(result) { testIndex = testIndex.replace(`/${sourcefile}`, `/${outfile}`); benchmarksIndex = benchmarksIndex.replace(`/${sourcefile}`, `/${outfile}`); } - fs.writeFileSync("public/waforth/index.html", index); - fs.mkdirSync("public/waforth/tests", { recursive: true }); - fs.writeFileSync("public/waforth/tests/index.html", testIndex); - fs.mkdirSync("public/waforth/benchmarks", { recursive: true }); - fs.writeFileSync("public/waforth/benchmarks/index.html", benchmarksIndex); + await fs.promises.writeFile("public/waforth/index.html", index); + await fs.promises.mkdir("public/waforth/tests", { recursive: true }); + await fs.promises.writeFile("public/waforth/tests/index.html", testIndex); + await fs.promises.mkdir("public/waforth/benchmarks", { recursive: true }); + await fs.promises.writeFile( + "public/waforth/benchmarks/index.html", + benchmarksIndex + ); } if (watch) { // Simple static file server - createServer(function (req, res) { + createServer(async function (req, res) { 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"); } - fs.readFile(f, function (err, data) { - if (err) { - res.writeHead(404); - res.end(JSON.stringify(err)); - return; - } + try { + const data = await fs.promises.readFile(f); res.writeHead(200); res.end(data); - }); + } catch (err) { + res.writeHead(404); + res.end(JSON.stringify(err)); + } }).listen(8080); console.log("listening on port 8080"); buildConfig = withWatcher(buildConfig, handleBuildFinished, 8081); } + esbuild.build(buildConfig).then(handleBuildFinished, () => process.exit(1)); diff --git a/scripts/esbuild/wasm-text.js b/scripts/esbuild/wasm-text.js index a8dd8cf..a9ac5ce 100644 --- a/scripts/esbuild/wasm-text.js +++ b/scripts/esbuild/wasm-text.js @@ -24,15 +24,23 @@ function wasmTextPlugin() { }); build.onLoad({ filter: /.*/, namespace: "wasm-text" }, async (args) => { // Would be handy if we could get output from stdout without going through file - const out = args.path.replace(".wat", ".wasm"); - const flags = ""; - // flags = --debug-names - // console.log("wat: compiling %s", args.path); - await exec(`wat2wasm ${flags} --output=${out} ${args.path}`); - return { - contents: await fs.promises.readFile(out), - loader: "binary", - }; + const out = `wat2wasm.${Math.floor( + Math.random() * 1000000000 + )}.tmp.wasm`; + try { + const flags = ""; + // flags = --debug-names + // console.log("wat: compiling %s", args.path); + await exec(`wat2wasm ${flags} --output=${out} ${args.path}`); + return { + contents: await fs.promises.readFile(out), + loader: "binary", + }; + } finally { + if ((await fs.promises.lstat(out)).isFile()) { + await fs.promises.unlink(out); + } + } }); }, };