diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 20:54:46 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 20:55:30 -0800 |
commit | d8c2d69dd253bd6f67e1f3c374be2c20d43dfc03 (patch) | |
tree | 39cfe002d1ff5d58f462a8472ac79c28ead43a2e /src/wasm-js.cpp | |
parent | bb6f33c9f19581e2ea60b46653bb1a10ced4a2eb (diff) | |
download | binaryen-d8c2d69dd253bd6f67e1f3c374be2c20d43dfc03.tar.gz binaryen-d8c2d69dd253bd6f67e1f3c374be2c20d43dfc03.tar.bz2 binaryen-d8c2d69dd253bd6f67e1f3c374be2c20d43dfc03.zip |
save module and base of mapped imported globals, and fix writing of mapped imported globals
Diffstat (limited to 'src/wasm-js.cpp')
-rw-r--r-- | src/wasm-js.cpp | 26 |
1 files changed, 21 insertions, 5 deletions
diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index bedc65d48..14dfa563c 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -15,7 +15,10 @@ using namespace cashew; using namespace wasm; +// global singletons +Asm2WasmBuilder* asm2wasm = nullptr; ModuleInstance* instance = nullptr; +Module* module = nullptr; // receives asm.js code, parses into wasm and returns an instance handle. // this creates a module, an external interface, a builder, and a module instance, @@ -49,22 +52,22 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { cashew::Parser<Ref, DotZeroValueBuilder> builder; Ref asmjs = builder.parseToplevel(input); - Module* wasm = new Module(); + module = new Module(); if (debug) std::cerr << "wasming...\n"; - Asm2WasmBuilder* asm2wasm = new Asm2WasmBuilder(*wasm); + asm2wasm = new Asm2WasmBuilder(*module); asm2wasm->processAsm(asmjs); if (debug) std::cerr << "optimizing...\n"; asm2wasm->optimize(); - //std::cerr << *wasm << '\n'; + //std::cerr << *module << '\n'; if (debug) std::cerr << "generating exports...\n"; EM_ASM({ Module['asmExports'] = {}; }); - for (auto& curr : wasm->exports) { + for (auto& curr : module->exports) { EM_ASM_({ var name = Pointer_stringify($0); Module['asmExports'][name] = function() { @@ -159,11 +162,24 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { } }; - instance = new ModuleInstance(*wasm, new JSExternalInterface()); + instance = new ModuleInstance(*module, new JSExternalInterface()); } // Ready the provided imported globals, copying them to their mapped locations. extern "C" void EMSCRIPTEN_KEEPALIVE load_mapped_globals() { + for (auto& pair : asm2wasm->mappedGlobals) { + auto name = pair.first; + auto& global = pair.second; + if (!global.import) continue; // non-imports are initialized to zero in the typed array anyhow, so nothing to do here + double value = EM_ASM_DOUBLE({ return Module['lookupImport'](Pointer_stringify($0), Pointer_stringify($1)) }, global.module.str, global.base.str); + unsigned address = global.address; + switch (global.type) { + case i32: EM_ASM_({ Module['info'].parent['HEAP32'][$0] = $1 }, address, value); break; + case f32: EM_ASM_({ Module['info'].parent['HEAPF32'][$0] = $1 }, address, value); break; + case f64: EM_ASM_({ Module['info'].parent['HEAPF64'][$0] = $1 }, address, value); break; + default: abort(); + } + } } // Does a call from js into an export of the module. |