Low level tooling for WebAssembly in JavaScript using wasm-tools

Overview

js-wasm-tools

js-wasm-tools compiles some of the API of wasm-tools to JavaScript and WebAssembly via wasm-bindgen. This offers low level tooling for WebAssembly in JavaScript, such as parsing WAT (WebAssembly Text Format) into bytes, translating the WebAssembly binary format to text, and more.

Install

npm install js-wasm-tools

Playground

You can try it out live on StackBlitz.com.

Usage

Using Vite:

import initWasmTools, * as wasmTools from 'js-wasm-tools';
import WASM_TOOLS_WASM_URL from 'js-wasm-tools/wasm_tools_js_bg.wasm?url';

await initWasmTools(WASM_TOOLS_WASM_URL);

const source = '(module)';

const binary = wasmTools.parseWat(source);

With Node.js:

import initWasmTools, * as wasmTools from 'js-wasm-tools';
import fs from 'node:fs';
import path from 'node:path';
import * as url from 'url';

const __dirname = url.fileURLToPath(new URL('.', import.meta.url));

const bytes = fs.readFileSync(path.join(__dirname, 'node_modules/js-wasm-tools/dist/js_wasm_tools_bg.wasm'));

await initWasmTools(bytes);

const source = '(module)';

const binary = wasmTools.parseWat(source);

console.log(binary);

API

parseWat(source: string): Uint8Array

Parses a string as the WebAssembly Text format, returning the WebAssembly binary format.

parseBytes(bytes: Uint8Array): Uint8Array

Parses bytes as either the WebAssembly Text format, or a binary WebAssembly module.

This function will attempt to interpret the given bytes as one of two options:

  • A utf-8 string which is a *.wat file to be parsed
  • A binary WebAssembly file starting with b"\0asm"
const encoder = new TextEncoder();

const bytes = wasmTools.parseWat(encoder.encode('(module)'));

expect(bytes).toEqual([0, 97, 115, 109, 1, 0, 0, 0]);
const bytes = wasmTools.parseWat([0, 97, 115, 109, 1, 0, 0, 0]);

expect(bytes).toEqual([0, 97, 115, 109, 1, 0, 0, 0]);

printBytes(bytes: Uint8Array): string

Prints a Wasm binary blob into a string which is its textual representation.

const wat = wasmTools.printBytes(new Uint8Array([0, 97, 115, 109, 1, 0, 0, 0]));

expect(wat).toEqual('(module)');

desugarWat(source: string): { wat: string, bytes: Uint8Array }

Parses a string as the WebAssembly Text format, and desugars the module, e.g. unfolding expressions.

const { wat, bytes } = wasmTools.desugarWat(`
  (module
    (func $foo
      (call $bar (i32.const 1) (i32.const 2))
    )
    (func $bar (param i32 i32))
  )
`);

expect(wat).toEqual(`
  (module
    (type (;0;) (func))
    (type (;1;) (func (param i32 i32)))
    (func $foo (;0;) (type 0)
      i32.const 1
      i32.const 2
      call $bar
    )
    (func $bar (;1;) (type 1) (param i32 i32))
  )
`);

validate(bytes: Uint8Array): Types

Test whether the given buffer contains a valid WebAssembly module or component, analogous to WebAssembly.validate in the JS API.

Upon success returns the type information for the top-level module or component.

const encoder = new TextEncoder();

const types = wasmTools.validate(
  encoder.encode(`
    (module
      (func $foo (result f32)
        f32.const 1
      )
      (func $bar (param i32 i32))
    )
  `)
);

expect(types).toEqual({
  types: [
    {
      params: [],
      results: ['f32'],
    },
    {
      params: ['i32', 'i32'],
      results: [],
    },
  ],
  functions: [
    {
      params: [],
      results: ['f32'],
    },
    {
      params: ['i32', 'i32'],
      results: [],
    },
  ],
  globals: [],
  memories: [],
  tables: [],
  elements: [],
});
You might also like...
RDF playground using WASM-compiled Sophia

SoWasm: an RDF playground based on Sophia This started as an experiment of compiling Sophia into WebAssembly, and grew into a (hopefully) useful playg

The JavaScript runtime that aims for productivity and ease

Byte Byte is a easy and productive runtime for Javascript . It makes making complex programs simple and easy-to-scale with its large and fast Rust API

A JavaScript Runtime built with Mozilla's SpiderMonkey Engine and Rust

Spiderfire Spiderfire is a javascript runtime built with Mozilla's SpiderMonkey engine and Rust. Spiderfire aims to disrupt the server-side javascript

