diff options
author | Rasmus <rasmus@notion.se> | 2016-09-02 13:41:08 -0700 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2016-09-02 13:41:08 -0700 |
commit | f872f2d456921df1c2b256b9c448031371d8a713 (patch) | |
tree | 5e88c767aed7b2c2474aaae06cd8cd53d958a74f /test/binaryen.js | |
parent | 1651ac798181107f8547b58c4c129c977f2b11e8 (diff) | |
download | binaryen-f872f2d456921df1c2b256b9c448031371d8a713.tar.gz binaryen-f872f2d456921df1c2b256b9c448031371d8a713.tar.bz2 binaryen-f872f2d456921df1c2b256b9c448031371d8a713.zip |
Improvements to build-js.sh and JS API (#679)
* Adds command-line argument to build-js.sh for specifying the location of Emscripten. Also spreads out program arguments on separate lines
* Exposes WasmBinaryWriter and BufferWithRandomAccess in the JS API to allow writing WASM modules and access the produced bytes via the JS API
* Updates bin/binaryen.js and bin/wasm.js from changes to build-js.sh
* Adds exclude patterns to .gitignore for some files generated by build-js.sh and .DS_Store for macOS
* Changes build-js.sh to use EMSCRIPTEN env var instead of a command-line argument to provide the path to emscripten
* Improvements to JS builds
- Adds -g flag to build-js.sh that will build unoptimized and unmangled versions of binaryen.js and wasm.js (output has a "-g.js" suffix to allow co-existence with regular optimized builds).
- Enables closure compiler for non-debug builds
- Adds browser test for s-expression parser + WASM code gen (requires wasm to be enabled in the browser.)
- Adds iterator interface to BufferWithRandomAccess (when Symbol.iterator is available)
- Adds toArrayBuffer to BufferWithRandomAccess (when TypedArray is available)
- Adds compileWast(sourceText :string) :ArrayBuffer to the module, parsing & compiling s-expression code to a WASM module
- Changes the way binaryen.js is exported to allow usage in CommonJS, AMD and UMD envionments.
* Expose "Binaryen" global in a better way to work with a.js generated by check.py
* Fix to binaryen.js to only export a global variable when running the test (a.js) while inside a module (avoids polluting global in e.g. nodejs). Also fixes a spelling mistake.
* Better "no WASM detected" message in test/binaryen.js/browser.html
* Small change to error message in build-js.sh where $EMSCRIPTEN does not point to a directory
* Changes emcc args in build-js.sh after investingating a large number of argument combinations. Also adds a browser benchmark. The result of emcc arguments and the effect on performance is summarized in this doc: https://gist.github.com/rsms/e33c61a25a31c08260161a087be03169
* Enable inferring emscripten path by looking in PATH when EMSCRIPTEN is not set in env
Diffstat (limited to 'test/binaryen.js')
-rw-r--r-- | test/binaryen.js/browser-benchmark-compile-wast.html | 91 | ||||
-rw-r--r-- | test/binaryen.js/browser.html | 80 |
2 files changed, 171 insertions, 0 deletions
diff --git a/test/binaryen.js/browser-benchmark-compile-wast.html b/test/binaryen.js/browser-benchmark-compile-wast.html new file mode 100644 index 000000000..2431f372e --- /dev/null +++ b/test/binaryen.js/browser-benchmark-compile-wast.html @@ -0,0 +1,91 @@ +<!DOCTYPE HTML> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Binaryen</title> + <style type="text/css"> + +* { margin:0; padding:0; font-family:inherit; } +body { + margin:2em; + background:white; + color:black; + font:14px monospace; + white-space: pre; +} +p { + white-space: normal; + font-family:sans-serif; +} + + </style> + <script type="text/javascript" src="../../bin/binaryen.js"></script> + </head> + <body><script type="text/javascript"> +// Separate javascript tag to support <=ES3 browser (the other block uses ES5 and ES6 features) +if (!window.Wasm) { + var e = document.createElement('p'); + e.innerHTML = 'No WASM support detected.<br>Please see <a href="https://github.com/kripken/emscripten/wiki/WebAssembly#testing-native-webassembly-in-browsers">https://github.com/kripken/emscripten/wiki/WebAssembly#testing-native-webassembly-in-browsers</a> for instructions on how to enable it.'; + document.body.appendChild(e); +} +</script><script type="text/javascript"> +if (window.Wasm) { + document.write("See console"); + + let initStart = Date.now(); + Binaryen = Binaryen(); + let initDuration = Date.now() - initStart; + console.log('initializing module by calling Binaryen(): '+initDuration+'ms'); + + let input1 =` + (module + (export "add" $add) + (import $add2 "env" "add" (param f64 f64) (result f64)) + (func $add (param $x f64) (param $y f64) (result f64) + (f64.add + (call_import $add2 (get_local $x) (get_local $y)) + (f64.add (get_local $x) (get_local $y)) + ) + ) + )`; + let startTime = 0, endTime = 0; + let yieldStartTime = 0, yieldEndTime = 0; + let iterations = 10000000; + let yieldAt = 10000; + let i = 0; + let end = () => { + let duration = endTime - startTime; + console.log('benchmark ended: '+(duration / iterations)+' ms/call ('+duration+' ms)') + } + + let runChunk = () => { + yieldEndTime = Date.now(); + if (yieldStartTime) { + // subtract time spent yielding + startTime += yieldEndTime - yieldStartTime; + } + for (; i < iterations; ++i) { + if (i != 0 && i % yieldAt == 0) { + break; + } + let buf = Binaryen.compileWast(input1); + } + if (i < iterations) { + endTime = Date.now(); + end(); + } else { + // yield main thread + yieldStartTime = Date.now(); + setTimeout(chunk, 0); + } + } + + console.log('benchmark starting') + startTime = Date.now(); + runChunk(); + +} + +</script> +</body> +</html>
\ No newline at end of file diff --git a/test/binaryen.js/browser.html b/test/binaryen.js/browser.html new file mode 100644 index 000000000..fcb0fe082 --- /dev/null +++ b/test/binaryen.js/browser.html @@ -0,0 +1,80 @@ +<!DOCTYPE HTML> +<html lang="en"> + <head> + <meta charset="utf-8"> + <title>Binaryen</title> + <style type="text/css"> + +* { margin:0; padding:0; font-family:inherit; } +body { + margin:2em; + background:white; + color:black; + font:14px monospace; + white-space: pre; +} +p { + white-space: normal; + font-family:sans-serif; +} + + </style> + <script type="text/javascript" src="../../bin/binaryen.js"></script> + </head> + <body><script type="text/javascript"> +// Separate javascript tag to support <=ES3 browser (the other block uses ES5 and ES6 features) +if (!window.Wasm) { + var e = document.createElement('p'); + e.innerHTML = 'No WASM support detected.<br>Please see <a href="https://github.com/kripken/emscripten/wiki/WebAssembly#testing-native-webassembly-in-browsers">https://github.com/kripken/emscripten/wiki/WebAssembly#testing-native-webassembly-in-browsers</a> for instructions on how to enable it.'; + document.body.appendChild(e); +} +</script><script type="text/javascript"> +if (window.Wasm) { + try { + let startTime = new Date; + document.write("Start at "+startTime.toLocaleTimeString()+"\n"); + + Binaryen = Binaryen(); + + var input1 =` + (module + (export "add" $add) + (import $add2 "env" "add" (param f64 f64) (result f64)) + (func $add (param $x f64) (param $y f64) (result f64) + (f64.add + (call_import $add2 (get_local $x) (get_local $y)) + (f64.add (get_local $x) (get_local $y)) + ) + ) + )`; + + document.write("let buf = Binaryen.compileWast('"+input1+"')\n"); + let buf = Binaryen.compileWast(input1); + + document.write("Wasm.verifyModule(buf) ..."); + Wasm.verifyModule(buf); + document.write(" OK\n"); + + document.write("let m = Wasm.instantiateModule(buf, {env:{add: function(a, b){...}}})\n"); + let m = Wasm.instantiateModule(buf, { + env: { + add: function(a, b) { return a + b; } + } + }); + + let res = m.exports.add(10.0, 20) + document.write("m.exports.add(10.0, 20) => "+res+"\n"); + + let endTime = new Date; + document.write("Completed at "+endTime.toLocaleTimeString()+ + " (total of "+(endTime - startTime)+" ms)\n"); + + } catch (err) { + document.write("Error: " + (err.stack || String(err))); + throw err; + } +} + +</script> +</body> +</html>
\ No newline at end of file |