diff options
-rw-r--r-- | .gitignore | 1 | ||||
-rwxr-xr-x | build.sh | 3 | ||||
-rw-r--r-- | src/js/post.js | 30 | ||||
-rw-r--r-- | src/wasm-js.cpp | 3 |
4 files changed, 36 insertions, 1 deletions
diff --git a/.gitignore b/.gitignore index 7f8b1a540..9e5aa6211 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ bin/asm2wasm +bin/wasm.js *~ *.diff @@ -1,5 +1,6 @@ echo "building asm2wasm" g++ -std=c++11 src/asm2wasm-main.cpp src/parser.cpp src/simple_ast.cpp src/optimizer-shared.cpp -g -o bin/asm2wasm echo "building interpreter/js" -em++ -std=c++11 src/wasm-js.cpp src/parser.cpp src/simple_ast.cpp src/optimizer-shared.cpp -o bin/wasm.js +em++ -std=c++11 src/wasm-js.cpp src/parser.cpp src/simple_ast.cpp src/optimizer-shared.cpp -o bin/wasm.js -s MODULARIZE=1 -s 'EXPORT_NAME="WasmJS"' +cat src/js/post.js >> bin/wasm.js diff --git a/src/js/post.js b/src/js/post.js new file mode 100644 index 000000000..571fca101 --- /dev/null +++ b/src/js/post.js @@ -0,0 +1,30 @@ + +(function() { + // XXX don't be confused. Module here is in the outside program. WasmJS is the inner wasm-js.cpp. + + // Generate a module instance of the asm.js converted into wasm. + var code = read(Module['asmjsCodeFile']); + var temp = WasmJS._malloc(code.length + 1); + WasmJS.writeAsciiToMemory(code, temp); + var instance = WasmJS.load_asm(temp); + WasmJS._free(temp); + + // Generate memory XXX TODO get the right size + var theBuffer = Module['buffer'] = new ArrayBuffer(16*1024*1024); + + // Information for the instance of the module. + var instance = WasmJS['instance'] = { + global: null, + env: null, + parent: Module // Module inside wasm-js.cpp refers to wasm-js.cpp; this allows access to the outside program. + }; + + // 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. + instance.global = global; + instance.env = env; + }; +})(); + diff --git a/src/wasm-js.cpp b/src/wasm-js.cpp index e2ef47bbb..2bea09a16 100644 --- a/src/wasm-js.cpp +++ b/src/wasm-js.cpp @@ -3,6 +3,9 @@ // wasm intepreter for asm2wasm output, in a js environment. receives asm.js, // generates a runnable module suitable as a polyfill. // +// this polyfills an emscripten --separate-asm *.asm.js file, as a drop-in +// replacement. it writes the wasm module to Module.asm, and sets Module.buffer. +// #include <emscripten.h> |