diff options
author | Alon Zakai <alonzakai@gmail.com> | 2016-03-04 20:49:23 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-03-04 20:49:23 -0800 |
commit | 90ec61b409e3a4d0fc2418d2ba19264b38cdf095 (patch) | |
tree | b3d868fafac305c921a47458f81401bcd5dbb51d /src/js/wasm.js-post.js | |
parent | 4bde5052f0183a33cbaa454e1a4d01785c0d257b (diff) | |
download | binaryen-90ec61b409e3a4d0fc2418d2ba19264b38cdf095.tar.gz binaryen-90ec61b409e3a4d0fc2418d2ba19264b38cdf095.tar.bz2 binaryen-90ec61b409e3a4d0fc2418d2ba19264b38cdf095.zip |
move to more unification of import handling in interpreter and native paths
Diffstat (limited to 'src/js/wasm.js-post.js')
-rw-r--r-- | src/js/wasm.js-post.js | 66 |
1 files changed, 28 insertions, 38 deletions
diff --git a/src/js/wasm.js-post.js b/src/js/wasm.js-post.js index 84ae825d9..e5e745af2 100644 --- a/src/js/wasm.js-post.js +++ b/src/js/wasm.js-post.js @@ -40,17 +40,29 @@ function integrateWasmJS(Module) { }, }; - function flatten(obj) { - var ret = {}; - for (var x in obj) { - for (var y in obj[x]) { - if (ret[y]) Module['printErr']('warning: flatten dupe: ' + y); - if (typeof obj[x][y] === 'function') { - ret[y] = obj[x][y]; - } - } + var info = { + global: null, + env: null, + asm2wasm: asm2wasmImports, + parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. + }; + + function lookupImport(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]]; + } + if (base) { + lookup = (lookup || {})[base]; } - return ret; + if (lookup === undefined) { + abort('bad lookupImport to (' + mod + ').' + base); + } + return lookup; } function mergeMemory(newBuffer) { @@ -104,16 +116,13 @@ function integrateWasmJS(Module) { // Load the wasm module var binary = Module['readBinary'](Module['wasmCodeFile']); // Create an instance of the module using native support in the JS engine. - var importObj = { - "global.Math": global.Math, - "env": env, - "asm2wasm": asm2wasmImports - }; + info['global'] = { 'Math': global.Math }; + info['env'] = env; var instance; if (typeof WASM === 'object') { - instance = WASM.instantiateModule(binary, flatten(importObj)); + instance = WASM.instantiateModule(binary, info); } else if (typeof wasmEval === 'function') { - instance = wasmEval(binary.buffer, importObj); + instance = wasmEval(binary.buffer, info); } else { throw 'how to wasm?'; } @@ -142,28 +151,9 @@ function integrateWasmJS(Module) { wasmJS['outside'] = Module; // Inside wasm-js.cpp, Module['outside'] reaches the outside module. // Information for the instance of the module. - var info = wasmJS['info'] = { - global: null, - env: null, - asm2wasm: asm2wasmImports, - parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. - }; + wasmJS['info'] = info; - 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 === undefined) { - abort('bad lookupImport to (' + mod + ').' + base); - } - return lookup; - } + wasmJS['lookupImport'] = lookupImport; // The asm.js function, called to "link" the asm.js module. At that time, we are provided imports // and respond with exports, and so forth. |