npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@haribala/wasm2js

v1.1.1

Published

A compiler from WASM to JS written in Typescript.

Downloads

19

Readme

WASM to JS compiler

A compiler (decompiler?) from WebAssembly to Javascript written in Typescript.

https://www.npmjs.com/package/@haribala/wasm2js

Demos: https://haribala.dev/wasm2js

NOTE: This library is not the same as packages like https://github.com/thlorenz/wasm2js , https://github.com/mafintosh/wat2js , etc. which only embed the WASM binary in a Javascript module. Unlike the above packages, this library parses the WASM binary and compiles the abstract syntax tree to Javascript code instruction by instruction. The result is similar to porting your WASM app to Javascript line by line.

Technically it might be called a decompiler since we are going from a low-level assembly language (WASM) to a high-level language (Javascript).

Usage

import { compile, instantiate } from '@haribala/wasm2js';

const fetch_wasm_binary = async (url) => {
    const res = await fetch(url);
    if (!res.ok) throw new Error('failed to fetch the wasm binary');
    const wasmBytes = new Uint8Array(await res.arrayBuffer());
    return wasmBytes;
};

const compile_and_execute = async (wasmBytes) => {
    // Compile the wasm binary to javascript code.
    // The generated code is a string, so you can print it.
    const jsCode = compile(wasmBytes);
    console.log('javascript code:');
    console.log(jsCode);
    // Create an instance of the wasm module.
    // NOTE: 'instantiate' will also call the 'start' function if it exists.
    // https://webassembly.github.io/spec/core/text/modules.html#start-function
    const importObject = { /* imports required by the wasm module */ };
    const instance = instantiate(jsCode, importObject);
    console.log('instance:', instance);
    // Call an exported wasm function.
    const result = instance.exports.myfunc( /* arguments */ );
    return result;
};

fetch_wasm_binary('/url/to/myapp.wasm')
    .then(compile_and_execute)
    .then(console.log)
    .catch(console.error);

API

const compile: (
    wasmBytes: Uint8Array,
    debug_mode?: boolean = false,
    strict_maths?: boolean = true,
) => string;

const instantiate: (
    compiledJSCode: string,
    importObject: object,
) => MyWasmInstance;

The debug_mode optional parameter adds comments to the generated Javascript code so that it's easier to understand which WASM instructions got compiled to what JS. It also adds extra checks and console.log statements in the code to help detect bugs.

The strict_maths optional parameter is necessary for getting correct results from signed and unsigned math operations. However, it leads to a bigger code size and might slow down the execution.

To do

  • Add support for imported global variable immutability (const)
  • Add support for vector/SIMD WASM instructions
  • Add option to optimize the generated JS code. Example: local.get 0, local.get 1, i32.add, local.set 0 -> x = x + y