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.
Inheritors
Properties
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.
WasmPStack instance.
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
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.
Allocates a C-style string and returns the pointer to it.
Allocates a C-style string and returns a CString object.
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.
Allocates a pointer and set is to 0.
Allocates howMany pointers as a single chunk of memory and zeroes them out.
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.
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.
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.
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).
Requires n to be one of:
Type-safe WasmMemory.heapForSize.
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.
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.
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.
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.
Type-safe WasmMemory.peek.
Equivalent to peek(X,'i16').
Equivalent to peek(X,'i32').
Equivalent to peek(X,'f32').
Equivalent to peek(X,'i64').
Equivalent to peek(X,'f64').
Equivalent to peek(X,'i8').
Equivalent to peek(X,'*'). Most frequently used for fetching output pointer values.
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.
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.
Equivalent to poke(X, Y,'i16').
Equivalent to poke(X, Y,'i32').
Equivalent to poke(X, Y,'f32').
Equivalent to poke(X, Y,'i64').
Equivalent to poke(X, Y,'f64').
Equivalent to poke(X, Y,'i8').
Equivalent to poke(X, Y,'*'). Most frequently used for fetching output pointer values.
Works just like alloc(n) but stores the result of the allocation in the current scope.
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:
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.
Allocates a C-style string and returns the pointer to it. Must be called in a scoped allocation scope.
Allocates a C-style string and returns a CString object. Must be called in a scoped allocation scope.
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.
Works just like allocPtr() but stores the result of the allocation in the current scope.
Allocates a pointer and set is to 0. Must be called in a scoped allocation scope.
Allocates howMany pointers as a single chunk of memory and zeroes them out. Must be called in a scoped allocation scope.
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().
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.
Returns either aTypedArray.slice(begin,end) (if aTypedArray.buffer is a SharedArrayBuffer) or aTypedArray.subarray(begin,end) (if it's not).
Uses TextDecoder to decode the given half-open range of the given TypedArray to a string.