@thi.ng/wasm-api 0.9.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 +60 -1
- package/README.md +169 -127
- package/api.d.ts +102 -23
- package/api.js +1 -0
- package/bridge.d.ts +35 -6
- package/bridge.js +55 -15
- 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 +38 -23
- package/include/wasmapi.zig +69 -65
- package/index.d.ts +3 -0
- package/index.js +3 -0
- package/package.json +37 -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,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.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
|
+
|
|
59
|
+
## [0.10.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.10.0) (2022-08-24)
|
|
60
|
+
|
|
61
|
+
#### 🚀 Features
|
|
62
|
+
|
|
63
|
+
- add events, update allocator handling ([89416b1](https://github.com/thi-ng/umbrella/commit/89416b1))
|
|
64
|
+
- add INotify impl for WasmBridge
|
|
65
|
+
- emit event when WASM memory has changed (e.g. to recreate user views)
|
|
66
|
+
- reverse logic so that NO allocator is used by default and instead must
|
|
67
|
+
be explicitly enabled (rather than disabled)
|
|
68
|
+
- update Zig & C bindings
|
|
69
|
+
- add/update docstrings
|
|
70
|
+
|
|
12
71
|
## [0.9.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.9.0) (2022-08-23)
|
|
13
72
|
|
|
14
73
|
#### 🚀 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
|
|
@@ -418,12 +351,30 @@ TypeScript code generator explicitly.
|
|
|
418
351
|
|
|
419
352
|
### Memory allocations
|
|
420
353
|
|
|
421
|
-
|
|
422
|
-
linear WASM memory)
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
354
|
+
If explicitly enabled on the WASM side, the `WasmBridge` includes support for
|
|
355
|
+
malloc/free-style allocations (within the linear WASM memory) from the JS side
|
|
356
|
+
(Note: This is a breaking change in v0.10.0, now using a more flexible approach
|
|
357
|
+
& reverse logic of earlier alpha versions).
|
|
358
|
+
|
|
359
|
+
The actual allocator is implementation specific and suitable generic mechanisms
|
|
360
|
+
are defined for both the included Zig & C bindings. Please see for further
|
|
361
|
+
reference:
|
|
362
|
+
|
|
363
|
+
- [`/include/wasmapi.zig`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.zig#L6):
|
|
364
|
+
comments about WASM-side allocator handling in Zig
|
|
365
|
+
- [`/include/wasmapi.h`](https://github.com/thi-ng/umbrella/blob/develop/packages/wasm-api/include/wasmapi.h#L19):
|
|
366
|
+
comments about WASM-side allocator handling in C/C++
|
|
367
|
+
- [`WasmBridge.allocate()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#allocate):
|
|
368
|
+
allocating memory from JS side
|
|
369
|
+
- [`WasmBridge.free()`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html#free):
|
|
370
|
+
freeing previously allocated memory from JS side
|
|
371
|
+
|
|
372
|
+
Note: The provided Zig mechanism supports the idiomatic (Zig) pattern of working
|
|
373
|
+
with multiple allocators in different parts of the application and supports
|
|
374
|
+
dynamic assignments/swapping of the exposed allocator. See comments in source
|
|
375
|
+
file and
|
|
376
|
+
[tests](https://github.com/thi-ng/umbrella/tree/develop/packages/wasm-api/test)
|
|
377
|
+
for more details...
|
|
427
378
|
|
|
428
379
|
```ts
|
|
429
380
|
try {
|
|
@@ -431,7 +382,7 @@ try {
|
|
|
431
382
|
const addr = bridge.allocate(256);
|
|
432
383
|
|
|
433
384
|
// write string to reserved memory
|
|
434
|
-
// max. 256 bytes
|
|
385
|
+
// max. 256 bytes, zero terminated
|
|
435
386
|
const num = bridge.setString("hello WASM world!", addr, 256, true);
|
|
436
387
|
|
|
437
388
|
// call WASM function doing something w/ the string
|
|
@@ -440,11 +391,100 @@ try {
|
|
|
440
391
|
// cleanup
|
|
441
392
|
bridge.free(addr, 256);
|
|
442
393
|
} catch(e) {
|
|
443
|
-
// allocation error
|
|
394
|
+
// deal with allocation error
|
|
444
395
|
// ...
|
|
445
396
|
}
|
|
446
397
|
```
|
|
447
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
|
+
|
|
448
488
|
### Object indices & handles
|
|
449
489
|
|
|
450
490
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
@@ -492,7 +532,7 @@ canvases.delete(0);
|
|
|
492
532
|
// true
|
|
493
533
|
```
|
|
494
534
|
|
|
495
|
-
|
|
535
|
+
## Status
|
|
496
536
|
|
|
497
537
|
**ALPHA** - bleeding edge / work-in-progress
|
|
498
538
|
|
|
@@ -521,11 +561,11 @@ node --experimental-repl-await
|
|
|
521
561
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
522
562
|
```
|
|
523
563
|
|
|
524
|
-
Package sizes (gzipped, pre-treeshake): ESM:
|
|
564
|
+
Package sizes (gzipped, pre-treeshake): ESM: 6.05 KB
|
|
525
565
|
|
|
526
|
-
**IMPORTANT:** The package includes
|
|
527
|
-
|
|
528
|
-
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!
|
|
529
569
|
|
|
530
570
|
## Dependencies
|
|
531
571
|
|
|
@@ -540,6 +580,7 @@ in production will be MUCH smaller!
|
|
|
540
580
|
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
541
581
|
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
|
|
542
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)
|
|
543
584
|
|
|
544
585
|
## API
|
|
545
586
|
|
|
@@ -615,16 +656,16 @@ The resulting WASM:
|
|
|
615
656
|
(type $none_=>_none (func))
|
|
616
657
|
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
617
658
|
(import "wasmapi" "_printStr" (func $fimport$0 (param i32 i32)))
|
|
618
|
-
(global $global$0 (mut i32) (i32.const
|
|
619
|
-
(memory $0
|
|
620
|
-
(data (i32.const
|
|
659
|
+
(global $global$0 (mut i32) (i32.const 1048576))
|
|
660
|
+
(memory $0 17)
|
|
661
|
+
(data (i32.const 1048576) "hello world!\00")
|
|
621
662
|
(export "memory" (memory $0))
|
|
622
663
|
(export "start" (func $0))
|
|
623
664
|
(export "_wasm_allocate" (func $1))
|
|
624
665
|
(export "_wasm_free" (func $2))
|
|
625
666
|
(func $0
|
|
626
667
|
(call $fimport$0
|
|
627
|
-
(i32.const
|
|
668
|
+
(i32.const 1048576)
|
|
628
669
|
(i32.const 12)
|
|
629
670
|
)
|
|
630
671
|
)
|
|
@@ -651,7 +692,7 @@ void WASM_KEEP start() {
|
|
|
651
692
|
Building the WASM module:
|
|
652
693
|
|
|
653
694
|
```bash
|
|
654
|
-
emcc -Os -Inode_modules/@thi.ng/wasm-api/include
|
|
695
|
+
emcc -Os -Inode_modules/@thi.ng/wasm-api/include \
|
|
655
696
|
-sERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry \
|
|
656
697
|
-o hello.wasm hello.c
|
|
657
698
|
```
|
|
@@ -664,6 +705,7 @@ Resulting WASM:
|
|
|
664
705
|
(type $none_=>_none (func))
|
|
665
706
|
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
666
707
|
(type $none_=>_i32 (func (result i32)))
|
|
708
|
+
(type $i32_i32_=>_none (func (param i32 i32)))
|
|
667
709
|
(import "wasmapi" "_printStr0" (func $fimport$0 (param i32)))
|
|
668
710
|
(global $global$0 (mut i32) (i32.const 5243936))
|
|
669
711
|
(memory $0 256 256)
|
|
@@ -686,7 +728,7 @@ Resulting WASM:
|
|
|
686
728
|
(func $1 (param $0 i32) (result i32)
|
|
687
729
|
(i32.const 0)
|
|
688
730
|
)
|
|
689
|
-
(func $2 (param $0 i32)
|
|
731
|
+
(func $2 (param $0 i32) (param $1 i32)
|
|
690
732
|
(nop)
|
|
691
733
|
)
|
|
692
734
|
(func $3
|