Skip to content

Gentle/wizerjs

Repository files navigation

WizerJs

Thanks to recent advancements in wasmtime, wizer can now be compiled without the wasmtime runtime. This enables us to compile wizer to wasm and use it together with JS WebAssembly.Instance

This version of wizer should be compatible with any JS runtime that implements WebAssembly, including Cloudflare Workers and browsers.

For now I'm bundling the wasm as base64 so that it can run anywhere, so the package size is 1.5MB (510kb gzipped), which seems fine, but the wasm itself would be around 1MB.

API

The API mirrors wasmtime-wizer's API, but with camelCased methods

export declare class Wizer {
    constructor();
    run(wasm_bytes: Uint8Array): Promise<Uint8Array>;
    /**
     * After initialization, should the Wasm module still export the
     * initialization function?
     *
     * This is `false` by default, meaning that the initialization function is
     * no longer exported from the Wasm module.
     */
    keepInitFunc(keep: boolean): void;
    /**
     * Add a function rename to perform.
     */
    addFuncRename(new_name: string, old_name: string): void;
    /**
     * The export name of the initializer function.
     *
     * Defaults to `"wizer-initialize"`.
     */
    initFunc: string;
}

notably, the run method is a bit more complex to support providing wasip1 or other interfaces during preinitialization

https://github.com/bjorn3/browser_wasi_shim

Example with wasi

import { Wizer } from "wizerjs";
import { WASI, File, OpenFile, ConsoleStdout } from "@bjorn3/browser_wasi_shim";

async function example(inputWasm) {
  let args = ["bin", "arg1", "arg2"];
  let env = ["FOO=bar"];
  let fds = [
    new OpenFile(new File([])), // stdin
    ConsoleStdout.lineBuffered((msg) => console.log(`[WASI stdout] ${msg}`)),
    ConsoleStdout.lineBuffered((msg) => console.warn(`[WASI stderr] ${msg}`)),
  ];
  let wasi = new WASI(args, env, fds);
  let wizer = new Wizer();
  wizer.initFunc = "wizer.initialize";
  wizer.addFuncRename("newname", "oldname");
  wizer.keepInitFunc(false);
  return await wizer.run(
    inputWasm,
    { wasi_snapshot_preview1: wasi.wasiImport },
    (instance) => { wasi.initialize(instance) }
  );
}

About

wizer without wasmtime, using wasm-bindgen

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published