@thi.ng/wasm-api 0.5.0 → 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 +35 -1
- package/README.md +169 -16
- package/api.d.ts +143 -5
- package/api.js +9 -1
- package/bridge.d.ts +41 -7
- package/bridge.js +44 -8
- 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} +45 -32
- package/index.d.ts +4 -0
- package/index.js +4 -0
- package/package.json +27 -7
- package/dev/custom.zig +0 -12
- package/dev/fieldinfo.zig +0 -135
- package/dev/hello.zig +0 -14
- package/dev/zig-cache/o/0fd683610fe16c12563bf410950c8193/builtin.zig +0 -39
- package/test/custom.zig +0 -16
- 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,40 @@ 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
|
+
|
|
12
46
|
## [0.5.0](https://github.com/thi-ng/umbrella/tree/@thi.ng/wasm-api@0.5.0) (2022-08-08)
|
|
13
47
|
|
|
14
48
|
#### 🚀 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:
|
|
@@ -113,12 +131,12 @@ export fn test_randomVec2() void {
|
|
|
113
131
|
|
|
114
132
|
Since only numeric values can be exchanged between the WASM module and the JS
|
|
115
133
|
host, any JS native objects the WASM side might want to be working with must be
|
|
116
|
-
managed in JS. For this purpose the [`ObjectIndex`
|
|
134
|
+
managed manually in JS. For this purpose the [`ObjectIndex`
|
|
117
135
|
class](https://docs.thi.ng/umbrella/wasm-api/classes/ObjectIndex.html) can be
|
|
118
136
|
used by API modules to handle ID generation (incl. recycling, using
|
|
119
137
|
[@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen))
|
|
120
|
-
|
|
121
|
-
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...
|
|
122
140
|
|
|
123
141
|
```ts
|
|
124
142
|
import { ObjectIndex } from "@thi.ng/wasm-api";
|
|
@@ -156,6 +174,29 @@ canvases.delete(0);
|
|
|
156
174
|
// true
|
|
157
175
|
```
|
|
158
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
|
+
|
|
159
200
|
### Status
|
|
160
201
|
|
|
161
202
|
**ALPHA** - bleeding edge / work-in-progress
|
|
@@ -185,11 +226,19 @@ node --experimental-repl-await
|
|
|
185
226
|
> const wasmApi = await import("@thi.ng/wasm-api");
|
|
186
227
|
```
|
|
187
228
|
|
|
188
|
-
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!
|
|
189
234
|
|
|
190
235
|
## Dependencies
|
|
191
236
|
|
|
192
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)
|
|
193
242
|
- [@thi.ng/errors](https://github.com/thi-ng/umbrella/tree/develop/packages/errors)
|
|
194
243
|
- [@thi.ng/hex](https://github.com/thi-ng/umbrella/tree/develop/packages/hex)
|
|
195
244
|
- [@thi.ng/idgen](https://github.com/thi-ng/umbrella/tree/develop/packages/idgen)
|
|
@@ -199,6 +248,8 @@ Package sizes (gzipped, pre-treeshake): ESM: 1.61 KB
|
|
|
199
248
|
|
|
200
249
|
[Generated API docs](https://docs.thi.ng/umbrella/wasm-api/)
|
|
201
250
|
|
|
251
|
+
## Basic usage example
|
|
252
|
+
|
|
202
253
|
```ts
|
|
203
254
|
import { WasmBridge, WasmExports } from "@thi.ng/wasm-api";
|
|
204
255
|
import { readFileSync } from "fs";
|
|
@@ -223,12 +274,20 @@ interface App extends WasmExports {
|
|
|
223
274
|
})();
|
|
224
275
|
```
|
|
225
276
|
|
|
277
|
+
### Zig version
|
|
278
|
+
|
|
279
|
+
Requires [Zig](https://ziglang.org) to be installed:
|
|
280
|
+
|
|
226
281
|
```zig
|
|
227
282
|
//! Example Zig application (hello.zig)
|
|
228
283
|
|
|
229
284
|
/// import externals
|
|
230
285
|
/// see build command for configuration
|
|
231
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;
|
|
232
291
|
|
|
233
292
|
export fn start() void {
|
|
234
293
|
js.printStr("hello world!");
|
|
@@ -242,7 +301,7 @@ folder):
|
|
|
242
301
|
```bash
|
|
243
302
|
# compile WASM binary
|
|
244
303
|
zig build-lib \
|
|
245
|
-
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/
|
|
304
|
+
--pkg-begin wasmapi node_modules/@thi.ng/wasm-api/include/wasmapi.zig --pkg-end \
|
|
246
305
|
-target wasm32-freestanding \
|
|
247
306
|
-O ReleaseSmall -dynamic --strip \
|
|
248
307
|
hello.zig
|
|
@@ -257,18 +316,112 @@ The resulting WASM:
|
|
|
257
316
|
(module
|
|
258
317
|
(type $i32_i32_=>_none (func (param i32 i32)))
|
|
259
318
|
(type $none_=>_none (func))
|
|
260
|
-
(
|
|
319
|
+
(type $i32_=>_i32 (func (param i32) (result i32)))
|
|
320
|
+
(import "wasmapi" "_printStr" (func $fimport$0 (param i32 i32)))
|
|
261
321
|
(global $global$0 (mut i32) (i32.const 65536))
|
|
262
322
|
(memory $0 2)
|
|
263
323
|
(data (i32.const 65536) "hello world!\00")
|
|
264
324
|
(export "memory" (memory $0))
|
|
265
325
|
(export "start" (func $0))
|
|
326
|
+
(export "_wasm_allocate" (func $1))
|
|
327
|
+
(export "_wasm_free" (func $2))
|
|
266
328
|
(func $0
|
|
267
329
|
(call $fimport$0
|
|
268
330
|
(i32.const 65536)
|
|
269
331
|
(i32.const 12)
|
|
270
332
|
)
|
|
271
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
|
+
)
|
|
272
425
|
)
|
|
273
426
|
```
|
|
274
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
|
|
@@ -60,12 +61,22 @@ export interface WasmExports {
|
|
|
60
61
|
* Implementation specific function to free a previously allocated chunk of
|
|
61
62
|
* of WASM memory (allocated via {@link WasmExports._wasm_allocate}).
|
|
62
63
|
*
|
|
63
|
-
* @remarks
|
|
64
|
-
* In the supplied Zig bindings (/zig/core.zig) this is a no-op (currently).
|
|
65
|
-
*
|
|
66
64
|
* @param addr
|
|
65
|
+
* @param numBytes
|
|
67
66
|
*/
|
|
68
|
-
_wasm_free(addr: number): void;
|
|
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;
|
|
69
80
|
}
|
|
70
81
|
/**
|
|
71
82
|
* Core API of WASM imports defined by the {@link WasmBridge}. The same
|
|
@@ -100,4 +111,131 @@ export interface CoreAPI extends WebAssembly.ModuleImports {
|
|
|
100
111
|
_printStr0: (addr: number) => void;
|
|
101
112
|
_printStr: (addr: number, len: number) => void;
|
|
102
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;
|
|
103
241
|
//# sourceMappingURL=api.d.ts.map
|
package/api.js
CHANGED
package/bridge.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
/// <reference types="node" />
|
|
2
2
|
import type { NumericArray } from "@thi.ng/api";
|
|
3
3
|
import type { ILogger } from "@thi.ng/logger";
|
|
4
|
-
import type { BigIntArray, CoreAPI, IWasmAPI, WasmExports } from "./api.js";
|
|
4
|
+
import type { BigIntArray, CoreAPI, IWasmAPI, WasmExports, WasmMemViews } from "./api.js";
|
|
5
5
|
export declare const OutOfMemoryError: {
|
|
6
6
|
new (msg?: string | undefined): {
|
|
7
7
|
name: string;
|
|
@@ -28,7 +28,7 @@ export declare const OutOfMemoryError: {
|
|
|
28
28
|
* 64bit integers are handled via JS `BigInt` and hence require the host env to
|
|
29
29
|
* support it. No polyfill is provided.
|
|
30
30
|
*/
|
|
31
|
-
export declare class WasmBridge<T extends WasmExports = WasmExports> {
|
|
31
|
+
export declare class WasmBridge<T extends WasmExports = WasmExports> implements WasmMemViews {
|
|
32
32
|
modules: Record<string, IWasmAPI<T>>;
|
|
33
33
|
logger: ILogger;
|
|
34
34
|
i8: Int8Array;
|
|
@@ -45,7 +45,7 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> {
|
|
|
45
45
|
utf8Encoder: TextEncoder;
|
|
46
46
|
imports: WebAssembly.Imports;
|
|
47
47
|
exports: T;
|
|
48
|
-
|
|
48
|
+
api: CoreAPI;
|
|
49
49
|
constructor(modules?: Record<string, IWasmAPI<T>>, logger?: ILogger);
|
|
50
50
|
/**
|
|
51
51
|
* Instantiates WASM module from given `src` (and optional provided extra
|
|
@@ -95,7 +95,7 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> {
|
|
|
95
95
|
* bridge.getImports();
|
|
96
96
|
* {
|
|
97
97
|
* // imports defined by the core API of the bridge itself
|
|
98
|
-
*
|
|
98
|
+
* wasmapi: { ... },
|
|
99
99
|
* // imports defined by the CustomAPI module
|
|
100
100
|
* custom: { ... }
|
|
101
101
|
* }
|
|
@@ -121,15 +121,26 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> {
|
|
|
121
121
|
* Attempts to allocate `numBytes` using the exported WASM core API function
|
|
122
122
|
* {@link WasmExports._wasm_allocate} (implementation specific) and returns
|
|
123
123
|
* start address of the new memory block. If unsuccessful, throws an
|
|
124
|
-
* {@link OutOfMemoryError}.
|
|
124
|
+
* {@link OutOfMemoryError}. If `clear` is true, the allocated region will
|
|
125
|
+
* be zero-filled.
|
|
125
126
|
*
|
|
126
127
|
* @remarks
|
|
127
128
|
* See {@link WasmExports._wasm_allocate} docs for further details.
|
|
128
129
|
*
|
|
129
130
|
* @param numBytes
|
|
131
|
+
* @param clear
|
|
130
132
|
*/
|
|
131
|
-
allocate(numBytes: number): number;
|
|
132
|
-
|
|
133
|
+
allocate(numBytes: number, clear?: boolean): number;
|
|
134
|
+
/**
|
|
135
|
+
* Frees a previous allocated memory region using the exported WASM core API
|
|
136
|
+
* function {@link WasmExports._wasm_free} (implementation specific). The
|
|
137
|
+
* `numBytes` value must be the same as previously given to
|
|
138
|
+
* {@link WasmBridge.allocate}.
|
|
139
|
+
*
|
|
140
|
+
* @param addr
|
|
141
|
+
* @param numBytes
|
|
142
|
+
*/
|
|
143
|
+
free(addr: number, numBytes: number): void;
|
|
133
144
|
getI8(addr: number): number;
|
|
134
145
|
getU8(addr: number): number;
|
|
135
146
|
getI16(addr: number): number;
|
|
@@ -170,7 +181,30 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> {
|
|
|
170
181
|
setU64Array(addr: number, buf: BigIntArray): this;
|
|
171
182
|
setF32Array(addr: number, buf: NumericArray): this;
|
|
172
183
|
setF64Array(addr: number, buf: NumericArray): this;
|
|
184
|
+
/**
|
|
185
|
+
* Reads UTF-8 encoded string from given address and optional byte length.
|
|
186
|
+
* The default length is 0, which will be interpreted as a zero-terminated
|
|
187
|
+
* string. Returns string.
|
|
188
|
+
*
|
|
189
|
+
* @param addr
|
|
190
|
+
* @param len
|
|
191
|
+
*/
|
|
173
192
|
getString(addr: number, len?: number): string;
|
|
193
|
+
/**
|
|
194
|
+
* Encodes given string as UTF-8 and writes it to WASM memory starting at
|
|
195
|
+
* `addr`. By default the string will be zero-terminated and only `maxBytes`
|
|
196
|
+
* will be written. Returns the number of bytes written.
|
|
197
|
+
*
|
|
198
|
+
* @remarks
|
|
199
|
+
* An error will be thrown if the encoded string doesn't fully fit into the
|
|
200
|
+
* designated memory region (also note that there might need to be space for
|
|
201
|
+
* the additional sentinel/termination byte).
|
|
202
|
+
*
|
|
203
|
+
* @param str
|
|
204
|
+
* @param addr
|
|
205
|
+
* @param maxBytes
|
|
206
|
+
* @param terminate
|
|
207
|
+
*/
|
|
174
208
|
setString(str: string, addr: number, maxBytes: number, terminate?: boolean): number;
|
|
175
209
|
getElementById(addr: number, len?: number): HTMLElement;
|
|
176
210
|
}
|