diff options
author | Alon Zakai <alonzakai@gmail.com> | 2015-11-21 17:06:32 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2015-11-21 17:06:32 -0800 |
commit | 46a76c03e09f3854d76aba0f076a01ce4e0441cc (patch) | |
tree | daffa5a33eb8131750bed54873d5bc86f4a6e6bd /src | |
parent | 8b77837812708b1ca87b8b38ab1a6a3d243dd0aa (diff) | |
download | binaryen-46a76c03e09f3854d76aba0f076a01ce4e0441cc.tar.gz binaryen-46a76c03e09f3854d76aba0f076a01ce4e0441cc.tar.bz2 binaryen-46a76c03e09f3854d76aba0f076a01ce4e0441cc.zip |
refactor memory allocation code to create the buffer during asm.js link
Diffstat (limited to 'src')
-rw-r--r-- | src/js/post.js | 70 |
1 files changed, 41 insertions, 29 deletions
diff --git a/src/js/post.js b/src/js/post.js index c7d99147f..640c4b93f 100644 --- a/src/js/post.js +++ b/src/js/post.js @@ -5,32 +5,6 @@ function integrateWasmJS(Module) { // XXX don't be confused. Module here is in the outside program. wasmJS is the inner wasm-js.cpp. wasmJS['outside'] = Module; // Inside wasm-js.cpp, Module['outside'] reaches the outside module. - // Generate a module instance of the asm.js converted into wasm. - var code; - if (typeof read === 'function') { - // spidermonkey or v8 shells - code = read(Module['asmjsCodeFile']); - } else if (typeof process === 'object' && typeof require === 'function') { - // node.js - code = require('fs')['readFileSync'](Module['asmjsCodeFile']).toString(); - } else { - throw 'TODO: loading in other platforms'; - } - - var theBuffer = Module['buffer'] = new ArrayBuffer(Module['providedTotalMemory'] || 64*1024*1024); - wasmJS['providedTotalMemory'] = theBuffer.byteLength; - - Module['reallocBuffer'] = function(size) { - var old = Module['buffer']; - wasmJS['asmExports']['__growWasmMemory'](size); // tiny wasm method that just does grow_memory - return Module['buffer'] !== old ? Module['buffer'] : null; // if it was reallocated, it changed - }; - - var temp = wasmJS['_malloc'](code.length + 1); - wasmJS['writeAsciiToMemory'](code, temp); - wasmJS['_load_asm'](temp); - wasmJS['_free'](temp); - // Information for the instance of the module. var info = wasmJS['info'] = { global: null, @@ -65,9 +39,47 @@ function integrateWasmJS(Module) { return lookup; } - // The asm.js function, called to "link" the asm.js module. - Module['asm'] = function(global, env, buffer) { - assert(buffer === Module['buffer']); // we should not even need to pass it as a 3rd arg for wasm, but that's the asm.js way. + // 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. + Module['asm'] = function(global, env, providedBuffer) { + assert(providedBuffer === Module['buffer']); // we should not even need to pass it as a 3rd arg for wasm, but that's the asm.js way. + + // Generate a module instance of the asm.js converted into wasm. + var code; + if (typeof read === 'function') { + // spidermonkey or v8 shells + code = read(Module['asmjsCodeFile']); + } else if (typeof process === 'object' && typeof require === 'function') { + // node.js + code = require('fs')['readFileSync'](Module['asmjsCodeFile']).toString(); + } else { + throw 'TODO: loading in other platforms'; + } + + // wasm code would create its own buffer, at this time. But static init code might have + // written to the buffer already, and we must copy it over. We could just avoid + // this copy in wasm.js polyfilling, but to be as close as possible to real wasm, + // we do what wasm would do. + // TODO: avoid this copy, by avoiding such static init writes + // TODO: in shorter term, just copy up to the last static init write + var oldBuffer = Module['buffer']; + var newBuffer = ArrayBuffer(oldBuffer.byteLength); + (new Int8Array(newBuffer)).set(new Int8Array(oldBuffer)); + updateGlobalBuffer(newBuffer); + updateGlobalBufferViews(); + wasmJS['providedTotalMemory'] = Module['buffer'].byteLength; + + Module['reallocBuffer'] = function(size) { + var old = Module['buffer']; + wasmJS['asmExports']['__growWasmMemory'](size); // tiny wasm method that just does grow_memory + return Module['buffer'] !== old ? Module['buffer'] : null; // if it was reallocated, it changed + }; + + var temp = wasmJS['_malloc'](code.length + 1); + wasmJS['writeAsciiToMemory'](code, temp); + wasmJS['_load_asm'](temp); + wasmJS['_free'](temp); + // write the provided data to a location the wasm instance can get at it. info.global = global; info.env = env; |