A simple parser for WebAssembly imports with WebAssembly Type Reflection JS API compatibility.
Typically useful for constructing shared memory with a limit requested by imports of a WebAssembly module.
npm install wasm-imports-parser
import { parseImports } from 'wasm-imports-parser';
const moduleBytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x02, 0x06, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01,
]);
const imports = parseImports(moduleBytes);
console.log(imports);
// > [
// > {
// > module: '',
// > name: '',
// > kind: 'memory',
// > type: { minimum: 1, shared: false, index: 'i32' }
// > }
// > ]
As a polyfill for WebAssembly Type Reflection JS API
This parser can be used as a polyfill for the WebAssembly Type Reflection JS API.
import { polyfill } from 'wasm-imports-parser/polyfill.js';
const WebAssembly = polyfill(globalThis.WebAssembly);
const moduleBytes = new Uint8Array([
0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00,
0x02, 0x06, 0x01, 0x00, 0x00, 0x02, 0x00, 0x01,
]);
const module = await WebAssembly.compile(moduleBytes);
const imports = WebAssembly.Module.imports(module);
console.log(imports);
// > [
// > {
// > module: '',
// > name: '',
// > kind: 'memory',
// > type: { minimum: 1, shared: false, index: 'i32' }
// > }
// > ]
Implementation Status in JavaScript engines
Engine | Status | Note |
---|---|---|
V8 | ✅ | Available in Chrome 78 and later, and Node.js 13.0.0 and later |
SpiderMonkey | 🚧 | Available in Firefox Nightly |
JavaScriptCore | 🚧 | Available in Safari Technology Preview 202 and later |
This project is licensed under the MIT License - see the LICENSE file for details.