summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2015-11-29 10:48:54 -0800
committerAlon Zakai <alonzakai@gmail.com>2015-11-29 10:48:54 -0800
commitddb9959668e64b4bc81153407aef679fc352baaa (patch)
tree6fd1e14d03c1d6e29d7413541024b401689e0550 /src
parentf1cb59bda5688f761fadad997bda18658bf9a32c (diff)
downloadbinaryen-ddb9959668e64b4bc81153407aef679fc352baaa.tar.gz
binaryen-ddb9959668e64b4bc81153407aef679fc352baaa.tar.bz2
binaryen-ddb9959668e64b4bc81153407aef679fc352baaa.zip
apply mapped globals in wasm.js on all asm2wasm output, whether done on the client or ahead of time
Diffstat (limited to 'src')
-rw-r--r--src/js/post.js36
1 files changed, 23 insertions, 13 deletions
diff --git a/src/js/post.js b/src/js/post.js
index 0a45df680..2d04d0e63 100644
--- a/src/js/post.js
+++ b/src/js/post.js
@@ -1,6 +1,26 @@
function integrateWasmJS(Module) {
+ // wasm lacks globals, so asm2wasm maps them into locations in memory. that information cannot
+ // be present in the wasm output of asm2wasm, so we store it in a side file. If we load asm2wasm
+ // output, either generated ahead of time or on the client, we need to apply those mapped
+ // globals after loading the module.
+ function applyMappedGlobals() {
+ var mappedGlobals = JSON.parse(Module['read'](Module['wasmCodeFile'] + '.mappedGlobals'));
+ for (var name in mappedGlobals) {
+ var global = mappedGlobals[name];
+ if (!global.import) continue; // non-imports are initialized to zero in the typed array anyhow, so nothing to do here
+ var value = wasmJS['lookupImport'](global.module, global.base);
+ var address = global.address;
+ switch (global.type) {
+ case WasmTypes.i32: Module['HEAP32'][address >> 2] = value; break;
+ case WasmTypes.f32: Module['HEAPF32'][address >> 2] = value; break;
+ case WasmTypes.f64: Module['HEAPF64'][address >> 3] = value; break;
+ default: abort();
+ }
+ }
+ }
+
if (typeof WASM === 'object') {
// Provide an "asm.js function" for the application, called to "link" the asm.js module. We instantiate
// the wasm module at that time, and it receives imports and provides exports and so forth, the app
@@ -33,6 +53,8 @@ function integrateWasmJS(Module) {
return Module['buffer'] !== old ? Module['buffer'] : null; // if it was reallocated, it changed
};
+ applyMappedGlobals();
+
return instance;
};
@@ -124,19 +146,7 @@ function integrateWasmJS(Module) {
wasmJS['_load_asm2wasm'](temp);
} else {
wasmJS['_load_s_expr2wasm'](temp);
- var mappedGlobals = JSON.parse(Module['read'](Module['wasmCodeFile'] + '.mappedGlobals'));
- for (var name in mappedGlobals) {
- var global = mappedGlobals[name];
- if (!global.import) continue; // non-imports are initialized to zero in the typed array anyhow, so nothing to do here
- var value = wasmJS['lookupImport'](global.module, global.base);
- var address = global.address;
- switch (global.type) {
- case WasmTypes.i32: Module['HEAP32'][address >> 2] = value; break;
- case WasmTypes.f32: Module['HEAPF32'][address >> 2] = value; break;
- case WasmTypes.f64: Module['HEAPF64'][address >> 3] = value; break;
- default: abort();
- }
- }
+ applyMappedGlobals();
}
wasmJS['_free'](temp);