@thi.ng/wasm-api 1.0.1 → 1.1.1
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 +10 -1
- package/README.md +31 -16
- package/api.d.ts +1 -0
- package/api.js +1 -0
- package/bridge.d.ts +1 -1
- package/bridge.js +5 -2
- package/package.json +10 -10
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-11-
|
|
3
|
+
- **Last updated**: 2022-11-30T22:27:37Z
|
|
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,15 @@ 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
|
+
## [1.1.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@1.1.0) (2022-11-28)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- update panic handler, add event support ([ab940d9](https://github.com/thi-ng/umbrella/commit/ab940d9))
|
|
17
|
+
- update panic handler to broadcast event w/ panic message
|
|
18
|
+
- only throw Panic error if no listeners could be notified
|
|
19
|
+
- add `EVENT_PANIC` constant
|
|
20
|
+
|
|
12
21
|
# [1.0.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@1.0.0) (2022-11-23)
|
|
13
22
|
|
|
14
23
|
#### 🛑 Breaking changes
|
package/README.md
CHANGED
|
@@ -10,11 +10,12 @@ This project is part of the
|
|
|
10
10
|
[@thi.ng/umbrella](https://github.com/thi-ng/umbrella/) monorepo.
|
|
11
11
|
|
|
12
12
|
- [About](#about)
|
|
13
|
-
|
|
13
|
+
- [Custom API modules](#custom-api-modules)
|
|
14
14
|
- [Building Zig projects with these hybrid API modules](#building-zig-projects-with-these-hybrid-api-modules)
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
- [
|
|
15
|
+
- [String handling](#string-handling)
|
|
16
|
+
- [Memory allocations](#memory-allocations)
|
|
17
|
+
- [API module auto-initialization](#api-module-auto-initialization)
|
|
18
|
+
- [Object indices & handles](#object-indices--handles)
|
|
18
19
|
- [Status](#status)
|
|
19
20
|
- [Support packages](#support-packages)
|
|
20
21
|
- [Installation](#installation)
|
|
@@ -64,7 +65,7 @@ only bindings for these mentioned langs are included.
|
|
|
64
65
|
frontend/utility](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-bindgen/README.md#cli-generator)
|
|
65
66
|
for the code generator(s)
|
|
66
67
|
|
|
67
|
-
|
|
68
|
+
## Custom API modules
|
|
68
69
|
|
|
69
70
|
The
|
|
70
71
|
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
@@ -126,7 +127,8 @@ export const bridge = new WasmBridge([new CustomAPI()]);
|
|
|
126
127
|
```
|
|
127
128
|
|
|
128
129
|
In Zig (or any other language of your choice) we can then utilize this custom
|
|
129
|
-
API like so (Please also see [example
|
|
130
|
+
API like so (Please also see [example
|
|
131
|
+
projects](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-canvas/)
|
|
130
132
|
& other example snippets in this readme):
|
|
131
133
|
|
|
132
134
|
Bindings file / lib:
|
|
@@ -146,7 +148,7 @@ Main Zig file:
|
|
|
146
148
|
|
|
147
149
|
```zig
|
|
148
150
|
// Import JS core API
|
|
149
|
-
const js = @import("
|
|
151
|
+
const js = @import("wasm-api");
|
|
150
152
|
const custom = @import("custom.zig");
|
|
151
153
|
|
|
152
154
|
export fn test_randomVec4() void {
|
|
@@ -176,7 +178,7 @@ To avoid guesswork about the internals of these API modules, all of them are
|
|
|
176
178
|
using an overall uniform structure, with the main Zig entry point in
|
|
177
179
|
`/zig/lib.zig`...
|
|
178
180
|
|
|
179
|
-
|
|
181
|
+
## String handling
|
|
180
182
|
|
|
181
183
|
Most low-level languages deal with strings very differently and alas there's no
|
|
182
184
|
general standard. Some have UTF-8/16 support, others don't. In some languages
|
|
@@ -198,7 +200,7 @@ Finally, see more information in the
|
|
|
198
200
|
[@thi.ng/wasm-api-bindgen](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-bindgen/README.md#string-handling)
|
|
199
201
|
package readme.
|
|
200
202
|
|
|
201
|
-
|
|
203
|
+
## Memory allocations
|
|
202
204
|
|
|
203
205
|
If explicitly enabled on the WASM side, the `WasmBridge` includes support for
|
|
204
206
|
malloc/free-style allocations (within the linear WASM memory) from the JS side.
|
|
@@ -244,7 +246,18 @@ try {
|
|
|
244
246
|
}
|
|
245
247
|
```
|
|
246
248
|
|
|
247
|
-
###
|
|
249
|
+
### API module auto-initialization
|
|
250
|
+
|
|
251
|
+
The supplied child APIs
|
|
252
|
+
([wasm-api-dom](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom),
|
|
253
|
+
[wasm-api-schedule](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-schedule)
|
|
254
|
+
etc.) use an auto-intialization hook related to the above `WASM_ALLOCATOR`
|
|
255
|
+
mechanism: If that allocator is available, the WASM side of these modules will
|
|
256
|
+
auto initialize and thus reduce boilerplate. However, if no such central
|
|
257
|
+
allocator is defined and/or a custom allocator should be used, then these API
|
|
258
|
+
modules will be have to be initialized manually.
|
|
259
|
+
|
|
260
|
+
## Object indices & handles
|
|
248
261
|
|
|
249
262
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
250
263
|
host, any JS native objects the WASM side might want to be working with must be
|
|
@@ -291,13 +304,14 @@ canvases.delete(0);
|
|
|
291
304
|
// true
|
|
292
305
|
```
|
|
293
306
|
|
|
294
|
-
|
|
307
|
+
The supplied Zig core library also includes a
|
|
295
308
|
[`ManagedIndex`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/managed-index.zig)
|
|
296
|
-
for similar
|
|
309
|
+
for similar resource management on the Zig side of the application. For example,
|
|
310
|
+
in the
|
|
297
311
|
[@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-dom/)
|
|
298
312
|
&
|
|
299
313
|
[@thi.ng/wasm-api-schedule](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-schedule/)
|
|
300
|
-
packages this is used to manage Zig event listeners.
|
|
314
|
+
packages this is used to manage Zig-side event listeners.
|
|
301
315
|
|
|
302
316
|
## Status
|
|
303
317
|
|
|
@@ -334,7 +348,7 @@ node --experimental-repl-await
|
|
|
334
348
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
335
349
|
```
|
|
336
350
|
|
|
337
|
-
Package sizes (
|
|
351
|
+
Package sizes (brotli'd, pre-treeshake): ESM: 2.47 KB
|
|
338
352
|
|
|
339
353
|
## Dependencies
|
|
340
354
|
|
|
@@ -357,6 +371,7 @@ A selection:
|
|
|
357
371
|
| Screenshot | Description | Live demo | Source |
|
|
358
372
|
|:---------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------|:----------------------------------------------------|:---------------------------------------------------------------------------------|
|
|
359
373
|
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-canvas.png" width="240"/> | Zig-based DOM creation & canvas drawing app | [Demo](https://demo.thi.ng/umbrella/zig-canvas/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-canvas) |
|
|
374
|
+
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-cellular.jpg" width="240"/> | Zig-based 2D multi-behavior cellular automata | [Demo](https://demo.thi.ng/umbrella/zig-cellular/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-cellular) |
|
|
360
375
|
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-counter.png" width="240"/> | Simple Zig/WASM click counter DOM component | [Demo](https://demo.thi.ng/umbrella/zig-counter/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-counter) |
|
|
361
376
|
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-todo-list.png" width="240"/> | Zig-based To-Do list, DOM creation, local storage task persistence | [Demo](https://demo.thi.ng/umbrella/zig-todo-list/) | [Source](https://github.com/thi-ng/umbrella/tree/develop/examples/zig-todo-list) |
|
|
362
377
|
|
|
@@ -399,7 +414,7 @@ Requires [Zig](https://ziglang.org) to be installed:
|
|
|
399
414
|
|
|
400
415
|
/// import externals
|
|
401
416
|
/// see build command for configuration
|
|
402
|
-
const js = @import("
|
|
417
|
+
const js = @import("wasm-api");
|
|
403
418
|
|
|
404
419
|
export fn start() void {
|
|
405
420
|
js.printStr("hello world!");
|
|
@@ -413,7 +428,7 @@ folder):
|
|
|
413
428
|
```bash
|
|
414
429
|
# compile WASM binary
|
|
415
430
|
zig build-lib \
|
|
416
|
-
--pkg-begin
|
|
431
|
+
--pkg-begin wasm-api node_modules/@thi.ng/wasm-api/zig/lib.zig --pkg-end \
|
|
417
432
|
-target wasm32-freestanding \
|
|
418
433
|
-O ReleaseSmall -dynamic \
|
|
419
434
|
hello.zig
|
package/api.d.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import type { Fn, IDeref, ILength } from "@thi.ng/api";
|
|
2
2
|
import type { WasmBridge } from "./bridge.js";
|
|
3
3
|
export declare const EVENT_MEMORY_CHANGED = "memory-changed";
|
|
4
|
+
export declare const EVENT_PANIC = "panic";
|
|
4
5
|
export declare type BigIntArray = bigint[] | BigInt64Array | BigUint64Array;
|
|
5
6
|
export declare type ReadonlyWasmString = IDeref<string> & ILength & {
|
|
6
7
|
readonly addr: number;
|
package/api.js
CHANGED
package/bridge.d.ts
CHANGED
|
@@ -181,6 +181,6 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
|
|
|
181
181
|
/** {@inheritDoc @thi.ng/api#INotify.removeListener} */
|
|
182
182
|
removeListener(id: string, fn: Listener, scope?: any): boolean;
|
|
183
183
|
/** {@inheritDoc @thi.ng/api#INotify.notify} */
|
|
184
|
-
notify(event: Event):
|
|
184
|
+
notify(event: Event): boolean;
|
|
185
185
|
}
|
|
186
186
|
//# sourceMappingURL=bridge.d.ts.map
|
package/bridge.js
CHANGED
|
@@ -5,7 +5,7 @@ import { assert } from "@thi.ng/errors/assert";
|
|
|
5
5
|
import { defError } from "@thi.ng/errors/deferror";
|
|
6
6
|
import { hexdumpLines, U16, U32, U64BIG, U8 } from "@thi.ng/hex";
|
|
7
7
|
import { ConsoleLogger } from "@thi.ng/logger/console";
|
|
8
|
-
import { EVENT_MEMORY_CHANGED, } from "./api.js";
|
|
8
|
+
import { EVENT_MEMORY_CHANGED, EVENT_PANIC, } from "./api.js";
|
|
9
9
|
export const Panic = defError(() => "Panic");
|
|
10
10
|
export const OutOfMemoryError = defError(() => "Out of memory");
|
|
11
11
|
/**
|
|
@@ -69,7 +69,10 @@ let WasmBridge = class WasmBridge {
|
|
|
69
69
|
debugger;
|
|
70
70
|
},
|
|
71
71
|
_panic: (addr, len) => {
|
|
72
|
-
|
|
72
|
+
const msg = this.getString(addr, len);
|
|
73
|
+
if (!this.notify({ id: EVENT_PANIC, value: msg })) {
|
|
74
|
+
throw new Panic(msg);
|
|
75
|
+
}
|
|
73
76
|
},
|
|
74
77
|
timer: () => performance.now(),
|
|
75
78
|
epoch: () => BigInt(Date.now()),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@thi.ng/wasm-api",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.1.1",
|
|
4
4
|
"description": "Generic, modular, extensible API bridge and infrastructure for hybrid JS & WebAssembly projects",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"module": "./index.js",
|
|
@@ -35,17 +35,17 @@
|
|
|
35
35
|
"test:build-zig": "zig build-lib -O ReleaseSmall -target wasm32-freestanding -dynamic --strip --pkg-begin wasmapi zig/lib.zig --pkg-end test/custom.zig && wasm-dis -o custom.wast custom.wasm && cp custom.wasm test"
|
|
36
36
|
},
|
|
37
37
|
"dependencies": {
|
|
38
|
-
"@thi.ng/api": "^8.
|
|
39
|
-
"@thi.ng/arrays": "^2.4.
|
|
40
|
-
"@thi.ng/checks": "^3.3.
|
|
41
|
-
"@thi.ng/errors": "^2.2.
|
|
42
|
-
"@thi.ng/hex": "^2.3.
|
|
43
|
-
"@thi.ng/idgen": "^2.1.
|
|
44
|
-
"@thi.ng/logger": "^1.4.
|
|
38
|
+
"@thi.ng/api": "^8.5.1",
|
|
39
|
+
"@thi.ng/arrays": "^2.4.4",
|
|
40
|
+
"@thi.ng/checks": "^3.3.4",
|
|
41
|
+
"@thi.ng/errors": "^2.2.5",
|
|
42
|
+
"@thi.ng/hex": "^2.3.1",
|
|
43
|
+
"@thi.ng/idgen": "^2.1.20",
|
|
44
|
+
"@thi.ng/logger": "^1.4.4"
|
|
45
45
|
},
|
|
46
46
|
"devDependencies": {
|
|
47
47
|
"@microsoft/api-extractor": "^7.33.5",
|
|
48
|
-
"@thi.ng/testament": "^0.3.
|
|
48
|
+
"@thi.ng/testament": "^0.3.6",
|
|
49
49
|
"rimraf": "^3.0.2",
|
|
50
50
|
"tools": "^0.0.1",
|
|
51
51
|
"typedoc": "^0.23.20",
|
|
@@ -110,5 +110,5 @@
|
|
|
110
110
|
"status": "alpha",
|
|
111
111
|
"year": 2022
|
|
112
112
|
},
|
|
113
|
-
"gitHead": "
|
|
113
|
+
"gitHead": "1fe40da507070653f420156d91e6b27cf682004f\n"
|
|
114
114
|
}
|