WasmMemory

interface WasmMemory(source)

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.

MemoryManagement

Inheritors

Properties

Link copied to clipboard
abstract val alloc: WasmAlloc

Allocates n bytes of memory from the WASM heap and returns the address of the first byte in the block. alloc() throws a WasmAllocError if allocation fails. If non-thowing allocation is required, use alloc.impl(n), which returns a WASM NULL pointer (the integer 0) if allocation fails.

Link copied to clipboard
abstract val pstack: WasmPStack

WasmPStack instance.

Link copied to clipboard
abstract val ptr: WasmPtr

WasmPtr instance.

Link copied to clipboard
abstract val realloc: WasmRealloc

Semantically equivalent to realloc(3) or sqlite3_realloc(), this routine reallocates memory allocated via this routine or alloc(). Its first argument is either 0 or a pointer returned by this routine or alloc(). Its second argument is the number of bytes to (re)allocate, or 0 to free the memory specified in the first argument. On allocation error, realloc() throws a WasmAllocError, whereas realloc.impl() will return 0 on allocation error.

Functions

Link copied to clipboard
abstract fun allocCString(jsString: JsString, returnWithLength: Boolean): JsAny

Uses alloc() to allocate enough memory for the byte-length of the given JS string, plus 1 (for a NUL terminator), copies the given JS string to that memory using jstrcpy(), NUL-terminates it, and returns the pointer to that C-string. Ownership of the pointer is transfered to the caller, who must eventually pass the pointer to dealloc() to free it.

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
abstract fun allocFromTypedArray(srcTypedArray: Int8Array<*>): WasmPointer

wasm.alloc()'s srcTypedArray.byteLength bytes, populates them with the values from the source TypedArray, and returns the pointer to that memory. The returned pointer must eventually be passed to wasm.dealloc() to clean it up.

Link copied to clipboard
abstract fun allocPtr(howMany: Int, safePtrSize: Boolean): JsAny

Allocates one or more pointers as a single chunk of memory and zeroes them out.

Link copied to clipboard

Allocates a pointer and set is to 0.

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

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

Link copied to clipboard
abstract fun cstrlen(ptr: WasmPointer): Int

Expects its argument to be a pointer into the WASM heap memory which refers to a NUL-terminated C-style string encoded as UTF-8.

Link copied to clipboard
abstract fun cstrncpy(tgtPtr: WasmPointer, srcPtr: WasmPointer, n: Int): Int

Works similarly to C's strncpy(3), copying, at most, n bytes (not characters) from srcPtr to tgtPtr. It copies until n bytes have been copied or a 0 byte is reached in src. Unlike strncpy(), it returns the number of bytes it assigns in tgtPtr, including the NUL byte (if any). If n is reached before a NUL byte in srcPtr, tgtPtr will not be NUL-terminated. If a NUL byte is reached before n bytes are copied, tgtPtr will be NUL-terminated.

Link copied to clipboard
abstract fun cstrToJs(ptr: WasmPointer): JsString?

Expects its argument to be a pointer into the WASM heap memory which refers to a NUL-terminated C-style string encoded as UTF-8.

Link copied to clipboard
abstract fun dealloc(pointer: WasmPointer)

Frees memory returned by alloc(). Results are undefined if it is passed any value other than a value returned by alloc() or null/undefined/0 (all of which are no-ops).

Link copied to clipboard
abstract fun heap16(): Int16Array<*>

Equivalent of heapForSize(16, false) -> Int16Array.

Link copied to clipboard
abstract fun heap16u(): Uint16Array<*>

Equivalent of heapForSize(16, true) -> Uint16Array.

Link copied to clipboard
abstract fun heap32(): Int32Array<*>

Equivalent of heapForSize(32, false) -> Int32Array.

Link copied to clipboard
abstract fun heap32u(): Uint32Array<*>

Equivalent of heapForSize(32, true) -> Uint32Array.

Link copied to clipboard
abstract fun heap8(): Int8Array<*>

Equivalent of heapForSize(8, false) -> Int8Array.

