Wasm Builders 🧱

Cover image for JavaScript Compiled With WebAssembly
Kirtee Prajapati
Kirtee Prajapati

Posted on

JavaScript Compiled With WebAssembly

JavaScript is found to be the most beloved language by web developers and Compiling it with webassembly will be a great approach.

Javascript is an extremely dynamic language and it was expected it would have large runtime for compilation if done to webassembly.

Before we begin you have to install certain toolchains to compile your javascript code.

Environment Setup

1. Install Javy Toolchain

  • First fork the Javy repo and then clone it to your desktop
$ https://github.com/<username>/javy.git
Enter fullscreen mode Exit fullscreen mode

The username will be filled once you fork the repo.

  • Navigate to javy directory
$ cd javy
Enter fullscreen mode Exit fullscreen mode
  • Install dependencies

Build

  1. Rustup installer for Rust programing language
$ curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
Enter fullscreen mode Exit fullscreen mode
  1. wasm32-wasi
$ rustup target add wasm32-wasi
Enter fullscreen mode Exit fullscreen mode
  1. wasi-sdk
$ make download-wasi-sdk
Enter fullscreen mode Exit fullscreen mode

Development

  1. wasmtime-cli
$ cargo install wasmtime-cli
Enter fullscreen mode Exit fullscreen mode
  1. cargo-wasi
$ cargo install cargo-wasi
Enter fullscreen mode Exit fullscreen mode

Building

# access to the executable in target/release/javy
$ make

# alternatively for global installation.
$ make && cargo install --path crates/cli
Enter fullscreen mode Exit fullscreen mode

Now come out of the directory

$ cd ~/
Enter fullscreen mode Exit fullscreen mode

2. get msgpack-tools

Are command-line utilities, that Convert MessagePack to JSON and vice-versa.

  • msgpack2json -- Convert MessagePack to JSON
  • json2msgpack -- Convert JSON to MessagePack we'll be using these commands while compiling.
  1. Installation Can be installed directly via git-repo or navigate the link provided above for alternative methods
$ git clone https://github.com/ludocode/msgpack-tools.git
Enter fullscreen mode Exit fullscreen mode

Inside masgpack-tools run the following and return the home directory

$ cd msgpack-tools
$ ./configure && make && sudo make install
$ cd ~/
Enter fullscreen mode Exit fullscreen mode
  1. Install Wasmtime
curl https://wasmtime.dev/install.sh -sSf | bash
Enter fullscreen mode Exit fullscreen mode

Image description

at your home directory you'll be having this tools installed

Compile JavaScript Code

Note: If you run make while Building globally then you can run any .js folder outside of it
but in my case, I run it locally with the command make

navigate to the release folder and create a .js file for code snippets and

.json file for providing input.

the default implementation of Javy expects a msgpack input to be sent through stdin and so we created a .json file.

$ cd /javy/target/release
$ touch index.js
$ touch input.json
Enter fullscreen mode Exit fullscreen mode

inside your .js file paste your code snippet

Pascal's Triangle

function generatePascal(n) {
    //2D array
    var arr = [];
    var tmp;
    for (var i = 0; i < n; i++) {
        //Each element in array is in turn an array
        // The index is the row number and the array values are the row values
        arr[i] = [];
        for (var j = 0; j <= i; j++) {
            //If index is last element the value is always 1
            if (j == i) {
                arr[i].push(1);
            } else {
                //The following line adds up the two numbers directly above this element.
                tmp = (!!arr[i - 1][j - 1] ? arr[i - 1][j - 1] : 0) + (!!arr[i - 1][j] ? arr[i - 1][j] : 0);
                arr[i].push(tmp);
            }
        }
    }
    return arr;
}
Enter fullscreen mode Exit fullscreen mode

Inside .json file provide your input
I provided 15.

Image description

Convert JavaScript(.js) to WebAssembly(.wasm)

$ javy index.js -o index.wasm
Enter fullscreen mode Exit fullscreen mode

Convert JSON to msgpack format

$ json2msgpack -i input.json | wasmtime run index.wasm | msgpack2json
Enter fullscreen mode Exit fullscreen mode

Output in the terminal would be
Image description

References:

  1. https://enarx.dev/docs/WebAssembly/JavaScript
  2. https://www.wasm.builders/deepanshu1484/javascript-and-wasi-24k8
  3. https://github.com/ludocode/msgpack-tools#installation
  4. https://wasmtime.dev/
  5. https://github.com/ludocode/msgpack-tools/releases/tag/v0.6
  6. https://github.com/WebAssembly/design/issues/219

Top comments (1)

Collapse
 
flyingzebra profile image
Schrimpy

Followed the instructions, but after hitting 'build', I get the following:

<wasi/api.h> is only supported on WASI platforms.

It must be something with the stdio library, but not sure what it is, but I am wondering right now if this is supposed to build flawlessly on MacOS Big Sur (Intel Core i7) after all.