diff options
author | Alon Zakai <alonzakai@gmail.com> | 2019-04-16 14:37:35 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-16 14:37:35 -0700 |
commit | a8fde2fa899cfbca9069f0796fce284ab7a6ffc4 (patch) | |
tree | 485f4ad3c5ea4a4a35b186ea72c5fde664b7afce /src | |
parent | b30e2c576b317b4cbc49e9e18139cbd260ed0317 (diff) | |
download | binaryen-a8fde2fa899cfbca9069f0796fce284ab7a6ffc4.tar.gz binaryen-a8fde2fa899cfbca9069f0796fce284ab7a6ffc4.tar.bz2 binaryen-a8fde2fa899cfbca9069f0796fce284ab7a6ffc4.zip |
wasm2js: support memory imports properly, updating their buffer too (#2013)
* Emit an import statement for the memory.
* Update the imported memory's buffer when we grow.
Diffstat (limited to 'src')
-rw-r--r-- | src/wasm2js.h | 32 |
1 files changed, 29 insertions, 3 deletions
diff --git a/src/wasm2js.h b/src/wasm2js.h index 50968046f..d563409f9 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -279,7 +279,7 @@ private: void addExports(Ref ast, Module* wasm); void addGlobal(Ref ast, Global* global); void setNeedsAlmostASM(const char *reason); - void addMemoryGrowthFuncs(Ref ast); + void addMemoryGrowthFuncs(Ref ast, Module* wasm); Wasm2JSBuilder() = delete; Wasm2JSBuilder(const Wasm2JSBuilder &) = delete; @@ -321,6 +321,18 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { ValueBuilder::appendArgumentToFunction(asmFunc, ENV); ValueBuilder::appendArgumentToFunction(asmFunc, BUFFER); asmFunc[3]->push_back(ValueBuilder::makeStatement(ValueBuilder::makeString(USE_ASM))); + // add memory import + if (wasm->memory.exists && wasm->memory.imported()) { + Ref theVar = ValueBuilder::makeVar(); + asmFunc[3]->push_back(theVar); + ValueBuilder::appendToVar(theVar, + "memory", + ValueBuilder::makeDot( + ValueBuilder::makeName(ENV), + ValueBuilder::makeName("memory") + ) + ); + } // create heaps, etc addBasics(asmFunc[3]); ModuleUtils::iterImportedFunctions(*wasm, [&](Function* import) { @@ -589,7 +601,7 @@ void Wasm2JSBuilder::addExports(Ref ast, Module* wasm) { if (almostASM) { // replace "use asm" ast[0] = ValueBuilder::makeStatement(ValueBuilder::makeString(ALMOST_ASM)); - addMemoryGrowthFuncs(ast); + addMemoryGrowthFuncs(ast, wasm); } ast->push_back(ValueBuilder::makeStatement(ValueBuilder::makeReturn(exports))); } @@ -1849,7 +1861,7 @@ void Wasm2JSBuilder::setNeedsAlmostASM(const char *reason) { } } -void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast) { +void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) { Ref growMemoryFunc = ValueBuilder::makeFunction(WASM_GROW_MEMORY); ValueBuilder::appendArgumentToFunction(growMemoryFunc, IString("pagesToAdd")); @@ -1978,6 +1990,20 @@ void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast) { ) ); + // apply the changes to the memory import + if (wasm->memory.imported()) { + ValueBuilder::appendToBlock(block, + ValueBuilder::makeBinary( + ValueBuilder::makeDot( + ValueBuilder::makeName("memory"), + ValueBuilder::makeName(BUFFER) + ), + SET, + ValueBuilder::makeName(IString("newBuffer")) + ) + ); + } + growMemoryFunc[3]->push_back( ValueBuilder::makeReturn( ValueBuilder::makeName(IString("oldPages")))); |