Link copied to clipboard
abstract fun heap8u(): Uint8Array<*>

Equivalent of heapForSize(8, true) -> Uint8Array.

Link copied to clipboard
abstract fun heapForSize(n: Int, unsigned: Boolean): TypedArray<*, *, *, *>

Requires n to be one of:

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
abstract fun jstrcpy(jsString: JsString, tgt: Uint8Array<*>, offset: Int = definedExternally, maxBytes: Int = definedExternally, addNul: Boolean = definedExternally): Int

Encodes the given JS string as UTF-8 into the given TypedArray tgt (which must be a Int8Array or Uint8Array), starting at the given offset and writing, at most, maxBytes bytes (including the NUL terminator if addNul is true, else no NUL is added). If it writes any bytes at all and addNul is true, it always NUL-terminates the output, even if doing so means that the NUL byte is all that it writes.

Link copied to clipboard
abstract fun jstrlen(jsString: JsString): Int

Given a JS string, this function returns its UTF-8 length in bytes. Returns null if its argument is not a string. This is a relatively expensive calculation and should be avoided when not necessary.

Link copied to clipboard
abstract fun jstrToUintArray(jsString: JsString, addNul: Boolean = definedExternally): Uint8Array<*>

For the given JS string, returns a Uint8Array of its contents encoded as UTF-8. If addNul is true, the returned array will have a trailing 0 entry, else it will not.

Link copied to clipboard
abstract fun peek(addresses: ReadonlyArray<WasmPointer>, representation: JsString): ReadonlyArray<JsAny>

The second form fetches the value from each pointer in the given array and returns the array of values. The heap view used for reading the memory is specified by the second argument, defaulting to byte-oriented view.

abstract fun peek(address: WasmPointer, representation: JsString): JsAny

