diff --git a/Makefile b/Makefile index 22533c0f4..7c6b6f3eb 100644 --- a/Makefile +++ b/Makefile @@ -89,6 +89,9 @@ dist/pyodide.d.ts: src/js/*.ts src/js/pyproxy.gen.ts src/js/error_handling.gen.t src/js/error_handling.gen.ts : src/core/error_handling.ts cp $< $@ +%.wasm.gen.js: %.wat + node tools/assemble_wat.js $@ + src/js/pyproxy.gen.ts : src/core/pyproxy.* src/core/*.h # We can't input pyproxy.js directly because CC will be unhappy about the file # extension. Instead cat it and have CC read from stdin. diff --git a/tools/assemble_wat.js b/tools/assemble_wat.js new file mode 100644 index 000000000..eb8a710ca --- /dev/null +++ b/tools/assemble_wat.js @@ -0,0 +1,29 @@ +const fs = require("fs"); +const { execFileSync } = require("child_process"); + +process.argv[2].split(); + +const path = process.argv[2].split("/"); +const filename = path.pop().split(".")[0]; +process.chdir(path.join("/")); + +try { + execFileSync("wat2wasms", [filename + ".wat", "--enable-all"]); +} catch (e) { + if (e.code === "ENOENT") { + process.stderr.write( + "assemble_wat.js: wat2wasm is not on path. " + + "Please install the WebAssembly Binary Toolkit.\n", + ); + process.stderr.write("Quitting.\n"); + process.exit(1); + } + throw e; +} + +const f = fs.readFileSync(filename + ".wasm"); +fs.unlinkSync(filename + ".wasm"); + +const s = Array.from(f, (x) => x.toString(16).padStart(2, "0")).join(""); +const output = `const ${filename}_wasm = decodeHexString("${s}");`; +fs.writeFileSync(filename + ".wasm.gen.js", output);