Sqlite3Wasm
The sqlite3.wasm namespace, abbreviated as wasm for the remainder of this page, holds a number of routines for working with WASM-side constructs. They include APIs for such tasks as...
Memory management.
Allocating and freeing memory.
Helpers for working with WASM heap memory, e.g. getting and setting primitive values from/to the WASM heap.
Configurable result value and argument type conversion for WASM-exported functions.
JS/C String conversions.
Binding JS functions into the WASM runtime, so that they may be called from WASM code (i.e. from C).
In short, if a WASM-specific feature has been needed during the development of the sqlite3 JS API, it's been added to this namespace. For the most part, high-level client code will rarely need to make use of more than a few of these, whereas clients using the C-style APIs may make heavy use of them.
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.
Wasm exports namespace.
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).
Given a function pointer, returns the WASM function table entry if found, else returns a falsy value.
Returns the WASM module's indirect function table.
Requires n to be one of:
Type-safe WasmMemory.heapForSize.
Expects a JS function and signature, exactly as for wasm.jsFuncToWasm(). It uses that function to create a WASM-exported function, installs that function to the next available slot of wasm.functionTable(), and returns the function's index in that table (which acts as a pointer to that function). The returned pointer can be passed to wasm.uninstallFunction() to uninstall it and free up the table slot for reuse.
Installs a JS function.
Creates a WASM function which wraps the given JS function and returns the JS binding of that WASM function. The function signature string must be in the form used by jaccwabyt or Emscripten's addFunction(). In short: in may have one of the following formats:
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().
This works exactly like installFunction() except that the installation is scoped to the current allocation scope and is uninstalled when the current allocation scope is popped. It will throw if no allocation scope is active.
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.
Requires a pointer value previously returned from wasm.installFunction(). Removes that function from the WASM function table, marks its table slot as free for re-use, and returns that function. It is illegal to call this before installFunction() has been called and results are undefined if the argument was not returned by that function. The returned function may be passed back to installFunction() to reinstall it.
Calls a WASM-exported function by name, passing on all supplied arguments (which may optionally be supplied as an array). If throws if the function is not exported or if the argument count does not match. This routine does no type conversion and is essentially equivalent to:
Functions like xCall() but performs argument and result type conversions as for xWrap().
Returns a WASM-exported function by name, or throws if the function is not found.