diff options
author | Daniel Wirtz <dcode@dcode.io> | 2019-12-20 00:36:11 +0100 |
---|---|---|
committer | Alon Zakai <azakai@google.com> | 2019-12-19 15:36:11 -0800 |
commit | 02e6ba2b139e6c7ac0a97baa2af75df4250e140f (patch) | |
tree | abef9ae255bf9d7369c47ef8c0ff714ef4d85b46 /test/binaryen.js/hello-world.js | |
parent | 81c16df401347e8e067fe9ccf0c26ae532bc03d5 (diff) | |
download | binaryen-02e6ba2b139e6c7ac0a97baa2af75df4250e140f.tar.gz binaryen-02e6ba2b139e6c7ac0a97baa2af75df4250e140f.tar.bz2 binaryen-02e6ba2b139e6c7ac0a97baa2af75df4250e140f.zip |
Compile Binaryen to WebAssembly (#2503)
This PR enables compiling Binaryen to WebAssembly when building binaryen.js. Since WebAssembly is best compiled and instantiated asynchronously in browsers, it also adds a new mechanism to tell if respectively when the module is ready by means of one of the following:
// Using a promise
const binaryen = require("binaryen");
binaryen.ready.then(() => {
... use normally ...
});
// Using await
const binaryen = require("binaryen");
(async () => {
await binaryen.ready;
... use normally ...
})();
// Where top-level await is available
const binaryen = await require("binaryen").ready;
... use normally ...
One can also tell if Binaryen is already ready (for example when assuming it in follow-up code) by:
if (/* we already know that */ binaryen.isReady) {
... use normally ...
} else {
throw Error("Binaryen is supposed to be ready here but isn't");
}
The JS test cases have been updated accordingly by wrapping everything in a test function and invoking it once ready. Documentation will have to be updated as well to cover this of course. New file size is about 2.5mb, even though the Wasm becomes inlined into the JS file which makes distribution across different environments a lot easier.
Also makes building binaryen (to either js or wasm) emit binaryen.js, and not binaryen_js.js etc.
Supersedes and thus fixes #1381
With .ready it also fixes #2452
Diffstat (limited to 'test/binaryen.js/hello-world.js')
-rw-r--r-- | test/binaryen.js/hello-world.js | 96 |
1 files changed, 50 insertions, 46 deletions
diff --git a/test/binaryen.js/hello-world.js b/test/binaryen.js/hello-world.js index 414e5bfd1..d14c97912 100644 --- a/test/binaryen.js/hello-world.js +++ b/test/binaryen.js/hello-world.js @@ -5,49 +5,53 @@ function assert(x) { // "hello world" type example: create a function that adds two i32s and // returns the result -// Create a module to work on -var module = new Binaryen.Module(); - -// Start to create the function, starting with the contents: Get the 0 and -// 1 arguments, and add them, then return them -var left = module.local.get(0, Binaryen.i32); -var right = module.local.get(1, Binaryen.i32); -var add = module.i32.add(left, right); -var ret = module.return(add); - -// Create the add function -// Note: no additional local variables (that's the []) -var ii = Binaryen.createType([Binaryen.i32, Binaryen.i32]) -module.addFunction('adder', ii, Binaryen.i32, [], ret); - -// Export the function, so we can call it later (for simplicity we -// export it as the same name as it has internally) -module.addFunctionExport('adder', 'adder'); - -// Print out the text -console.log(module.emitText()); - -// Optimize the module! This removes the 'return', since the -// output of the add can just fall through -module.optimize(); - -// Print out the optimized module's text -console.log('optimized:\n\n' + module.emitText()); - -// Get the binary in typed array form -var binary = module.emitBinary(); -console.log('binary size: ' + binary.length); -console.log(); -assert(module.validate()); - -// We don't need the Binaryen module anymore, so we can tell it to -// clean itself up -module.dispose(); - -// Compile the binary and create an instance -var wasm = new WebAssembly.Instance(new WebAssembly.Module(binary), {}) -console.log("exports: " + Object.keys(wasm.exports).sort().join(",")); -console.log(); - -// Call the code! -console.log('an addition: ' + wasm.exports.adder(40, 2)); +function test() { + // Create a module to work on + var module = new Binaryen.Module(); + + // Start to create the function, starting with the contents: Get the 0 and + // 1 arguments, and add them, then return them + var left = module.local.get(0, Binaryen.i32); + var right = module.local.get(1, Binaryen.i32); + var add = module.i32.add(left, right); + var ret = module.return(add); + + // Create the add function + // Note: no additional local variables (that's the []) + var ii = Binaryen.createType([Binaryen.i32, Binaryen.i32]) + module.addFunction('adder', ii, Binaryen.i32, [], ret); + + // Export the function, so we can call it later (for simplicity we + // export it as the same name as it has internally) + module.addFunctionExport('adder', 'adder'); + + // Print out the text + console.log(module.emitText()); + + // Optimize the module! This removes the 'return', since the + // output of the add can just fall through + module.optimize(); + + // Print out the optimized module's text + console.log('optimized:\n\n' + module.emitText()); + + // Get the binary in typed array form + var binary = module.emitBinary(); + console.log('binary size: ' + binary.length); + console.log(); + assert(module.validate()); + + // We don't need the Binaryen module anymore, so we can tell it to + // clean itself up + module.dispose(); + + // Compile the binary and create an instance + var wasm = new WebAssembly.Instance(new WebAssembly.Module(binary), {}) + console.log("exports: " + Object.keys(wasm.exports).sort().join(",")); + console.log(); + + // Call the code! + console.log('an addition: ' + wasm.exports.adder(40, 2)); +} + +Binaryen.ready.then(test); |