Skip to content

The BLESS WASM based JavaScript Engine. Portable Embeddable JavaScript.

License

Notifications You must be signed in to change notification settings

blessnetwork/bls-javy

 
 

Repository files navigation

Javy

A JavaScript to Webassembly toolchain

A Bytecode Alliance project

Build status zulip chat

Installation

You can install b7s using either curl or wget:

sh -c "curl https://raw.githubusercontent.com/blocklessnetwork/bls-javy/main/download.sh | bash"

About this repo

Introduction: Run your JavaScript on WebAssembly. Javy takes your JavaScript code, and executes it in a WebAssembly embedded JavaScript runtime. Javy can create very small Wasm modules in the 1 to 16 KB range with use of dynamic linking. The default static linking produces modules that are at least 869 KB in size.

Installation

Pre-compiled binaries of the Javy CLI can be found on the releases page.

Build bls-javy and plugins

# build the core plugins
cargo build --release --target wasm32-wasip1 -p javy-plugin
# build test plugin (for testing)
cargo build --target=wasm32-wasip1 --release -p javy-test-plugin
# rebuild the plugin with javy runtime
cargo run -p javy-cli -- init-plugin ./target/wasm32-wasip1/release/test_plugin.wasm -o test_plugin.wasm
# create dummy js file
echo "console.log('Hello, World!');" > index.js
# compile javascript to wasm with javy runtime and plugin
cargo run -p javy-cli -- build -C plugin=test_plugin.wasm ./index.js

Example

Define your JavaScript like:

// Read input from stdin
const input = readInput();
// Call the function with the input
const result = foo(input);
// Write the result to stdout
writeOutput(result);

// The main function.
function foo(input) {
    return { foo: input.n + 1, newBar: input.bar + "!" };
}

// Read input from stdin
function readInput() {
    const chunkSize = 1024;
    const inputChunks = [];
    let totalBytes = 0;

    // Read all the available bytes
    while (1) {
        const buffer = new Uint8Array(chunkSize);
        // Stdin file descriptor
        const fd = 0;
        const bytesRead = Javy.IO.readSync(fd, buffer);

        totalBytes += bytesRead;
        if (bytesRead === 0) {
            break;
        }
        inputChunks.push(buffer.subarray(0, bytesRead));
    }

    // Assemble input into a single Uint8Array
    const { finalBuffer } = inputChunks.reduce((context, chunk) => {
        context.finalBuffer.set(chunk, context.bufferOffset);
        context.bufferOffset += chunk.length;
        return context;
    }, { bufferOffset: 0, finalBuffer: new Uint8Array(totalBytes) });

    return JSON.parse(new TextDecoder().decode(finalBuffer));
}

// Write output to stdout
function writeOutput(output) {
    const encodedOutput = new TextEncoder().encode(JSON.stringify(output));
    const buffer = new Uint8Array(encodedOutput);
    // Stdout file descriptor
    const fd = 1;
    Javy.IO.writeSync(fd, buffer);
}

Create a WebAssembly binary from your JavaScript by:

javy build index.js -o destination/index.wasm

For more information on the commands you can run javy --help

You can then execute your WebAssembly binary using a WebAssembly engine:

$ echo '{ "n": 2, "bar": "baz" }' | wasmtime index.wasm
{"foo":3,"newBar":"baz!"}%   

Documentation

Read the documentation here

About

The BLESS WASM based JavaScript Engine. Portable Embeddable JavaScript.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Rust 87.8%
  • JavaScript 9.4%
  • Shell 1.2%
  • Other 1.6%