@thi.ng/wasm-api 0.7.0 → 0.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -1,6 +1,6 @@
1
1
  # Change Log
2
2
 
3
- - **Last updated**: 2022-08-15T23:41:37Z
3
+ - **Last updated**: 2022-08-24T09:52:32Z
4
4
  - **Generator**: [thi.ng/monopub](https://thi.ng/monopub)
5
5
 
6
6
  All notable changes to this project will be documented in this file.
@@ -9,6 +9,40 @@ See [Conventional Commits](https://conventionalcommits.org/) for commit guidelin
9
9
  **Note:** Unlisted _patch_ versions only involve non-code or otherwise excluded changes
10
10
  and/or version bumps of transitive dependencies.
11
11
 
12
+ ## [0.10.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.10.0) (2022-08-24)
13
+
14
+ #### 🚀 Features
15
+
16
+ - add events, update allocator handling ([89416b1](https://github.com/thi-ng/umbrella/commit/89416b1))
17
+ - add INotify impl for WasmBridge
18
+ - emit event when WASM memory has changed (e.g. to recreate user views)
19
+ - reverse logic so that NO allocator is used by default and instead must
20
+ be explicitly enabled (rather than disabled)
21
+ - update Zig & C bindings
22
+ - add/update docstrings
23
+
24
+ ## [0.9.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.9.0) (2022-08-23)
25
+
26
+ #### 🚀 Features
27
+
28
+ - add debug() core API function ([ca01978](https://github.com/thi-ng/umbrella/commit/ca01978))
29
+
30
+ #### 🩹 Bug fixes
31
+
32
+ - update setString() return value ([116dd0b](https://github.com/thi-ng/umbrella/commit/116dd0b))
33
+ - always only return num bytes written w/o sentinel
34
+
35
+ ## [0.8.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.8.0) (2022-08-16)
36
+
37
+ #### 🚀 Features
38
+
39
+ - add preliminary string handling support ([3da4efe](https://github.com/thi-ng/umbrella/commit/3da4efe))
40
+ - update/rename IWasmMemoryAccess (add string getter/setter)
41
+ - update StructField.type (add `string`)
42
+ - add CodeGenOpts.stringType option
43
+ - update codegen fns
44
+ - update TS & Zig codegen impls
45
+
12
46
  ## [0.7.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.7.0) (2022-08-15)
13
47
 
14
48
  #### 🚀 Features
package/README.md CHANGED
@@ -11,11 +11,13 @@ This project is part of the
11
11
 
12
12
  - [About](#about)
13
13
  - [Custom API modules](#custom-api-modules)
14
- - [Object indices & handles](#object-indices--handles)
15
14
  - [Data bindings & code generators](#data-bindings--code-generators)
16
15
  - [CLI generator](#cli-generator)
17
16
  - [Data type definitions](#data-type-definitions)
18
17
  - [Example usage](#example-usage)
18
+ - [String handling](#string-handling)
19
+ - [Memory allocations](#memory-allocations)
20
+ - [Object indices & handles](#object-indices--handles)
19
21
  - [Status](#status)
20
22
  - [Installation](#installation)
21
23
  - [Dependencies](#dependencies)
@@ -36,9 +38,10 @@ This package provides a the following:
36
38
  [`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
37
39
  class as interop basis and much reduced boilerplate for hybrid JS/WebAssembly
38
40
  applications.
39
- 2. A minimal core API for debug output, string, pointer, typed array accessors
40
- for 8/16/32/64 bit (u)ints and 32/64 bit floats. In the future we aim to also
41
- supply support modules for DOM manipulation, WebGL, WebGPU, WebAudio etc.
41
+ 2. A minimal core API for memory allocation (can be disabled), debug output,
42
+ string/pointer/typedarray accessors for 8/16/32/64 bit (u)ints and 32/64 bit
43
+ floats. In the future we aim to also supply support modules for DOM
44
+ manipulation, WebGL, WebGPU, WebAudio etc.
42
45
  3. [Include files for C11/C++ and
43
46
  Zig](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/include),
44
47
  defining WASM imports of the JS [core
@@ -46,17 +49,20 @@ supply support modules for DOM manipulation, WebGL, WebGPU, WebAudio etc.
46
49
  by this package
47
50
  4. Extensible shared datatype code generators for (currently)
48
51
  [Zig](https://ziglang.org) & TypeScript. The latter also generates fully type
49
- checked memory-mapped accessors of WASM-side data. In general, all languages
50
- with a WebAssembly target are supported, however currently only bindings for
51
- these few langs are included.
52
+ checked memory-mapped (zero-copy) accessors of WASM-side data. In general,
53
+ all languages with a WebAssembly target are supported, however currently only
54
+ bindings for these few langs are included.
52
55
  5. [CLI frontend/utility](#cli-generator) to invoke the code generator(s)
53
56
 
54
57
  ### Custom API modules
55
58
 
56
- The [`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html) is extensible via custom defined API modules. Such API extensions
57
- will consist of a collection of JS/TS functions & variables, their related
58
- counterparts (import definitions) for the WASM target and (optionally) some
59
- shared data types ([bindings for which _can_ be generated by this package too](#data-bindings--code-generators)).
59
+ The
60
+ [`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html) is
61
+ extensible via custom defined API modules. Such API extensions will consist of a
62
+ collection of JS/TS functions & variables, their related counterparts (import
63
+ definitions) for the WASM target and (optionally) some shared data types
64
+ ([bindings for which _can_ be generated by this package
65
+ too](#data-bindings--code-generators)).
60
66
 
61
67
  On the JS side, custom API modules can be easily integrated via the [`IWasmAPI`
62
68
  interface](https://docs.thi.ng/umbrella/wasm-api/interfaces/IWasmAPI.html). The
@@ -131,53 +137,6 @@ export fn test_randomVec2() void {
131
137
  }
132
138
  ```
133
139
 
134
- ### Object indices & handles
135
-
136
- Since only numeric values can be exchanged between the WASM module and the JS
137
- host, any JS native objects the WASM side might want to be working with must be
138
- managed manually in JS. For this purpose the [`ObjectIndex`
139
- class](https://docs.thi.ng/umbrella/wasm-api/classes/ObjectIndex.html) can be
140
- used by API modules to handle ID generation (incl. recycling, using
141
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen))
142
- and the indexing of different types of JS objects/values. Only the numeric IDs
143
- (handles) will then need to be exchanged with the WASM module...
144
-
145
- ```ts
146
- import { ObjectIndex } from "@thi.ng/wasm-api";
147
-
148
- const canvases = new ObjectIndex<HTMLCanvasElement>({ name: "canvas" });
149
-
150
- // index item and assign new ID
151
- canvases.add(document.createElement("canvas"));
152
- // 0
153
-
154
- // look up item by ID
155
- canvases.get(0);
156
- // <canvas ...>
157
-
158
- // work w/ retrieved item
159
- canvases.get(0).id = "foo";
160
-
161
- // check if item for ID exists (O(1))
162
- canvases.has(1)
163
- // false
164
-
165
- // by default invalid IDs throw error
166
- canvases.get(1)
167
- // Uncaught Error: Assertion failed: missing canvas for ID: 2
168
-
169
- // error can be disabled via 2nd arg
170
- canvases.get(1, false)
171
- // undefined
172
-
173
- // find ID using custom predicate (same failure behavior as .get())
174
- canvases.find((x) => x.id == "bar")
175
- // Uncaught Error: Assertion failed: given predicate matched no canvas
176
-
177
- canvases.delete(0);
178
- // true
179
- ```
180
-
181
140
  ### Data bindings & code generators
182
141
 
183
142
  The package provides an extensible codegeneration framework to simplify the
@@ -193,7 +152,7 @@ to invoke the codegenerator(s) from JSON type definitions and to write the
193
152
  generated source code(s) to different files:
194
153
 
195
154
  ```text
196
- $ npx run @thi.ng/wasm-api
155
+ $ npx @thi.ng/wasm-api
197
156
 
198
157
  █ █ █ │
199
158
  ██ █ │
@@ -261,7 +220,9 @@ further details:
261
220
  Below is an example file with JSON type definitions and the resulting source
262
221
  codes:
263
222
 
264
- <details><summary>types.json (Type definitions, click to expand)</summary>
223
+ **⬇︎ CLICK TO EXPAND EACH CODE BLOCK ⬇︎**
224
+
225
+ <details><summary>types.json (Type definitions)</summary>
265
226
 
266
227
  ```json
267
228
  [
@@ -298,7 +259,7 @@ codes:
298
259
  ```
299
260
  </details>
300
261
 
301
- <details><summary>generated.ts (generated TypeScript source, click to expand)</summary>
262
+ <details><summary>generated.ts (generated TypeScript source)</summary>
302
263
 
303
264
  ```ts
304
265
  /** Generated by @thi.ng/wasm-api at 2022-08-15T22:32:21.189Z - DO NOT EDIT! */
@@ -373,7 +334,7 @@ export enum Kind {
373
334
  ```
374
335
  </details>
375
336
 
376
- <details><summary>generated.zig (generated Zig source, click to expand)</summary>
337
+ <details><summary>generated.zig (generated Zig source)</summary>
377
338
 
378
339
  ```zig
379
340
  //! Generated by @thi.ng/wasm-api at 2022-08-15T22:32:21.191Z - DO NOT EDIT!
@@ -422,13 +383,131 @@ foo.color
422
383
  // Float32Array(4) [0.1, 0.2, 0.3, 0.4]
423
384
 
424
385
  // this even applies to arrays using other types
425
- // (setters are currently only supported for scalar values, incl. enums)
426
386
  foo.bars[2].kind = Kind.BEST;
427
387
 
428
388
  // IMPORTANT: any modifications like this are directly
429
389
  // applied to the underlying WASM memory...
430
390
  ```
431
391
 
392
+ **IMPORTANT:** Struct field setters are currently only supported for single
393
+ values, incl. enums, strings, structs. The latter 2 will always be copied by
394
+ value (mem copy). Arrays or slices of strings do not currently provide write
395
+ access...
396
+
397
+ ### String handling
398
+
399
+ Most low-level languages deal with strings very differently and alas there's no
400
+ general standard. Some have UTF-8/16 support, others don't. In some languages
401
+ (incl. C & Zig), strings are stored as zero terminated, in others they aren't...
402
+ It's outside the scope of this package to provide an allround out-of-the-box
403
+ solution. The `WasmBridge` provides read & write accessors to obtain JS strings
404
+ from UTF-8 encoded WASM memory. See
405
+ [`getString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#getString)
406
+ and
407
+ [`setString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#setString)
408
+ for details.
409
+
410
+ The code generators too provide a global `stringType` option to
411
+ interpret the `string` type of a struct field in different ways:
412
+
413
+ - `slice` (default): Considers strings as Zig-style slices (i.e. pointer + length)
414
+ - `ptr`: Considers strings as C-style raw `*char` pointer (without any length)
415
+
416
+ Note: If setting this global option to `ptr`, it also has to be repeated for the
417
+ TypeScript code generator explicitly.
418
+
419
+ ### Memory allocations
420
+
421
+ If explicitly enabled on the WASM side, the `WasmBridge` includes support for
422
+ malloc/free-style allocations (within the linear WASM memory) from the JS side
423
+ (Note: This is a breaking change in v0.10.0, now using a more flexible approach
424
+ & reverse logic of earlier alpha versions).
425
+
426
+ The actual allocator is implementation specific and suitable generic mechanisms
427
+ are defined for both the included Zig & C bindings. Please see for further
428
+ reference:
429
+
430
+ - [`/include/wasmapi.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.zig#L6):
431
+ comments about WASM-side allocator handling in Zig
432
+ - [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#L19):
433
+ comments about WASM-side allocator handling in C/C++
434
+ - [`WasmBridge.allocate()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#allocate):
435
+ allocating memory from JS side
436
+ - [`WasmBridge.free()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#free):
437
+ freeing previously allocated memory from JS side
438
+
439
+ Note: The provided Zig mechanism supports the idiomatic (Zig) pattern of working
440
+ with multiple allocators in different parts of the application and supports
441
+ dynamic assignments/swapping of the exposed allocator. See comments in source
442
+ file for more details...
443
+
444
+ ```ts
445
+ try {
446
+ // allocate 1KB of memory for passing a string to WASM side
447
+ const addr = bridge.allocate(256);
448
+
449
+ // write string to reserved memory
450
+ // max. 256 bytes, zero terminated
451
+ const num = bridge.setString("hello WASM world!", addr, 256, true);
452
+
453
+ // call WASM function doing something w/ the string
454
+ bridge.exports.doSomethingWithString(addr, num);
455
+
456
+ // cleanup
457
+ bridge.free(addr, 256);
458
+ } catch(e) {
459
+ // deal with allocation error
460
+ // ...
461
+ }
462
+ ```
463
+
464
+ ### Object indices & handles
465
+
466
+ Since only numeric values can be exchanged between the WASM module and the JS
467
+ host, any JS native objects the WASM side might want to be working with must be
468
+ managed manually in JS. For this purpose the [`ObjectIndex`
469
+ class](https://docs.thi.ng/umbrella/wasm-api/classes/ObjectIndex.html) can be
470
+ used by API modules to handle ID generation (incl. recycling, using
471
+ [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen))
472
+ and the indexing of different types of JS objects/values. Only the numeric IDs
473
+ (handles) will then need to be exchanged with the WASM module...
474
+
475
+ ```ts
476
+ import { ObjectIndex } from "@thi.ng/wasm-api";
477
+
478
+ const canvases = new ObjectIndex<HTMLCanvasElement>({ name: "canvas" });
479
+
480
+ // index item and assign new ID
481
+ canvases.add(document.createElement("canvas"));
482
+ // 0
483
+
484
+ // look up item by ID
485
+ canvases.get(0);
486
+ // <canvas ...>
487
+
488
+ // work w/ retrieved item
489
+ canvases.get(0).id = "foo";
490
+
491
+ // check if item for ID exists (O(1))
492
+ canvases.has(1)
493
+ // false
494
+
495
+ // by default invalid IDs throw error
496
+ canvases.get(1)
497
+ // Uncaught Error: Assertion failed: missing canvas for ID: 2
498
+
499
+ // error can be disabled via 2nd arg
500
+ canvases.get(1, false)
501
+ // undefined
502
+
503
+ // find ID using custom predicate (same failure behavior as .get())
504
+ canvases.find((x) => x.id == "bar")
505
+ // Uncaught Error: Assertion failed: given predicate matched no canvas
506
+
507
+ canvases.delete(0);
508
+ // true
509
+ ```
510
+
432
511
  ### Status
433
512
 
434
513
  **ALPHA** - bleeding edge / work-in-progress
@@ -458,7 +537,7 @@ node --experimental-repl-await
458
537
  > const wasmApi = await import("@thi.ng/wasm-api");
459
538
  ```
460
539
 
461
- Package sizes (gzipped, pre-treeshake): ESM: 4.03 KB
540
+ Package sizes (gzipped, pre-treeshake): ESM: 4.48 KB
462
541
 
463
542
  **IMPORTANT:** The package includes various code generators and supporting
464
543
  functions which are NOT required during runtime. Hence the actual package size
package/api.d.ts CHANGED
@@ -1,6 +1,7 @@
1
1
  import type { BigType, FloatType, Fn, Fn2 } from "@thi.ng/api";
2
2
  import type { WasmBridge } from "./bridge.js";
3
3
  export declare const PKG_NAME = "@thi.ng/wasm-api";
4
+ export declare const EVENT_MEMORY_CHANGED = "memory-changed";
4
5
  export declare type BigIntArray = bigint[] | BigInt64Array | BigUint64Array;
5
6
  /**
6
7
  * Common interface for WASM/JS child APIs which will be used in combination
@@ -27,9 +28,9 @@ export interface IWasmAPI<T extends WasmExports = WasmExports> {
27
28
  getImports(): WebAssembly.ModuleImports;
28
29
  }
29
30
  /**
30
- * Base interface of exports declared by the WASM module. At the very least, the
31
- * module needs to export its memory and the functions defined in this
32
- * interface.
31
+ * Base interface of exports declared by the WASM module. At the very least, a
32
+ * compatible module needs to export its memory and the functions defined in
33
+ * this interface.
33
34
  *
34
35
  * @remarks
35
36
  * This interface is supposed to be extended with the concrete exports defined
@@ -45,28 +46,41 @@ export interface WasmExports {
45
46
  */
46
47
  memory: WebAssembly.Memory;
47
48
  /**
48
- * Implementation specific memory allocation function (likely heap-based).
49
- * If successful returns address of new memory block, or zero if
50
- * unsuccessful.
49
+ * Implementation specific WASM memory allocation function. If successful
50
+ * returns address of new memory block, or zero if unsuccessful.
51
51
  *
52
52
  * @remarks
53
- * In the supplied Zig bindings (see `/zig/core.zig`), by default this is
54
- * using the `std.heap.GeneralPurposeAllocator` (which also automatically
55
- * handles growing the WASM memory), however as mentioned the underlying
56
- * mechanism is purposefully left to the actual WASM-side implementation. In
57
- * a C program, this would likely use `malloc()` or similar...
53
+ * #### Zig
54
+ *
55
+ * Using the supplied Zig bindings (see `/include/wasmapi.zig`), it's the
56
+ * user's responsibility to define a public `WASM_ALLOCATOR` in the root
57
+ * source file to enable allocations, e.g. using the
58
+ * [`std.heap.GeneralPurposeAllocator`](https://ziglang.org/documentation/master/#Choosing-an-Allocator)
59
+ * (which also automatically handles growing the WASM memory). However, as
60
+ * mentioned, the underlying mechanism is purposefully left to the actual
61
+ * WASM-side implementation. If no allocator is defined this function
62
+ * returns zero, which in turn will cause {@link WasmBridge.allocate} to
63
+ * throw an error.
64
+ *
65
+ * #### C/C++
66
+ *
67
+ * Using the supplied C bindings (see `/include/wasmapi.h`), it's the user's
68
+ * responsibility to enable allocation support by defining the
69
+ * `WASMAPI_MALLOC` symbol (and compiling the WASM module with a malloc
70
+ * implementation).
58
71
  */
59
72
  _wasm_allocate(numBytes: number): number;
60
73
  /**
61
74
  * Implementation specific function to free a previously allocated chunk of
62
- * of WASM memory (allocated via {@link WasmExports._wasm_allocate}).
75
+ * of WASM memory (allocated via {@link WasmExports._wasm_allocate}, also
76
+ * see remarks for that function).
63
77
  *
64
78
  * @param addr
65
79
  * @param numBytes
66
80
  */
67
81
  _wasm_free(addr: number, numBytes: number): void;
68
82
  }
69
- export interface WasmMemViews {
83
+ export interface IWasmMemoryAccess {
70
84
  i8: Int8Array;
71
85
  u8: Uint8Array;
72
86
  i16: Int16Array;
@@ -77,6 +91,31 @@ export interface WasmMemViews {
77
91
  u64: BigUint64Array;
78
92
  f32: Float32Array;
79
93
  f64: Float64Array;
94
+ /**
95
+ * Reads UTF-8 encoded string from given address and optional byte length.
96
+ * The default length is 0, which will be interpreted as a zero-terminated
97
+ * string. Returns string.
98
+ *
99
+ * @param addr
100
+ * @param len
101
+ */
102
+ getString(addr: number, len?: number): string;
103
+ /**
104
+ * Encodes given string as UTF-8 and writes it to WASM memory starting at
105
+ * `addr`. By default the string will be zero-terminated and only `maxBytes`
106
+ * will be written. Returns the number of bytes written.
107
+ *
108
+ * @remarks
109
+ * An error will be thrown if the encoded string doesn't fully fit into the
110
+ * designated memory region (also note that there might need to be space for
111
+ * the additional sentinel/termination byte).
112
+ *
113
+ * @param str
114
+ * @param addr
115
+ * @param maxBytes
116
+ * @param terminate
117
+ */
118
+ setString(str: string, addr: number, maxBytes: number, terminate?: boolean): number;
80
119
  }
81
120
  /**
82
121
  * Core API of WASM imports defined by the {@link WasmBridge}. The same
@@ -110,6 +149,7 @@ export interface CoreAPI extends WebAssembly.ModuleImports {
110
149
  _printF64Array: (addr: number, len: number) => void;
111
150
  _printStr0: (addr: number) => void;
112
151
  _printStr: (addr: number, len: number) => void;
152
+ debug: () => void;
113
153
  }
114
154
  export interface WasmTypeBase {
115
155
  /**
@@ -126,7 +166,7 @@ export interface WasmType<T> {
126
166
  readonly size: number;
127
167
  instance: Fn<number, T>;
128
168
  }
129
- export declare type WasmTypeConstructor<T> = Fn<WasmMemViews, WasmType<T>>;
169
+ export declare type WasmTypeConstructor<T> = Fn<IWasmMemoryAccess, WasmType<T>>;
130
170
  export declare type WasmInt = "i8" | "i16" | "i32" | "i64";
131
171
  export declare type WasmUint = "u8" | "u16" | "u32" | "u64";
132
172
  export declare type WasmFloat = FloatType;
@@ -210,13 +250,16 @@ export interface StructField extends TypeInfo {
210
250
  */
211
251
  tag?: "scalar" | "array" | "ptr" | "slice" | "vec";
212
252
  /**
213
- * Field base type. If not a {@link WasmPrim} or `opaque`, the value is
214
- * interpreted as another type name in the {@link TypeColl}.
253
+ * Field base type. If not a {@link WasmPrim}, `string` or `opaque`, the
254
+ * value is interpreted as another type name in the {@link TypeColl}.
255
+ *
256
+ * @remarks
257
+ * Please see {@link CodeGenOpts.stringType} and consult package readme for
258
+ * further details re: string handling.
215
259
  *
216
260
  * TODO `opaque` currently unsupported.
217
- * TODO add string support (see {@link StructField.sentinel})
218
261
  */
219
- type: WasmPrim | "opaque" | string;
262
+ type: WasmPrim | "string" | "opaque" | string;
220
263
  /**
221
264
  * TODO currently unsupported & ignored!
222
265
  */
package/api.js CHANGED
@@ -1,4 +1,5 @@
1
1
  export const PKG_NAME = "@thi.ng/wasm-api";
2
+ export const EVENT_MEMORY_CHANGED = "memory-changed";
2
3
  /**
3
4
  * WASM usize type. Assuming wasm32 until wasm64 surfaces, then need an option.
4
5
  */
package/bridge.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="node" />
2
- import type { NumericArray } from "@thi.ng/api";
2
+ import type { Event, INotify, Listener, NumericArray } from "@thi.ng/api";
3
3
  import type { ILogger } from "@thi.ng/logger";
4
- import type { BigIntArray, CoreAPI, IWasmAPI, WasmExports, WasmMemViews } from "./api.js";
4
+ import { BigIntArray, CoreAPI, IWasmAPI, WasmExports, IWasmMemoryAccess } from "./api.js";
5
5
  export declare const OutOfMemoryError: {
6
6
  new (msg?: string | undefined): {
7
7
  name: string;
@@ -28,7 +28,7 @@ export declare const OutOfMemoryError: {
28
28
  * 64bit integers are handled via JS `BigInt` and hence require the host env to
29
29
  * support it. No polyfill is provided.
30
30
  */
31
- export declare class WasmBridge<T extends WasmExports = WasmExports> implements WasmMemViews {
31
+ export declare class WasmBridge<T extends WasmExports = WasmExports> implements IWasmMemoryAccess, INotify {
32
32
  modules: Record<string, IWasmAPI<T>>;
33
33
  logger: ILogger;
34
34
  i8: Int8Array;
@@ -66,14 +66,23 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
66
66
  * then initializes all declared bridge child API modules. Returns false if
67
67
  * any of the module initializations failed.
68
68
  *
69
+ * @remarks
70
+ * Emits the {@link EVENT_MEMORY_CHANGED} event just before returning (and
71
+ * AFTER all child API modules have been initialized).
72
+ *
69
73
  * @param exports
70
74
  */
71
75
  init(exports: T): Promise<boolean>;
72
76
  /**
73
- * Called automatically. Initializes and/or updates the various typed WASM
74
- * memory views (e.g. after growing the WASM memory).
77
+ * Called automatically during initialization. Initializes and/or updates
78
+ * the various typed WASM memory views (e.g. after growing the WASM memory
79
+ * and the previous buffer becoming detached). Unless `notify` is false,
80
+ * the {@link EVENT_MEMORY_CHANGED} event will be emitted if the memory
81
+ * views had to be updated.
82
+ *
83
+ * @param notify
75
84
  */
76
- ensureMemory(): void;
85
+ ensureMemory(notify?: boolean): void;
77
86
  /**
78
87
  * Required use for WASM module instantiation to provide JS imports to the
79
88
  * module. Returns an object of all WASM imports declared by the bridge core
@@ -137,6 +146,10 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
137
146
  * `numBytes` value must be the same as previously given to
138
147
  * {@link WasmBridge.allocate}.
139
148
  *
149
+ * @remarks
150
+ * This function always succeeds, regardless of presence of an active
151
+ * allocator on the WASM side or validity of given arguments.
152
+ *
140
153
  * @param addr
141
154
  * @param numBytes
142
155
  */
@@ -193,7 +206,8 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
193
206
  /**
194
207
  * Encodes given string as UTF-8 and writes it to WASM memory starting at
195
208
  * `addr`. By default the string will be zero-terminated and only `maxBytes`
196
- * will be written. Returns the number of bytes written.
209
+ * will be written. Returns the number of bytes written (excluding final
210
+ * sentinel, if any).
197
211
  *
198
212
  * @remarks
199
213
  * An error will be thrown if the encoded string doesn't fully fit into the
@@ -207,5 +221,11 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
207
221
  */
208
222
  setString(str: string, addr: number, maxBytes: number, terminate?: boolean): number;
209
223
  getElementById(addr: number, len?: number): HTMLElement;
224
+ /** {@inheritDoc @thi.ng/api#INotify.addListener} */
225
+ addListener(id: string, fn: Listener, scope?: any): boolean;
226
+ /** {@inheritDoc @thi.ng/api#INotify.removeListener} */
227
+ removeListener(id: string, fn: Listener, scope?: any): boolean;
228
+ /** {@inheritDoc @thi.ng/api#INotify.notify} */
229
+ notify(event: Event): void;
210
230
  }
211
231
  //# sourceMappingURL=bridge.d.ts.map