Package-level declarations

Types

Link copied to clipboard
data class CString(val pointer: WasmPointer, val byteLength: Int)

C-style string pointer and its associated size.

Link copied to clipboard
value class FunctionSignature(signature: String)

Type-safe function signature types in Emscripten format.

Link copied to clipboard
sealed interface IR

Data type descriptors, sometimes referred to as "IR" (internal representation). These are short strings which identify the specific data types supported by WASM and/or the JS/WASM glue code.

Link copied to clipboard
typealias JsFunction = JsAny
Link copied to clipboard
interface WasmAlloc : JsAny

Wasm alloc function.

Link copied to clipboard
typealias WasmFunction = JsAny
Link copied to clipboard
interface WasmFunctions

A WASM module exposes all exported functions to the user, but they are in "raw" form. That is, they perform no argument or result type conversion and only support data types supported by WASM (i.e. only numeric types). That's fine for functions which only accept and return numbers, but is generally less helpful for functions which take or return strings or have output pointers. For usability reasons, it's desirable to reduce the JS/C friction by automatically performing mundane tasks such as the allocation and deallocation of memory needed for converting strings between JS and WASM.

Link copied to clipboard
interface WasmMemory

Just like in C, WASM offers a memory "heap," and transfering values between JS and WASM often requires manipulation of that memory, including low-level allocation and deallocation of it. The following subsections describe the various memory management APIs.

Link copied to clipboard
typealias WasmPointer = JsBigInt

Wasm pointer type.

Link copied to clipboard
interface WasmPStack

The "pstack" (pseudo-stack) API is a special-purpose allocator intended solely for use with allocating small amounts of memory such as that needed for output pointers. It is more efficient than the scoped allocation API, and covers many of the use cases for that API, but it has a tiny static memory limit (with an unspecified total size no less than 2kb).

Link copied to clipboard
interface WasmPtr

The wasm.ptr API was added to assist in smoothing over the differences between 32- and 64-bit JS/WASM environments.

Link copied to clipboard
interface WasmRealloc : JsAny

Wasm realloc function.

Properties

Link copied to clipboard

Returns the size in bytes without the null character.

Functions

Link copied to clipboard

Attempts to allocate the given number of bytes from the pstack.

Link copied to clipboard
fun WasmPStack.allocChunks(n: Int, sz: IR): ReadonlyArray<WasmPointer>

alloc()'s n chunks, each sz bytes, as a single memory block and returns the addresses as an array of n element, each holding the address of one chunk.

Link copied to clipboard

Allocates a C-style string and returns the pointer to it.

Link copied to clipboard

Allocates a C-style string and returns a CString object.

Link copied to clipboard

Allocates a pointer and set is to 0.

fun WasmMemory.allocPtr(howMany: UInt): ReadonlyArray<WasmPointer>
fun WasmPStack.allocPtr(howMany: UInt): ReadonlyArray<WasmPointer>

Allocates howMany pointers as a single chunk of memory and zeroes them out.

Link copied to clipboard
fun WasmMemory.heapForSize(n: IR.Integer, unsigned: Boolean): TypedArray<*, *, *, *>
fun WasmMemory.heapForSize(n: IR.Number, unsigned: Boolean): TypedArray<*, *, *, JsNumber>
Link copied to clipboard

Installs a JS function.

Link copied to clipboard
fun WasmMemory.peek(addresses: ReadonlyArray<WasmPointer>, representation: IR): ReadonlyArray<JsAny>
fun WasmMemory.peek(address: WasmPointer, representation: IR): JsAny

Type-safe WasmMemory.peek.

Link copied to clipboard
fun WasmMemory.poke(addresses: ReadonlyArray<WasmPointer>, value: JsBigInt): WasmMemory
fun WasmMemory.poke(addresses: ReadonlyArray<WasmPointer>, value: JsNumber, representation: IR.Number): WasmMemory

Fetches the heapForSize() for the given representation then writes the given numeric value to it. Only numbers may be written this way, and passing a non-number might trigger an exception. If passed an array of pointers, it writes the given value to all of them.

Link copied to clipboard

Allocates a C-style string and returns the pointer to it. Must be called in a scoped allocation scope.

Link copied to clipboard

Allocates a C-style string and returns a CString object. Must be called in a scoped allocation scope.

Link copied to clipboard

Allocates a pointer and set is to 0. Must be called in a scoped allocation scope.

fun WasmMemory.scopedAllocPtr(howMany: UInt): ReadonlyArray<WasmPointer>

Allocates howMany pointers as a single chunk of memory and zeroes them out. Must be called in a scoped allocation scope.

Link copied to clipboard

Return the size of ir value.