TypeScript / JavaScript
WASM integration
Optional Emscripten module contract for a custom JavaScript WASM factory.This page documents the optional Emscripten module contract used when supplying a custom wasmFactory to Engine.create(). Normal applications use Engine.create() and do not call these module methods. See EngineOptions for the public creation options. Examples that omit setup use the page reference context, which supplies module from the documented release factory and the binding imports.
| Used by | EngineOptions.wasmFactory |
|---|
Interface: WasmModule
TypeScript · interfaceSince v0.10.0
WasmModuleThe factory module contract used by the WASM transport. Application code normally lets Engine.create load it.
Fields
HEAPU8Uint8Arrayfield
Current byte view of WASM memory; reacquire it after memory growth.
Methods
- WasmModule._free()Release a buffer previously reserved in this module.
- WasmModule._malloc()Reserve a buffer in the module linear memory.
- WasmModule.ccall()Invoke a named C export through the Emscripten module.
console.log('byte view:', module.HEAPU8 instanceof Uint8Array)byte view: true
Methods
WasmModule._free
TypeScript · methodSince v0.10.0
WasmModule._freeWasmModule._free(
pointer: number
): voidRelease a buffer previously reserved in this module.
Arguments
pointernumber
Pointer returned by this module allocation.
Return value
voidReturns no value.
const pointer = module._malloc(4)
if (pointer === 0) throw new Error('Allocation failed')
const returned = module._free(pointer)
console.log('return:', returned)return: undefined
WasmModule._malloc
TypeScript · methodSince v0.10.0
WasmModule._mallocWasmModule._malloc(
bytes: number
): numberReserve a buffer in the module linear memory.
Arguments
bytesnumber
Number of bytes to reserve in linear memory.
Return value
numberReturns a module pointer, or zero when allocation fails.
const pointer = module._malloc(4)
if (pointer === 0) throw new Error('Allocation failed')
try {
module.HEAPU8.set([65, 66, 67, 0], pointer)
console.log('bytes:', Array.from(module.HEAPU8.subarray(pointer, pointer + 4)))
} finally { module._free(pointer) }bytes: [ 65, 66, 67, 0 ]
WasmModule.ccall
TypeScript · methodSince v0.10.0
WasmModule.ccallWasmModule.ccall(
name: string,
result: string | null,
types: string[],
args: number[]
): number | stringInvoke a named C export through the Emscripten module.
Arguments
namestring
Name of the C export to invoke.
resultstring | null
Emscripten return type, or null for no return value.
typesstring[]
Ordered Emscripten argument type names.
argsnumber[]
Ordered arguments passed to the export.
Return value
number | stringReturns the value of the named export as a number or string. Use null as the return type for an export that returns no value.
const pointer = module.ccall('malloc', 'number', ['number'], [4])
if (typeof pointer !== 'number' || pointer === 0) throw new Error('Allocation failed')
try {
module.HEAPU8.set([65, 66, 67, 0], pointer)
console.log('bytes:', Array.from(module.HEAPU8.subarray(pointer, pointer + 4)))
} finally { module.ccall('free', null, ['number'], [pointer]) }bytes: [ 65, 66, 67, 0 ]