diff options
author | Alon Zakai <alonzakai@gmail.com> | 2019-04-11 15:57:05 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-11 15:57:05 -0700 |
commit | fc3b8eed2b9fb2aa7e55ac833ea08cf7eae46a0a (patch) | |
tree | 2a8787a3143c2c437905fc9533c7575cb03604a1 /src | |
parent | b769b4ede65eb014376b67f78ba5e6cb04e0cef8 (diff) | |
download | binaryen-fc3b8eed2b9fb2aa7e55ac833ea08cf7eae46a0a.tar.gz binaryen-fc3b8eed2b9fb2aa7e55ac833ea08cf7eae46a0a.tar.bz2 binaryen-fc3b8eed2b9fb2aa7e55ac833ea08cf7eae46a0a.zip |
wasm2js: emscripten glue option (#2000)
Add a wasm2js option for the glue to be in emscripten-compatible format (as opposed to ES6). This does a few things so far:
* Emit START_FUNCTIONS, END_FUNCTIONS markers in the code, for future use in the optimizer.
* Emit the glue as a function to be called from emscripten.
Diffstat (limited to 'src')
-rw-r--r-- | src/tools/wasm2js.cpp | 5 | ||||
-rw-r--r-- | src/wasm2js.h | 65 |
2 files changed, 66 insertions, 4 deletions
diff --git a/src/tools/wasm2js.cpp b/src/tools/wasm2js.cpp index 76a724f86..8704dbf49 100644 --- a/src/tools/wasm2js.cpp +++ b/src/tools/wasm2js.cpp @@ -370,6 +370,11 @@ int main(int argc, const char *argv[]) { [&](Options* o, const std::string& argument) { flags.pedantic = true; }) + .add("--emscripten", "", "Emulate the glue in emscripten-compatible form (and not ES6 module form)", + Options::Arguments::Zero, + [&](Options* o, const std::string& argument) { + flags.emscripten = true; + }) .add_positional("INFILE", Options::Arguments::One, [](Options *o, const std::string& argument) { o->extra["infile"] = argument; diff --git a/src/wasm2js.h b/src/wasm2js.h index c6970e3e8..e93b3f273 100644 --- a/src/wasm2js.h +++ b/src/wasm2js.h @@ -142,6 +142,7 @@ public: bool debug = false; bool pedantic = false; bool allowAsserts = false; + bool emscripten = false; }; Wasm2JSBuilder(Flags f) : flags(f) {} @@ -358,6 +359,9 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { generateFetchHighBits = true; } }); + if (flags.emscripten) { + asmFunc[3]->push_back(ValueBuilder::makeName("// EMSCRIPTEN_START_FUNCS")); + } // functions ModuleUtils::iterDefinedFunctions(*wasm, [&](Function* func) { asmFunc[3]->push_back(processFunction(wasm, func)); @@ -379,6 +383,9 @@ Ref Wasm2JSBuilder::processWasm(Module* wasm, Name funcName) { e->kind = ExternalKind::Function; wasm->addExport(e); } + if (flags.emscripten) { + asmFunc[3]->push_back(ValueBuilder::makeName("// EMSCRIPTEN_END_FUNCS")); + } addTables(asmFunc[3], wasm); // memory XXX @@ -2002,9 +2009,26 @@ private: Output& out; Wasm2JSBuilder::Flags flags; Name moduleName; + + void emitPreEmscripten(); + void emitPreES6(); + void emitPostEmscripten(); + void emitPostES6(); }; void Wasm2JSGlue::emitPre() { + if (flags.emscripten) { + emitPreEmscripten(); + } else { + emitPreES6(); + } +} + +void Wasm2JSGlue::emitPreEmscripten() { + out << "function instantiate(asmLibraryArg, wasmMemory, wasmTable) {\n\n"; +} + +void Wasm2JSGlue::emitPreES6() { std::unordered_map<Name, Name> baseModuleMap; auto noteImport = [&](Name module, Name base) { @@ -2018,10 +2042,10 @@ void Wasm2JSGlue::emitPre() { baseModuleMap[base] = module; out << "import { " - << base.str - << " } from '" - << module.str - << "';\n"; + << base.str + << " } from '" + << module.str + << "';\n"; }; ImportInfo imports(wasm); @@ -2038,6 +2062,39 @@ void Wasm2JSGlue::emitPre() { } void Wasm2JSGlue::emitPost() { + if (flags.emscripten) { + emitPostEmscripten(); + } else { + emitPostES6(); + } +} + +void Wasm2JSGlue::emitPostEmscripten() { + out << "return asmFunc(\n" + << " {\n" + << " 'env': asmLibraryArg,\n" + << " 'global': {\n" + << " 'Int8Array': Int8Array,\n" + << " 'Int16Array': Int16Array,\n" + << " 'Int32Array': Int32Array,\n" + << " 'Uint8Array': Uint8Array,\n" + << " 'Uint16Array': Uint16Array,\n" + << " 'Uint32Array': Uint32Array,\n" + << " 'Float32Array': Float32Array,\n" + << " 'Float64Array': Float64Array,\n" + << " 'NaN': NaN,\n" + << " 'Infinity': Infinity,\n" + << " 'Math': Math\n" + << " }\n" + << " },\n" + << " wasmMemory.buffer\n" + << ")" + << "\n" + << "\n" + << "}"; +} + +void Wasm2JSGlue::emitPostES6() { // Create an initial `ArrayBuffer` and populate it with static data. // Currently we use base64 encoding to encode static data and we decode it at // instantiation time. |