@thi.ng/wasm-api 0.18.1 → 1.0.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 +182 -102
- package/README.md +96 -468
- package/api.d.ts +24 -329
- package/api.js +0 -15
- package/bridge.d.ts +0 -31
- package/bridge.js +8 -33
- package/include/wasmapi.h +46 -42
- package/include/wasmapi_types.h +35 -0
- package/index.d.ts +0 -5
- package/index.js +0 -5
- package/package.json +15 -47
- package/pointer.d.ts +20 -0
- package/pointer.js +27 -0
- package/string.d.ts +48 -15
- package/string.js +67 -17
- package/zig/lib.zig +6 -31
- package/zig/types.zig +52 -0
- package/bin/wasm-api +0 -12
- package/bin/wasm-api.mjs_ +0 -17
- package/cli.d.ts +0 -5
- package/cli.js +0 -188
- package/codegen/align.d.ts +0 -14
- package/codegen/align.js +0 -35
- package/codegen/c.d.ts +0 -33
- package/codegen/c.js +0 -100
- package/codegen/c11.d.ts +0 -22
- package/codegen/c11.js +0 -145
- package/codegen/typescript.d.ts +0 -25
- package/codegen/typescript.js +0 -244
- package/codegen/utils.d.ts +0 -96
- package/codegen/utils.js +0 -120
- package/codegen/zig.d.ts +0 -23
- package/codegen/zig.js +0 -149
- package/codegen.d.ts +0 -32
- package/codegen.js +0 -198
package/README.md
CHANGED
|
@@ -10,14 +10,10 @@ 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
|
-
- [Data bindings & code generators](#data-bindings--code-generators)
|
|
14
|
-
- [CLI generator](#cli-generator)
|
|
15
|
-
- [Data type definitions](#data-type-definitions)
|
|
16
|
-
- [Example usage](#example-usage)
|
|
17
|
-
- [String handling](#string-handling)
|
|
18
|
-
- [Memory allocations](#memory-allocations)
|
|
19
13
|
- [Custom API modules](#custom-api-modules)
|
|
20
14
|
- [Building Zig projects with these hybrid API modules](#building-zig-projects-with-these-hybrid-api-modules)
|
|
15
|
+
- [String handling](#string-handling)
|
|
16
|
+
- [Memory allocations](#memory-allocations)
|
|
21
17
|
- [Object indices & handles](#object-indices--handles)
|
|
22
18
|
- [Status](#status)
|
|
23
19
|
- [Support packages](#support-packages)
|
|
@@ -27,465 +23,47 @@ This project is part of the
|
|
|
27
23
|
- [API](#api)
|
|
28
24
|
- [Basic usage example](#basic-usage-example)
|
|
29
25
|
- [Zig version](#zig-version)
|
|
30
|
-
- [
|
|
26
|
+
- [C version](#c-version)
|
|
31
27
|
- [Authors](#authors)
|
|
32
28
|
- [License](#license)
|
|
33
29
|
|
|
34
30
|
## About
|
|
35
31
|
|
|
36
|
-
Generic, modular, extensible API bridge
|
|
32
|
+
Generic, modular, extensible API bridge and infrastructure for hybrid JS & WebAssembly projects.
|
|
37
33
|
|
|
38
|
-
This package provides
|
|
34
|
+
This package provides the following:
|
|
39
35
|
|
|
40
|
-
1. A small
|
|
36
|
+
1. A small
|
|
41
37
|
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
42
|
-
class as interop basis and much reduced boilerplate for hybrid JS/WebAssembly
|
|
38
|
+
class as generic interop basis and much reduced boilerplate for hybrid JS/WebAssembly
|
|
43
39
|
applications.
|
|
44
40
|
2. A minimal core API for debug output, string/pointer/typedarray accessors for
|
|
45
|
-
8/16/32/64 bit (u)ints and 32/64 bit floats,
|
|
46
|
-
|
|
41
|
+
8/16/32/64 bit (u)ints and 32/64 bit floats. Additionally, a number of support
|
|
42
|
+
modules for [DOM
|
|
47
43
|
manipulation](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom/),
|
|
44
|
+
[scheduled function
|
|
45
|
+
execution](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-schedule/),
|
|
48
46
|
WebGL, WebGPU, WebAudio etc. is being actively worked on.
|
|
49
|
-
3.
|
|
50
|
-
|
|
51
|
-
|
|
47
|
+
3. Different types of memory-mapped (UTF-8) string abstractions (slice or pointer based)
|
|
48
|
+
5. Shared (opt-in) memory allocation mechanism, also accessible from JS/TS side
|
|
49
|
+
4. Simple registration & dependency-order initialization for child WASM API modules
|
|
50
|
+
6. Include files for
|
|
52
51
|
[Zig](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/zig),
|
|
53
|
-
|
|
52
|
+
and
|
|
53
|
+
[C/C++](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/include)
|
|
54
|
+
defining glue code for the TypeScript [core
|
|
54
55
|
API](https://docs.thi.ng/umbrella/wasm-api/interfaces/CoreAPI.html) defined
|
|
55
56
|
by this package
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
(
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
The package provides an extensible codegeneration framework to simplify the
|
|
67
|
-
bilateral design & exchange of data structures shared between the WASM & JS host
|
|
68
|
-
env. Currently, code generators for TypeScript, Zig and C11 are supplied. A CLI
|
|
69
|
-
wrapper is available too. See the
|
|
70
|
-
[@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom/)
|
|
71
|
-
support package for a more thorough realworld example...
|
|
72
|
-
|
|
73
|
-
#### CLI generator
|
|
74
|
-
|
|
75
|
-
The package includes a [small CLI
|
|
76
|
-
wrapper](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/src/cli.ts)
|
|
77
|
-
to invoke the code generator(s) from JSON type definitions and to write the
|
|
78
|
-
generated source code(s) to different files:
|
|
79
|
-
|
|
80
|
-
```text
|
|
81
|
-
$ npx @thi.ng/wasm-api
|
|
82
|
-
|
|
83
|
-
█ █ █ │
|
|
84
|
-
██ █ │
|
|
85
|
-
█ █ █ █ █ █ █ █ │ @thi.ng/wasm-api 0.15.0
|
|
86
|
-
█ █ █ █ █ █ █ █ █ │ Multi-language data bindings code generator
|
|
87
|
-
█ │
|
|
88
|
-
█ █ │
|
|
89
|
-
|
|
90
|
-
usage: wasm-api [OPTS] JSON-INPUT-FILE(S) ...
|
|
91
|
-
wasm-api --help
|
|
92
|
-
|
|
93
|
-
Flags:
|
|
94
|
-
|
|
95
|
-
-d, --debug enable debug output & functions
|
|
96
|
-
--dry-run enable dry run (don't overwrite files)
|
|
97
|
-
|
|
98
|
-
Main:
|
|
99
|
-
|
|
100
|
-
-a FILE, --analytics FILE output file path for raw codegen analytics
|
|
101
|
-
-c FILE, --config FILE JSON config file with codegen options
|
|
102
|
-
-l ID[,..], --lang ID[,..] [multiple] target language: "c11", "ts", "zig" (default: ["ts","zig"])
|
|
103
|
-
-o FILE, --out FILE [multiple] output file path
|
|
104
|
-
-s TYPE, --string TYPE Force string type implementation: "slice", "ptr"
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
By default, the CLI generates sources for TypeScript and Zig (in this order!).
|
|
108
|
-
Order is important, since the output file paths must be given in the same order
|
|
109
|
-
as the target languages. It's recommended to be explicit with this. An example
|
|
110
|
-
invocation looks like:
|
|
111
|
-
|
|
112
|
-
```bash
|
|
113
|
-
wasm-api \
|
|
114
|
-
--config codegen-opts.json \
|
|
115
|
-
--lang ts -o src/generated.ts \
|
|
116
|
-
--lang zig -o src.zig/generated.zig \
|
|
117
|
-
typedefs.json
|
|
118
|
-
```
|
|
119
|
-
|
|
120
|
-
The structure of the config file is as follows (all optional):
|
|
121
|
-
|
|
122
|
-
```json
|
|
123
|
-
{
|
|
124
|
-
"global": { ... },
|
|
125
|
-
"c11": { ... },
|
|
126
|
-
"ts": { ... },
|
|
127
|
-
"zig": { ... },
|
|
128
|
-
}
|
|
129
|
-
```
|
|
130
|
-
|
|
131
|
-
More details about possible
|
|
132
|
-
[`global`](https://docs.thi.ng/umbrella/wasm-api/interfaces/CodeGenOpts.html),
|
|
133
|
-
[`c`](https://docs.thi.ng/umbrella/wasm-api/interfaces/C11Opts.html) and
|
|
134
|
-
[`ts`](https://docs.thi.ng/umbrella/wasm-api/interfaces/TSOpts.html) and
|
|
135
|
-
[`zig`](https://docs.thi.ng/umbrella/wasm-api/interfaces/ZigOpts.html) config
|
|
136
|
-
options & values.
|
|
137
|
-
|
|
138
|
-
All code generators have support for custom code prologues & epilogues which can
|
|
139
|
-
be specified via the above options. These config options exist for both non-CLI
|
|
140
|
-
& CLI usage. For the latter, these custom code sections can also be loaded from
|
|
141
|
-
external files by specifying their file paths using `@` as prefix, e.g.
|
|
142
|
-
|
|
143
|
-
```json
|
|
144
|
-
{
|
|
145
|
-
"ts": { "pre": "@tpl/prelude.ts" },
|
|
146
|
-
"zig": { "pre": "@tpl/prelude.zig", "post": "@tpl/epilogue.zig" },
|
|
147
|
-
}
|
|
148
|
-
```
|
|
149
|
-
|
|
150
|
-
#### Data type definitions
|
|
151
|
-
|
|
152
|
-
Currently, the code generator supports enums, structs and unions. See API docs for
|
|
153
|
-
further details:
|
|
154
|
-
|
|
155
|
-
- [`Enum`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Enum.html)
|
|
156
|
-
- [`EnumValue`](https://docs.thi.ng/umbrella/wasm-api/interfaces/EnumValue.html) (individual enum value spec)
|
|
157
|
-
- [`Field`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Field.html) (individual spec for values contained in structs/unions)
|
|
158
|
-
- [`Struct`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Struct.html)
|
|
159
|
-
- [`Union`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Union.html)
|
|
160
|
-
- [`TopLevelType`](https://docs.thi.ng/umbrella/wasm-api/interfaces/TopLevelType.html)
|
|
161
|
-
|
|
162
|
-
#### Example usage
|
|
163
|
-
|
|
164
|
-
The following example defines 1x enum, 2x structs and 1x union. Shown here are
|
|
165
|
-
the JSON type definitions and the resulting source codes:
|
|
166
|
-
|
|
167
|
-
**⬇︎ CLICK TO EXPAND EACH CODE BLOCK ⬇︎**
|
|
168
|
-
|
|
169
|
-
<details><summary>types.json (Type definitions)</summary>
|
|
170
|
-
|
|
171
|
-
```json tangle:export/readme-types.json
|
|
172
|
-
[
|
|
173
|
-
{
|
|
174
|
-
"name": "EventType",
|
|
175
|
-
"type": "enum",
|
|
176
|
-
"tag": "u8",
|
|
177
|
-
"values": [
|
|
178
|
-
"unknown",
|
|
179
|
-
{ "name": "mouse", "value": 16 },
|
|
180
|
-
{ "name": "key", "value": 32 }
|
|
181
|
-
]
|
|
182
|
-
},
|
|
183
|
-
{
|
|
184
|
-
"name": "MouseEvent",
|
|
185
|
-
"type": "struct",
|
|
186
|
-
"doc": "Example struct",
|
|
187
|
-
"fields": [
|
|
188
|
-
{ "name": "type", "type": "EventType" },
|
|
189
|
-
{ "name": "pos", "type": "u16", "tag": "vec", "len": 2 }
|
|
190
|
-
]
|
|
191
|
-
},
|
|
192
|
-
{
|
|
193
|
-
"name": "KeyEvent",
|
|
194
|
-
"type": "struct",
|
|
195
|
-
"doc": "Example struct",
|
|
196
|
-
"fields": [
|
|
197
|
-
{ "name": "type", "type": "EventType" },
|
|
198
|
-
{ "name": "key", "type": "string" },
|
|
199
|
-
{ "name": "modifiers", "type": "u8", "doc": "Bitmask of modifier keys" }
|
|
200
|
-
]
|
|
201
|
-
},
|
|
202
|
-
{
|
|
203
|
-
"name": "Event",
|
|
204
|
-
"type": "union",
|
|
205
|
-
"fields": [
|
|
206
|
-
{ "name": "mouse", "type": "MouseEvent" },
|
|
207
|
-
{ "name": "key", "type": "KeyEvent" }
|
|
208
|
-
]
|
|
209
|
-
}
|
|
210
|
-
]
|
|
211
|
-
```
|
|
212
|
-
</details>
|
|
213
|
-
|
|
214
|
-
<details><summary>generated.ts (generated TypeScript source)</summary>
|
|
215
|
-
|
|
216
|
-
```ts
|
|
217
|
-
/**
|
|
218
|
-
* Generated by @thi.ng/wasm-api at 2022-10-26T08:36:16.825Z - DO NOT EDIT!
|
|
219
|
-
*/
|
|
220
|
-
|
|
221
|
-
// @ts-ignore possibly includes unused imports
|
|
222
|
-
import { Pointer, WasmStringSlice, WasmTypeBase, WasmTypeConstructor } from "@thi.ng/wasm-api";
|
|
223
|
-
|
|
224
|
-
export enum EventType {
|
|
225
|
-
UNKNOWN,
|
|
226
|
-
MOUSE = 16,
|
|
227
|
-
KEY = 32,
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Example struct
|
|
232
|
-
*/
|
|
233
|
-
export interface MouseEvent extends WasmTypeBase {
|
|
234
|
-
type: EventType;
|
|
235
|
-
pos: Uint16Array;
|
|
236
|
-
}
|
|
237
|
-
|
|
238
|
-
export const $MouseEvent: WasmTypeConstructor<MouseEvent> = (mem) => ({
|
|
239
|
-
get align() {
|
|
240
|
-
return 4;
|
|
241
|
-
},
|
|
242
|
-
get size() {
|
|
243
|
-
return 8;
|
|
244
|
-
},
|
|
245
|
-
instance: (base) => {
|
|
246
|
-
return {
|
|
247
|
-
get __base() {
|
|
248
|
-
return base;
|
|
249
|
-
},
|
|
250
|
-
get __bytes() {
|
|
251
|
-
return mem.u8.subarray(base, base + 8);
|
|
252
|
-
},
|
|
253
|
-
get type(): EventType {
|
|
254
|
-
return mem.u8[base];
|
|
255
|
-
},
|
|
256
|
-
set type(x: EventType) {
|
|
257
|
-
mem.u8[base] = x;
|
|
258
|
-
},
|
|
259
|
-
get pos(): Uint16Array {
|
|
260
|
-
const addr = (base + 4) >>> 1;
|
|
261
|
-
return mem.u16.subarray(addr, addr + 2);
|
|
262
|
-
},
|
|
263
|
-
};
|
|
264
|
-
}
|
|
265
|
-
});
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* Example struct
|
|
269
|
-
*/
|
|
270
|
-
export interface KeyEvent extends WasmTypeBase {
|
|
271
|
-
type: EventType;
|
|
272
|
-
key: WasmStringSlice;
|
|
273
|
-
/**
|
|
274
|
-
* Bitmask of modifier keys
|
|
275
|
-
*/
|
|
276
|
-
modifiers: number;
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
export const $KeyEvent: WasmTypeConstructor<KeyEvent> = (mem) => ({
|
|
280
|
-
get align() {
|
|
281
|
-
return 4;
|
|
282
|
-
},
|
|
283
|
-
get size() {
|
|
284
|
-
return 16;
|
|
285
|
-
},
|
|
286
|
-
instance: (base) => {
|
|
287
|
-
let $key: WasmStringSlice | null = null;
|
|
288
|
-
return {
|
|
289
|
-
get __base() {
|
|
290
|
-
return base;
|
|
291
|
-
},
|
|
292
|
-
get __bytes() {
|
|
293
|
-
return mem.u8.subarray(base, base + 16);
|
|
294
|
-
},
|
|
295
|
-
get type(): EventType {
|
|
296
|
-
return mem.u8[base];
|
|
297
|
-
},
|
|
298
|
-
set type(x: EventType) {
|
|
299
|
-
mem.u8[base] = x;
|
|
300
|
-
},
|
|
301
|
-
get key(): WasmStringSlice {
|
|
302
|
-
return $key || ($key = new WasmStringSlice(mem, (base + 4), true));
|
|
303
|
-
},
|
|
304
|
-
get modifiers(): number {
|
|
305
|
-
return mem.u8[(base + 12)];
|
|
306
|
-
},
|
|
307
|
-
set modifiers(x: number) {
|
|
308
|
-
mem.u8[(base + 12)] = x;
|
|
309
|
-
},
|
|
310
|
-
};
|
|
311
|
-
}
|
|
312
|
-
});
|
|
313
|
-
|
|
314
|
-
export interface Event extends WasmTypeBase {
|
|
315
|
-
mouse: MouseEvent;
|
|
316
|
-
key: KeyEvent;
|
|
317
|
-
}
|
|
318
|
-
|
|
319
|
-
export const $Event: WasmTypeConstructor<Event> = (mem) => ({
|
|
320
|
-
get align() {
|
|
321
|
-
return 4;
|
|
322
|
-
},
|
|
323
|
-
get size() {
|
|
324
|
-
return 16;
|
|
325
|
-
},
|
|
326
|
-
instance: (base) => {
|
|
327
|
-
return {
|
|
328
|
-
get __base() {
|
|
329
|
-
return base;
|
|
330
|
-
},
|
|
331
|
-
get __bytes() {
|
|
332
|
-
return mem.u8.subarray(base, base + 16);
|
|
333
|
-
},
|
|
334
|
-
get mouse(): MouseEvent {
|
|
335
|
-
return $MouseEvent(mem).instance(base);
|
|
336
|
-
},
|
|
337
|
-
set mouse(x: MouseEvent) {
|
|
338
|
-
mem.u8.set(x.__bytes, base);
|
|
339
|
-
},
|
|
340
|
-
get key(): KeyEvent {
|
|
341
|
-
return $KeyEvent(mem).instance(base);
|
|
342
|
-
},
|
|
343
|
-
set key(x: KeyEvent) {
|
|
344
|
-
mem.u8.set(x.__bytes, base);
|
|
345
|
-
},
|
|
346
|
-
};
|
|
347
|
-
}
|
|
348
|
-
});
|
|
349
|
-
```
|
|
350
|
-
</details>
|
|
351
|
-
|
|
352
|
-
<details><summary>generated.zig (generated Zig source)</summary>
|
|
353
|
-
|
|
354
|
-
```zig
|
|
355
|
-
//! Generated by @thi.ng/wasm-api at 2022-10-26T08:36:16.827Z - DO NOT EDIT!
|
|
356
|
-
|
|
357
|
-
const std = @import("std");
|
|
358
|
-
|
|
359
|
-
pub const EventType = enum(u8) {
|
|
360
|
-
UNKNOWN,
|
|
361
|
-
MOUSE = 16,
|
|
362
|
-
KEY = 32,
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
/// Example struct
|
|
366
|
-
pub const MouseEvent = struct {
|
|
367
|
-
type: EventType,
|
|
368
|
-
pos: @Vector(2, u16),
|
|
369
|
-
};
|
|
370
|
-
|
|
371
|
-
/// Example struct
|
|
372
|
-
pub const KeyEvent = struct {
|
|
373
|
-
type: EventType,
|
|
374
|
-
key: []const u8,
|
|
375
|
-
/// Bitmask of modifier keys
|
|
376
|
-
modifiers: u8,
|
|
377
|
-
};
|
|
378
|
-
|
|
379
|
-
pub const Event = union {
|
|
380
|
-
mouse: MouseEvent,
|
|
381
|
-
key: KeyEvent,
|
|
382
|
-
};
|
|
383
|
-
```
|
|
384
|
-
</details>
|
|
385
|
-
|
|
386
|
-
On the TypeScript/JS side, the memory-mapped wrappers (e.g. `$Event`)
|
|
387
|
-
can be used in combination with the `WasmBridge` to obtain fully typed views
|
|
388
|
-
(according to the generated types) of the underlying WASM memory. Basic usage is
|
|
389
|
-
like:
|
|
390
|
-
|
|
391
|
-
```ts
|
|
392
|
-
import { WasmBridge } from "@thi.ng/wasm-api";
|
|
393
|
-
import { $Event, EventType } from "./generated.ts";
|
|
394
|
-
|
|
395
|
-
const bridge = new WasmBridge();
|
|
396
|
-
// bridge initialization omitted here (see other examples below)
|
|
397
|
-
// ...
|
|
398
|
-
|
|
399
|
-
// Create an instance using the bridge's memory views
|
|
400
|
-
// and mapping a `Event` union from given address
|
|
401
|
-
// (e.g. obtained from an exported WASM function/value)
|
|
402
|
-
const event = $Event(bridge).instance(0x10000);
|
|
403
|
-
|
|
404
|
-
// then use like normal JS object
|
|
405
|
-
event.mouse.pos
|
|
406
|
-
// Uint16Array(2) [100, 200]
|
|
407
|
-
|
|
408
|
-
// IMPORTANT: any modifications like this are directly
|
|
409
|
-
// applied to the underlying WASM memory...
|
|
410
|
-
event.mouse.pos[0] = 300;
|
|
411
|
-
|
|
412
|
-
event.mouse.type === EventType.MOUSE
|
|
413
|
-
// true
|
|
414
|
-
```
|
|
415
|
-
|
|
416
|
-
**IMPORTANT:** Field setters are currently only supported for single values,
|
|
417
|
-
incl. enums, strings, structs, unions. The latter 2 will always be copied by
|
|
418
|
-
value (mem copy). Arrays or slices of strings do not currently provide write
|
|
419
|
-
access...
|
|
420
|
-
|
|
421
|
-
### String handling
|
|
422
|
-
|
|
423
|
-
Most low-level languages deal with strings very differently and alas there's no
|
|
424
|
-
general standard. Some have UTF-8/16 support, others don't. In some languages
|
|
425
|
-
(incl. C & Zig), strings are stored as zero terminated, in others they aren't...
|
|
426
|
-
It's outside the scope of this package to provide an allround out-of-the-box
|
|
427
|
-
solution. The `WasmBridge` provides read & write accessors to obtain JS strings
|
|
428
|
-
from UTF-8 encoded WASM memory. See
|
|
429
|
-
[`getString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#getString)
|
|
430
|
-
and
|
|
431
|
-
[`setString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#setString)
|
|
432
|
-
for details.
|
|
433
|
-
|
|
434
|
-
The code generators too provide a global `stringType` option to
|
|
435
|
-
interpret the `string` type of a struct field in different ways:
|
|
436
|
-
|
|
437
|
-
- `slice` (default): Considers strings as Zig-style slices (i.e. pointer + length)
|
|
438
|
-
- `ptr`: Considers strings as C-style raw `*char` pointer (without any length)
|
|
439
|
-
|
|
440
|
-
Note: If setting this global option to `ptr`, it also has to be repeated for the
|
|
441
|
-
TypeScript code generator explicitly.
|
|
442
|
-
|
|
443
|
-
### Memory allocations
|
|
444
|
-
|
|
445
|
-
If explicitly enabled on the WASM side, the `WasmBridge` includes support for
|
|
446
|
-
malloc/free-style allocations (within the linear WASM memory) from the JS side
|
|
447
|
-
(Note: This is a breaking change in v0.10.0, now using a more flexible approach
|
|
448
|
-
& reverse logic of earlier alpha versions).
|
|
449
|
-
|
|
450
|
-
The actual allocator is implementation specific and suitable generic mechanisms
|
|
451
|
-
are defined for both the included Zig & C bindings. Please see for further
|
|
452
|
-
reference:
|
|
453
|
-
|
|
454
|
-
- [`/zig/lib.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/lib.zig#L64):
|
|
455
|
-
comments about WASM-side allocator handling in Zig
|
|
456
|
-
- [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#L18):
|
|
457
|
-
comments about WASM-side allocator handling in C/C++
|
|
458
|
-
- [`WasmBridge.allocate()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#allocate):
|
|
459
|
-
allocating memory from JS side
|
|
460
|
-
- [`WasmBridge.free()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#free):
|
|
461
|
-
freeing previously allocated memory from JS side
|
|
462
|
-
|
|
463
|
-
Note: The provided Zig mechanism supports the idiomatic (Zig) pattern of working
|
|
464
|
-
with multiple allocators in different parts of the application and supports
|
|
465
|
-
dynamic assignments/swapping of the exposed allocator. See comments in source
|
|
466
|
-
file and
|
|
467
|
-
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
468
|
-
for more details...
|
|
469
|
-
|
|
470
|
-
```ts
|
|
471
|
-
try {
|
|
472
|
-
// allocate 1KB of memory for passing a string to WASM side
|
|
473
|
-
const addr = bridge.allocate(256);
|
|
474
|
-
|
|
475
|
-
// write string to reserved memory
|
|
476
|
-
// max. 256 bytes, zero terminated
|
|
477
|
-
const num = bridge.setString("hello WASM world!", addr, 256, true);
|
|
478
|
-
|
|
479
|
-
// call WASM function doing something w/ the string
|
|
480
|
-
bridge.exports.doSomethingWithString(addr, num);
|
|
481
|
-
|
|
482
|
-
// cleanup
|
|
483
|
-
bridge.free(addr, 256);
|
|
484
|
-
} catch(e) {
|
|
485
|
-
// deal with allocation error
|
|
486
|
-
// ...
|
|
487
|
-
}
|
|
488
|
-
```
|
|
57
|
+
7. Extensible shared [datatype code generator
|
|
58
|
+
infrastructure](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-bindgen/)
|
|
59
|
+
for (currently) Zig & TypeScript and C11. For TS fully type checked and
|
|
60
|
+
memory-mapped (zero-copy) accessors of WASM-side data are generated. In
|
|
61
|
+
principle, all languages with a WASM target are supported, however currently
|
|
62
|
+
only bindings for these mentioned langs are included. Other languages require
|
|
63
|
+
custom bindings, e.g. based on the flexible primitives provided here.
|
|
64
|
+
8. [CLI
|
|
65
|
+
frontend/utility](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-bindgen/README.md#cli-generator)
|
|
66
|
+
for the code generator(s)
|
|
489
67
|
|
|
490
68
|
### Custom API modules
|
|
491
69
|
|
|
@@ -599,6 +177,65 @@ To avoid guesswork about the internals of these API modules, all of them are
|
|
|
599
177
|
using an overall uniform structure, with the main Zig entry point in
|
|
600
178
|
`/zig/lib.zig`...
|
|
601
179
|
|
|
180
|
+
### String handling
|
|
181
|
+
|
|
182
|
+
Most low-level languages deal with strings very differently and alas there's no
|
|
183
|
+
general standard. Some have UTF-8/16 support, others don't. In some languages
|
|
184
|
+
(incl. C & Zig), strings are stored as zero terminated, in others they aren't...
|
|
185
|
+
It's outside the scope of this package to provide an allround out-of-the-box
|
|
186
|
+
solution. The `WasmBridge` provides read & write accessors to obtain JS strings
|
|
187
|
+
from UTF-8 encoded WASM memory. See
|
|
188
|
+
[`getString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#getString)
|
|
189
|
+
and
|
|
190
|
+
[`setString()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#setString)
|
|
191
|
+
for details.
|
|
192
|
+
|
|
193
|
+
### Memory allocations
|
|
194
|
+
|
|
195
|
+
If explicitly enabled on the WASM side, the `WasmBridge` includes support for
|
|
196
|
+
malloc/free-style allocations (within the linear WASM memory) from the JS side.
|
|
197
|
+
|
|
198
|
+
The actual allocator is implementation specific and suitable generic mechanisms
|
|
199
|
+
are defined for both the included Zig & C bindings. Please see for further
|
|
200
|
+
reference:
|
|
201
|
+
|
|
202
|
+
- [`/zig/lib.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/lib.zig#L37-L71):
|
|
203
|
+
comments about WASM-side allocator handling in Zig
|
|
204
|
+
- [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#L20-L30):
|
|
205
|
+
comments about WASM-side allocator handling in C/C++
|
|
206
|
+
- [`WasmBridge.allocate()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#allocate):
|
|
207
|
+
allocating memory from JS side
|
|
208
|
+
- [`WasmBridge.free()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#free):
|
|
209
|
+
freeing previously allocated memory from JS side
|
|
210
|
+
|
|
211
|
+
Note: The provided Zig library supports the idiomatic (Zig) pattern of working
|
|
212
|
+
with multiple allocators in different parts of the application and supports
|
|
213
|
+
dynamic assignments/swapping of the exposed allocator. See comments in source
|
|
214
|
+
file and
|
|
215
|
+
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
216
|
+
for more details...
|
|
217
|
+
|
|
218
|
+
```ts
|
|
219
|
+
try {
|
|
220
|
+
// allocate 256 bytes of memory for passing a string to WASM side
|
|
221
|
+
// the function returns a tuple of `[address, len]`
|
|
222
|
+
const [addr, len] = bridge.allocate(256);
|
|
223
|
+
|
|
224
|
+
// write zero terminated string to reserved memory (max. `len` bytes)
|
|
225
|
+
// function returns number of bytes written (excl. sentinel)
|
|
226
|
+
const num = bridge.setString("hello WASM world!", addr, len, true);
|
|
227
|
+
|
|
228
|
+
// call WASM function doing something w/ the string
|
|
229
|
+
bridge.exports.doSomethingWithString(addr, num);
|
|
230
|
+
|
|
231
|
+
// cleanup
|
|
232
|
+
bridge.free([addr, len]);
|
|
233
|
+
} catch(e) {
|
|
234
|
+
// deal with allocation error
|
|
235
|
+
// ...
|
|
236
|
+
}
|
|
237
|
+
```
|
|
238
|
+
|
|
602
239
|
### Object indices & handles
|
|
603
240
|
|
|
604
241
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
@@ -662,8 +299,9 @@ modules this is used to manage Zig event listeners.
|
|
|
662
299
|
|
|
663
300
|
## Support packages
|
|
664
301
|
|
|
302
|
+
- [@thi.ng/wasm-api-bindgen](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-bindgen) - Polyglot bindings code generators for hybrid JS & WebAssembly projects
|
|
665
303
|
- [@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom) - Browser DOM bridge API for hybrid TypeScript & WASM (Zig) applications
|
|
666
|
-
- [@thi.ng/wasm-api-
|
|
304
|
+
- [@thi.ng/wasm-api-schedule](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-schedule) - Delayed & scheduled function execution (via setTimeout() etc.) for hybrid WASM apps
|
|
667
305
|
|
|
668
306
|
## Installation
|
|
669
307
|
|
|
@@ -688,28 +326,17 @@ node --experimental-repl-await
|
|
|
688
326
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
689
327
|
```
|
|
690
328
|
|
|
691
|
-
Package sizes (gzipped, pre-treeshake): ESM:
|
|
692
|
-
|
|
693
|
-
**IMPORTANT:** The package includes multiple language code generators which are
|
|
694
|
-
**not** required for normal use of the API bridge. Hence, the actual package
|
|
695
|
-
size in production will be MUCH smaller than what's stated here!
|
|
329
|
+
Package sizes (gzipped, pre-treeshake): ESM: 2.70 KB
|
|
696
330
|
|
|
697
331
|
## Dependencies
|
|
698
332
|
|
|
699
333
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
700
|
-
- [@thi.ng/args](https://github.com/thi-ng/umbrella/tree/develop/packages/args)
|
|
701
334
|
- [@thi.ng/arrays](https://github.com/thi-ng/umbrella/tree/develop/packages/arrays)
|
|
702
|
-
- [@thi.ng/binary](https://github.com/thi-ng/umbrella/tree/develop/packages/binary)
|
|
703
335
|
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
704
|
-
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare)
|
|
705
|
-
- [@thi.ng/defmulti](https://github.com/thi-ng/umbrella/tree/develop/packages/defmulti)
|
|
706
336
|
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
707
|
-
- [@thi.ng/file-io](https://github.com/thi-ng/umbrella/tree/develop/packages/file-io)
|
|
708
337
|
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
709
338
|
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
|
|
710
339
|
- [@thi.ng/logger](https://github.com/thi-ng/umbrella/tree/develop/packages/logger)
|
|
711
|
-
- [@thi.ng/paths](https://github.com/thi-ng/umbrella/tree/develop/packages/paths)
|
|
712
|
-
- [@thi.ng/strings](https://github.com/thi-ng/umbrella/tree/develop/packages/strings)
|
|
713
340
|
|
|
714
341
|
## Usage examples
|
|
715
342
|
|
|
@@ -719,10 +346,11 @@ directory are using this package.
|
|
|
719
346
|
|
|
720
347
|
A selection:
|
|
721
348
|
|
|
722
|
-
| Screenshot
|
|
723
|
-
|
|
724
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-canvas.png" width="240"/>
|
|
725
|
-
| <img src="https://raw.githubusercontent.com/thi-ng/umbrella/develop/assets/examples/zig-counter.png" width="240"/>
|
|
349
|
+
| Screenshot | Description | Live demo | Source |
|
|
350
|
+
|:---------------------------------------------------------------------------------------------------------------------|:-------------------------------------------------------------------|:----------------------------------------------------|:---------------------------------------------------------------------------------|
|
|
351
|
+
| <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) |
|
|
352
|
+
| <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) |
|
|
353
|
+
| <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) |
|
|
726
354
|
|
|
727
355
|
## API
|
|
728
356
|
|
|
@@ -819,7 +447,7 @@ The resulting WASM:
|
|
|
819
447
|
)
|
|
820
448
|
```
|
|
821
449
|
|
|
822
|
-
###
|
|
450
|
+
### C version
|
|
823
451
|
|
|
824
452
|
Requires [Emscripten](https://emscripten.org/) to be installed:
|
|
825
453
|
|