Fetches a single value from memory. The heap view used for reading the memory is specified by the second argument, defaulting to byte-oriented view.

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
abstract fun peek16(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsNumber>
abstract fun peek16(address: WasmPointer): JsNumber

Equivalent to peek(X,'i16').

Link copied to clipboard
abstract fun peek32(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsNumber>
abstract fun peek32(address: WasmPointer): JsNumber

Equivalent to peek(X,'i32').

Link copied to clipboard
abstract fun peek32f(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsNumber>
abstract fun peek32f(address: WasmPointer): JsNumber

Equivalent to peek(X,'f32').

Link copied to clipboard
abstract fun peek64(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsBigInt>
abstract fun peek64(address: WasmPointer): JsBigInt

Equivalent to peek(X,'i64').

Link copied to clipboard
abstract fun peek64f(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsNumber>
abstract fun peek64f(address: WasmPointer): JsNumber

Equivalent to peek(X,'f64').

Link copied to clipboard
abstract fun peek8(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<JsNumber>
abstract fun peek8(address: WasmPointer): JsNumber

Equivalent to peek(X,'i8').

Link copied to clipboard
abstract fun peekPtr(addresses: ReadonlyArray<WasmPointer>): ReadonlyArray<WasmPointer>
abstract fun peekPtr(address: WasmPointer): WasmPointer

Equivalent to peek(X,'*'). Most frequently used for fetching output pointer values.

Link copied to clipboard
abstract fun poke(addresses: ReadonlyArray<WasmPointer>, value: JsAny, representation: JsString): WasmMemory
abstract fun poke(address: WasmPointer, value: JsAny, representation: JsString): 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
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
abstract fun poke16(addresses: ReadonlyArray<WasmPointer>, value: Short): WasmMemory
abstract fun poke16(address: WasmPointer, value: Short): WasmMemory

Equivalent to poke(X, Y,'i16').

Link copied to clipboard
abstract fun poke32(addresses: ReadonlyArray<WasmPointer>, value: Int): WasmMemory
abstract fun poke32(address: WasmPointer, value: Int): WasmMemory

Equivalent to poke(X, Y,'i32').

Link copied to clipboard
abstract fun poke32f(addresses: ReadonlyArray<WasmPointer>, value: Float): WasmMemory
abstract fun poke32f(address: WasmPointer, value: Float): WasmMemory

Equivalent to poke(X, Y,'f32').

Link copied to clipboard
abstract fun poke64(addresses: ReadonlyArray<WasmPointer>, value: JsBigInt): WasmMemory

Equivalent to poke(X, Y,'i64').

Link copied to clipboard
abstract fun poke64f(addresses: ReadonlyArray<WasmPointer>, value: Double): WasmMemory
abstract fun poke64f(address: WasmPointer, value: Double): WasmMemory

Equivalent to poke(X, Y,'f64').

Link copied to clipboard
abstract fun poke8(addresses: ReadonlyArray<WasmPointer>, value: Byte): WasmMemory
abstract fun poke8(address: WasmPointer, value: Byte): WasmMemory

Equivalent to poke(X, Y,'i8').

Link copied to clipboard
abstract fun pokePtr(addresses: ReadonlyArray<WasmPointer>, value: WasmPointer = definedExternally): WasmMemory
abstract fun pokePtr(address: WasmPointer, value: WasmPointer = definedExternally): WasmMemory

Equivalent to poke(X, Y,'*'). Most frequently used for fetching output pointer values.

Link copied to clipboard
abstract fun scopedAlloc(n: Int): WasmPointer

Works just like alloc(n) but stores the result of the allocation in the current scope.

Link copied to clipboard
abstract fun <R : JsAny> scopedAllocCall(callback: () -> R): R

Calls scopedAllocPush(), calls the given callback, and then calls scopedAllocPop(), propagating any exception from the callback or returning its result. This is essentially a convenience form of:

Link copied to clipboard
abstract fun scopedAllocCString(jsString: JsString, returnWithLength: Boolean): JsAny

Uses alloc() to allocate enough memory for the byte-length of the given JS string, plus 1 (for a NUL terminator), copies the given JS string to that memory using jstrcpy(), NUL-terminates it, and returns the pointer to that C-string. Ownership of the pointer is transfered to the caller, who must eventually pass the pointer to dealloc() to free it.

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
abstract fun scopedAllocPop(scope: JsAny)

Given a value returned from scopedAllocPush(), this "pops" that allocation scope and frees all memory allocated in that scope by the scopedAllocXyz() family of APIs.

Link copied to clipboard
abstract fun scopedAllocPtr(howMany: Int, safePtrSize: Boolean): JsAny

Works just like allocPtr() but stores the result of the allocation in the current 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
abstract fun scopedAllocPush(): JsAny

Opens a new "scope" for allocations. All allocations made via the scopedAllocXyz() APIs will store their results into the current (most recently pushed) allocation scope for later cleanup. The returned value must be retained for passing to scopedAllocPop().

Link copied to clipboard
abstract fun sizeofIR(ir: JsString): Int

For the given IR-like string in the set ('i8', 'i16', 'i32', 'f32', 'float', 'i64', 'f64', 'double', ''), or any string value ending in '', returns the sizeof for that value (wasm.ptrSizeof in the latter case). For any other value, it returns the undefined value.

Link copied to clipboard

Return the size of ir value.

Link copied to clipboard
abstract fun <Array : TypedArray<*, *, *, *>> typedArrayPart(aTypedArray: Array, begin: Int, end: Int): Array
abstract fun <Array : TypedArray<*, *, *, *>> typedArrayPart(aTypedArray: Array, begin: JsBigInt, end: JsBigInt): Array

Returns either aTypedArray.slice(begin,end) (if aTypedArray.buffer is a SharedArrayBuffer) or aTypedArray.subarray(begin,end) (if it's not).

Link copied to clipboard
abstract fun typedArrayToString(typedArray: TypedArray<*, *, *, *>, begin: Int, end: Int): JsString
abstract fun typedArrayToString(typedArray: TypedArray<*, *, *, *>, begin: JsBigInt, end: JsBigInt): JsString

Uses TextDecoder to decode the given half-open range of the given TypedArray to a string.