summaryrefslogtreecommitdiff
path: root/test/binaryen.js/hello-world.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/binaryen.js/hello-world.js')
-rw-r--r--test/binaryen.js/hello-world.js80
1 files changed, 36 insertions, 44 deletions
diff --git a/test/binaryen.js/hello-world.js b/test/binaryen.js/hello-world.js
index d14c97912..d82de6116 100644
--- a/test/binaryen.js/hello-world.js
+++ b/test/binaryen.js/hello-world.js
@@ -1,57 +1,49 @@
-function assert(x) {
- if (!x) throw 'error!';
-}
-
// "hello world" type example: create a function that adds two i32s and
// returns the result
-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 a module to work on
+var module = new binaryen.Module();
- // 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);
+// 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);
- // 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');
+// 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);
- // Print out the text
- console.log(module.emitText());
+// 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');
- // Optimize the module! This removes the 'return', since the
- // output of the add can just fall through
- module.optimize();
+// Print out the text
+console.log(module.emitText());
- // Print out the optimized module's text
- console.log('optimized:\n\n' + module.emitText());
+// Optimize the module! This removes the 'return', since the
+// output of the add can just fall through
+module.optimize();
- // Get the binary in typed array form
- var binary = module.emitBinary();
- console.log('binary size: ' + binary.length);
- console.log();
- assert(module.validate());
+// Print out the optimized module's text
+console.log('optimized:\n\n' + module.emitText());
- // We don't need the Binaryen module anymore, so we can tell it to
- // clean itself up
- module.dispose();
+// Get the binary in typed array form
+var binary = module.emitBinary();
+console.log('binary size: ' + binary.length);
+console.log();
+assert(module.validate());
- // 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();
+// We don't need the Binaryen module anymore, so we can tell it to
+// clean itself up
+module.dispose();
- // Call the code!
- console.log('an addition: ' + wasm.exports.adder(40, 2));
-}
+// 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();
-Binaryen.ready.then(test);
+// Call the code!
+console.log('an addition: ' + wasm.exports.adder(40, 2));