assemblyscript-loader v0.3.0
AssemblyScript Loader
AssemblyScript's loader component to run and work with compiled WebAssembly modules, as a stand-alone module.
Usage
$> npm install assemblyscript-loaderimport load from "assemblyscript-loader"; // JS: var load = require("assemblyscript-loader").load;
load("path/to/myModule.wasm", {
imports: {
...
}
}).then(module => {
...
// i.e. call module.exports.main()
});Alternatively, when LoadOptions#exports is specified, the respective object is pre-initialized
with the (always present) ready promise that is resolved when loading is complete:
// myModule.js (CommonJS)
require("assemblyscript-loader").load("path/to/myModule.wasm", { exports: module.exports });// otherModule.js (CommonJS)
var myModule = require("./myModule.js");
myModule.ready.then(() => {
...
});API
load(file:
string | Uint8Array | ArrayBuffer, options:LoadOptions):Promise<Module>Loads a WebAssembly module either from a file or a buffer and returns a promise for the loadedModule.LoadOptions Options to set up the environment created by
load.- memory:
WebAssembly.MemoryMemory instance to import, if applicable. - imports:
{ [key: string]: any }Import elements. Usually functions. - exports:
{ [key: string]: any }Object to populate with exports. Creates a new object if omitted.
- memory:
Module Common module interface as returned by
load.- imports:
ImportsImported elements. Usually functions. - exports:
ExportsExported elements. Usually functions. - memory:
MemoryA reference to the underlying memory instance. - log(type:
LogType, message:string):voidAn overridable method receiving console outputs.
- imports:
Imports An object of imported functions.
Exports An object of exported functions (plus the
readypromise).LogType An enum of log types:
Key Value LOG 0 INFO 1 WARN 2 ERROR 3 Memory extends WebAssembly.Memory The WebAssembly Memory instance populated with additional accessors for more convenient memory access.
- sbyte / s8:
NumberAccessorSigned 8-bit integer accessors. - byte / u8:
NumberAccessorUnsigned 8-bit integer accessors. - short / s16:
NumberAccessorSigned 16-bit integer accessors. - ushort / u16:
NumberAccessorUnsigned 16-bit integer accessors. - int / s32:
NumberAccessorSigned 32-bit integer accessors. - uint / u32:
NumberAccessorUnsigned 32-bit integer accessors. - long / s64:
LongAccessorSigned 64-bit integer accessors. - ulong / u64:
LongAccessorUnsigned 64-bit integer accessors. - float / f32:
NumberAccessor32-bit float accessors. - double / f64:
NumberAccessor64-bit float accessors. - array:
ArrayAccessorArray accessors. - string:
StringAccessorString accessors.
- sbyte / s8:
NumberAccessor Number memory accessor.
- get(ptr:
number):numberGets a value of the underlying type from memory at the specified pointer. - set(ptr:
number, value:number):voidSets a value of the underlying type in memory at the specified pointer.
- get(ptr:
LongAccessor Long memory accessor. See also: long.js
- get(ptr:
number):LongGets a Long from memory at the specified pointer. - set(ptr:
number, value:Long):voidSets a Long in memory at the specified pointer.
- get(ptr:
ArrayAccessor Array memory accessor.
- get(ptr:
number):{ length: number, base: number }Gets an array from memory at the specified pointer and returns its length and element base pointer. - create(length:
number, elementByteSize:number):{ ptr: number, base: number }Creates an array in memory and returns its pointer and element base pointer.
- get(ptr:
StringAccessor String memory accessor.
- get(ptr:
number):stringGets a string from memory at the specified pointer. - create(value:
string):numberCreates a string in memory and returns its pointer.
- get(ptr:
initializeMemory(memoryInstance:
WebAssembly.Memory, malloc:Function, memset:Function):MemoryJust populates a WebAssembly Memory instance with the AssemblyScript-typical accessors.xfetch(file:
string):Promise<Response>Underlying fetch implementation that also works under node.js.
Note that the create methods of array and string accessors require an exported or imported
implementation of malloc, memset, free etc. to be present. Also remember that memory is
unmanaged here and that free must be called manually to clean up memory, just like in C. Once
WebAssembly exposes the garbage collector natively, there will be other options as well.
The long.js dependency can be safely excluded if working with
long/ulong values isn't needed. In this case, the implementation will still accept and produce
Long-like objects having a low and a high property representing the respective low and high
32-bits.
Building
Clone the GitHub repository and install the development dependencies:
$> git clone https://github.com/AssemblyScript/loader.git
$> cd loader
$> npm installAfterwards, to build the distribution files to dist/, run:
$> npm run build