@thi.ng/wasm-api 0.14.0 → 0.16.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 +60 -1
- package/README.md +173 -108
- package/api.d.ts +126 -28
- package/api.js +10 -4
- package/bridge.d.ts +3 -29
- package/bridge.js +2 -28
- package/cli.js +8 -4
- package/codegen/align.d.ts +14 -0
- package/codegen/align.js +35 -0
- package/codegen/c11.js +71 -55
- package/codegen/typescript.js +34 -14
- package/codegen/utils.d.ts +20 -11
- package/codegen/utils.js +28 -9
- package/codegen/zig.d.ts +6 -1
- package/codegen/zig.js +99 -71
- package/codegen.d.ts +2 -1
- package/codegen.js +77 -54
- package/package.json +25 -21
- package/string.d.ts +4 -0
- package/string.js +4 -0
- package/zig/managed-index.zig +223 -0
- package/{include → zig}/wasmapi.zig +2 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-10-
|
|
3
|
+
- **Last updated**: 2022-10-28T19:08:39Z
|
|
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,65 @@ 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.16.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.16.0) (2022-10-28)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- update TS docstring generator ([5a060b6](https://github.com/thi-ng/umbrella/commit/5a060b6))
|
|
17
|
+
- include native/WASM type in docstring (use Zig type sigs)
|
|
18
|
+
- update ensureLines() helper
|
|
19
|
+
- update test fixtures
|
|
20
|
+
- update IWasmMemoryAccess & impls ([bb8a3ca](https://github.com/thi-ng/umbrella/commit/bb8a3ca))
|
|
21
|
+
- add MemorySlice tuple to describe a memory region
|
|
22
|
+
- update allocate() & free() to use MemorySlice, migrate to IWasmMemoryAccess
|
|
23
|
+
- migrate growMemory() to IWasmMemoryAccess
|
|
24
|
+
- add MemorySlice to default imports in TS codegen
|
|
25
|
+
- update docstring & user code codegen config ([68694ba](https://github.com/thi-ng/umbrella/commit/68694ba))
|
|
26
|
+
- allow doc strings & user codes to be given as string array (lines)
|
|
27
|
+
- add InjectedBody type to support multiple injection sites
|
|
28
|
+
(e.g. TS interface declarations vs. wrapper impls)
|
|
29
|
+
- update TS & Zig codegens
|
|
30
|
+
- add internal ensureLines() helper
|
|
31
|
+
|
|
32
|
+
#### 🩹 Bug fixes
|
|
33
|
+
|
|
34
|
+
- fix ensureLines(), update tests & fixtures ([f8a4668](https://github.com/thi-ng/umbrella/commit/f8a4668))
|
|
35
|
+
|
|
36
|
+
## [0.15.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.15.0) (2022-10-26)
|
|
37
|
+
|
|
38
|
+
#### 🚀 Features
|
|
39
|
+
|
|
40
|
+
- import ManagedIndex, migrate Zig API ([d8bb3ee](https://github.com/thi-ng/umbrella/commit/d8bb3ee))
|
|
41
|
+
- migrate ManagedIndex from [@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/tree/main/packages/wasm-api-dom)
|
|
42
|
+
- move all Zig sources from /include => /zig
|
|
43
|
+
- update pkg
|
|
44
|
+
- add codegen support for union types ([bbc1f98](https://github.com/thi-ng/umbrella/commit/bbc1f98))
|
|
45
|
+
- add Union type, update TopLevelType
|
|
46
|
+
- update all codegens (C,TS,Zig)
|
|
47
|
+
- update alignOf(), sizeOf()
|
|
48
|
+
- update selectAlignment()
|
|
49
|
+
- add/update codegen alignment logic ([9c19ad9](https://github.com/thi-ng/umbrella/commit/9c19ad9))
|
|
50
|
+
- add AlignmentStrategy & impls
|
|
51
|
+
- update alignOf(), sizeOf(), prepareType()
|
|
52
|
+
- extract DEFAULT_CODEGEN_OPTS
|
|
53
|
+
- add Struct.align config option
|
|
54
|
+
- add WasmTarget codegen opt & usize support ([62c049b](https://github.com/thi-ng/umbrella/commit/62c049b))
|
|
55
|
+
|
|
56
|
+
#### 🩹 Bug fixes
|
|
57
|
+
|
|
58
|
+
- update CLI wrapper to allow unions ([904716c](https://github.com/thi-ng/umbrella/commit/904716c))
|
|
59
|
+
- fix i64/u64 handling in sizeof() ([825add3](https://github.com/thi-ng/umbrella/commit/825add3))
|
|
60
|
+
- fix padding in Zig packed structs ([8d70cf6](https://github.com/thi-ng/umbrella/commit/8d70cf6))
|
|
61
|
+
- since packed structs can't contain `[n]u8` types,
|
|
62
|
+
generate padding as potentially multiple `uXXX` fields
|
|
63
|
+
|
|
64
|
+
#### ♻️ Refactoring
|
|
65
|
+
|
|
66
|
+
- rename types, use predicates ([4148e1e](https://github.com/thi-ng/umbrella/commit/4148e1e))
|
|
67
|
+
- rename StructField => Field
|
|
68
|
+
- update codegens to use more predicates instead of inline checks
|
|
69
|
+
- minor updates C & Zig codegens ([a94e1cc](https://github.com/thi-ng/umbrella/commit/a94e1cc))
|
|
70
|
+
|
|
12
71
|
## [0.14.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.14.0) (2022-10-17)
|
|
13
72
|
|
|
14
73
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -45,24 +45,27 @@ applications.
|
|
|
45
45
|
Additionally, a number of support modules for [DOM
|
|
46
46
|
manipulation](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom/),
|
|
47
47
|
WebGL, WebGPU, WebAudio etc. is being actively worked on.
|
|
48
|
-
3.
|
|
49
|
-
|
|
50
|
-
|
|
48
|
+
3. Include files for
|
|
49
|
+
[C11/C++](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/include)
|
|
50
|
+
and
|
|
51
|
+
[Zig](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/zig),
|
|
52
|
+
defining glue code for the JS [core
|
|
51
53
|
API](https://docs.thi.ng/umbrella/wasm-api/interfaces/CoreAPI.html) defined
|
|
52
54
|
by this package
|
|
53
|
-
4. Extensible shared datatype code generators for (currently) C11,
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
55
|
+
4. Extensible shared datatype code generators for (currently) C11, Zig &
|
|
56
|
+
TypeScript. The latter also generates fully type checked memory-mapped
|
|
57
|
+
(zero-copy) accessors of WASM-side data. In general, all languages with a
|
|
58
|
+
WebAssembly target are supported, however currently only bindings for these
|
|
59
|
+
mentioned langs are included. Other languages require custom bindings, e.g.
|
|
60
|
+
based on the flexible primitives provided here.
|
|
58
61
|
5. [CLI frontend/utility](#cli-generator) to invoke the code generator(s)
|
|
59
62
|
|
|
60
63
|
### Data bindings & code generators
|
|
61
64
|
|
|
62
65
|
The package provides an extensible codegeneration framework to simplify the
|
|
63
66
|
bilateral design & exchange of data structures shared between the WASM & JS host
|
|
64
|
-
env. Currently, code generators for TypeScript
|
|
65
|
-
|
|
67
|
+
env. Currently, code generators for TypeScript, Zig and C11 are supplied. A CLI
|
|
68
|
+
wrapper is available too. See the
|
|
66
69
|
[@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api-dom/)
|
|
67
70
|
support package for a more thorough realworld example...
|
|
68
71
|
|
|
@@ -78,7 +81,7 @@ $ npx @thi.ng/wasm-api
|
|
|
78
81
|
|
|
79
82
|
█ █ █ │
|
|
80
83
|
██ █ │
|
|
81
|
-
█ █ █ █ █ █ █ █ │ @thi.ng/wasm-api 0.
|
|
84
|
+
█ █ █ █ █ █ █ █ │ @thi.ng/wasm-api 0.15.0
|
|
82
85
|
█ █ █ █ █ █ █ █ █ │ Multi-language data bindings code generator
|
|
83
86
|
█ │
|
|
84
87
|
█ █ │
|
|
@@ -88,7 +91,7 @@ usage: wasm-api [OPTS] JSON-INPUT-FILE(S) ...
|
|
|
88
91
|
|
|
89
92
|
Flags:
|
|
90
93
|
|
|
91
|
-
-d, --debug enable debug output
|
|
94
|
+
-d, --debug enable debug output & functions
|
|
92
95
|
--dry-run enable dry run (don't overwrite files)
|
|
93
96
|
|
|
94
97
|
Main:
|
|
@@ -102,8 +105,8 @@ Main:
|
|
|
102
105
|
|
|
103
106
|
By default, the CLI generates sources for TypeScript and Zig (in this order!).
|
|
104
107
|
Order is important, since the output file paths must be given in the same order
|
|
105
|
-
as the target languages. It's recommended to be
|
|
106
|
-
|
|
108
|
+
as the target languages. It's recommended to be explicit with this. An example
|
|
109
|
+
invocation looks like:
|
|
107
110
|
|
|
108
111
|
```bash
|
|
109
112
|
wasm-api \
|
|
@@ -145,53 +148,62 @@ external files by specifying their file paths using `@` as prefix, e.g.
|
|
|
145
148
|
|
|
146
149
|
#### Data type definitions
|
|
147
150
|
|
|
148
|
-
Currently, the code generator supports structs and
|
|
151
|
+
Currently, the code generator supports enums, structs and unions. See API docs for
|
|
149
152
|
further details:
|
|
150
153
|
|
|
151
154
|
- [`Enum`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Enum.html)
|
|
152
|
-
- [`EnumValue`](https://docs.thi.ng/umbrella/wasm-api/interfaces/EnumValue.html)
|
|
155
|
+
- [`EnumValue`](https://docs.thi.ng/umbrella/wasm-api/interfaces/EnumValue.html) (individual enum value spec)
|
|
156
|
+
- [`Field`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Field.html) (individual spec for values contained in structs/unions)
|
|
153
157
|
- [`Struct`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Struct.html)
|
|
154
|
-
- [`
|
|
158
|
+
- [`Union`](https://docs.thi.ng/umbrella/wasm-api/interfaces/Union.html)
|
|
155
159
|
- [`TopLevelType`](https://docs.thi.ng/umbrella/wasm-api/interfaces/TopLevelType.html)
|
|
156
160
|
|
|
157
161
|
#### Example usage
|
|
158
162
|
|
|
159
|
-
|
|
160
|
-
codes:
|
|
163
|
+
The following example defines 1x enum, 2x structs and 1x union. Shown here are
|
|
164
|
+
the JSON type definitions and the resulting source codes:
|
|
161
165
|
|
|
162
166
|
**⬇︎ CLICK TO EXPAND EACH CODE BLOCK ⬇︎**
|
|
163
167
|
|
|
164
168
|
<details><summary>types.json (Type definitions)</summary>
|
|
165
169
|
|
|
166
|
-
```json
|
|
170
|
+
```json tangle:export/readme-types.json
|
|
167
171
|
[
|
|
168
172
|
{
|
|
169
|
-
"name": "
|
|
173
|
+
"name": "EventType",
|
|
174
|
+
"type": "enum",
|
|
175
|
+
"tag": "u8",
|
|
176
|
+
"values": [
|
|
177
|
+
"unknown",
|
|
178
|
+
{ "name": "mouse", "value": 16 },
|
|
179
|
+
{ "name": "key", "value": 32 }
|
|
180
|
+
]
|
|
181
|
+
},
|
|
182
|
+
{
|
|
183
|
+
"name": "MouseEvent",
|
|
170
184
|
"type": "struct",
|
|
171
185
|
"doc": "Example struct",
|
|
172
|
-
"auto": true,
|
|
173
186
|
"fields": [
|
|
174
|
-
{ "name": "
|
|
175
|
-
{ "name": "
|
|
176
|
-
{ "name": "color", "type": "f32", "tag": "vec", "len": 4 }
|
|
187
|
+
{ "name": "type", "type": "EventType" },
|
|
188
|
+
{ "name": "pos", "type": "u16", "tag": "vec", "len": 2 }
|
|
177
189
|
]
|
|
178
190
|
},
|
|
179
191
|
{
|
|
180
|
-
"name": "
|
|
192
|
+
"name": "KeyEvent",
|
|
181
193
|
"type": "struct",
|
|
194
|
+
"doc": "Example struct",
|
|
182
195
|
"fields": [
|
|
183
|
-
{ "name": "
|
|
184
|
-
{ "name": "
|
|
196
|
+
{ "name": "type", "type": "EventType" },
|
|
197
|
+
{ "name": "key", "type": "string" },
|
|
198
|
+
{ "name": "modifiers", "type": "u8", "doc": "Bitmask of modifier keys" }
|
|
185
199
|
]
|
|
186
200
|
},
|
|
187
201
|
{
|
|
188
|
-
"name": "
|
|
189
|
-
"type": "
|
|
190
|
-
"
|
|
191
|
-
|
|
192
|
-
"
|
|
193
|
-
{ "name": "good", "value": 100 },
|
|
194
|
-
{ "name": "best", "value": 1000 }
|
|
202
|
+
"name": "Event",
|
|
203
|
+
"type": "union",
|
|
204
|
+
"fields": [
|
|
205
|
+
{ "name": "mouse", "type": "MouseEvent" },
|
|
206
|
+
{ "name": "key", "type": "KeyEvent" }
|
|
195
207
|
]
|
|
196
208
|
}
|
|
197
209
|
]
|
|
@@ -202,71 +214,113 @@ codes:
|
|
|
202
214
|
|
|
203
215
|
```ts
|
|
204
216
|
/**
|
|
205
|
-
* Generated by @thi.ng/wasm-api at 2022-10-
|
|
217
|
+
* Generated by @thi.ng/wasm-api at 2022-10-26T08:36:16.825Z - DO NOT EDIT!
|
|
206
218
|
*/
|
|
207
219
|
|
|
208
220
|
// @ts-ignore possibly includes unused imports
|
|
209
221
|
import { Pointer, WasmStringSlice, WasmTypeBase, WasmTypeConstructor } from "@thi.ng/wasm-api";
|
|
210
222
|
|
|
223
|
+
export enum EventType {
|
|
224
|
+
UNKNOWN,
|
|
225
|
+
MOUSE = 16,
|
|
226
|
+
KEY = 32,
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Example struct
|
|
231
|
+
*/
|
|
232
|
+
export interface MouseEvent extends WasmTypeBase {
|
|
233
|
+
type: EventType;
|
|
234
|
+
pos: Uint16Array;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
export const $MouseEvent: WasmTypeConstructor<MouseEvent> = (mem) => ({
|
|
238
|
+
get align() {
|
|
239
|
+
return 4;
|
|
240
|
+
},
|
|
241
|
+
get size() {
|
|
242
|
+
return 8;
|
|
243
|
+
},
|
|
244
|
+
instance: (base) => {
|
|
245
|
+
return {
|
|
246
|
+
get __base() {
|
|
247
|
+
return base;
|
|
248
|
+
},
|
|
249
|
+
get __bytes() {
|
|
250
|
+
return mem.u8.subarray(base, base + 8);
|
|
251
|
+
},
|
|
252
|
+
get type(): EventType {
|
|
253
|
+
return mem.u8[base];
|
|
254
|
+
},
|
|
255
|
+
set type(x: EventType) {
|
|
256
|
+
mem.u8[base] = x;
|
|
257
|
+
},
|
|
258
|
+
get pos(): Uint16Array {
|
|
259
|
+
const addr = (base + 4) >>> 1;
|
|
260
|
+
return mem.u16.subarray(addr, addr + 2);
|
|
261
|
+
},
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
});
|
|
265
|
+
|
|
211
266
|
/**
|
|
212
267
|
* Example struct
|
|
213
268
|
*/
|
|
214
|
-
export interface
|
|
215
|
-
|
|
216
|
-
|
|
269
|
+
export interface KeyEvent extends WasmTypeBase {
|
|
270
|
+
type: EventType;
|
|
271
|
+
key: WasmStringSlice;
|
|
217
272
|
/**
|
|
218
|
-
*
|
|
273
|
+
* Bitmask of modifier keys
|
|
219
274
|
*/
|
|
220
|
-
|
|
275
|
+
modifiers: number;
|
|
221
276
|
}
|
|
222
277
|
|
|
223
|
-
export const $
|
|
278
|
+
export const $KeyEvent: WasmTypeConstructor<KeyEvent> = (mem) => ({
|
|
224
279
|
get align() {
|
|
225
|
-
return
|
|
280
|
+
return 4;
|
|
226
281
|
},
|
|
227
282
|
get size() {
|
|
228
|
-
return
|
|
283
|
+
return 16;
|
|
229
284
|
},
|
|
230
285
|
instance: (base) => {
|
|
286
|
+
let $key: WasmStringSlice | null = null;
|
|
231
287
|
return {
|
|
232
288
|
get __base() {
|
|
233
289
|
return base;
|
|
234
290
|
},
|
|
235
291
|
get __bytes() {
|
|
236
|
-
return mem.u8.subarray(base, base +
|
|
292
|
+
return mem.u8.subarray(base, base + 16);
|
|
293
|
+
},
|
|
294
|
+
get type(): EventType {
|
|
295
|
+
return mem.u8[base];
|
|
237
296
|
},
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
return mem.f32.subarray(addr, addr + 4);
|
|
297
|
+
set type(x: EventType) {
|
|
298
|
+
mem.u8[base] = x;
|
|
241
299
|
},
|
|
242
|
-
get
|
|
243
|
-
|
|
244
|
-
const inst = $Bar(mem);
|
|
245
|
-
const slice: Bar[] = [];
|
|
246
|
-
for(let i = 0; i < 3; i++) slice.push(inst.instance(addr + i * 24));
|
|
247
|
-
return slice;
|
|
300
|
+
get key(): WasmStringSlice {
|
|
301
|
+
return $key || ($key = new WasmStringSlice(mem, (base + 4), true));
|
|
248
302
|
},
|
|
249
|
-
get
|
|
250
|
-
return mem.u8[(base +
|
|
303
|
+
get modifiers(): number {
|
|
304
|
+
return mem.u8[(base + 12)];
|
|
251
305
|
},
|
|
252
|
-
set
|
|
253
|
-
mem.u8[(base +
|
|
306
|
+
set modifiers(x: number) {
|
|
307
|
+
mem.u8[(base + 12)] = x;
|
|
254
308
|
},
|
|
255
309
|
};
|
|
256
310
|
}
|
|
257
311
|
});
|
|
258
312
|
|
|
259
|
-
export interface
|
|
260
|
-
|
|
261
|
-
|
|
313
|
+
export interface Event extends WasmTypeBase {
|
|
314
|
+
mouse: MouseEvent;
|
|
315
|
+
key: KeyEvent;
|
|
262
316
|
}
|
|
263
317
|
|
|
264
|
-
export const $
|
|
318
|
+
export const $Event: WasmTypeConstructor<Event> = (mem) => ({
|
|
265
319
|
get align() {
|
|
266
320
|
return 4;
|
|
267
321
|
},
|
|
268
322
|
get size() {
|
|
269
|
-
return
|
|
323
|
+
return 16;
|
|
270
324
|
},
|
|
271
325
|
instance: (base) => {
|
|
272
326
|
return {
|
|
@@ -274,91 +328,92 @@ export const $Bar: WasmTypeConstructor<Bar> = (mem) => ({
|
|
|
274
328
|
return base;
|
|
275
329
|
},
|
|
276
330
|
get __bytes() {
|
|
277
|
-
return mem.u8.subarray(base, base +
|
|
331
|
+
return mem.u8.subarray(base, base + 16);
|
|
278
332
|
},
|
|
279
|
-
get
|
|
280
|
-
return mem.
|
|
333
|
+
get mouse(): MouseEvent {
|
|
334
|
+
return $MouseEvent(mem).instance(base);
|
|
281
335
|
},
|
|
282
|
-
set
|
|
283
|
-
mem.
|
|
336
|
+
set mouse(x: MouseEvent) {
|
|
337
|
+
mem.u8.set(x.__bytes, base);
|
|
284
338
|
},
|
|
285
|
-
get
|
|
286
|
-
return mem.
|
|
339
|
+
get key(): KeyEvent {
|
|
340
|
+
return $KeyEvent(mem).instance(base);
|
|
287
341
|
},
|
|
288
|
-
set
|
|
289
|
-
mem.
|
|
342
|
+
set key(x: KeyEvent) {
|
|
343
|
+
mem.u8.set(x.__bytes, base);
|
|
290
344
|
},
|
|
291
345
|
};
|
|
292
346
|
}
|
|
293
347
|
});
|
|
294
|
-
|
|
295
|
-
export enum Kind {
|
|
296
|
-
UNKNOWN,
|
|
297
|
-
GOOD = 100,
|
|
298
|
-
BEST = 1000,
|
|
299
|
-
}
|
|
300
348
|
```
|
|
301
349
|
</details>
|
|
302
350
|
|
|
303
351
|
<details><summary>generated.zig (generated Zig source)</summary>
|
|
304
352
|
|
|
305
353
|
```zig
|
|
306
|
-
//! Generated by @thi.ng/wasm-api at 2022-10-
|
|
354
|
+
//! Generated by @thi.ng/wasm-api at 2022-10-26T08:36:16.827Z - DO NOT EDIT!
|
|
307
355
|
|
|
308
356
|
const std = @import("std");
|
|
309
357
|
|
|
358
|
+
pub const EventType = enum(u8) {
|
|
359
|
+
UNKNOWN,
|
|
360
|
+
MOUSE = 16,
|
|
361
|
+
KEY = 32,
|
|
362
|
+
};
|
|
363
|
+
|
|
310
364
|
/// Example struct
|
|
311
|
-
pub const
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
/// Unique ID
|
|
315
|
-
id: u8,
|
|
365
|
+
pub const MouseEvent = struct {
|
|
366
|
+
type: EventType,
|
|
367
|
+
pos: @Vector(2, u16),
|
|
316
368
|
};
|
|
317
369
|
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
370
|
+
/// Example struct
|
|
371
|
+
pub const KeyEvent = struct {
|
|
372
|
+
type: EventType,
|
|
373
|
+
key: []const u8,
|
|
374
|
+
/// Bitmask of modifier keys
|
|
375
|
+
modifiers: u8,
|
|
321
376
|
};
|
|
322
377
|
|
|
323
|
-
pub const
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
BEST = 1000,
|
|
378
|
+
pub const Event = union {
|
|
379
|
+
mouse: MouseEvent,
|
|
380
|
+
key: KeyEvent,
|
|
327
381
|
};
|
|
328
382
|
```
|
|
329
383
|
</details>
|
|
330
384
|
|
|
331
|
-
On the TypeScript/JS side, the memory-mapped wrappers (e.g. `$
|
|
385
|
+
On the TypeScript/JS side, the memory-mapped wrappers (e.g. `$Event`)
|
|
332
386
|
can be used in combination with the `WasmBridge` to obtain fully typed views
|
|
333
387
|
(according to the generated types) of the underlying WASM memory. Basic usage is
|
|
334
388
|
like:
|
|
335
389
|
|
|
336
390
|
```ts
|
|
337
391
|
import { WasmBridge } from "@thi.ng/wasm-api";
|
|
338
|
-
import { $
|
|
392
|
+
import { $Event, EventType } from "./generated.ts";
|
|
339
393
|
|
|
340
394
|
const bridge = new WasmBridge();
|
|
341
395
|
// bridge initialization omitted here (see other examples below)
|
|
342
396
|
// ...
|
|
343
397
|
|
|
344
398
|
// Create an instance using the bridge's memory views
|
|
345
|
-
// and mapping a `
|
|
399
|
+
// and mapping a `Event` union from given address
|
|
346
400
|
// (e.g. obtained from an exported WASM function/value)
|
|
347
|
-
const
|
|
401
|
+
const event = $Event(bridge).instance(0x10000);
|
|
348
402
|
|
|
349
403
|
// then use like normal JS object
|
|
350
|
-
|
|
351
|
-
//
|
|
352
|
-
|
|
353
|
-
// this even applies to arrays using other types
|
|
354
|
-
foo.bars[2].kind = Kind.BEST;
|
|
404
|
+
event.mouse.pos
|
|
405
|
+
// Uint16Array(2) [100, 200]
|
|
355
406
|
|
|
356
407
|
// IMPORTANT: any modifications like this are directly
|
|
357
408
|
// applied to the underlying WASM memory...
|
|
409
|
+
event.mouse.pos[0] = 300;
|
|
410
|
+
|
|
411
|
+
event.mouse.type === EventType.MOUSE
|
|
412
|
+
// true
|
|
358
413
|
```
|
|
359
414
|
|
|
360
|
-
**IMPORTANT:**
|
|
361
|
-
|
|
415
|
+
**IMPORTANT:** Field setters are currently only supported for single values,
|
|
416
|
+
incl. enums, strings, structs, unions. The latter 2 will always be copied by
|
|
362
417
|
value (mem copy). Arrays or slices of strings do not currently provide write
|
|
363
418
|
access...
|
|
364
419
|
|
|
@@ -395,9 +450,9 @@ The actual allocator is implementation specific and suitable generic mechanisms
|
|
|
395
450
|
are defined for both the included Zig & C bindings. Please see for further
|
|
396
451
|
reference:
|
|
397
452
|
|
|
398
|
-
- [`/
|
|
453
|
+
- [`/zig/wasmapi.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/wasmapi.zig#L64):
|
|
399
454
|
comments about WASM-side allocator handling in Zig
|
|
400
|
-
- [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#
|
|
455
|
+
- [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#L18):
|
|
401
456
|
comments about WASM-side allocator handling in C/C++
|
|
402
457
|
- [`WasmBridge.allocate()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#allocate):
|
|
403
458
|
allocating memory from JS side
|
|
@@ -490,6 +545,8 @@ API like so (Please also see
|
|
|
490
545
|
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
491
546
|
& other examples in this readme):
|
|
492
547
|
|
|
548
|
+
Bindings file / lib:
|
|
549
|
+
|
|
493
550
|
```zig
|
|
494
551
|
//! custom.zig - extern definitions of custom JS API
|
|
495
552
|
|
|
@@ -498,9 +555,11 @@ API like so (Please also see
|
|
|
498
555
|
/// Here we declare an external binding belonging to the "custom" import group
|
|
499
556
|
///
|
|
500
557
|
/// The bridge core API uses "wasmapi" as reserved import group name
|
|
501
|
-
extern "custom" fn fillRandom(addr:
|
|
558
|
+
extern "custom" fn fillRandom(addr: [*]f32, num: usize) void;
|
|
502
559
|
```
|
|
503
560
|
|
|
561
|
+
Main Zig file:
|
|
562
|
+
|
|
504
563
|
```zig
|
|
505
564
|
// Import JS core API
|
|
506
565
|
const js = @import("wasmapi");
|
|
@@ -513,7 +572,7 @@ export fn test_randomVec4() void {
|
|
|
513
572
|
js.printF32Array(foo[0..]);
|
|
514
573
|
|
|
515
574
|
// populate foo with random numbers
|
|
516
|
-
custom.fillRandom(
|
|
575
|
+
custom.fillRandom(&foo, foo.len);
|
|
517
576
|
|
|
518
577
|
// print result
|
|
519
578
|
js.printF32Array(foo[0..]);
|
|
@@ -567,6 +626,12 @@ canvases.delete(0);
|
|
|
567
626
|
// true
|
|
568
627
|
```
|
|
569
628
|
|
|
629
|
+
Since v0.15.0, the supplied Zig core bindings lib also includes a
|
|
630
|
+
[`ManagedIndex`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/zig/managed-index.zig)
|
|
631
|
+
for similar dealings on the Zig side of the application. For example, in the
|
|
632
|
+
[@thi.ng/wasm-api-dom](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api-dom/)
|
|
633
|
+
module this is used to manage Zig event listeners.
|
|
634
|
+
|
|
570
635
|
## Status
|
|
571
636
|
|
|
572
637
|
**ALPHA** - bleeding edge / work-in-progress
|
|
@@ -600,7 +665,7 @@ node --experimental-repl-await
|
|
|
600
665
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
601
666
|
```
|
|
602
667
|
|
|
603
|
-
Package sizes (gzipped, pre-treeshake): ESM: 6.
|
|
668
|
+
Package sizes (gzipped, pre-treeshake): ESM: 6.84 KB
|
|
604
669
|
|
|
605
670
|
**IMPORTANT:** The package includes code generators for various languages which
|
|
606
671
|
are **not** required for just using the API bridge. Hence, the usual package
|
|
@@ -691,7 +756,7 @@ folder):
|
|
|
691
756
|
```bash
|
|
692
757
|
# compile WASM binary
|
|
693
758
|
zig build-lib \
|
|
694
|
-
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/
|
|
759
|
+
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/zig/wasmapi.zig --pkg-end \
|
|
695
760
|
-target wasm32-freestanding \
|
|
696
761
|
-O ReleaseSmall -dynamic --strip \
|
|
697
762
|
hello.zig
|