diff options
-rw-r--r-- | scripts/test/wasm2js.py | 3 | ||||
-rw-r--r-- | scripts/wasm2js.js | 6 | ||||
-rw-r--r-- | src/wasm2js.h | 46 | ||||
-rw-r--r-- | test/wasm2js/deterministic.2asm.js | 10 | ||||
-rw-r--r-- | test/wasm2js/deterministic.2asm.js.opt | 10 | ||||
-rw-r--r-- | test/wasm2js/dynamicLibrary.2asm.js | 10 | ||||
-rw-r--r-- | test/wasm2js/dynamicLibrary.2asm.js.opt | 10 | ||||
-rw-r--r-- | test/wasm2js/emscripten-grow-no.2asm.js | 3 | ||||
-rw-r--r-- | test/wasm2js/emscripten-grow-no.2asm.js.opt | 3 | ||||
-rw-r--r-- | test/wasm2js/emscripten-grow-yes.2asm.js | 4 | ||||
-rw-r--r-- | test/wasm2js/emscripten-grow-yes.2asm.js.opt | 4 | ||||
-rw-r--r-- | test/wasm2js/emscripten.2asm.js | 3 | ||||
-rw-r--r-- | test/wasm2js/emscripten.2asm.js.opt | 3 | ||||
-rw-r--r-- | test/wasm2js/minified-memory.2asm.js | 11 | ||||
-rw-r--r-- | test/wasm2js/minified-memory.2asm.js.opt | 11 |
15 files changed, 89 insertions, 48 deletions
diff --git a/scripts/test/wasm2js.py b/scripts/test/wasm2js.py index 38a2defd7..11f7011a1 100644 --- a/scripts/test/wasm2js.py +++ b/scripts/test/wasm2js.py @@ -30,6 +30,9 @@ wasm2js_blacklist = ['empty_imported_table.wast'] def check_for_stale_files(): + if shared.options.test_name_filter: + return + # TODO(sbc): Generalize and apply other test suites all_tests = [] for t in tests + spec_tests + wasm2js_tests: diff --git a/scripts/wasm2js.js b/scripts/wasm2js.js index 7bb8453bc..babf31267 100644 --- a/scripts/wasm2js.js +++ b/scripts/wasm2js.js @@ -6,12 +6,6 @@ var WebAssembly = { Memory: function(opts) { return { buffer: new ArrayBuffer(opts['initial'] * 64 * 1024), - grow: function(amount) { - var oldBuffer = this.buffer; - var ret = __growWasmMemory(amount); - assert(this.buffer !== oldBuffer); // the call should have updated us - return ret; - } }; }, diff --git a/src/wasm2js.h b/src/wasm2js.h index 0a2e45d49..c5297a12a 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -280,7 +280,7 @@ private: void addExports(Ref ast, Module* wasm); void addGlobal(Ref ast, Global* global); void addMemoryFuncs(Ref ast, Module* wasm); - void addMemoryGrowthFuncs(Ref ast, Module* wasm); + void addMemoryGrowFunc(Ref ast, Module* wasm); Wasm2JSBuilder() = delete; Wasm2JSBuilder(const Wasm2JSBuilder&) = delete; @@ -383,10 +383,10 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { ret[1]->push_back(asmFunc); ValueBuilder::appendArgumentToFunction(asmFunc, GLOBAL); ValueBuilder::appendArgumentToFunction(asmFunc, ENV); + if (wasm->memory.exists) { - ValueBuilder::appendArgumentToFunction(asmFunc, BUFFER); - // add memory import if (wasm->memory.imported()) { + // find memory and buffer in imports Ref theVar = ValueBuilder::makeVar(); asmFunc[3]->push_back(theVar); ValueBuilder::appendToVar( @@ -394,8 +394,32 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { "memory", ValueBuilder::makeDot(ValueBuilder::makeName(ENV), ValueBuilder::makeName(wasm->memory.base))); + + // Assign `buffer = memory.buffer` + Ref buf = ValueBuilder::makeVar(); + asmFunc[3]->push_back(buf); + ValueBuilder::appendToVar( + buf, + BUFFER, + ValueBuilder::makeDot(ValueBuilder::makeName("memory"), + ValueBuilder::makeName("buffer"))); + + // If memory is growable, override the imported memory's grow method to + // ensure so that when grow is called from the output it works as expected + if (wasm->memory.max > wasm->memory.initial) { + asmFunc[3]->push_back( + ValueBuilder::makeStatement(ValueBuilder::makeBinary( + ValueBuilder::makeDot(ValueBuilder::makeName("memory"), + ValueBuilder::makeName("grow")), + SET, + ValueBuilder::makeName(WASM_MEMORY_GROW)))); + } + } else { + // find memory as third argument + ValueBuilder::appendArgumentToFunction(asmFunc, BUFFER); } } + // add table import if (wasm->table.exists && wasm->table.imported()) { Ref theVar = ValueBuilder::makeVar(); @@ -2205,11 +2229,11 @@ void Wasm2JSBuilder::addMemoryFuncs(Ref ast, Module* wasm) { ast->push_back(memorySizeFunc); if (wasm->memory.max > wasm->memory.initial) { - addMemoryGrowthFuncs(ast, wasm); + addMemoryGrowFunc(ast, wasm); } } -void Wasm2JSBuilder::addMemoryGrowthFuncs(Ref ast, Module* wasm) { +void Wasm2JSBuilder::addMemoryGrowFunc(Ref ast, Module* wasm) { Ref memoryGrowFunc = ValueBuilder::makeFunction(WASM_MEMORY_GROW); ValueBuilder::appendArgumentToFunction(memoryGrowFunc, IString("pagesToAdd")); @@ -2506,6 +2530,16 @@ void Wasm2JSGlue::emitPostES6() { out << ",\n " << asmangle(import->base.str); }); + ModuleUtils::iterImportedMemories(wasm, [&](Memory* import) { + // The special helpers are emitted in the glue, see code and comments + // below. + if (ABI::wasm2js::isHelper(import->base)) { + return; + } + out << ",\n " << asmangle(import->base.str) << ": { buffer : mem" + << moduleName.str << " }"; + }); + ModuleUtils::iterImportedTables(wasm, [&](Table* import) { // The special helpers are emitted in the glue, see code and comments // below. @@ -2515,7 +2549,7 @@ void Wasm2JSGlue::emitPostES6() { out << ",\n " << asmangle(import->base.str); }); - if (wasm.memory.exists) { + if (wasm.memory.exists && !wasm.memory.imported()) { out << "\n },\n mem" << moduleName.str << "\n);\n"; } else { out << "\n });\n"; diff --git a/test/wasm2js/deterministic.2asm.js b/test/wasm2js/deterministic.2asm.js index b5ed9b88e..9b98a8695 100644 --- a/test/wasm2js/deterministic.2asm.js +++ b/test/wasm2js/deterministic.2asm.js @@ -1,6 +1,7 @@ -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -53,8 +54,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + memory: { buffer : memasmFunc } + }); export var foo = retasmFunc.foo; diff --git a/test/wasm2js/deterministic.2asm.js.opt b/test/wasm2js/deterministic.2asm.js.opt index 5c3d6cea6..0875a5180 100644 --- a/test/wasm2js/deterministic.2asm.js.opt +++ b/test/wasm2js/deterministic.2asm.js.opt @@ -1,6 +1,7 @@ -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -52,8 +53,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + memory: { buffer : memasmFunc } + }); export var foo = retasmFunc.foo; diff --git a/test/wasm2js/dynamicLibrary.2asm.js b/test/wasm2js/dynamicLibrary.2asm.js index 1824b2b9b..48057ed9c 100644 --- a/test/wasm2js/dynamicLibrary.2asm.js +++ b/test/wasm2js/dynamicLibrary.2asm.js @@ -12,8 +12,9 @@ function Table(ret) { return ret; } -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -96,8 +97,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + memory: { buffer : memasmFunc } + }); export var baz = retasmFunc.baz; diff --git a/test/wasm2js/dynamicLibrary.2asm.js.opt b/test/wasm2js/dynamicLibrary.2asm.js.opt index 42af848b6..76ec540d9 100644 --- a/test/wasm2js/dynamicLibrary.2asm.js.opt +++ b/test/wasm2js/dynamicLibrary.2asm.js.opt @@ -12,8 +12,9 @@ function Table(ret) { return ret; } -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -88,8 +89,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + memory: { buffer : memasmFunc } + }); export var baz = retasmFunc.baz; diff --git a/test/wasm2js/emscripten-grow-no.2asm.js b/test/wasm2js/emscripten-grow-no.2asm.js index 96c4c195c..3b9c81248 100644 --- a/test/wasm2js/emscripten-grow-no.2asm.js +++ b/test/wasm2js/emscripten-grow-no.2asm.js @@ -1,6 +1,7 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); diff --git a/test/wasm2js/emscripten-grow-no.2asm.js.opt b/test/wasm2js/emscripten-grow-no.2asm.js.opt index 96c4c195c..3b9c81248 100644 --- a/test/wasm2js/emscripten-grow-no.2asm.js.opt +++ b/test/wasm2js/emscripten-grow-no.2asm.js.opt @@ -1,6 +1,7 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); diff --git a/test/wasm2js/emscripten-grow-yes.2asm.js b/test/wasm2js/emscripten-grow-yes.2asm.js index 5acd1e0fe..157b5510d 100644 --- a/test/wasm2js/emscripten-grow-yes.2asm.js +++ b/test/wasm2js/emscripten-grow-yes.2asm.js @@ -1,6 +1,8 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; + memory.grow = __wasm_memory_grow; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); diff --git a/test/wasm2js/emscripten-grow-yes.2asm.js.opt b/test/wasm2js/emscripten-grow-yes.2asm.js.opt index 5acd1e0fe..157b5510d 100644 --- a/test/wasm2js/emscripten-grow-yes.2asm.js.opt +++ b/test/wasm2js/emscripten-grow-yes.2asm.js.opt @@ -1,6 +1,8 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; + memory.grow = __wasm_memory_grow; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); diff --git a/test/wasm2js/emscripten.2asm.js b/test/wasm2js/emscripten.2asm.js index 5d6e11b85..f4169a148 100644 --- a/test/wasm2js/emscripten.2asm.js +++ b/test/wasm2js/emscripten.2asm.js @@ -1,6 +1,7 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var FUNCTION_TABLE = env.table; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); diff --git a/test/wasm2js/emscripten.2asm.js.opt b/test/wasm2js/emscripten.2asm.js.opt index 7cd4868b3..8de23758f 100644 --- a/test/wasm2js/emscripten.2asm.js.opt +++ b/test/wasm2js/emscripten.2asm.js.opt @@ -1,6 +1,7 @@ function instantiate(asmLibraryArg, wasmMemory) { -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.memory; + var buffer = memory.buffer; var FUNCTION_TABLE = env.table; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); diff --git a/test/wasm2js/minified-memory.2asm.js b/test/wasm2js/minified-memory.2asm.js index d7c73a6bb..e9a89166c 100644 --- a/test/wasm2js/minified-memory.2asm.js +++ b/test/wasm2js/minified-memory.2asm.js @@ -1,6 +1,8 @@ -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.a; + var buffer = memory.buffer; + memory.grow = __wasm_memory_grow; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -72,8 +74,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + a: { buffer : memasmFunc } + }); export var foo = retasmFunc.foo; diff --git a/test/wasm2js/minified-memory.2asm.js.opt b/test/wasm2js/minified-memory.2asm.js.opt index 1e79d3202..2a09422ec 100644 --- a/test/wasm2js/minified-memory.2asm.js.opt +++ b/test/wasm2js/minified-memory.2asm.js.opt @@ -1,6 +1,8 @@ -function asmFunc(global, env, buffer) { +function asmFunc(global, env) { var memory = env.a; + var buffer = memory.buffer; + memory.grow = __wasm_memory_grow; var HEAP8 = new global.Int8Array(buffer); var HEAP16 = new global.Int16Array(buffer); var HEAP32 = new global.Int32Array(buffer); @@ -72,8 +74,7 @@ var retasmFunc = asmFunc({ NaN, Infinity }, { - abort: function() { throw new Error('abort'); } - }, - memasmFunc -); + abort: function() { throw new Error('abort'); }, + a: { buffer : memasmFunc } + }); export var foo = retasmFunc.foo; |