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 /src | |
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 'src')
-rw-r--r-- | src/js/binaryen.idl | 13 | ||||
-rw-r--r-- | src/js/binaryen.js-extended.js | 56 | ||||
-rw-r--r-- | src/js/binaryen.js-post.js | 29 | ||||
-rw-r--r-- | src/js/binaryen.js-pre.js | 1 |
4 files changed, 98 insertions, 1 deletions
diff --git a/src/js/binaryen.idl b/src/js/binaryen.idl index 6a1d63d8a..b7244fbe2 100644 --- a/src/js/binaryen.idl +++ b/src/js/binaryen.idl @@ -30,7 +30,18 @@ interface ModuleInstance { [Value] Literal callExport([Ref] Name name, [Ref] LiteralList arguments); }; -[Prefix="ModuleInstance::"] + +interface BufferWithRandomAccess { + void BufferWithRandomAccess(boolean debug); + unsigned long size(); + [Operator="[]"] octet at(unsigned long index); +}; + +interface WasmBinaryWriter { + void WasmBinaryWriter(Module m, [Ref] BufferWithRandomAccess outbuf, boolean debug); + void write(); +}; + interface LiteralList { void LiteralList(); diff --git a/src/js/binaryen.js-extended.js b/src/js/binaryen.js-extended.js new file mode 100644 index 000000000..74e3e4bbe --- /dev/null +++ b/src/js/binaryen.js-extended.js @@ -0,0 +1,56 @@ +// Extended API + +// Makes the internal buffer iterable so we can use Uint8Array.from +if (typeof Symbol !== 'undefined' && Symbol.iterator) { + Module['BufferWithRandomAccess'].prototype[Symbol.iterator] = function() { + var b = this, i = 0, L = b.size(); + return { + next: function() { + if (i < L) { + return { done: false, value: b.at(i++) } + } else { + return { done: true } + } + } + } + }; +} + +// Converts (copies) a BufferWithRandomAccess to native ArrayBuffer +if (typeof Uint8Array !== 'undefined') { + Module['BufferWithRandomAccess'].prototype['toArrayBuffer'] = function() { + return Uint8Array['from'](this).buffer; // :ArrayBuffer + }; +} + +// Parse and compile S-expression text syntax into WASM code. +// When running in NodeJS, this functions returns a Buffer instead of ArrayBuffer. +Module['compileWast'] = function compileWast(sourceText /*:string*/) /*:ArrayBuffer*/ { + var parser = new Module['SExpressionParser'](sourceText); + + // console.log('s-expr dump:'); + // parser.get_root().dump(); + + var s_module = parser.get_root().getChild(0); + // console.log('s_module', s_module) + + // strange API for creating WASM `module` from S-expression AST + var module = new Module['Module'](); + new Module['SExpressionWasmBuilder'](module, s_module); + + // console.log('module:', module); + // this.WasmPrinter.prototype.printModule(module); + + // emit WASM + var debug = false; + var buf0 = new Module['BufferWithRandomAccess'](debug); + var wasmWriter = new Module['WasmBinaryWriter'](module, buf0, debug); + wasmWriter.write(); + + if (ENVIRONMENT_IS_NODE) { + return Buffer['from'](Uint8Array['from'](buf0)); + } else { + return buf0['toArrayBuffer'](); + } +}; + diff --git a/src/js/binaryen.js-post.js b/src/js/binaryen.js-post.js new file mode 100644 index 000000000..7389f5afe --- /dev/null +++ b/src/js/binaryen.js-post.js @@ -0,0 +1,29 @@ +return Module; +}; +if (typeof exports != 'undefined') { + (function(){ + var a = Binaryen(); + if (typeof module === 'object') { + module.exports = a; + } else { + for (var k in a) { + exports[k] = a[k]; + } + } + })(); +} +(typeof window !== 'undefined' ? window : + typeof global !== 'undefined' && ( + typeof process === 'undefined' || + + // Note: We must export "Binaryen" even inside a CommonJS/AMD/UMD module + // space because check.py generates a.js which requires Binaryen global var + ( process.argv && + Array.isArray(process.argv) && + process.argv[1] && + process.argv[1].substr(-5) === '/a.js' + ) + + ) ? global : + this +)['Binaryen'] = Binaryen; diff --git a/src/js/binaryen.js-pre.js b/src/js/binaryen.js-pre.js new file mode 100644 index 000000000..ceb25f165 --- /dev/null +++ b/src/js/binaryen.js-pre.js @@ -0,0 +1 @@ +var Binaryen = function(Module){ |