Add a tool to allow us to embed wasm modules with hand coded wat (#3230)

This allows us to embed extra wasm modules into the output. This is split off of #3210.
We need this for #3210 since
1. We don't want to assume that externref is supported but we need to use it as part of
the JS Promise integration proposal. To handle this we segment off the externrefs into
separate wasm modules. If externref is not supported, we load most everything
successfully but these small modules fail to load

2. Even supposing that we know that externref is supported, there is no way to write C
code such that the compiled code uses externref. It's possible using the emscripten
assembly format, but that is less well documented and harder to use for our purposes
than wat.

This PR doesn't actually change anything by itself.
This commit is contained in:
Hood Chatham 2022-11-10 14:22:59 -08:00 committed by GitHub
parent dfd8149c63
commit 8523da0f36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 32 additions and 0 deletions

View File

@ -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.

29
tools/assemble_wat.js Normal file
View File

@ -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);