Guarding 是一个用于 Java、JavaScript、Rust、Golang 等语言的架构守护工具。借助于易于理解的 DSL,来编写守护规则。Guarding is a guardians for code, architecture, layered.

Guarding Guarding is a guardians for code, architecture, layered. Using git hooks and DSL for design guard rules. Usage install cargo install guarding

Javascript wrapper bindings for diamond types

Diamond JS wrapper library This is a javascript / WASM wrapper around diamond types. This code is currently experimental WIP. Do not trust this for an

A programming environment that aims to help people learn how to program in JavaScript, while giving them a tour on how old computers and their limitations used to be.
A programming environment that aims to help people learn how to program in JavaScript, while giving them a tour on how old computers and their limitations used to be.

This repository is for the new under renovation rewrite of the LIKO-12 project. The legacy version with the original stars and contributions is still

Diamond is a minimalistic, powerful, and modern Javascript runtime that uses Deno_Core.

Diamond Diamond is a minimalistic, powerful, and modern Javascript runtime that uses Deno_Core. Installation Diamond is currently in beta(not even Alp

Modern JavaScript runtime for Sony PSP, based on rust-psp and QuickJS.

PSP.js Modern JavaScript runtime for Sony PSP, based on rust-psp and QuickJS. ⚠️ Currently in PoC state, unusable for developing JavaScript apps yet.

Node.js bindings to the ripgrep library, for fast file searching in JavaScript without child processes!

ripgrepjs ripgrepjs: Node.js bindings to the ripgrep library, for direct integration with JS programs without spawning an extra subprocess! This proje

Owner
Dominic Elm
Founding Engineer @StackBlitz • Building WebContainer • GDE • @thoughtram • JavaScript stuff • Node.js • Rust 🦀 • Deep Learning 🤖 • Cyclist 🚴‍♂️
Dominic Elm
Rust bindings for Supabase JavaScript library via WebAssembly.

supabase-js-rs Rust bindings for Supabase JavaScript library via WebAssembly. Usage Add supabase-js-rs to Cargo.toml supabase-js-rs = { version = "0.1

Valery Stepanov 8 Jan 13, 2023
libnotcurses-sys is a low-level Rust wrapper for the notcurses C library

libnotcurses-sys is a low-level Rust wrapper for the notcurses C library This library is built with several layers of zero-overhead abstractions over

nick black 29 Nov 26, 2022
`wasm-snip` replaces a WebAssembly function's body with an `unreachable`

wasm-snip wasm-snip replaces a Wasm function's body with an unreachable instruction. API Docs | Contributing | Chat Built with ?? ?? by The Rust and W

Rust and WebAssembly 177 Dec 28, 2022
WebAssembly (Wasm) interpreter.

Continuous Integration Test Coverage Documentation Crates.io wasmi- WebAssembly (Wasm) Interpreter wasmi was conceived as a component of parity-ethere

Parity Technologies 1k Jan 4, 2023
Mod_wasm - an extension module for the Apache HTTP Server (httpd) that enables the usage of WebAssembly (Wasm).

mod_wasm is an extension module for the Apache HTTP Server (httpd) that enables the usage of WebAssembly (Wasm). This module will allow to execute certain tasks in the backend in a very efficient and secure way.

VMware  Labs 67 Dec 21, 2022
👾 Run WebAssembly (WASM-4) games on small devices (like PyBadge)

?? gamgee Run WebAssembly (WASM-4) games on small devices. Gamgee is a WASM-4 games emulator written in Rust and designed to be executed on devices wi

Orsinium Labs 5 Feb 27, 2024
Distribute a wasm SPA as HTML by wrapping it as a polyglot "html+wasm+zip"

A packer that adds a webpage to WASM module, making it self-hosted! Motivation At the moment, Browsers can not execute WebAssembly as a native single

Andreas Molzer 3 Jan 2, 2023
plugy empowers you to construct agnostic dynamic plugin systems using Rust and WebAssembly.

plugy plugy is a plugin system designed to enable the seamless integration of Rust-based plugins into your application. It provides a runtime environm

Geoffrey Mureithi 22 Aug 12, 2023
Parametric surfaces drawn using the Rust + WASM toolchain with WebGL, React, and TypeScript.

Parametric Surfaces in the Browser My.Movie.3.mp4 Wanted to experiment with WebGL using the Rust + WASM toolchain, with React and TypeScript to glue e

Benji Nguyen 45 Oct 21, 2022
Realtime audio processing / synthesis using Rust/WASM in the browser.

Rust Audio About This repo is my investigation into using Rust for creative audio coding on various platforms (e.g. desktop, web, etc.), but especiall

Austin Theriot 30 Jan 5, 2023