mirror of https://github.com/pyodide/pyodide.git
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:
parent
dfd8149c63
commit
8523da0f36
3
Makefile
3
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.
|
||||
|
|
|
@ -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);
|
Loading…
Reference in New Issue