|
| 1 | +/** |
| 2 | +* @license Apache-2.0 |
| 3 | +* |
| 4 | +* Copyright (c) 2025 The Stdlib Authors. |
| 5 | +* |
| 6 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +* you may not use this file except in compliance with the License. |
| 8 | +* You may obtain a copy of the License at |
| 9 | +* |
| 10 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +* |
| 12 | +* Unless required by applicable law or agreed to in writing, software |
| 13 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +* See the License for the specific language governing permissions and |
| 16 | +* limitations under the License. |
| 17 | +*/ |
| 18 | + |
| 19 | +// TypeScript Version: 4.1 |
| 20 | + |
| 21 | +/// <reference types="@stdlib/types"/> |
| 22 | + |
| 23 | +import { Collection, AccessorArrayLike } from '@stdlib/types/array'; |
| 24 | + |
| 25 | +/** |
| 26 | +* WebAssembly memory interface. |
| 27 | +*/ |
| 28 | +export interface Memory { |
| 29 | + /** |
| 30 | + * Underlying `ArrayBuffer` (or `SharedArrayBuffer`) associated with a memory instance. |
| 31 | + */ |
| 32 | + buffer: ArrayBuffer; |
| 33 | + |
| 34 | + /** |
| 35 | + * Increases the size of the memory instance by a specified number of WebAssembly pages (i.e., 64KiB). |
| 36 | + * |
| 37 | + * ## Notes |
| 38 | + * |
| 39 | + * - Upon increasing the size, the previous `ArrayBuffer` is detached, thus invalidating any typed arrays which were views over the previous `ArrayBuffer`. |
| 40 | + * - Detachment means that the previous `ArrayBuffer` byte length becomes zero, and it no longer has bytes accessible to JavaScript. |
| 41 | + * - `ArrayBuffer` detachment applies even when `delta` is zero. |
| 42 | + * - Detachment only applies for non-shared memory instances. For a shared memory instance, the initial buffer (which is a `SharedArrayBuffer`) will not become detached and, instead, its length will not be updated. |
| 43 | + * - Accesses to the `buffer` property after growing a `SharedArrayBuffer` will yield a larger `SharedArrayBuffer` which may access a larger span of memory than the buffer before growing memory. |
| 44 | + * - Every `SharedArrayBuffer` accessed via the `buffer` property will always refer to the start of the same memory address range and thus manipulate the same data. |
| 45 | + * |
| 46 | + * @param delta - number of WebAssembly pages (i.e., 64KiB) by which to grow the underlying memory |
| 47 | + * @returns size of the previous `ArrayBuffer` (or `SharedArrayBuffer`) |
| 48 | + */ |
| 49 | + grow( delta: number ): number; |
| 50 | +} |
| 51 | + |
| 52 | +/** |
| 53 | +* WebAssembly module wrapper interface. |
| 54 | +*/ |
| 55 | +export interface ModuleWrapper { |
| 56 | + /** |
| 57 | + * WebAssembly binary code. |
| 58 | + */ |
| 59 | + binary: Uint8Array; |
| 60 | + |
| 61 | + /** |
| 62 | + * WebAssembly memory. |
| 63 | + */ |
| 64 | + memory: Memory | null; |
| 65 | + |
| 66 | + /** |
| 67 | + * WebAssembly memory buffer as a Uint8Array. |
| 68 | + */ |
| 69 | + buffer: Uint8Array | null; |
| 70 | + |
| 71 | + /** |
| 72 | + * WebAssembly memory buffer as a DataView. |
| 73 | + */ |
| 74 | + view: DataView | null; |
| 75 | + |
| 76 | + /** |
| 77 | + * "Raw" WebAssembly module exports. |
| 78 | + */ |
| 79 | + exports: Object | null; |
| 80 | + |
| 81 | + /** |
| 82 | + * Asynchronously initializes a WebAssembly module instance. |
| 83 | + * |
| 84 | + * @returns promise which resolves upon initializing a WebAssembly module instance |
| 85 | + */ |
| 86 | + initialize(): Promise<ModuleWrapper>; |
| 87 | + |
| 88 | + /** |
| 89 | + * Asynchronously initializes a WebAssembly module instance. |
| 90 | + * |
| 91 | + * @param clbk - callback to invoke upon initializing a WebAssembly module |
| 92 | + */ |
| 93 | + initializeAsync( clbk: ( error: Error | null, mod?: ModuleWrapper ) => void ): void; |
| 94 | + |
| 95 | + /** |
| 96 | + * Synchronously initializes a WebAssembly module instance. |
| 97 | + * |
| 98 | + * @returns module wrapper instance |
| 99 | + */ |
| 100 | + initializeSync(): ModuleWrapper; |
| 101 | + |
| 102 | + /** |
| 103 | + * Reallocates the underlying WebAssembly memory instance to a specified number of bytes. |
| 104 | + * |
| 105 | + * ## Notes |
| 106 | + * |
| 107 | + * - WebAssembly memory can only **grow**, not shrink. Hence, if provided a number of bytes which is less than or equal to the size of the current memory, the function does nothing. |
| 108 | + * - When non-shared memory is resized, the underlying the `ArrayBuffer` is detached, consequently invalidating any associated typed array views. Before resizing non-shared memory, ensure that associated typed array views no longer need byte access and can be garbage collected. |
| 109 | + * |
| 110 | + * @param nbytes - memory size (in bytes) |
| 111 | + * @returns boolean indicating whether the resize operation was successful |
| 112 | + */ |
| 113 | + realloc( nbytes: number ): boolean; |
| 114 | + |
| 115 | + /** |
| 116 | + * Returns a boolean indicating whether the underlying WebAssembly memory instance has the capacity to store a provided list of values starting from a specified byte offset. |
| 117 | + * |
| 118 | + * @param byteOffset - byte offset at which to start writing values |
| 119 | + * @param values - input array containing values to write |
| 120 | + * @returns boolean indicating whether the underlying WebAssembly memory instance has enough capacity |
| 121 | + */ |
| 122 | + hasCapacity( byteOffset: number, values: Collection | AccessorArrayLike<any> ): boolean; |
| 123 | + |
| 124 | + /** |
| 125 | + * Returns a boolean indicating whether a provided list of values is a view of the underlying memory of the WebAssembly module. |
| 126 | + * |
| 127 | + * @param values - input array |
| 128 | + * @returns boolean indicating whether the list is a memory view |
| 129 | + */ |
| 130 | + isView( values: Collection | AccessorArrayLike<any> ): boolean; |
| 131 | + |
| 132 | + /** |
| 133 | + * Writes values to the underlying WebAssembly memory instance. |
| 134 | + * |
| 135 | + * ## Notes |
| 136 | + * |
| 137 | + * - The function infers element size (i.e., number of bytes per element) from the data type of the input array. For example, if provided a `Float32Array`, the function writes each element as a single-precision floating-point number to the underlying WebAssembly memory instance. |
| 138 | + * - In order to write elements as a different data type, you need to perform an explicit cast **before** calling this method. For example, in order to write single-precision floating-point numbers contained in a `Float32Array` as signed 32-bit integers, you must first convert the `Float32Array` to an `Int32Array` before passing the values to this method. |
| 139 | + * - If provided an array having an unknown or "generic" data type, elements are written as double-precision floating-point numbers. |
| 140 | + * |
| 141 | + * @param byteOffset - byte offset at which to start writing values |
| 142 | + * @param values - input array containing values to write |
| 143 | + * @returns module wrapper instance |
| 144 | + */ |
| 145 | + write( byteOffset: number, values: Collection | AccessorArrayLike<any> ): ModuleWrapper; |
| 146 | + |
| 147 | + /** |
| 148 | + * Reads values from the underlying WebAssembly memory instance. |
| 149 | + * |
| 150 | + * ## Notes |
| 151 | + * |
| 152 | + * - The function infers element size (i.e., number of bytes per element) from the data type of the output array. For example, if provided a `Float32Array`, the function reads each element as a single-precision floating-point number from the underlying WebAssembly memory instance. |
| 153 | + * - In order to read elements as a different data type, you need to perform an explicit cast **after** calling this method. For example, in order to read single-precision floating-point numbers contained in a `Float32Array` as signed 32-bit integers, you must convert the `Float32Array` to an `Int32Array` after reading memory values using this method. |
| 154 | + * - If provided an output array having an unknown or "generic" data type, elements are read as double-precision floating-point numbers. |
| 155 | + * |
| 156 | + * @param byteOffset - byte offset at which to start reading values |
| 157 | + * @param out - output array |
| 158 | + * @returns module wrapper instance |
| 159 | + */ |
| 160 | + read( byteOffset: number, out: Collection | AccessorArrayLike<any> ): ModuleWrapper; |
| 161 | +} |
0 commit comments