Skip to main content

WebAssembly with JavaScript

note
  • WebAssembly support for JavaScript is good thanks to Javy.
  • Javy compiles QuickJS, a tiny JavaScript runtime, to Wasm along with the script to be executed.
  • JavaScript is the second most popular and desirable language to work with WebAssembly.
  • TypeScript, a superset of JavaScript, can be transpiled into JavaScript before being compiled to Wasm.

Install Javy

Install Javy following the instructions here:

https://github.com/bytecodealliance/javy

note

All of the build dependencies of the Javy toolchain have to be installed after cloning the GitHub repository.

JavaScript code

We will create a simple JavaScript application that will return us the Fibonacci sequence. Create a file named index.js:

function fibonacci(){
var num = 10;
var a = 1, b = 0, temp;

while (num >= 0){
temp = a;
a = a + b;
b = temp;
num--;
}
console.log("Fibonacci result is: ",b);

}

var Shopify = {
main: fibonacci
};
tip

Access the JavaScript codex repository for code samples, including the fibonacci example.

note

The Javy toolchain expects Shopify.main to point to our main function, in this case, fibonacci.

Compile to Wasm

Navigate to /javy/target/release directory to generate the Wasm file using the ./javy executable. You will have to save your JavaScript source code in this directory as well.

./javy index.js -o index.wasm

Run with Enarx

Enarx run index.wasm