@thi.ng/wasm-api 0.3.1 → 0.6.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 +55 -1
- package/README.md +179 -22
- package/api.d.ts +166 -4
- package/api.js +9 -1
- package/bridge.d.ts +102 -6
- package/bridge.js +129 -30
- package/codegen/typescript.d.ts +22 -0
- package/codegen/typescript.js +146 -0
- package/codegen/utils.d.ts +5 -0
- package/codegen/utils.js +7 -0
- package/codegen/zig.d.ts +22 -0
- package/codegen/zig.js +75 -0
- package/codegen.d.ts +16 -0
- package/codegen.js +116 -0
- package/include/wasmapi.h +60 -0
- package/{zig/core.zig → include/wasmapi.zig} +76 -26
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/package.json +28 -7
- package/dev/custom.zig +0 -12
- package/dev/fieldinfo.zig +0 -135
- package/dev/hello.zig +0 -9
- package/dev/zig-cache/o/0fd683610fe16c12563bf410950c8193/builtin.zig +0 -39
- package/dev/zig-cache/o/85202d15b9c43c8c66de01ab98d6eb2c/builtin.zig +0 -39
- package/dev/zig-cache/o/9e1187e9310422c9c2330ebceeaecce7/builtin.zig +0 -39
- package/dev/zig-cache/o/f647d1186cb589536c92b809f6e25f75/builtin.zig +0 -39
- package/test/custom.zig +0 -12
- package/test/zig-cache/o/117d44467ded6ce3864cde3c180ef73c/builtin.zig +0 -39
package/CHANGELOG.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Change Log
|
|
2
2
|
|
|
3
|
-
- **Last updated**: 2022-08-
|
|
3
|
+
- **Last updated**: 2022-08-15T15:40:55Z
|
|
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,60 @@ 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.6.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.6.0) (2022-08-15)
|
|
13
|
+
|
|
14
|
+
#### 🚀 Features
|
|
15
|
+
|
|
16
|
+
- add C11 header/include file, update WasmBridge ([a67dc00](https://github.com/thi-ng/umbrella/commit/a67dc00))
|
|
17
|
+
- migrate headers/includes to /include
|
|
18
|
+
- rename "core" import section => "wasmapi"
|
|
19
|
+
- rename WasmBridge.core => WasmBridge.api
|
|
20
|
+
- update pkg file
|
|
21
|
+
- update codegens, add opts, fix alignments ([5c1fec5](https://github.com/thi-ng/umbrella/commit/5c1fec5))
|
|
22
|
+
- add global CodeGenOpts
|
|
23
|
+
- update generateTypes() to consider new opts
|
|
24
|
+
- add global USIZE type (for pointer sizes & codegens)
|
|
25
|
+
- add options for Zig codegen (extra debug helpers)
|
|
26
|
+
- simplify TS codegen
|
|
27
|
+
- fix sizeOf() for struct fields
|
|
28
|
+
- make prepareType() idempotent
|
|
29
|
+
- add bindings code generator framework ([17ee06f](https://github.com/thi-ng/umbrella/commit/17ee06f))
|
|
30
|
+
- add/update deps
|
|
31
|
+
- add preliminary codegens for Zig & TS
|
|
32
|
+
- add supporting types & utils
|
|
33
|
+
- add generateTypes() codegen facade fn
|
|
34
|
+
- update allocate/free() fns, update Zig core API ([8a55989](https://github.com/thi-ng/umbrella/commit/8a55989))
|
|
35
|
+
- add _wasm_free() Zig impl
|
|
36
|
+
- add printFmt() Zig fn
|
|
37
|
+
- update WasmBridge.allocate() (add clear option)
|
|
38
|
+
- update WasmBridge.free()
|
|
39
|
+
- ensure memory in WasmBridge.getString()
|
|
40
|
+
- add/update docstrings
|
|
41
|
+
|
|
42
|
+
#### ♻️ Refactoring
|
|
43
|
+
|
|
44
|
+
- extract WasmMemViews interface, update test WASM ([4c73e65](https://github.com/thi-ng/umbrella/commit/4c73e65))
|
|
45
|
+
|
|
46
|
+
## [0.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.5.0) (2022-08-08)
|
|
47
|
+
|
|
48
|
+
#### 🚀 Features
|
|
49
|
+
|
|
50
|
+
- add memory allocation ([980c1f2](https://github.com/thi-ng/umbrella/commit/980c1f2))
|
|
51
|
+
- add WasmBridge.allocate()/free()
|
|
52
|
+
- add WasmBridge.growMemory()
|
|
53
|
+
- extract WasmBridge.ensureMemory()
|
|
54
|
+
- update WasmExports
|
|
55
|
+
- update Zig bindings (configurable allocator, GPA as default)
|
|
56
|
+
|
|
57
|
+
## [0.4.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.4.0) (2022-08-07)
|
|
58
|
+
|
|
59
|
+
#### 🚀 Features
|
|
60
|
+
|
|
61
|
+
- use named import objects ([4965f20](https://github.com/thi-ng/umbrella/commit/4965f20))
|
|
62
|
+
- switch to name import objects to avoid merging into flat namespace
|
|
63
|
+
- update externs in core.zig
|
|
64
|
+
- update docstrings
|
|
65
|
+
|
|
12
66
|
## [0.3.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.3.0) (2022-08-04)
|
|
13
67
|
|
|
14
68
|
#### 🚀 Features
|
package/README.md
CHANGED
|
@@ -12,30 +12,48 @@ This project is part of the
|
|
|
12
12
|
- [About](#about)
|
|
13
13
|
- [Custom API modules](#custom-api-modules)
|
|
14
14
|
- [Object indices & handles](#object-indices--handles)
|
|
15
|
+
- [Data bindings & code generators](#data-bindings--code-generators)
|
|
16
|
+
- [Data type definitions](#data-type-definitions)
|
|
17
|
+
- [Code generation](#code-generation)
|
|
18
|
+
- [Example usage](#example-usage)
|
|
15
19
|
- [Status](#status)
|
|
16
20
|
- [Installation](#installation)
|
|
17
21
|
- [Dependencies](#dependencies)
|
|
18
22
|
- [API](#api)
|
|
23
|
+
- [Basic usage example](#basic-usage-example)
|
|
24
|
+
- [Zig version](#zig-version)
|
|
25
|
+
- [C11 version](#c11-version)
|
|
19
26
|
- [Authors](#authors)
|
|
20
27
|
- [License](#license)
|
|
21
28
|
|
|
22
29
|
## About
|
|
23
30
|
|
|
24
|
-
|
|
31
|
+
Generic, modular, extensible API bridge, glue code and bindings code generator for hybrid JS & WebAssembly projects.
|
|
25
32
|
|
|
26
|
-
This package provides a
|
|
27
|
-
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
28
|
-
class as interop basis and a much reduced boilerplate for hybrid JS/WebAssembly
|
|
29
|
-
applications. At the moment only a minimal core API is provided (i.e. for debug
|
|
30
|
-
output, string, pointer, typed array accessors [8/16/32/64 bit (u)ints, 32/64
|
|
31
|
-
bit floats]), but in the future we aim to also supply support modules for DOM
|
|
32
|
-
manipulation, WebGL, WebGPU, WebAudio etc.
|
|
33
|
+
This package provides a the following:
|
|
33
34
|
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
1. A small, generic and modular
|
|
36
|
+
[`WasmBridge`](https://docs.thi.ng/umbrella/wasm-api/classes/WasmBridge.html)
|
|
37
|
+
class as interop basis and much reduced boilerplate for hybrid JS/WebAssembly
|
|
38
|
+
applications.
|
|
39
|
+
2. A minimal core API for debug output, string, pointer, typed array accessors
|
|
40
|
+
(8/16/32/64 bit (u)ints, 32/64 bit floats). In the future we aim to also supply
|
|
41
|
+
support modules for DOM manipulation, WebGL, WebGPU, WebAudio etc.
|
|
42
|
+
3. Extensible shared datatype code generators for (currently)
|
|
43
|
+
[Zig](https://ziglang.org) & TypeScript. The latter also generates fully type
|
|
44
|
+
checked memory-mapped accessors of WASM-side data. In general, all languages
|
|
45
|
+
with a WebAssembly target are supported, however currently only bindings for
|
|
46
|
+
these few langs are included.
|
|
47
|
+
4. Include files for C11 and Zig, defining imports for the JS core API defined
|
|
48
|
+
by this package
|
|
36
49
|
|
|
37
50
|
### Custom API modules
|
|
38
51
|
|
|
52
|
+
The WasmBridge is extensible via custom defined API modules. Such API extensions
|
|
53
|
+
will consist of a collection of JS/TS functions & variables, their related
|
|
54
|
+
counterparts (import definitions) for the WASM target and (optionally) some
|
|
55
|
+
shared data types (bindings for which _can_ be generated by this package too).
|
|
56
|
+
|
|
39
57
|
On the JS side, custom API modules can be easily integrated via the [`IWasmAPI`
|
|
40
58
|
interface](https://docs.thi.ng/umbrella/wasm-api/interfaces/IWasmAPI.html). The
|
|
41
59
|
following example provides a brief overview:
|
|
@@ -57,15 +75,15 @@ export class CustomAPI implements IWasmAPI {
|
|
|
57
75
|
|
|
58
76
|
/**
|
|
59
77
|
* Returns object of functions to import as externals into
|
|
60
|
-
* the WASM module. These imports are merged
|
|
61
|
-
*
|
|
78
|
+
* the WASM module. These imports are merged into a larger
|
|
79
|
+
* imports object alongside the bridge's core API...
|
|
62
80
|
*/
|
|
63
81
|
getImports(): WebAssembly.Imports {
|
|
64
82
|
return {
|
|
65
83
|
/**
|
|
66
84
|
* Writes 2 random float32 numbers to given address
|
|
67
85
|
*/
|
|
68
|
-
|
|
86
|
+
randomVec2: (addr: number) => {
|
|
69
87
|
this.parent.f32.set(
|
|
70
88
|
[Math.random(), Math.random()],
|
|
71
89
|
addr >> 2
|
|
@@ -83,14 +101,17 @@ export const bridge = new WasmBridge({ custom: new CustomAPI() });
|
|
|
83
101
|
```
|
|
84
102
|
|
|
85
103
|
In Zig (or any other language of your choice) we can then utilize this custom
|
|
86
|
-
API like so (Please also see example further below in this
|
|
104
|
+
API like so (Please also see /test/index.ts` & the example further below in this
|
|
105
|
+
readme):
|
|
87
106
|
|
|
88
107
|
```zig
|
|
89
108
|
// Import JS core API
|
|
90
109
|
const js = @import("wasmapi");
|
|
91
110
|
|
|
92
111
|
/// JS external to fill vec2 w/ random values
|
|
93
|
-
|
|
112
|
+
/// Note: Each API module uses a separate import object to avoid naming clashes
|
|
113
|
+
/// Here we declare an external binding belonging to the "custom" import group
|
|
114
|
+
extern "custom" fn randomVec2(addr: usize) void;
|
|
94
115
|
|
|
95
116
|
export fn test_randomVec2() void {
|
|
96
117
|
var foo = [2]f32{ 0, 0 };
|
|
@@ -99,7 +120,7 @@ export fn test_randomVec2() void {
|
|
|
99
120
|
js.printF32Array(foo[0..]);
|
|
100
121
|
|
|
101
122
|
// populate foo with random numbers
|
|
102
|
-
|
|
123
|
+
randomVec2(@ptrToInt(&foo));
|
|
103
124
|
|
|
104
125
|
// print result
|
|
105
126
|
js.printF32Array(foo[0..]);
|
|
@@ -110,12 +131,12 @@ export fn test_randomVec2() void {
|
|
|
110
131
|
|
|
111
132
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
112
133
|
host, any JS native objects the WASM side might want to be working with must be
|
|
113
|
-
managed in JS. For this purpose the [`ObjectIndex`
|
|
134
|
+
managed manually in JS. For this purpose the [`ObjectIndex`
|
|
114
135
|
class](https://docs.thi.ng/umbrella/wasm-api/classes/ObjectIndex.html) can be
|
|
115
136
|
used by API modules to handle ID generation (incl. recycling, using
|
|
116
137
|
[@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen))
|
|
117
|
-
|
|
118
|
-
then need to be exchanged with the WASM module...
|
|
138
|
+
and the indexing of different types of JS objects/values. Only the numeric IDs
|
|
139
|
+
(handles) will then need to be exchanged with the WASM module...
|
|
119
140
|
|
|
120
141
|
```ts
|
|
121
142
|
import { ObjectIndex } from "@thi.ng/wasm-api";
|
|
@@ -153,6 +174,29 @@ canvases.delete(0);
|
|
|
153
174
|
// true
|
|
154
175
|
```
|
|
155
176
|
|
|
177
|
+
### Data bindings & code generators
|
|
178
|
+
|
|
179
|
+
The package provides an extensible codegeneration framework to simplify the
|
|
180
|
+
bilateral design & exchange of data structures shared between the WASM & JS host
|
|
181
|
+
env. Currently, code generators for TypeScript & Zig are supplied (more are
|
|
182
|
+
planned). A CLI wrapper is worked on too.
|
|
183
|
+
|
|
184
|
+
#### Data type definitions
|
|
185
|
+
|
|
186
|
+
TODO
|
|
187
|
+
|
|
188
|
+
##### Struct
|
|
189
|
+
|
|
190
|
+
##### Enum
|
|
191
|
+
|
|
192
|
+
#### Code generation
|
|
193
|
+
|
|
194
|
+
TODO
|
|
195
|
+
|
|
196
|
+
#### Example usage
|
|
197
|
+
|
|
198
|
+
TODO
|
|
199
|
+
|
|
156
200
|
### Status
|
|
157
201
|
|
|
158
202
|
**ALPHA** - bleeding edge / work-in-progress
|
|
@@ -182,11 +226,19 @@ node --experimental-repl-await
|
|
|
182
226
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
183
227
|
```
|
|
184
228
|
|
|
185
|
-
Package sizes (gzipped, pre-treeshake): ESM:
|
|
229
|
+
Package sizes (gzipped, pre-treeshake): ESM: 3.98 KB
|
|
230
|
+
|
|
231
|
+
**IMPORTANT:** The package includes various code generators and supporting
|
|
232
|
+
functions which are NOT required during runtime. Hence the actual package size
|
|
233
|
+
in production will be MUCH smaller!
|
|
186
234
|
|
|
187
235
|
## Dependencies
|
|
188
236
|
|
|
189
237
|
- [@thi.ng/api](https://github.com/thi-ng/umbrella/tree/develop/packages/api)
|
|
238
|
+
- [@thi.ng/binary](https://github.com/thi-ng/umbrella/tree/develop/packages/binary)
|
|
239
|
+
- [@thi.ng/checks](https://github.com/thi-ng/umbrella/tree/develop/packages/checks)
|
|
240
|
+
- [@thi.ng/compare](https://github.com/thi-ng/umbrella/tree/develop/packages/compare)
|
|
241
|
+
- [@thi.ng/defmulti](https://github.com/thi-ng/umbrella/tree/develop/packages/defmulti)
|
|
190
242
|
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
191
243
|
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
192
244
|
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
|
|
@@ -196,6 +248,8 @@ Package sizes (gzipped, pre-treeshake): ESM: 1.63 KB
|
|
|
196
248
|
|
|
197
249
|
[Generated API docs](https://docs.thi.ng/umbrella/wasm-api/)
|
|
198
250
|
|
|
251
|
+
## Basic usage example
|
|
252
|
+
|
|
199
253
|
```ts
|
|
200
254
|
import { WasmBridge, WasmExports } from "@thi.ng/wasm-api";
|
|
201
255
|
import { readFileSync } from "fs";
|
|
@@ -212,6 +266,7 @@ interface App extends WasmExports {
|
|
|
212
266
|
|
|
213
267
|
// instantiate WASM module using imports provided by the bridge
|
|
214
268
|
// this also initializes any bindings & bridge child APIs (if any)
|
|
269
|
+
// (also accepts a fetch() `Response` as input)
|
|
215
270
|
await bridge.instantiate(readFileSync("hello.wasm"));
|
|
216
271
|
|
|
217
272
|
// call an exported WASM function
|
|
@@ -219,12 +274,20 @@ interface App extends WasmExports {
|
|
|
219
274
|
})();
|
|
220
275
|
```
|
|
221
276
|
|
|
277
|
+
### Zig version
|
|
278
|
+
|
|
279
|
+
Requires [Zig](https://ziglang.org) to be installed:
|
|
280
|
+
|
|
222
281
|
```zig
|
|
223
282
|
//! Example Zig application (hello.zig)
|
|
224
283
|
|
|
225
284
|
/// import externals
|
|
226
285
|
/// see build command for configuration
|
|
227
286
|
const js = @import("wasmapi");
|
|
287
|
+
const std = @import("std");
|
|
288
|
+
|
|
289
|
+
// set custom memory allocator (here to disable)
|
|
290
|
+
pub const WASM_ALLOCATOR: ?std.mem.Allocator = null;
|
|
228
291
|
|
|
229
292
|
export fn start() void {
|
|
230
293
|
js.printStr("hello world!");
|
|
@@ -238,7 +301,7 @@ folder):
|
|
|
238
301
|
```bash
|
|
239
302
|
# compile WASM binary
|
|
240
303
|
zig build-lib \
|
|
241
|
-
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/
|
|
304
|
+
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/include/wasmapi.zig --pkg-end \
|
|
242
305
|
-target wasm32-freestanding \
|
|
243
306
|
-O ReleaseSmall -dynamic --strip \
|
|
244
307
|
hello.zig
|
|
@@ -253,18 +316,112 @@ The resulting WASM:
|
|
|
253
316
|
(module
|
|
254
317
|
(type $i32_i32_=>_none (func (param i32 i32)))
|
|
255
318
|
(type $none_=>_none (func))
|
|
256
|
-
(
|
|
319
|
+
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
320
|
+
(import "wasmapi" "_printStr" (func $fimport$0 (param i32 i32)))
|
|
257
321
|
(global $global$0 (mut i32) (i32.const 65536))
|
|
258
322
|
(memory $0 2)
|
|
259
323
|
(data (i32.const 65536) "hello world!\00")
|
|
260
324
|
(export "memory" (memory $0))
|
|
261
325
|
(export "start" (func $0))
|
|
326
|
+
(export "_wasm_allocate" (func $1))
|
|
327
|
+
(export "_wasm_free" (func $2))
|
|
262
328
|
(func $0
|
|
263
329
|
(call $fimport$0
|
|
264
330
|
(i32.const 65536)
|
|
265
331
|
(i32.const 12)
|
|
266
332
|
)
|
|
267
333
|
)
|
|
334
|
+
(func $1 (param $0 i32) (result i32)
|
|
335
|
+
(i32.const 0)
|
|
336
|
+
)
|
|
337
|
+
(func $2 (param $0 i32) (param $1 i32)
|
|
338
|
+
)
|
|
339
|
+
)
|
|
340
|
+
```
|
|
341
|
+
|
|
342
|
+
### C11 version
|
|
343
|
+
|
|
344
|
+
Requires [Emscripten](https://emscripten.org/) to be installed:
|
|
345
|
+
|
|
346
|
+
```c
|
|
347
|
+
#include <wasmapi.h>
|
|
348
|
+
|
|
349
|
+
void WASM_KEEP start() {
|
|
350
|
+
wasm_printStr0("hello world!");
|
|
351
|
+
}
|
|
352
|
+
```
|
|
353
|
+
|
|
354
|
+
Building the WASM module:
|
|
355
|
+
|
|
356
|
+
```bash
|
|
357
|
+
emcc -Os -Inode_modules/@thi.ng/wasm-api/include -DWASMAPI_NO_MALLOC \
|
|
358
|
+
-sERROR_ON_UNDEFINED_SYMBOLS=0 --no-entry \
|
|
359
|
+
-o hello.wasm hello.c
|
|
360
|
+
```
|
|
361
|
+
|
|
362
|
+
Resulting WASM:
|
|
363
|
+
|
|
364
|
+
```wasm
|
|
365
|
+
(module
|
|
366
|
+
(type $i32_=>_none (func (param i32)))
|
|
367
|
+
(type $none_=>_none (func))
|
|
368
|
+
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
369
|
+
(type $none_=>_i32 (func (result i32)))
|
|
370
|
+
(import "wasmapi" "_printStr0" (func $fimport$0 (param i32)))
|
|
371
|
+
(global $global$0 (mut i32) (i32.const 5243936))
|
|
372
|
+
(memory $0 256 256)
|
|
373
|
+
(data (i32.const 1024) "hello world!")
|
|
374
|
+
(table $0 2 2 funcref)
|
|
375
|
+
(elem (i32.const 1) $0)
|
|
376
|
+
(export "memory" (memory $0))
|
|
377
|
+
(export "_wasm_allocate" (func $1))
|
|
378
|
+
(export "_wasm_free" (func $2))
|
|
379
|
+
(export "start" (func $3))
|
|
380
|
+
(export "__indirect_function_table" (table $0))
|
|
381
|
+
(export "_initialize" (func $0))
|
|
382
|
+
(export "__errno_location" (func $7))
|
|
383
|
+
(export "stackSave" (func $4))
|
|
384
|
+
(export "stackRestore" (func $5))
|
|
385
|
+
(export "stackAlloc" (func $6))
|
|
386
|
+
(func $0
|
|
387
|
+
(nop)
|
|
388
|
+
)
|
|
389
|
+
(func $1 (param $0 i32) (result i32)
|
|
390
|
+
(i32.const 0)
|
|
391
|
+
)
|
|
392
|
+
(func $2 (param $0 i32)
|
|
393
|
+
(nop)
|
|
394
|
+
)
|
|
395
|
+
(func $3
|
|
396
|
+
(call $fimport$0
|
|
397
|
+
(i32.const 1024)
|
|
398
|
+
)
|
|
399
|
+
)
|
|
400
|
+
(func $4 (result i32)
|
|
401
|
+
(global.get $global$0)
|
|
402
|
+
)
|
|
403
|
+
(func $5 (param $0 i32)
|
|
404
|
+
(global.set $global$0
|
|
405
|
+
(local.get $0)
|
|
406
|
+
)
|
|
407
|
+
)
|
|
408
|
+
(func $6 (param $0 i32) (result i32)
|
|
409
|
+
(global.set $global$0
|
|
410
|
+
(local.tee $0
|
|
411
|
+
(i32.and
|
|
412
|
+
(i32.sub
|
|
413
|
+
(global.get $global$0)
|
|
414
|
+
(local.get $0)
|
|
415
|
+
)
|
|
416
|
+
(i32.const -16)
|
|
417
|
+
)
|
|
418
|
+
)
|
|
419
|
+
)
|
|
420
|
+
(local.get $0)
|
|
421
|
+
)
|
|
422
|
+
(func $7 (result i32)
|
|
423
|
+
(i32.const 1040)
|
|
424
|
+
)
|
|
268
425
|
)
|
|
269
426
|
```
|
|
270
427
|
|
package/api.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import type { Fn, Fn2 } from "@thi.ng/api";
|
|
1
|
+
import type { FloatType, Fn, Fn2 } from "@thi.ng/api";
|
|
2
2
|
import type { WasmBridge } from "./bridge.js";
|
|
3
|
+
export declare const PKG_NAME = "@thi.ng/wasm-api";
|
|
3
4
|
export declare type BigIntArray = bigint[] | BigInt64Array | BigUint64Array;
|
|
4
5
|
/**
|
|
5
6
|
* Common interface for WASM/JS child APIs which will be used in combination
|
|
@@ -27,7 +28,8 @@ export interface IWasmAPI<T extends WasmExports = WasmExports> {
|
|
|
27
28
|
}
|
|
28
29
|
/**
|
|
29
30
|
* Base interface of exports declared by the WASM module. At the very least, the
|
|
30
|
-
* module needs to export its memory
|
|
31
|
+
* module needs to export its memory and the functions defined in this
|
|
32
|
+
* interface.
|
|
31
33
|
*
|
|
32
34
|
* @remarks
|
|
33
35
|
* This interface is supposed to be extended with the concrete exports defined
|
|
@@ -39,16 +41,49 @@ export interface IWasmAPI<T extends WasmExports = WasmExports> {
|
|
|
39
41
|
export interface WasmExports {
|
|
40
42
|
/**
|
|
41
43
|
* The WASM module's linear memory buffer. The `WasmBridge` automatically
|
|
42
|
-
* creates various typed views of that memory.
|
|
44
|
+
* creates various typed views of that memory (i.e. u8, u16, u32, f32 etc.)
|
|
43
45
|
*/
|
|
44
46
|
memory: WebAssembly.Memory;
|
|
47
|
+
/**
|
|
48
|
+
* Implementation specific memory allocation function (likely heap-based).
|
|
49
|
+
* If successful returns address of new memory block, or zero if
|
|
50
|
+
* unsuccessful.
|
|
51
|
+
*
|
|
52
|
+
* @remarks
|
|
53
|
+
* In the supplied Zig bindings (see `/zig/core.zig`), by default this is
|
|
54
|
+
* using the `std.heap.GeneralPurposeAllocator` (which also automatically
|
|
55
|
+
* handles growing the WASM memory), however as mentioned the underlying
|
|
56
|
+
* mechanism is purposefully left to the actual WASM-side implementation. In
|
|
57
|
+
* a C program, this would likely use `malloc()` or similar...
|
|
58
|
+
*/
|
|
59
|
+
_wasm_allocate(numBytes: number): number;
|
|
60
|
+
/**
|
|
61
|
+
* Implementation specific function to free a previously allocated chunk of
|
|
62
|
+
* of WASM memory (allocated via {@link WasmExports._wasm_allocate}).
|
|
63
|
+
*
|
|
64
|
+
* @param addr
|
|
65
|
+
* @param numBytes
|
|
66
|
+
*/
|
|
67
|
+
_wasm_free(addr: number, numBytes: number): void;
|
|
68
|
+
}
|
|
69
|
+
export interface WasmMemViews {
|
|
70
|
+
i8: Int8Array;
|
|
71
|
+
u8: Uint8Array;
|
|
72
|
+
i16: Int16Array;
|
|
73
|
+
u16: Uint16Array;
|
|
74
|
+
i32: Int32Array;
|
|
75
|
+
u32: Uint32Array;
|
|
76
|
+
i64: BigInt64Array;
|
|
77
|
+
u64: BigUint64Array;
|
|
78
|
+
f32: Float32Array;
|
|
79
|
+
f64: Float64Array;
|
|
45
80
|
}
|
|
46
81
|
/**
|
|
47
82
|
* Core API of WASM imports defined by the {@link WasmBridge}. The same
|
|
48
83
|
* functions are declared as bindings in `/zig/core.zig`. Also see this file for
|
|
49
84
|
* documentation of each function...
|
|
50
85
|
*/
|
|
51
|
-
export interface CoreAPI {
|
|
86
|
+
export interface CoreAPI extends WebAssembly.ModuleImports {
|
|
52
87
|
printI8: Fn<number, void>;
|
|
53
88
|
printU8: Fn<number, void>;
|
|
54
89
|
printU8Hex: Fn<number, void>;
|
|
@@ -76,4 +111,131 @@ export interface CoreAPI {
|
|
|
76
111
|
_printStr0: (addr: number) => void;
|
|
77
112
|
_printStr: (addr: number, len: number) => void;
|
|
78
113
|
}
|
|
114
|
+
export interface WasmTypeBase {
|
|
115
|
+
/**
|
|
116
|
+
* Base address in linear WASM memory.
|
|
117
|
+
*/
|
|
118
|
+
readonly __base: number;
|
|
119
|
+
/**
|
|
120
|
+
* Obtain as byte buffer
|
|
121
|
+
*/
|
|
122
|
+
readonly __bytes: Uint8Array;
|
|
123
|
+
}
|
|
124
|
+
export interface WasmType<T> {
|
|
125
|
+
readonly align: number;
|
|
126
|
+
readonly size: number;
|
|
127
|
+
instance: Fn<number, T>;
|
|
128
|
+
}
|
|
129
|
+
export declare type WasmTypeConstructor<T> = Fn<WasmMemViews, WasmType<T>>;
|
|
130
|
+
export declare type WasmInt = "i8" | "i16" | "i32" | "i64";
|
|
131
|
+
export declare type WasmUint = "u8" | "u16" | "u32" | "u64";
|
|
132
|
+
export declare type WasmFloat = FloatType;
|
|
133
|
+
export declare type WasmPrim = WasmInt | WasmUint | WasmFloat;
|
|
134
|
+
export declare type TypeColl = Record<string, TopLevelType>;
|
|
135
|
+
export interface TypeInfo {
|
|
136
|
+
/**
|
|
137
|
+
* Auto-computed size (in bytes)
|
|
138
|
+
*
|
|
139
|
+
* @internal
|
|
140
|
+
*/
|
|
141
|
+
__size?: number;
|
|
142
|
+
/**
|
|
143
|
+
* Auto-computed offset (in bytes) in parent struct
|
|
144
|
+
*
|
|
145
|
+
* @internal
|
|
146
|
+
*/
|
|
147
|
+
__offset?: number;
|
|
148
|
+
/**
|
|
149
|
+
* Auto-computed alignment (in bytes)
|
|
150
|
+
*
|
|
151
|
+
* @internal
|
|
152
|
+
*/
|
|
153
|
+
__align?: number;
|
|
154
|
+
}
|
|
155
|
+
export interface TopLevelType extends TypeInfo {
|
|
156
|
+
name: string;
|
|
157
|
+
doc?: string;
|
|
158
|
+
type: "struct" | "enum";
|
|
159
|
+
}
|
|
160
|
+
export interface Struct extends TopLevelType {
|
|
161
|
+
type: "struct";
|
|
162
|
+
fields: StructField[];
|
|
163
|
+
/**
|
|
164
|
+
* If true, struct fields will be re-ordered in descending order based on
|
|
165
|
+
* their {@link TypeInfo.__align} size.
|
|
166
|
+
*
|
|
167
|
+
* @defaultValue false
|
|
168
|
+
*/
|
|
169
|
+
auto?: boolean;
|
|
170
|
+
}
|
|
171
|
+
export interface StructField extends TypeInfo {
|
|
172
|
+
/**
|
|
173
|
+
* Field name (prefix: "__" is reserved)
|
|
174
|
+
*/
|
|
175
|
+
name: string;
|
|
176
|
+
/**
|
|
177
|
+
* Field docstring (can be multiline, will be formatted)
|
|
178
|
+
*/
|
|
179
|
+
doc?: string;
|
|
180
|
+
/**
|
|
181
|
+
* Field type tag/qualifier (note: `slice` & `vec` are only supported by
|
|
182
|
+
* Zig & TS)
|
|
183
|
+
*
|
|
184
|
+
* @defaultValue "scalar"
|
|
185
|
+
*/
|
|
186
|
+
tag?: "scalar" | "array" | "ptr" | "slice" | "vec";
|
|
187
|
+
/**
|
|
188
|
+
* Field base type. If not a {@link WasmPrim} or `opaque`, the value is
|
|
189
|
+
* interpreted as another type name in the {@link TypeColl}.
|
|
190
|
+
*
|
|
191
|
+
* TODO `opaque` currently unsupported.
|
|
192
|
+
*/
|
|
193
|
+
type: WasmPrim | "opaque" | string;
|
|
194
|
+
/**
|
|
195
|
+
* TODO currently unsupported!
|
|
196
|
+
*/
|
|
197
|
+
sentinel?: number;
|
|
198
|
+
/**
|
|
199
|
+
* Array or vector length
|
|
200
|
+
*/
|
|
201
|
+
len?: number;
|
|
202
|
+
/**
|
|
203
|
+
* TODO currently unsupported!
|
|
204
|
+
*/
|
|
205
|
+
default?: any;
|
|
206
|
+
}
|
|
207
|
+
export interface Enum extends TopLevelType {
|
|
208
|
+
type: "enum";
|
|
209
|
+
/**
|
|
210
|
+
* No u64 support, due to Typescript not supporting bigint enum values
|
|
211
|
+
*/
|
|
212
|
+
tag: Exclude<WasmUint, "u64">;
|
|
213
|
+
values: (string | EnumValue)[];
|
|
214
|
+
}
|
|
215
|
+
export interface EnumValue {
|
|
216
|
+
name: string;
|
|
217
|
+
value?: number;
|
|
218
|
+
doc?: string;
|
|
219
|
+
}
|
|
220
|
+
export interface ICodeGen {
|
|
221
|
+
/**
|
|
222
|
+
* Optional prelude source, to be prepended before any generated type defs.
|
|
223
|
+
*/
|
|
224
|
+
pre?: string;
|
|
225
|
+
/**
|
|
226
|
+
* Optional source code to be appended after any generated type defs.
|
|
227
|
+
*/
|
|
228
|
+
post?: string;
|
|
229
|
+
doc: (doc: string, indent: string, acc: string[], topLevel?: boolean) => void;
|
|
230
|
+
enum: (type: Enum, types: TypeColl, acc: string[]) => void;
|
|
231
|
+
struct: (type: Struct, types: TypeColl, acc: string[]) => void;
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* WASM usize type. Assuming wasm32 until wasm64 surfaces, then need an option.
|
|
235
|
+
*/
|
|
236
|
+
export declare const USIZE = "u32";
|
|
237
|
+
/**
|
|
238
|
+
* Byte size of {@link USIZE}.
|
|
239
|
+
*/
|
|
240
|
+
export declare const USIZE_SIZE = 4;
|
|
79
241
|
//# sourceMappingURL=api.d.ts.map
|
package/api.js
CHANGED