Skip to content

Commit

Permalink
feat(wasm-api): update IWasmMemoryAccess & impls
Browse files Browse the repository at this point in the history
- add MemorySlice tuple to describe a memory region
- update allocate() & free() to use MemorySlice, migrate to IWasmMemoryAccess
- migrate growMemory() to IWasmMemoryAccess
- add MemorySlice to default imports in TS codegen
  • Loading branch information
postspectacular committed Oct 27, 2022
1 parent 68694ba commit bb8a3ca
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 29 deletions.
40 changes: 40 additions & 0 deletions packages/wasm-api/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export interface WasmExports {
_wasm_free(addr: number, numBytes: number): void;
}

export type MemorySlice = [addr: number, len: number];

export interface IWasmMemoryAccess {
i8: Int8Array;
u8: Uint8Array;
Expand All @@ -113,6 +115,44 @@ export interface IWasmMemoryAccess {
*/
ensureMemory(): void;

/**
* Attempts to grow the WASM memory by an additional `numPages` (64KB/page)
* and if successful updates all typed memory views to use the new
* underlying buffer.
*
* @param numPages
*/
growMemory(numPages: number): void;

/**
* Attempts to allocate `numBytes` using the exported WASM core API function
* {@link WasmExports._wasm_allocate} (implementation specific) and returns
* start address of the new memory block. If unsuccessful, throws an
* {@link OutOfMemoryError}. If `clear` is true, the allocated region will
* be zero-filled.
*
* @remarks
* See {@link WasmExports._wasm_allocate} docs for further details.
*
* @param numBytes
* @param clear
*/
allocate(numBytes: number, clear?: boolean): MemorySlice;

/**
* Frees a previous allocated memory region using the exported WASM core API
* function {@link WasmExports._wasm_free} (implementation specific). The
* `numBytes` value must be the same as previously given to
* {@link IWasmMemoryAccess.allocate}.
*
* @remarks
* This function always succeeds, regardless of presence of an active
* allocator on the WASM side or validity of given arguments.
*
* @param slice
*/
free(slice: MemorySlice): void;

/**
* Reads UTF-8 encoded string from given address and optional byte length.
* The default length is 0, which will be interpreted as a zero-terminated
Expand Down
31 changes: 3 additions & 28 deletions packages/wasm-api/src/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
EVENT_MEMORY_CHANGED,
IWasmAPI,
IWasmMemoryAccess,
MemorySlice,
WasmExports,
} from "./api.js";

Expand Down Expand Up @@ -252,19 +253,6 @@ export class WasmBridge<T extends WasmExports = WasmExports>
this.ensureMemory();
}

/**
* Attempts to allocate `numBytes` using the exported WASM core API function
* {@link WasmExports._wasm_allocate} (implementation specific) and returns
* start address of the new memory block. If unsuccessful, throws an
* {@link OutOfMemoryError}. If `clear` is true, the allocated region will
* be zero-filled.
*
* @remarks
* See {@link WasmExports._wasm_allocate} docs for further details.
*
* @param numBytes
* @param clear
*/
allocate(numBytes: number, clear = false) {
const addr = this.exports._wasm_allocate(numBytes);
if (!addr)
Expand All @@ -277,23 +265,10 @@ export class WasmBridge<T extends WasmExports = WasmExports>
);
this.ensureMemory();
clear && this.u8.fill(0, addr, addr + numBytes);
return addr;
return <MemorySlice>[addr, numBytes];
}

/**
* Frees a previous allocated memory region using the exported WASM core API
* function {@link WasmExports._wasm_free} (implementation specific). The
* `numBytes` value must be the same as previously given to
* {@link WasmBridge.allocate}.
*
* @remarks
* This function always succeeds, regardless of presence of an active
* allocator on the WASM side or validity of given arguments.
*
* @param addr
* @param numBytes
*/
free(addr: number, numBytes: number) {
free([addr, numBytes]: MemorySlice) {
this.logger.fine(
() =>
`freeing memory @ 0x${U32(addr)} .. 0x${U32(
Expand Down
2 changes: 1 addition & 1 deletion packages/wasm-api/src/codegen/typescript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ export const TYPESCRIPT = (opts: Partial<TSOpts> = {}) => {

const gen: ICodeGen = {
pre: (opts) => `// @ts-ignore possibly includes unused imports
import { Pointer, ${__stringImpl(
import { MemorySlice, Pointer, ${__stringImpl(
opts
)}, WasmTypeBase, WasmTypeConstructor } from "${PKG_NAME}";${
opts.pre ? `\n${opts.pre}` : ""
Expand Down

0 comments on commit bb8a3ca

Please sign in to comment.