WasmBuilder
raw JSON →WasmBuilder is a JavaScript package initially released in 2019, designed for the manual construction of WebAssembly (WASM) modules. It operates at an extremely low level, requiring developers to specify WASM bytecode directly in hexadecimal format. The library provides a programmatic interface to define functions, memory, and tables, offering fine-grained control over the WASM module's internal structure without relying on higher-level language compilation. Its last recorded update was in October 2019, with version 0.0.16, indicating the project is no longer actively maintained. This low-level approach differentiates it from compilers and makes it suitable primarily for educational purposes, deep dives into WASM internals, or highly specialized niche use cases requiring byte-level WASM manipulation.
Common errors
error ReferenceError: require is not defined ↓
"type": "module" from package.json, use a .cjs file extension, or run with older Node.js versions). If forced to use ESM, consider dynamic import('wasmbuilder') or a build process to convert CJS to ESM. error TypeError: WebAssembly.instantiate(): Wasm code is not valid: unknown opcode ↓
error TypeError: instance.exports.yourFunctionName is not a function ↓
builder.exportFunction('internalName', 'externalName') is called for every function you wish to expose, and that 'internalName' exactly matches the first argument given to builder.addFunction. Warnings
breaking The `wasmbuilder` package has been abandoned since its last update in October 2019 (version 0.0.16). It is not maintained, may contain unpatched security vulnerabilities, and is unlikely to be compatible with newer JavaScript/Node.js features or WASM specifications without significant manual intervention. ↓
gotcha This library requires deep knowledge of WebAssembly bytecode and its hexadecimal representation. Constructing WASM modules manually via hex strings is error-prone and complex, even for simple operations. ↓
gotcha The library primarily uses CommonJS (`require`) syntax. Attempting to use it directly in a modern ESM (`import`) environment will result in errors. ↓
Install
npm install wasmbuilder yarn add wasmbuilder pnpm add wasmbuilder Imports
- WasmBuilder wrong
import { WasmBuilder } from 'wasmbuilder';correctconst WasmBuilder = require('wasmbuilder'); - WasmBuilder instance
const builder = new WasmBuilder();
Quickstart
const WasmBuilder = require('wasmbuilder');
const fs = require('fs');
// Initialize the builder
const builder = new WasmBuilder();
// Define an 'add' function that takes two i32 and returns one i32.
// Its body in WASM bytecode (hex): (local.get 0) (local.get 1) (i32.add) (end)
// This translates to the hex string: "200020016a0b"
builder.addFunction(
"add", // function name for internal use
["i32", "i32"], // parameter types (two 32-bit integers)
["i32"], // return type (one 32-bit integer)
"200020016a0b" // WASM bytecode as hex string
);
// Export the 'add' function so it can be called from JavaScript
builder.exportFunction("add", "add");
// Build the WASM module to get its binary representation
const wasmBinary = builder.build();
// Optionally, write the WASM binary to a file
fs.writeFileSync('add.wasm', wasmBinary);
console.log('add.wasm created successfully.');
// Instantiate and use the generated WASM module
(async () => {
try {
const { instance } = await WebAssembly.instantiate(wasmBinary, {});
const addResult = instance.exports.add(5, 7);
console.log(`Result of add(5, 7): ${addResult}`); // Expected output: 12
} catch (e) {
console.error("Error instantiating or running WASM module:", e);
}
})();