diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 19:57:03 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-01 19:57:03 -0800 |
commit | d96c63dc0e6b0a9ccf0c327d8c6c0cb61f95d57f (patch) | |
tree | 828b1a47c2078fe7ec60c162fc57c2aa03e8cccf | |
parent | 90304bf9703e768fcdf87914e6117f87beea3f59 (diff) | |
download | binaryen-d96c63dc0e6b0a9ccf0c327d8c6c0cb61f95d57f.tar.gz binaryen-d96c63dc0e6b0a9ccf0c327d8c6c0cb61f95d57f.tar.bz2 binaryen-d96c63dc0e6b0a9ccf0c327d8c6c0cb61f95d57f.zip |
refactor a lookupImport method
-rw-r--r-- | README.md | 10 | ||||
-rwxr-xr-x | emcc_to_wasm.js.sh | 2 | ||||
-rw-r--r-- | src/js/post.js | 17 | ||||
-rw-r--r-- | src/wasm-js.cpp | 17 |
4 files changed, 26 insertions, 20 deletions
@@ -72,13 +72,10 @@ emcc src.cpp -o a.html --separate-asm That will emit `a.html`, `a.js`, and `a.asm.js`. That last file is the asm.js module, which you can pass into `asm2wasm`. -If you want an optimized buid, use +For basic tests, that command should work, but in general you need a few more arguments to emcc, see emcc's usage in `emcc_to_wasm.js.sh`, specifically -``` -emcc src.cpp -o a.html --separate-asm -O[2,3,etc.] -s ALIASING_FUNCTION_POINTERS=0 -``` - -You need `ALIASING_FUNCTION_POINTERS=0` because WebAssembly does not allow aliased function pointers (there is a single table). + * `ALIASING_FUNCTION_POINTERS=0` because WebAssembly does not allow aliased function pointers (there is a single table). + * `GLOBAL_BASE=1000` because WebAssembly lacks global variables, so `asm2wasm` maps them onto addresses in memory. This requires that you have some reserved space for those variables. With that argument, we reserve the area up to `1000`. ## Testing @@ -113,6 +110,5 @@ Same as Emscripten: MIT license. * Waiting for switch to stablize on the spec repo; switches are Nop'ed. * Reference interpreter lacks module importing support; imports are Nop'ed in native builds, but enabled in emcc builds (so wasm.js works). - * WebAssembly lacks global variables, so `asm2wasm` maps them onto addresses in memory. This requires that you have some reserved space for those variables. You can do that with `emcc -s GLOBAL_BASE=1000`. We still need to write the code to copy the globals there. * Memory section needs the right size. diff --git a/emcc_to_wasm.js.sh b/emcc_to_wasm.js.sh index 528871746..c4c3a4bf3 100755 --- a/emcc_to_wasm.js.sh +++ b/emcc_to_wasm.js.sh @@ -3,7 +3,7 @@ set -e echo "calling emcc" -emcc -o a.html --separate-asm -profiling -s TOTAL_MEMORY=67108864 -s GLOBAL_BASE=1024 $@ +emcc -o a.html --separate-asm -profiling -s TOTAL_MEMORY=67108864 -s GLOBAL_BASE=1024 -s ALIASING_FUNCTION_POINTERS=0 $@ # we now have a.asm.js and a.js diff --git a/src/js/post.js b/src/js/post.js index fe9e1b516..c9e29f6e8 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -31,12 +31,29 @@ parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. }; + wasmJS['lookupImport'] = function(mod, base) { + var lookup = info; + if (mod.indexOf('.') < 0) { + lookup = (lookup || {})[mod]; + } else { + var parts = mod.split('.'); + lookup = (lookup || {})[parts[0]]; + lookup = (lookup || {})[parts[1]]; + } + lookup = (lookup || {})[base]; + if (!lookup) { + abort('bad CallImport to (' + mod + ').' + base); + } + return lookup; + } + // The asm.js function, called to "link" the asm.js module. Module['asm'] = function(global, env, buffer) { assert(buffer === theBuffer); // we should not even need to pass it as a 3rd arg for wasm, but that's the asm.js way. // write the provided data to a location the wasm instance can get at it. info.global = global; info.env = env; + wasmJS['_load_mapped_globals'](); // now that we have global and env, we can ready the provided imported globals, copying them to their mapped locations. return wasmJS['asmExports']; }; })(); diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index 4c8e27423..bedc65d48 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -95,18 +95,7 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { var base = Pointer_stringify($1); var tempArguments = Module['tempArguments']; Module['tempArguments'] = null; - var lookup = Module['info']; - if (mod.indexOf('.') < 0) { - lookup = (lookup || {})[mod]; - } else { - var parts = mod.split('.'); - lookup = (lookup || {})[parts[0]]; - lookup = (lookup || {})[parts[1]]; - } - lookup = (lookup || {})[base]; - if (!lookup) { - abort('bad CallImport to (' + mod + ').' + base); - } + var lookup = Module['lookupImport'](mod, base); return lookup.apply(null, tempArguments); }, import->module.str, import->base.str)); } @@ -173,6 +162,10 @@ extern "C" void EMSCRIPTEN_KEEPALIVE load_asm(char *input) { instance = new ModuleInstance(*wasm, new JSExternalInterface()); } +// Ready the provided imported globals, copying them to their mapped locations. +extern "C" void EMSCRIPTEN_KEEPALIVE load_mapped_globals() { +} + // Does a call from js into an export of the module. extern "C" double EMSCRIPTEN_KEEPALIVE call_from_js(const char *target) { IString name(target); |