Skip to content

A simple parser for WebAssembly imports with WebAssembly Type Reflection JS API compatibility.

License

Notifications You must be signed in to change notification settings

swiftwasm/wasm-imports-parser

Repository files navigation

wasm-imports-parser

npm version

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.

Installation

npm install wasm-imports-parser

Example

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' }
// >   }
// > ]

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 Not available as of 2024-06

License

This project is licensed under the MIT License - see the LICENSE file for details.

About

A simple parser for WebAssembly imports with WebAssembly Type Reflection JS API compatibility.

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published