wasm-imports-parser
v1.0.4
Published
A simple parser for WebAssembly imports
Downloads
2,783
Readme
wasm-imports-parser
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' }
// > }
// > ]
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 | :white_check_mark: | Available in Chrome 78 and later, and Node.js 13.0.0 and later | | SpiderMonkey | :construction: | Available in Firefox Nightly | | JavaScriptCore | :x: | Not available as of 2024-06 |
License
This project is licensed under the MIT License - see the LICENSE file for details.