Package-level declarations
Types
C-style string pointer and its associated size.
Type-safe function signature types in Emscripten format.
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.
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.
Wasm pointer type.
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).
Wasm realloc function.
Functions
Attempts to allocate the given number of bytes from the pstack.
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.
Allocates a C-style string and returns the pointer to it.
Allocates a C-style string and returns a CString object.
Allocates a pointer and set is to 0.
Allocates howMany pointers as a single chunk of memory and zeroes them out.
Type-safe WasmMemory.heapForSize.
Installs a JS function.
Type-safe WasmMemory.peek.
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.
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.
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.