diff options
author | Sam Clegg <sbc@chromium.org> | 2022-09-14 12:36:37 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-09-14 19:36:37 +0000 |
commit | 64f9732c40157b84b85dea7763a91b4fe3199093 (patch) | |
tree | ddddb46c4e9d2b34e36aeabbc796e8c5200026c4 /src/wasm2js.h | |
parent | 795a70b02032f57f14d97d7555af97f0e527b4b0 (diff) | |
download | binaryen-64f9732c40157b84b85dea7763a91b4fe3199093.tar.gz binaryen-64f9732c40157b84b85dea7763a91b4fe3199093.tar.bz2 binaryen-64f9732c40157b84b85dea7763a91b4fe3199093.zip |
wasm2js: Have instantiate function take standard import object (#5018)
Previously we were assuming asmLibraryArg which is what emscripten
passes as the `env` import object but using this method is more
flexible and should allow wasm2js to work with import that are
not all form a single object.
The slight size increase here is just temporary until emscripten
gets updated.
See https://github.com/emscripten-core/emscripten/pull/17737
Diffstat (limited to 'src/wasm2js.h')
-rw-r--r-- | src/wasm2js.h | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/src/wasm2js.h b/src/wasm2js.h index cc4d363ec..05ba2ace9 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -53,6 +53,8 @@ namespace wasm { using namespace cashew; +static IString importObject("importObject"); + // Appends extra to block, flattening out if extra is a block as well void flattenAppend(Ref ast, Ref extra) { int index; @@ -411,7 +413,21 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { Ref ret = ValueBuilder::makeToplevel(); Ref asmFunc = ValueBuilder::makeFunction(funcName); ret[1]->push_back(asmFunc); - ValueBuilder::appendArgumentToFunction(asmFunc, ENV); + ValueBuilder::appendArgumentToFunction(asmFunc, importObject); + + // Retrieve `env` object from info. Older version of emscripten pass `env` + // *as* `info` so for now we make this conditional. + // TODO(sbc): Remove the makeBinary here once emscripten change has landed. + Ref envVar = ValueBuilder::makeVar(); + asmFunc[3]->push_back(envVar); + ValueBuilder::appendToVar( + envVar, + ENV, + ValueBuilder::makeBinary( + ValueBuilder::makeDot(ValueBuilder::makeName(importObject), + ValueBuilder::makeName(ENV)), + "||", + ValueBuilder::makeName(importObject))); // add memory import if (!wasm->memories.empty()) { @@ -2570,7 +2586,7 @@ void Wasm2JSGlue::emitPre() { } void Wasm2JSGlue::emitPreEmscripten() { - out << "function instantiate(asmLibraryArg) {\n"; + out << "function instantiate(info) {\n"; } void Wasm2JSGlue::emitPreES6() { @@ -2617,7 +2633,7 @@ void Wasm2JSGlue::emitPost() { } void Wasm2JSGlue::emitPostEmscripten() { - out << " return asmFunc(asmLibraryArg);\n}\n"; + out << " return asmFunc(info);\n}\n"; } void Wasm2JSGlue::emitPostES6() { |