A general-purpose module for preprocessing WebAssembly (wasm).
npm install wasm-module-preprocessorA general-purpose module for preprocessing WebAssembly (wasm).
``bash`
npm install --save-dev wasm-module-preprocessor
wasm-module-preprocessor by default loads wasm modules asynchronously, andimportObject
takes an optional . All wasm modules are validated on build, so
that there is no need for runtime testing and beyond just validating wasm
modules, the size of the wasm module is checked too.
file.wasm
``
test.js
`js
import wasmModulePreprocessor from 'wasm-module-preprocessor';
import { readFileSync } from 'fs';
import { runInNewContext } from 'vm';
const bufferSource = readFileSync('file.wasm');
const wasmModuleExportString = wasmModulePreprocessor(
bufferSource,
{
// Defaults to:
// sync: false,
}
);
const wasmModuleExport = runInNewContext((() => ${wasmModuleExportString})());
(async () => {
try {
const {
instance: {
exports: {
main = () => undefined,
},
},
} = await wasmModuleExport(
// Add an optional importObject:
// {
// global: {},
// env: {},
// }
);
console.log(main() === 3); // true
} catch (err) {
console.error(err);
}
})();
`
file.wasm
``
test.js
`js
import wasmModulePreprocessor from 'wasm-module-preprocessor';
import { readFileSync } from 'fs';
import { runInNewContext } from 'vm';
const bufferSource = readFileSync('file.wasm');
const wasmModuleExportString = wasmModulePreprocessor(
bufferSource,
// Only use this for small wasm modules. The max size of the
// binary will, by default, be restricted to less than 4KiB.
{ sync: true },
);
const wasmModuleExport = runInNewContext((() => ${wasmModuleExportString})());
const {
exports: {
main = () => undefined,
},
} = wasmModuleExport(
// Add an optional importObject:
// {
// global: {},
// env: {},
// }
);
console.log(main() === 3); // true
`
bufferSource |
* options
encoding
maxBufferSourceSize
maxBufferSourceSizeSync
validate
sync
* template
* importObjectArg
* defaultImportObject
* templateArgs
* importObjectArg
* bufferSource
* templateArgs
* buffer
* wasmModuleInstance
* templateArgs
* bufferSource
* importObject
* wasmModuleInstanceSync
* templateArgs
* bufferSource
* importObject
* moduleExport
* templateArgs
* bufferSource
* importObject
bufferSource is the data from the binary wasm file. encoding is ignored ifbufferSource is a buffer.
maxBufferSourceSize is the maximum size in bytes that the bufferSource canmaxBufferSourceSizeSync
be and defaults to the max allowed by Node/V8 and most browsers. is the maximum size in bytes that the bufferSource
can be if used synchronously and defaults the max allowed by Chrome and most
other browsers (Node/V8 has no such limitation).
validate is whether or not WebAssembly.validate is used to check thebufferSource before preprocessing.
sync is whether or not to preprocess the module as synchronous.
template` is an object with properties to construct the string returned.