@thi.ng/wasm-api 0.10.0 → 0.11.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 +48 -1
- package/README.md +146 -120
- package/api.d.ts +76 -11
- package/bridge.d.ts +11 -1
- package/bridge.js +13 -8
- package/cli.js +27 -6
- package/codegen/c.d.ts +33 -0
- package/codegen/c.js +100 -0
- package/codegen/c11.d.ts +22 -0
- package/codegen/c11.js +125 -0
- package/codegen/typescript.d.ts +2 -12
- package/codegen/typescript.js +120 -84
- package/codegen/utils.d.ts +48 -1
- package/codegen/utils.js +55 -0
- package/codegen/zig.d.ts +2 -9
- package/codegen/zig.js +54 -28
- package/codegen.d.ts +1 -23
- package/codegen.js +31 -8
- package/doc/assets/main.js +52 -0
- package/doc/assets/search.js +1 -0
- package/include/wasmapi.h +34 -20
- package/include/wasmapi.zig +53 -46
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +31 -19
- package/pointer.d.ts +23 -0
- package/pointer.js +27 -0
- package/string.d.ts +97 -0
- package/string.js +145 -0
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-
|
|
3
|
+
- **Last updated**: 2022-09-21T21:37:59Z
|
|
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,53 @@ 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.11.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.11.0) (2022-09-21)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- refactor CodeGenOpts, update CLI config handling ([fa2e30f](https://github.com/thi-ng/umbrella/commit/fa2e30f))
|
|
17
|
+
- extract CodeGenOptsBase interface for common pre/post config
|
|
18
|
+
- update CLI config loading to support loading pre/post contents
|
|
19
|
+
from separate files (if value given as `@path/to-file.ext`)
|
|
20
|
+
- update/fix codegens & config ([87def23](https://github.com/thi-ng/umbrella/commit/87def23))
|
|
21
|
+
- add global `CodeGenOpts.uppercaseEnums` option (migrate from TSOpts)
|
|
22
|
+
- set `i32` as enum default tag (for C compatibility)
|
|
23
|
+
- update C11 codegen, add `typePrefix` option
|
|
24
|
+
- fix C11 enum codegen (use `typedef enum`)
|
|
25
|
+
- add internal enumName() helper
|
|
26
|
+
- minor updates Zig codegen
|
|
27
|
+
- update codegen CLI ([435ad2b](https://github.com/thi-ng/umbrella/commit/435ad2b))
|
|
28
|
+
- add C11 support
|
|
29
|
+
- add support for padding fields (in `validateTypeRefs()`)
|
|
30
|
+
- add CLI arg for forcing string type impl
|
|
31
|
+
- update deps
|
|
32
|
+
- add C11 support, update codegens & config ([590311e](https://github.com/thi-ng/umbrella/commit/590311e))
|
|
33
|
+
- add preliminary C11 codegen
|
|
34
|
+
- add optional `tag` field for structs (extern/packed)
|
|
35
|
+
- add support for auto-labeled padding fields
|
|
36
|
+
- i.e. defined via unnamed fields with `pad` value
|
|
37
|
+
- move debug config option to global `CodeGenOpts`
|
|
38
|
+
- update TS codegen (array pointers) ([30360f2](https://github.com/thi-ng/umbrella/commit/30360f2))
|
|
39
|
+
- update CodeGenOpts ([b9fd7ff](https://github.com/thi-ng/umbrella/commit/b9fd7ff))
|
|
40
|
+
- major update codegens, string & pointer handling ([4f02295](https://github.com/thi-ng/umbrella/commit/4f02295))
|
|
41
|
+
- update/fix string wrappers (`WasmStringSlice/Ptr`)
|
|
42
|
+
- add generic `Pointer` wrapper
|
|
43
|
+
- add `const` field type support
|
|
44
|
+
- add applyIndents() formatter fn, simplify all codegens
|
|
45
|
+
- major update TS & Zig codegens
|
|
46
|
+
- WasmString codegen integration ([97fc318](https://github.com/thi-ng/umbrella/commit/97fc318))
|
|
47
|
+
- update TS codegen to wrap strings using new `WasmString` or `WasmStringPtr`
|
|
48
|
+
- update global codegen options handling, now passed to each codegen
|
|
49
|
+
- update TSOpts & ZigOpts to include custom pre/post
|
|
50
|
+
- add WasmString wrappers ([18d8fcb](https://github.com/thi-ng/umbrella/commit/18d8fcb))
|
|
51
|
+
- still unused (to be added to TS codegen)
|
|
52
|
+
- update core API & bindings ([b185ea5](https://github.com/thi-ng/umbrella/commit/b185ea5))
|
|
53
|
+
- add _panic(), timer(), epoch() core API fns
|
|
54
|
+
- update printI/U64() fns to accept bigint
|
|
55
|
+
- update C & Zig bindings
|
|
56
|
+
- update tests
|
|
57
|
+
- add clang-format
|
|
58
|
+
|
|
12
59
|
## [0.10.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.10.0) (2022-08-24)
|
|
13
60
|
|
|
14
61
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
<!-- This file is generated - DO NOT EDIT! -->
|
|
2
2
|
|
|
3
|
-
# 
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@thi.ng/wasm-api)
|
|
6
6
|

|
|
@@ -10,15 +10,15 @@ 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
|
-
- [Custom API modules](#custom-api-modules)
|
|
14
13
|
- [Data bindings & code generators](#data-bindings--code-generators)
|
|
15
14
|
- [CLI generator](#cli-generator)
|
|
16
15
|
- [Data type definitions](#data-type-definitions)
|
|
17
16
|
- [Example usage](#example-usage)
|
|
18
17
|
- [String handling](#string-handling)
|
|
19
18
|
- [Memory allocations](#memory-allocations)
|
|
19
|
+
- [Custom API modules](#custom-api-modules)
|
|
20
20
|
- [Object indices & handles](#object-indices--handles)
|
|
21
|
-
|
|
21
|
+
- [Status](#status)
|
|
22
22
|
- [Installation](#installation)
|
|
23
23
|
- [Dependencies](#dependencies)
|
|
24
24
|
- [API](#api)
|
|
@@ -30,7 +30,7 @@ This project is part of the
|
|
|
30
30
|
|
|
31
31
|
## About
|
|
32
32
|
|
|
33
|
-
Generic, modular, extensible API bridge, glue code and bindings code
|
|
33
|
+
Generic, modular, extensible API bridge, polyglot glue code and bindings code generators for hybrid JS & WebAssembly projects.
|
|
34
34
|
|
|
35
35
|
This package provides a the following:
|
|
36
36
|
|
|
@@ -38,117 +38,34 @@ This package provides a the following:
|
|
|
38
38
|
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
39
39
|
class as interop basis and much reduced boilerplate for hybrid JS/WebAssembly
|
|
40
40
|
applications.
|
|
41
|
-
2. A minimal core API for
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
41
|
+
2. A minimal core API for debug output, string/pointer/typedarray accessors for
|
|
42
|
+
8/16/32/64 bit (u)ints and 32/64 bit floats, memory allocation (optional). In
|
|
43
|
+
the future we aim to also supply support modules for DOM manipulation, WebGL,
|
|
44
|
+
WebGPU, WebAudio etc.
|
|
45
45
|
3. [Include files for C11/C++ and
|
|
46
46
|
Zig](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/include),
|
|
47
|
-
defining WASM imports of the JS [core
|
|
47
|
+
defining WASM imports of the extern JS [core
|
|
48
48
|
API](https://docs.thi.ng/umbrella/wasm-api/interfaces/CoreAPI.html) defined
|
|
49
49
|
by this package
|
|
50
|
-
4. Extensible shared datatype code generators for (currently)
|
|
51
|
-
[Zig](https://ziglang.org)
|
|
52
|
-
|
|
53
|
-
|
|
50
|
+
4. Extensible shared datatype code generators for (currently) C11, TypeScript &
|
|
51
|
+
[Zig](https://ziglang.org). The latter also generates fully type checked
|
|
52
|
+
memory-mapped (zero-copy) accessors of WASM-side data. In general, all
|
|
53
|
+
languages with a WebAssembly target are supported, however currently only
|
|
54
54
|
bindings for these few langs are included.
|
|
55
55
|
5. [CLI frontend/utility](#cli-generator) to invoke the code generator(s)
|
|
56
56
|
|
|
57
|
-
### Custom API modules
|
|
58
|
-
|
|
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)).
|
|
66
|
-
|
|
67
|
-
On the JS side, custom API modules can be easily integrated via the [`IWasmAPI`
|
|
68
|
-
interface](https://docs.thi.ng/umbrella/wasm-api/interfaces/IWasmAPI.html). The
|
|
69
|
-
following example provides a brief overview:
|
|
70
|
-
|
|
71
|
-
```ts
|
|
72
|
-
import { IWasmAPI, WasmBridge } from "@thi.ng/wasm-api";
|
|
73
|
-
|
|
74
|
-
export class CustomAPI implements IWasmAPI {
|
|
75
|
-
parent!: WasmBridge;
|
|
76
|
-
|
|
77
|
-
async init(parent: WasmBridge) {
|
|
78
|
-
this.parent = parent;
|
|
79
|
-
this.parent.logger.debug("initializing custom API");
|
|
80
|
-
|
|
81
|
-
// any other tasks you might need to do...
|
|
82
|
-
|
|
83
|
-
return true;
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* Returns object of functions to import as externals into
|
|
88
|
-
* the WASM module. These imports are merged into a larger
|
|
89
|
-
* imports object alongside the bridge's core API...
|
|
90
|
-
*/
|
|
91
|
-
getImports(): WebAssembly.Imports {
|
|
92
|
-
return {
|
|
93
|
-
/**
|
|
94
|
-
* Writes 2 random float32 numbers to given address
|
|
95
|
-
*/
|
|
96
|
-
randomVec2: (addr: number) => {
|
|
97
|
-
this.parent.f32.set(
|
|
98
|
-
[Math.random(), Math.random()],
|
|
99
|
-
addr >> 2
|
|
100
|
-
);
|
|
101
|
-
}
|
|
102
|
-
};
|
|
103
|
-
}
|
|
104
|
-
}
|
|
105
|
-
```
|
|
106
|
-
|
|
107
|
-
Now we can supply this custom API when creating the main WASM bridge:
|
|
108
|
-
|
|
109
|
-
```ts
|
|
110
|
-
export const bridge = new WasmBridge({ custom: new CustomAPI() });
|
|
111
|
-
```
|
|
112
|
-
|
|
113
|
-
In Zig (or any other language of your choice) we can then utilize this custom
|
|
114
|
-
API like so (Please also see /test/index.ts` & the example further below in this
|
|
115
|
-
readme):
|
|
116
|
-
|
|
117
|
-
```zig
|
|
118
|
-
// Import JS core API
|
|
119
|
-
const js = @import("wasmapi");
|
|
120
|
-
|
|
121
|
-
/// JS external to fill vec2 w/ random values
|
|
122
|
-
/// Note: Each API module uses a separate import object to avoid naming clashes
|
|
123
|
-
/// Here we declare an external binding belonging to the "custom" import group
|
|
124
|
-
extern "custom" fn randomVec2(addr: usize) void;
|
|
125
|
-
|
|
126
|
-
export fn test_randomVec2() void {
|
|
127
|
-
var foo = [2]f32{ 0, 0 };
|
|
128
|
-
|
|
129
|
-
// print original
|
|
130
|
-
js.printF32Array(foo[0..]);
|
|
131
|
-
|
|
132
|
-
// populate foo with random numbers
|
|
133
|
-
randomVec2(@ptrToInt(&foo));
|
|
134
|
-
|
|
135
|
-
// print result
|
|
136
|
-
js.printF32Array(foo[0..]);
|
|
137
|
-
}
|
|
138
|
-
```
|
|
139
|
-
|
|
140
57
|
### Data bindings & code generators
|
|
141
58
|
|
|
142
59
|
The package provides an extensible codegeneration framework to simplify the
|
|
143
60
|
bilateral design & exchange of data structures shared between the WASM & JS host
|
|
144
61
|
env. Currently, code generators for TypeScript & Zig are supplied (more are
|
|
145
|
-
planned). A CLI wrapper is
|
|
62
|
+
planned). A CLI wrapper is available too.
|
|
146
63
|
|
|
147
64
|
#### CLI generator
|
|
148
65
|
|
|
149
66
|
The package includes a [small CLI
|
|
150
67
|
wrapper](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/src/cli.ts)
|
|
151
|
-
to invoke the
|
|
68
|
+
to invoke the code generator(s) from JSON type definitions and to write the
|
|
152
69
|
generated source code(s) to different files:
|
|
153
70
|
|
|
154
71
|
```text
|
|
@@ -156,7 +73,7 @@ $ npx @thi.ng/wasm-api
|
|
|
156
73
|
|
|
157
74
|
█ █ █ │
|
|
158
75
|
██ █ │
|
|
159
|
-
█ █ █ █ █ █ █ █ │ @thi.ng/wasm-api 0.
|
|
76
|
+
█ █ █ █ █ █ █ █ │ @thi.ng/wasm-api 0.11.0
|
|
160
77
|
█ █ █ █ █ █ █ █ █ │ Multi-language data bindings code generator
|
|
161
78
|
█ │
|
|
162
79
|
█ █ │
|
|
@@ -172,17 +89,19 @@ Flags:
|
|
|
172
89
|
Main:
|
|
173
90
|
|
|
174
91
|
-c FILE, --config FILE JSON config file with codegen options
|
|
175
|
-
-l ID[,..], --lang ID[,..] [multiple] target language: "ts", "zig" (default: ["ts","zig"])
|
|
92
|
+
-l ID[,..], --lang ID[,..] [multiple] target language: "c11", "ts", "zig" (default: ["ts","zig"])
|
|
176
93
|
-o FILE, --out FILE [multiple] output file path
|
|
94
|
+
-s TYPE, --string TYPE Force string type implementation: "slice", "ptr"
|
|
177
95
|
```
|
|
178
96
|
|
|
179
|
-
By default, the CLI generates sources for
|
|
180
|
-
|
|
181
|
-
|
|
97
|
+
By default, the CLI generates sources for TypeScript and Zig (in this order!).
|
|
98
|
+
Order is important, since the output file paths must be given in the same order
|
|
99
|
+
as the target languages. It's recommended to be more explicit with this. An
|
|
182
100
|
example invocation looks like:
|
|
183
101
|
|
|
184
102
|
```bash
|
|
185
|
-
wasm-api
|
|
103
|
+
wasm-api \
|
|
104
|
+
--config codegen-opts.json \
|
|
186
105
|
--lang ts -o src/generated.ts \
|
|
187
106
|
--lang zig -o src.zig/generated.zig \
|
|
188
107
|
typedefs.json
|
|
@@ -190,20 +109,34 @@ wasm-api --config codegen-opts.json \
|
|
|
190
109
|
|
|
191
110
|
The structure of the config file is as follows (all optional):
|
|
192
111
|
|
|
193
|
-
```
|
|
112
|
+
```json
|
|
194
113
|
{
|
|
195
|
-
global: { ... },
|
|
196
|
-
|
|
197
|
-
|
|
114
|
+
"global": { ... },
|
|
115
|
+
"c11": { ... },
|
|
116
|
+
"ts": { ... },
|
|
117
|
+
"zig": { ... },
|
|
198
118
|
}
|
|
199
119
|
```
|
|
200
120
|
|
|
201
121
|
More details about possible
|
|
202
122
|
[`global`](https://docs.thi.ng/umbrella/wasm-api/interfaces/CodeGenOpts.html),
|
|
123
|
+
[`c`](https://docs.thi.ng/umbrella/wasm-api/interfaces/C11Opts.html) and
|
|
203
124
|
[`ts`](https://docs.thi.ng/umbrella/wasm-api/interfaces/TSOpts.html) and
|
|
204
125
|
[`zig`](https://docs.thi.ng/umbrella/wasm-api/interfaces/ZigOpts.html) config
|
|
205
126
|
options & values.
|
|
206
127
|
|
|
128
|
+
All code generators have support for custom code prologues & epilogues which can
|
|
129
|
+
be specified via the above options. These config options exist for both non-CLI
|
|
130
|
+
& CLI usage. For the latter, these custom code sections can also be loaded from
|
|
131
|
+
external files by specifying their file paths using `@` as prefix, e.g.
|
|
132
|
+
|
|
133
|
+
```json
|
|
134
|
+
{
|
|
135
|
+
"ts": { "pre": "@tpl/prelude.ts" },
|
|
136
|
+
"zig": { "pre": "@tpl/prelude.zig", "post": "@tpl/epilogue.zig" },
|
|
137
|
+
}
|
|
138
|
+
```
|
|
139
|
+
|
|
207
140
|
#### Data type definitions
|
|
208
141
|
|
|
209
142
|
Currently, the code generator supports structs and enums. See API docs for
|
|
@@ -439,7 +372,9 @@ reference:
|
|
|
439
372
|
Note: The provided Zig mechanism supports the idiomatic (Zig) pattern of working
|
|
440
373
|
with multiple allocators in different parts of the application and supports
|
|
441
374
|
dynamic assignments/swapping of the exposed allocator. See comments in source
|
|
442
|
-
file
|
|
375
|
+
file and
|
|
376
|
+
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
377
|
+
for more details...
|
|
443
378
|
|
|
444
379
|
```ts
|
|
445
380
|
try {
|
|
@@ -461,6 +396,95 @@ try {
|
|
|
461
396
|
}
|
|
462
397
|
```
|
|
463
398
|
|
|
399
|
+
### Custom API modules
|
|
400
|
+
|
|
401
|
+
The
|
|
402
|
+
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
403
|
+
can be extented via custom defined API modules. Such API extensions will consist
|
|
404
|
+
of a collection of JS/TS functions & variables, their related counterparts
|
|
405
|
+
(import definitions) for the WASM target and (optionally) some shared data types
|
|
406
|
+
([bindings for which _can_ be generated by this package
|
|
407
|
+
too](#data-bindings--code-generators)).
|
|
408
|
+
|
|
409
|
+
On the JS side, custom API modules can be easily integrated via the [`IWasmAPI`
|
|
410
|
+
interface](https://docs.thi.ng/umbrella/wasm-api/interfaces/IWasmAPI.html). The
|
|
411
|
+
following example provides a brief overview:
|
|
412
|
+
|
|
413
|
+
```ts
|
|
414
|
+
import { IWasmAPI, WasmBridge } from "@thi.ng/wasm-api";
|
|
415
|
+
|
|
416
|
+
export class CustomAPI implements IWasmAPI {
|
|
417
|
+
parent!: WasmBridge;
|
|
418
|
+
|
|
419
|
+
async init(parent: WasmBridge) {
|
|
420
|
+
this.parent = parent;
|
|
421
|
+
this.parent.logger.debug("initializing custom API");
|
|
422
|
+
|
|
423
|
+
// any other tasks you might need to do...
|
|
424
|
+
|
|
425
|
+
return true;
|
|
426
|
+
}
|
|
427
|
+
|
|
428
|
+
/**
|
|
429
|
+
* Returns object of functions to import as externals into the
|
|
430
|
+
* WASM module during instantiation. These imports are merged
|
|
431
|
+
* into a larger imports object alongside the bridge's core API...
|
|
432
|
+
*/
|
|
433
|
+
getImports(): WebAssembly.Imports {
|
|
434
|
+
return {
|
|
435
|
+
/**
|
|
436
|
+
* Writes `num` random float32 numbers from given address
|
|
437
|
+
*/
|
|
438
|
+
fillRandom: (addr: number, num: number) => {
|
|
439
|
+
addr >>>= 2;
|
|
440
|
+
while(num-- > 0) this.parent.f32[addr++] = Math.random();
|
|
441
|
+
}
|
|
442
|
+
};
|
|
443
|
+
}
|
|
444
|
+
}
|
|
445
|
+
```
|
|
446
|
+
|
|
447
|
+
Now we can supply this custom API when creating the main WASM bridge:
|
|
448
|
+
|
|
449
|
+
```ts
|
|
450
|
+
export const bridge = new WasmBridge({ custom: new CustomAPI() });
|
|
451
|
+
```
|
|
452
|
+
|
|
453
|
+
In Zig (or any other language of your choice) we can then utilize this custom
|
|
454
|
+
API like so (Please also see
|
|
455
|
+
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
456
|
+
& other examples in this readme):
|
|
457
|
+
|
|
458
|
+
```zig
|
|
459
|
+
//! custom.zig - extern definitions of custom JS API
|
|
460
|
+
|
|
461
|
+
/// JS external to fill a slice w/ random values
|
|
462
|
+
/// Note: Each API module uses a separate import object to avoid naming clashes
|
|
463
|
+
/// Here we declare an external binding belonging to the "custom" import group
|
|
464
|
+
///
|
|
465
|
+
/// The bridge core API uses "wasmapi" as reserved import group name
|
|
466
|
+
extern "custom" fn fillRandom(addr: usize, num: usize) void;
|
|
467
|
+
```
|
|
468
|
+
|
|
469
|
+
```zig
|
|
470
|
+
// Import JS core API
|
|
471
|
+
const js = @import("wasmapi");
|
|
472
|
+
const custom = @import("custom.zig");
|
|
473
|
+
|
|
474
|
+
export fn test_randomVec4() void {
|
|
475
|
+
var foo = [4]f32{ 1, 2, 3, 4 };
|
|
476
|
+
|
|
477
|
+
// print original
|
|
478
|
+
js.printF32Array(foo[0..]);
|
|
479
|
+
|
|
480
|
+
// populate foo with random numbers
|
|
481
|
+
custom.fillRandom(@ptrToInt(&foo), foo.len);
|
|
482
|
+
|
|
483
|
+
// print result
|
|
484
|
+
js.printF32Array(foo[0..]);
|
|
485
|
+
}
|
|
486
|
+
```
|
|
487
|
+
|
|
464
488
|
### Object indices & handles
|
|
465
489
|
|
|
466
490
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
@@ -508,7 +532,7 @@ canvases.delete(0);
|
|
|
508
532
|
// true
|
|
509
533
|
```
|
|
510
534
|
|
|
511
|
-
|
|
535
|
+
## Status
|
|
512
536
|
|
|
513
537
|
**ALPHA** - bleeding edge / work-in-progress
|
|
514
538
|
|
|
@@ -537,11 +561,11 @@ node --experimental-repl-await
|
|
|
537
561
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
538
562
|
```
|
|
539
563
|
|
|
540
|
-
Package sizes (gzipped, pre-treeshake): ESM:
|
|
564
|
+
Package sizes (gzipped, pre-treeshake): ESM: 6.05 KB
|
|
541
565
|
|
|
542
|
-
**IMPORTANT:** The package includes
|
|
543
|
-
|
|
544
|
-
in production will be MUCH smaller!
|
|
566
|
+
**IMPORTANT:** The package includes code generators for various languages which
|
|
567
|
+
are **not** required for just using the API bridge. Hence, the usual package
|
|
568
|
+
size in production will be MUCH smaller than what's stated here!
|
|
545
569
|
|
|
546
570
|
## Dependencies
|
|
547
571
|
|
|
@@ -556,6 +580,7 @@ in production will be MUCH smaller!
|
|
|
556
580
|
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
557
581
|
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
|
|
558
582
|
- [@thi.ng/logger](https://github.com/thi-ng/umbrella/tree/develop/packages/logger)
|
|
583
|
+
- [@thi.ng/paths](https://github.com/thi-ng/umbrella/tree/develop/packages/paths)
|
|
559
584
|
|
|
560
585
|
## API
|
|
561
586
|
|
|
@@ -631,16 +656,16 @@ The resulting WASM:
|
|
|
631
656
|
(type $none_=>_none (func))
|
|
632
657
|
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
633
658
|
(import "wasmapi" "_printStr" (func $fimport$0 (param i32 i32)))
|
|
634
|
-
(global $global$0 (mut i32) (i32.const
|
|
635
|
-
(memory $0
|
|
636
|
-
(data (i32.const
|
|
659
|
+
(global $global$0 (mut i32) (i32.const 1048576))
|
|
660
|
+
(memory $0 17)
|
|
661
|
+
(data (i32.const 1048576) "hello world!\00")
|
|
637
662
|
(export "memory" (memory $0))
|
|
638
663
|
(export "start" (func $0))
|
|
639
664
|
(export "_wasm_allocate" (func $1))
|
|
640
665
|
(export "_wasm_free" (func $2))
|
|
641
666
|
(func $0
|
|
642
667
|
(call $fimport$0
|
|
643
|
-
(i32.const
|
|
668
|
+
(i32.const 1048576)
|
|
644
669
|
(i32.const 12)
|
|
645
670
|
)
|
|
646
671
|
)
|
|
@@ -667,7 +692,7 @@ void WASM_KEEP start() {
|
|
|
667
692
|
Building the WASM module:
|
|
668
693
|
|
|
669
694
|
```bash
|
|
670
|
-
emcc -Os -Inode_modules/@thi.ng/wasm-api/include
|
|
695
|
+
emcc -Os -Inode_modules/@thi.ng/wasm-api/include \
|
|
671
696
|
-sERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry \
|
|
672
697
|
-o hello.wasm hello.c
|
|
673
698
|
```
|
|
@@ -680,6 +705,7 @@ Resulting WASM:
|
|
|
680
705
|
(type $none_=>_none (func))
|
|
681
706
|
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
682
707
|
(type $none_=>_i32 (func (result i32)))
|
|
708
|
+
(type $i32_i32_=>_none (func (param i32 i32)))
|
|
683
709
|
(import "wasmapi" "_printStr0" (func $fimport$0 (param i32)))
|
|
684
710
|
(global $global$0 (mut i32) (i32.const 5243936))
|
|
685
711
|
(memory $0 256 256)
|
|
@@ -702,7 +728,7 @@ Resulting WASM:
|
|
|
702
728
|
(func $1 (param $0 i32) (result i32)
|
|
703
729
|
(i32.const 0)
|
|
704
730
|
)
|
|
705
|
-
(func $2 (param $0 i32)
|
|
731
|
+
(func $2 (param $0 i32) (param $1 i32)
|
|
706
732
|
(nop)
|
|
707
733
|
)
|
|
708
734
|
(func $3
|
package/api.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { BigType, FloatType, Fn
|
|
1
|
+
import type { BigType, FloatType, Fn } from "@thi.ng/api";
|
|
2
2
|
import type { WasmBridge } from "./bridge.js";
|
|
3
3
|
export declare const PKG_NAME = "@thi.ng/wasm-api";
|
|
4
4
|
export declare const EVENT_MEMORY_CHANGED = "memory-changed";
|
|
@@ -132,9 +132,9 @@ export interface CoreAPI extends WebAssembly.ModuleImports {
|
|
|
132
132
|
printI32: Fn<number, void>;
|
|
133
133
|
printU32: Fn<number, void>;
|
|
134
134
|
printU32Hex: Fn<number, void>;
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
135
|
+
printI64: Fn<bigint, void>;
|
|
136
|
+
printU64: Fn<bigint, void>;
|
|
137
|
+
printU64Hex: Fn<bigint, void>;
|
|
138
138
|
printF32: Fn<number, void>;
|
|
139
139
|
printF64: Fn<number, void>;
|
|
140
140
|
_printI8Array: (addr: number, len: number) => void;
|
|
@@ -150,6 +150,9 @@ export interface CoreAPI extends WebAssembly.ModuleImports {
|
|
|
150
150
|
_printStr0: (addr: number) => void;
|
|
151
151
|
_printStr: (addr: number, len: number) => void;
|
|
152
152
|
debug: () => void;
|
|
153
|
+
_panic: (addr: number, len: number) => void;
|
|
154
|
+
timer: () => number;
|
|
155
|
+
epoch: () => bigint;
|
|
153
156
|
}
|
|
154
157
|
export interface WasmTypeBase {
|
|
155
158
|
/**
|
|
@@ -217,11 +220,18 @@ export interface Struct extends TopLevelType {
|
|
|
217
220
|
/**
|
|
218
221
|
* If true, struct fields will be re-ordered in descending order based on
|
|
219
222
|
* their {@link TypeInfo.__align} size. This might result in overall smaller
|
|
220
|
-
* structs due to minimizing inter-field padding
|
|
223
|
+
* structs due to minimizing implicit inter-field padding caused by
|
|
224
|
+
* alignment requirements. **If this option is enabled, then the struct MUST
|
|
225
|
+
* NOT contain any padding fields!**
|
|
221
226
|
*
|
|
222
227
|
* @defaultValue false
|
|
223
228
|
*/
|
|
224
229
|
auto?: boolean;
|
|
230
|
+
/**
|
|
231
|
+
* Optional qualifier for the kind of struct to be emitted (codegen specific
|
|
232
|
+
* interpretation, currently only used by {@link ZIG}).
|
|
233
|
+
*/
|
|
234
|
+
tag?: "extern" | "packed";
|
|
225
235
|
}
|
|
226
236
|
export interface StructField extends TypeInfo {
|
|
227
237
|
/**
|
|
@@ -260,6 +270,11 @@ export interface StructField extends TypeInfo {
|
|
|
260
270
|
* TODO `opaque` currently unsupported.
|
|
261
271
|
*/
|
|
262
272
|
type: WasmPrim | "string" | "opaque" | string;
|
|
273
|
+
/**
|
|
274
|
+
* Const qualifier (default is true for `string`, false for all other
|
|
275
|
+
* types). Only used for pointers or slices.
|
|
276
|
+
*/
|
|
277
|
+
const?: boolean;
|
|
263
278
|
/**
|
|
264
279
|
* TODO currently unsupported & ignored!
|
|
265
280
|
*/
|
|
@@ -272,11 +287,19 @@ export interface StructField extends TypeInfo {
|
|
|
272
287
|
* TODO currently unsupported & ignored!
|
|
273
288
|
*/
|
|
274
289
|
default?: any;
|
|
290
|
+
/**
|
|
291
|
+
* If defined and > 0, the field will be considered for padding purposes only and
|
|
292
|
+
* the value provided is the number of bytes used.
|
|
293
|
+
*/
|
|
294
|
+
pad?: number;
|
|
275
295
|
}
|
|
276
296
|
export interface Enum extends TopLevelType {
|
|
277
297
|
type: "enum";
|
|
278
298
|
/**
|
|
279
|
-
* No i64/u64 support, due to Typescript not supporting bigint enum values
|
|
299
|
+
* No i64/u64 support, due to Typescript not supporting bigint enum values.
|
|
300
|
+
* For C compatibility only i32 or u32 is allowed.
|
|
301
|
+
*
|
|
302
|
+
* @defaultValue "i32"
|
|
280
303
|
*/
|
|
281
304
|
tag: Exclude<WasmPrim32, FloatType>;
|
|
282
305
|
/**
|
|
@@ -299,27 +322,69 @@ export interface EnumValue {
|
|
|
299
322
|
*/
|
|
300
323
|
doc?: string;
|
|
301
324
|
}
|
|
325
|
+
export interface CodeGenOptsBase {
|
|
326
|
+
/**
|
|
327
|
+
* Optional string to be injected before generated type defs (but after
|
|
328
|
+
* codegen's own prelude, if any)
|
|
329
|
+
*/
|
|
330
|
+
pre: string;
|
|
331
|
+
/**
|
|
332
|
+
* Optional string to be injected after generated type defs (but before
|
|
333
|
+
* codegen's own epilogue, if any)
|
|
334
|
+
*/
|
|
335
|
+
post: string;
|
|
336
|
+
}
|
|
337
|
+
/**
|
|
338
|
+
* Global/shared code generator options.
|
|
339
|
+
*/
|
|
340
|
+
export interface CodeGenOpts extends CodeGenOptsBase {
|
|
341
|
+
/**
|
|
342
|
+
* Identifier how strings are stored on WASM side, e.g. in Zig string
|
|
343
|
+
* literals are slices (8 bytes), in C just plain pointers (4 bytes).
|
|
344
|
+
*
|
|
345
|
+
* @defaultValue "slice"
|
|
346
|
+
*/
|
|
347
|
+
stringType: "slice" | "ptr";
|
|
348
|
+
/**
|
|
349
|
+
* If true (default), forces uppercase enum identifiers
|
|
350
|
+
*
|
|
351
|
+
* @defaultValue true
|
|
352
|
+
*/
|
|
353
|
+
uppercaseEnums: boolean;
|
|
354
|
+
/**
|
|
355
|
+
* Unless set to false, the generated output will be prefixed with a header
|
|
356
|
+
* line comment of generator meta data
|
|
357
|
+
*/
|
|
358
|
+
header: boolean;
|
|
359
|
+
/**
|
|
360
|
+
* If true, codegens MAY generate various additional struct & struct field
|
|
361
|
+
* analysis functions (sizes, alignment, offsets etc.).
|
|
362
|
+
*
|
|
363
|
+
* @defaultValue false
|
|
364
|
+
*/
|
|
365
|
+
debug: boolean;
|
|
366
|
+
}
|
|
302
367
|
export interface ICodeGen {
|
|
303
368
|
/**
|
|
304
369
|
* Optional prelude source, to be prepended before any generated type defs.
|
|
305
370
|
*/
|
|
306
|
-
pre?: string
|
|
371
|
+
pre?: Fn<CodeGenOpts, string>;
|
|
307
372
|
/**
|
|
308
373
|
* Optional source code to be appended after any generated type defs.
|
|
309
374
|
*/
|
|
310
|
-
post?: string
|
|
375
|
+
post?: Fn<CodeGenOpts, string>;
|
|
311
376
|
/**
|
|
312
377
|
* Docstring codegen
|
|
313
378
|
*/
|
|
314
|
-
doc: (doc: string,
|
|
379
|
+
doc: (doc: string, acc: string[], topLevel?: boolean) => void;
|
|
315
380
|
/**
|
|
316
381
|
* Codegen for enum types.
|
|
317
382
|
*/
|
|
318
|
-
enum: (type: Enum, types: TypeColl, acc: string[]) => void;
|
|
383
|
+
enum: (type: Enum, types: TypeColl, acc: string[], opts: CodeGenOpts) => void;
|
|
319
384
|
/**
|
|
320
385
|
* Codegen for struct types.
|
|
321
386
|
*/
|
|
322
|
-
struct: (type: Struct, types: TypeColl, acc: string[]) => void;
|
|
387
|
+
struct: (type: Struct, types: TypeColl, acc: string[], opts: CodeGenOpts) => void;
|
|
323
388
|
}
|
|
324
389
|
/**
|
|
325
390
|
* WASM usize type. Assuming wasm32 until wasm64 surfaces, then need an option.
|
package/bridge.d.ts
CHANGED
|
@@ -1,7 +1,17 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { Event, INotify, Listener, NumericArray } from "@thi.ng/api";
|
|
3
3
|
import type { ILogger } from "@thi.ng/logger";
|
|
4
|
-
import { BigIntArray, CoreAPI, IWasmAPI,
|
|
4
|
+
import { BigIntArray, CoreAPI, IWasmAPI, IWasmMemoryAccess, WasmExports } from "./api.js";
|
|
5
|
+
export declare const Panic: {
|
|
6
|
+
new (msg?: string | undefined): {
|
|
7
|
+
name: string;
|
|
8
|
+
message: string;
|
|
9
|
+
stack?: string | undefined;
|
|
10
|
+
};
|
|
11
|
+
captureStackTrace(targetObject: object, constructorOpt?: Function | undefined): void;
|
|
12
|
+
prepareStackTrace?: ((err: Error, stackTraces: NodeJS.CallSite[]) => any) | undefined;
|
|
13
|
+
stackTraceLimit: number;
|
|
14
|
+
};
|
|
5
15
|
export declare const OutOfMemoryError: {
|
|
6
16
|
new (msg?: string | undefined): {
|
|
7
17
|
name: string;
|