@thi.ng/wasm-api 0.18.0 → 1.0.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 +190 -102
- package/README.md +96 -468
- package/api.d.ts +24 -329
- package/api.js +0 -15
- package/bridge.d.ts +0 -31
- package/bridge.js +8 -33
- package/include/wasmapi.h +46 -42
- package/include/wasmapi_types.h +35 -0
- package/index.d.ts +0 -5
- package/index.js +0 -5
- package/package.json +15 -47
- package/pointer.d.ts +20 -0
- package/pointer.js +27 -0
- package/string.d.ts +48 -15
- package/string.js +67 -17
- package/zig/lib.zig +6 -31
- package/zig/types.zig +52 -0
- package/bin/wasm-api +0 -12
- package/bin/wasm-api.mjs_ +0 -17
- package/cli.d.ts +0 -5
- package/cli.js +0 -188
- package/codegen/align.d.ts +0 -14
- package/codegen/align.js +0 -35
- package/codegen/c.d.ts +0 -33
- package/codegen/c.js +0 -100
- package/codegen/c11.d.ts +0 -22
- package/codegen/c11.js +0 -145
- package/codegen/typescript.d.ts +0 -25
- package/codegen/typescript.js +0 -240
- package/codegen/utils.d.ts +0 -96
- package/codegen/utils.js +0 -120
- package/codegen/zig.d.ts +0 -23
- package/codegen/zig.js +0 -149
- package/codegen.d.ts +0 -32
- package/codegen.js +0 -198
package/api.d.ts
CHANGED
|
@@ -1,9 +1,26 @@
|
|
|
1
|
-
import type {
|
|
2
|
-
import type { Pow2 } from "@thi.ng/binary";
|
|
1
|
+
import type { Fn, IDeref, ILength } from "@thi.ng/api";
|
|
3
2
|
import type { WasmBridge } from "./bridge.js";
|
|
4
|
-
export declare const PKG_NAME = "@thi.ng/wasm-api";
|
|
5
3
|
export declare const EVENT_MEMORY_CHANGED = "memory-changed";
|
|
6
4
|
export declare type BigIntArray = bigint[] | BigInt64Array | BigUint64Array;
|
|
5
|
+
export declare type ReadonlyWasmString = IDeref<string> & ILength & {
|
|
6
|
+
readonly addr: number;
|
|
7
|
+
};
|
|
8
|
+
export interface WasmTypeBase {
|
|
9
|
+
/**
|
|
10
|
+
* Base address in linear WASM memory.
|
|
11
|
+
*/
|
|
12
|
+
readonly __base: number;
|
|
13
|
+
/**
|
|
14
|
+
* Obtain as byte buffer
|
|
15
|
+
*/
|
|
16
|
+
readonly __bytes: Uint8Array;
|
|
17
|
+
}
|
|
18
|
+
export interface WasmType<T> {
|
|
19
|
+
readonly align: number;
|
|
20
|
+
readonly size: number;
|
|
21
|
+
instance: Fn<number, T>;
|
|
22
|
+
}
|
|
23
|
+
export declare type WasmTypeConstructor<T> = Fn<IWasmMemoryAccess, WasmType<T>>;
|
|
7
24
|
/**
|
|
8
25
|
* Common interface for WASM/JS child APIs which will be used in combination
|
|
9
26
|
* with a parent {@link WasmBridge}.
|
|
@@ -157,7 +174,8 @@ export interface IWasmMemoryAccess {
|
|
|
157
174
|
/**
|
|
158
175
|
* Encodes given string as UTF-8 and writes it to WASM memory starting at
|
|
159
176
|
* `addr`. By default the string will be zero-terminated and only `maxBytes`
|
|
160
|
-
* will be written. Returns the number of bytes written
|
|
177
|
+
* will be written. Returns the number of bytes written (excluding final
|
|
178
|
+
* sentinel, if any).
|
|
161
179
|
*
|
|
162
180
|
* @remarks
|
|
163
181
|
* An error will be thrown if the encoded string doesn't fully fit into the
|
|
@@ -205,335 +223,12 @@ export interface CoreAPI extends WebAssembly.ModuleImports {
|
|
|
205
223
|
_printU64Array: (addr: number, len: number) => void;
|
|
206
224
|
_printF32Array: (addr: number, len: number) => void;
|
|
207
225
|
_printF64Array: (addr: number, len: number) => void;
|
|
208
|
-
|
|
226
|
+
printStrZ: (addr: number) => void;
|
|
209
227
|
_printStr: (addr: number, len: number) => void;
|
|
228
|
+
printHexdump: (addr: number, len: number) => void;
|
|
210
229
|
debug: () => void;
|
|
211
230
|
_panic: (addr: number, len: number) => void;
|
|
212
231
|
timer: () => number;
|
|
213
232
|
epoch: () => bigint;
|
|
214
233
|
}
|
|
215
|
-
export interface WasmTypeBase {
|
|
216
|
-
/**
|
|
217
|
-
* Base address in linear WASM memory.
|
|
218
|
-
*/
|
|
219
|
-
readonly __base: number;
|
|
220
|
-
/**
|
|
221
|
-
* Obtain as byte buffer
|
|
222
|
-
*/
|
|
223
|
-
readonly __bytes: Uint8Array;
|
|
224
|
-
}
|
|
225
|
-
export interface WasmType<T> {
|
|
226
|
-
readonly align: number;
|
|
227
|
-
readonly size: number;
|
|
228
|
-
instance: Fn<number, T>;
|
|
229
|
-
}
|
|
230
|
-
export declare type WasmTypeConstructor<T> = Fn<IWasmMemoryAccess, WasmType<T>>;
|
|
231
|
-
export declare type WasmInt = "i8" | "i16" | "i32" | "i64";
|
|
232
|
-
export declare type WasmUint = "u8" | "u16" | "u32" | "u64";
|
|
233
|
-
export declare type WasmFloat = FloatType;
|
|
234
|
-
export declare type WasmPrim = WasmInt | WasmUint | WasmFloat;
|
|
235
|
-
export declare type WasmPrim32 = Exclude<WasmPrim, BigType>;
|
|
236
|
-
export declare type ReadonlyWasmString = IDeref<string> & ILength & {
|
|
237
|
-
readonly addr: number;
|
|
238
|
-
};
|
|
239
|
-
export declare type TypeColl = IObjectOf<TopLevelType>;
|
|
240
|
-
export interface TypeInfo {
|
|
241
|
-
/**
|
|
242
|
-
* Auto-computed size (in bytes)
|
|
243
|
-
*
|
|
244
|
-
* @internal
|
|
245
|
-
*/
|
|
246
|
-
__size?: number;
|
|
247
|
-
/**
|
|
248
|
-
* Auto-computed offset (in bytes) in parent struct.
|
|
249
|
-
*
|
|
250
|
-
* @internal
|
|
251
|
-
*/
|
|
252
|
-
__offset?: number;
|
|
253
|
-
/**
|
|
254
|
-
* Auto-computed alignment (in bytes) actually used.
|
|
255
|
-
*
|
|
256
|
-
* @internal
|
|
257
|
-
*/
|
|
258
|
-
__align?: Pow2;
|
|
259
|
-
}
|
|
260
|
-
export interface TopLevelType extends TypeInfo {
|
|
261
|
-
/**
|
|
262
|
-
* Type name
|
|
263
|
-
*/
|
|
264
|
-
name: string;
|
|
265
|
-
/**
|
|
266
|
-
* Optional (multi-line) docstring for this type
|
|
267
|
-
*/
|
|
268
|
-
doc?: string | string[];
|
|
269
|
-
/**
|
|
270
|
-
* Type / kind
|
|
271
|
-
*/
|
|
272
|
-
type: "enum" | "struct" | "union";
|
|
273
|
-
/**
|
|
274
|
-
* Optional object of user provided source codes to be injected into the
|
|
275
|
-
* generated type (after generated fields). Keys of this object are language
|
|
276
|
-
* IDs (`ts` for {@link TYPESCRIPT}, `zig` for {@link ZIG}).
|
|
277
|
-
*
|
|
278
|
-
* @remarks
|
|
279
|
-
* Currently only supported by the code gens mentioned, ignored otherwise.
|
|
280
|
-
*/
|
|
281
|
-
body?: IObjectOf<string | string[] | InjectedBody>;
|
|
282
|
-
}
|
|
283
|
-
export interface InjectedBody {
|
|
284
|
-
decl?: string | string[];
|
|
285
|
-
impl?: string | string[];
|
|
286
|
-
}
|
|
287
|
-
export interface Struct extends TopLevelType {
|
|
288
|
-
type: "struct";
|
|
289
|
-
/**
|
|
290
|
-
* Array of struct fields (might be re-ordered if {@link Struct.auto} is
|
|
291
|
-
* enabled).
|
|
292
|
-
*/
|
|
293
|
-
fields: Field[];
|
|
294
|
-
/**
|
|
295
|
-
* If true, struct fields will be re-ordered in descending order based on
|
|
296
|
-
* their {@link TypeInfo.__align} size. This might result in overall smaller
|
|
297
|
-
* structs due to minimizing implicit inter-field padding caused by
|
|
298
|
-
* alignment requirements. **If this option is enabled, then the struct MUST
|
|
299
|
-
* NOT contain any padding fields!**
|
|
300
|
-
*
|
|
301
|
-
* @defaultValue false
|
|
302
|
-
*/
|
|
303
|
-
auto?: boolean;
|
|
304
|
-
/**
|
|
305
|
-
* Optional qualifier for the kind of struct to be emitted (codegen specific
|
|
306
|
-
* interpretation, currently only used by {@link ZIG}).
|
|
307
|
-
*/
|
|
308
|
-
tag?: "extern" | "packed";
|
|
309
|
-
/**
|
|
310
|
-
* Optional user supplied {@link AlignStrategy}. By default uses
|
|
311
|
-
* {@link ALIGN_C} or {@link ALIGN_PACKED} (if using "packed" structs).
|
|
312
|
-
*/
|
|
313
|
-
align?: AlignStrategy;
|
|
314
|
-
}
|
|
315
|
-
export interface Union extends TopLevelType {
|
|
316
|
-
type: "union";
|
|
317
|
-
/**
|
|
318
|
-
* Array of union fields.
|
|
319
|
-
*/
|
|
320
|
-
fields: Field[];
|
|
321
|
-
/**
|
|
322
|
-
* Optional qualifier for the kind of struct to be emitted (codegen specific
|
|
323
|
-
* interpretation, currently only used by {@link ZIG}).
|
|
324
|
-
*/
|
|
325
|
-
tag?: "extern" | "packed";
|
|
326
|
-
/**
|
|
327
|
-
* Optional user supplied {@link AlignStrategy}. By default uses
|
|
328
|
-
* {@link ALIGN_C} or {@link ALIGN_PACKED} (if using "packed" union).
|
|
329
|
-
*/
|
|
330
|
-
align?: AlignStrategy;
|
|
331
|
-
}
|
|
332
|
-
export declare type FieldTag = "scalar" | "array" | "ptr" | "slice" | "vec";
|
|
333
|
-
export interface Field extends TypeInfo {
|
|
334
|
-
/**
|
|
335
|
-
* Field name (prefix: "__" is reserved)
|
|
336
|
-
*/
|
|
337
|
-
name: string;
|
|
338
|
-
/**
|
|
339
|
-
* Field docstring (can be multiline, will be formatted)
|
|
340
|
-
*/
|
|
341
|
-
doc?: string | string[];
|
|
342
|
-
/**
|
|
343
|
-
* Field type tag/qualifier (note: `slice` & `vec` are only supported by Zig
|
|
344
|
-
* & TS).
|
|
345
|
-
*
|
|
346
|
-
* @remarks
|
|
347
|
-
* - Array & vector fields are statically sized (using
|
|
348
|
-
* {@link Field.len})
|
|
349
|
-
* - Pointers are emitted as single-value pointers (where this distinction
|
|
350
|
-
* exist), i.e. even if they're pointing to multiple values, there's no
|
|
351
|
-
* explicit length encoded/available
|
|
352
|
-
* - Zig slices are essentially a pointer w/ associated length
|
|
353
|
-
* - Zig vectors will be processed using SIMD (if enabled in WASM target)
|
|
354
|
-
* and therefore will have stricter (larger) alignment requirements.
|
|
355
|
-
*
|
|
356
|
-
* @defaultValue "scalar"
|
|
357
|
-
*/
|
|
358
|
-
tag?: FieldTag;
|
|
359
|
-
/**
|
|
360
|
-
* Field base type. If not a {@link WasmPrim}, `string` or `opaque`, the
|
|
361
|
-
* value is interpreted as another type name in the {@link TypeColl}.
|
|
362
|
-
*
|
|
363
|
-
* @remarks
|
|
364
|
-
* Please see {@link CodeGenOpts.stringType} and consult package readme for
|
|
365
|
-
* further details re: string handling.
|
|
366
|
-
*
|
|
367
|
-
* TODO `opaque` currently unsupported.
|
|
368
|
-
*/
|
|
369
|
-
type: WasmPrim | "string" | "opaque" | string;
|
|
370
|
-
/**
|
|
371
|
-
* Const qualifier (default is true for `string`, false for all other
|
|
372
|
-
* types). Only used for pointers or slices.
|
|
373
|
-
*/
|
|
374
|
-
const?: boolean;
|
|
375
|
-
/**
|
|
376
|
-
* Currently only supported for {@link ZIG} arrays & slices, otherwise
|
|
377
|
-
* ignored!
|
|
378
|
-
*/
|
|
379
|
-
sentinel?: number;
|
|
380
|
-
/**
|
|
381
|
-
* Array or vector length (see {@link Field.tag})
|
|
382
|
-
*/
|
|
383
|
-
len?: number;
|
|
384
|
-
/**
|
|
385
|
-
* Currently only supported for {@link ZIG}, otherwise ignored!
|
|
386
|
-
*
|
|
387
|
-
* @remarks
|
|
388
|
-
* The object form allows for different default values per language (in
|
|
389
|
-
* theory). So if given as object, the keys refer to the lang ID and the
|
|
390
|
-
* values as the defaults for those languages.
|
|
391
|
-
*/
|
|
392
|
-
default?: NumOrString | IObjectOf<NumOrString>;
|
|
393
|
-
/**
|
|
394
|
-
* If defined and > 0, the field will be considered for padding purposes
|
|
395
|
-
* only and the value provided is the number of bytes used. All other config
|
|
396
|
-
* for this field will be ignored!
|
|
397
|
-
*/
|
|
398
|
-
pad?: number;
|
|
399
|
-
}
|
|
400
|
-
export interface Enum extends TopLevelType {
|
|
401
|
-
type: "enum";
|
|
402
|
-
/**
|
|
403
|
-
* No i64/u64 support, due to Typescript not supporting bigint enum values.
|
|
404
|
-
* For C compatibility only i32 or u32 is allowed.
|
|
405
|
-
*
|
|
406
|
-
* @defaultValue "i32"
|
|
407
|
-
*/
|
|
408
|
-
tag: Exclude<WasmPrim32, FloatType>;
|
|
409
|
-
/**
|
|
410
|
-
* List of possible values/IDs. Use {@link EnumValue}s for more detailed
|
|
411
|
-
* config.
|
|
412
|
-
*/
|
|
413
|
-
values: (string | EnumValue)[];
|
|
414
|
-
}
|
|
415
|
-
export interface EnumValue {
|
|
416
|
-
/**
|
|
417
|
-
* Enum value name/ID
|
|
418
|
-
*/
|
|
419
|
-
name: string;
|
|
420
|
-
/**
|
|
421
|
-
* Optional associated numeric value
|
|
422
|
-
*/
|
|
423
|
-
value?: number;
|
|
424
|
-
/**
|
|
425
|
-
* Optional docstring for this value
|
|
426
|
-
*/
|
|
427
|
-
doc?: string;
|
|
428
|
-
}
|
|
429
|
-
export interface AlignStrategy {
|
|
430
|
-
/**
|
|
431
|
-
* Returns implementation specific alignment for given struct field.
|
|
432
|
-
*/
|
|
433
|
-
align: Fn<Field, Pow2>;
|
|
434
|
-
/**
|
|
435
|
-
* Returns possibly rounded value for given base size & alignment.
|
|
436
|
-
*/
|
|
437
|
-
size: Fn2<number, Pow2, number>;
|
|
438
|
-
/**
|
|
439
|
-
* Returns possibly rounded value for given base offset & alignment.
|
|
440
|
-
*/
|
|
441
|
-
offset: Fn2<number, Pow2, number>;
|
|
442
|
-
}
|
|
443
|
-
export interface CodeGenOptsBase {
|
|
444
|
-
/**
|
|
445
|
-
* Optional string to be injected before generated type defs (but after
|
|
446
|
-
* codegen's own prelude, if any)
|
|
447
|
-
*/
|
|
448
|
-
pre?: string;
|
|
449
|
-
/**
|
|
450
|
-
* Optional string to be injected after generated type defs (but before
|
|
451
|
-
* codegen's own epilogue, if any)
|
|
452
|
-
*/
|
|
453
|
-
post?: string;
|
|
454
|
-
}
|
|
455
|
-
/**
|
|
456
|
-
* Global/shared code generator options.
|
|
457
|
-
*/
|
|
458
|
-
export interface CodeGenOpts extends CodeGenOptsBase {
|
|
459
|
-
/**
|
|
460
|
-
* WASM target specification.
|
|
461
|
-
*
|
|
462
|
-
* @defaultValue {@link WASM32}
|
|
463
|
-
*/
|
|
464
|
-
target: WasmTarget;
|
|
465
|
-
/**
|
|
466
|
-
* Identifier how strings are stored on WASM side, e.g. in Zig string
|
|
467
|
-
* literals are slices (8 bytes), in C just plain pointers (4 bytes).
|
|
468
|
-
*
|
|
469
|
-
* @defaultValue "slice"
|
|
470
|
-
*/
|
|
471
|
-
stringType: "slice" | "ptr";
|
|
472
|
-
/**
|
|
473
|
-
* If true (default), forces uppercase enum identifiers.
|
|
474
|
-
*
|
|
475
|
-
* @remarks
|
|
476
|
-
* This option is ignored in {@link ZIG} since it's idiomatic for that
|
|
477
|
-
* language to only use lowercase/camelCase enum IDs.
|
|
478
|
-
*
|
|
479
|
-
* @defaultValue true
|
|
480
|
-
*/
|
|
481
|
-
uppercaseEnums: boolean;
|
|
482
|
-
/**
|
|
483
|
-
* Unless set to false, the generated output will be prefixed with a header
|
|
484
|
-
* line comment of generator meta data
|
|
485
|
-
*/
|
|
486
|
-
header: boolean;
|
|
487
|
-
/**
|
|
488
|
-
* If true, codegens MAY generate various additional struct & struct field
|
|
489
|
-
* analysis functions (sizes, alignment, offsets etc.).
|
|
490
|
-
*
|
|
491
|
-
* @defaultValue false
|
|
492
|
-
*/
|
|
493
|
-
debug: boolean;
|
|
494
|
-
/**
|
|
495
|
-
* Target line width for word wrapping doc strings
|
|
496
|
-
*
|
|
497
|
-
* @defaultValue 80
|
|
498
|
-
*/
|
|
499
|
-
lineWidth: number;
|
|
500
|
-
}
|
|
501
|
-
export interface ICodeGen {
|
|
502
|
-
/**
|
|
503
|
-
* Optional prelude source, to be prepended before any generated type defs.
|
|
504
|
-
*/
|
|
505
|
-
pre?: Fn<CodeGenOpts, string>;
|
|
506
|
-
/**
|
|
507
|
-
* Optional source code to be appended after any generated type defs.
|
|
508
|
-
*/
|
|
509
|
-
post?: Fn<CodeGenOpts, string>;
|
|
510
|
-
/**
|
|
511
|
-
* Docstring codegen
|
|
512
|
-
*/
|
|
513
|
-
doc: (doc: string | string[], acc: string[], opts: CodeGenOpts, topLevel?: boolean) => void;
|
|
514
|
-
/**
|
|
515
|
-
* Codegen for enum types.
|
|
516
|
-
*/
|
|
517
|
-
enum: (type: Enum, types: TypeColl, acc: string[], opts: CodeGenOpts) => void;
|
|
518
|
-
/**
|
|
519
|
-
* Codegen for struct types.
|
|
520
|
-
*/
|
|
521
|
-
struct: (type: Struct, types: TypeColl, acc: string[], opts: CodeGenOpts) => void;
|
|
522
|
-
/**
|
|
523
|
-
* Codegen for union types.
|
|
524
|
-
*/
|
|
525
|
-
union: (type: Union, types: TypeColl, acc: string[], opts: CodeGenOpts) => void;
|
|
526
|
-
}
|
|
527
|
-
export interface WasmTarget {
|
|
528
|
-
usize: "u32" | "u64";
|
|
529
|
-
usizeBytes: number;
|
|
530
|
-
}
|
|
531
|
-
/**
|
|
532
|
-
* WASM32 target spec
|
|
533
|
-
*/
|
|
534
|
-
export declare const WASM32: WasmTarget;
|
|
535
|
-
/**
|
|
536
|
-
* WASM64 target spec
|
|
537
|
-
*/
|
|
538
|
-
export declare const WASM64: WasmTarget;
|
|
539
234
|
//# sourceMappingURL=api.d.ts.map
|
package/api.js
CHANGED
|
@@ -1,16 +1 @@
|
|
|
1
|
-
export const PKG_NAME = "@thi.ng/wasm-api";
|
|
2
1
|
export const EVENT_MEMORY_CHANGED = "memory-changed";
|
|
3
|
-
/**
|
|
4
|
-
* WASM32 target spec
|
|
5
|
-
*/
|
|
6
|
-
export const WASM32 = {
|
|
7
|
-
usize: "u32",
|
|
8
|
-
usizeBytes: 4,
|
|
9
|
-
};
|
|
10
|
-
/**
|
|
11
|
-
* WASM64 target spec
|
|
12
|
-
*/
|
|
13
|
-
export const WASM64 = {
|
|
14
|
-
usize: "u64",
|
|
15
|
-
usizeBytes: 8,
|
|
16
|
-
};
|
package/bridge.d.ts
CHANGED
|
@@ -130,13 +130,6 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
|
|
|
130
130
|
* ```
|
|
131
131
|
*/
|
|
132
132
|
getImports(): WebAssembly.Imports;
|
|
133
|
-
/**
|
|
134
|
-
* Attempts to grow the WASM memory by an additional `numPages` (64KB/page)
|
|
135
|
-
* and if successful updates all typed memory views to use the new
|
|
136
|
-
* underlying buffer.
|
|
137
|
-
*
|
|
138
|
-
* @param numPages
|
|
139
|
-
*/
|
|
140
133
|
growMemory(numPages: number): void;
|
|
141
134
|
allocate(numBytes: number, clear?: boolean): MemorySlice;
|
|
142
135
|
free([addr, numBytes]: MemorySlice): void;
|
|
@@ -180,31 +173,7 @@ export declare class WasmBridge<T extends WasmExports = WasmExports> implements
|
|
|
180
173
|
setU64Array(addr: number, buf: BigIntArray): this;
|
|
181
174
|
setF32Array(addr: number, buf: NumericArray): this;
|
|
182
175
|
setF64Array(addr: number, buf: NumericArray): this;
|
|
183
|
-
/**
|
|
184
|
-
* Reads UTF-8 encoded string from given address and optional byte length.
|
|
185
|
-
* The default length is 0, which will be interpreted as a zero-terminated
|
|
186
|
-
* string. Returns string.
|
|
187
|
-
*
|
|
188
|
-
* @param addr
|
|
189
|
-
* @param len
|
|
190
|
-
*/
|
|
191
176
|
getString(addr: number, len?: number): string;
|
|
192
|
-
/**
|
|
193
|
-
* Encodes given string as UTF-8 and writes it to WASM memory starting at
|
|
194
|
-
* `addr`. By default the string will be zero-terminated and only `maxBytes`
|
|
195
|
-
* will be written. Returns the number of bytes written (excluding final
|
|
196
|
-
* sentinel, if any).
|
|
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
|
-
*/
|
|
208
177
|
setString(str: string, addr: number, maxBytes: number, terminate?: boolean): number;
|
|
209
178
|
getElementById(addr: number, len?: number): HTMLElement;
|
|
210
179
|
/** {@inheritDoc @thi.ng/api#INotify.addListener} */
|
package/bridge.js
CHANGED
|
@@ -3,7 +3,7 @@ import { INotifyMixin } from "@thi.ng/api/mixins/inotify";
|
|
|
3
3
|
import { topoSort } from "@thi.ng/arrays/topo-sort";
|
|
4
4
|
import { assert } from "@thi.ng/errors/assert";
|
|
5
5
|
import { defError } from "@thi.ng/errors/deferror";
|
|
6
|
-
import { U16, U32, U64BIG, U8 } from "@thi.ng/hex";
|
|
6
|
+
import { hexdumpLines, U16, U32, U64BIG, U8 } from "@thi.ng/hex";
|
|
7
7
|
import { ConsoleLogger } from "@thi.ng/logger/console";
|
|
8
8
|
import { EVENT_MEMORY_CHANGED, } from "./api.js";
|
|
9
9
|
export const Panic = defError(() => "Panic");
|
|
@@ -47,6 +47,12 @@ let WasmBridge = class WasmBridge {
|
|
|
47
47
|
printU16Hex: (x) => this.logger.debug(() => `0x${U16(x)}`),
|
|
48
48
|
printU32Hex: (x) => this.logger.debug(() => `0x${U32(x)}`),
|
|
49
49
|
printU64Hex: (x) => this.logger.debug(() => `0x${U64BIG(x)}`),
|
|
50
|
+
printHexdump: (addr, len) => {
|
|
51
|
+
this.ensureMemory();
|
|
52
|
+
for (let line of hexdumpLines(this.u8, addr, len)) {
|
|
53
|
+
this.logger.debug(line);
|
|
54
|
+
}
|
|
55
|
+
},
|
|
50
56
|
_printI8Array: logA(this.getI8Array.bind(this)),
|
|
51
57
|
_printU8Array: logA(this.getU8Array.bind(this)),
|
|
52
58
|
_printI16Array: logA(this.getI16Array.bind(this)),
|
|
@@ -57,7 +63,7 @@ let WasmBridge = class WasmBridge {
|
|
|
57
63
|
_printU64Array: logA(this.getU64Array.bind(this)),
|
|
58
64
|
_printF32Array: logA(this.getF32Array.bind(this)),
|
|
59
65
|
_printF64Array: logA(this.getF64Array.bind(this)),
|
|
60
|
-
|
|
66
|
+
printStrZ: (addr) => this.logger.debug(() => this.getString(addr, 0)),
|
|
61
67
|
_printStr: (addr, len) => this.logger.debug(() => this.getString(addr, len)),
|
|
62
68
|
debug: () => {
|
|
63
69
|
debugger;
|
|
@@ -192,13 +198,6 @@ let WasmBridge = class WasmBridge {
|
|
|
192
198
|
}
|
|
193
199
|
return this.imports;
|
|
194
200
|
}
|
|
195
|
-
/**
|
|
196
|
-
* Attempts to grow the WASM memory by an additional `numPages` (64KB/page)
|
|
197
|
-
* and if successful updates all typed memory views to use the new
|
|
198
|
-
* underlying buffer.
|
|
199
|
-
*
|
|
200
|
-
* @param numPages
|
|
201
|
-
*/
|
|
202
201
|
growMemory(numPages) {
|
|
203
202
|
this.exports.memory.grow(numPages);
|
|
204
203
|
this.ensureMemory();
|
|
@@ -364,34 +363,10 @@ let WasmBridge = class WasmBridge {
|
|
|
364
363
|
this.f64.set(buf, addr >> 3);
|
|
365
364
|
return this;
|
|
366
365
|
}
|
|
367
|
-
/**
|
|
368
|
-
* Reads UTF-8 encoded string from given address and optional byte length.
|
|
369
|
-
* The default length is 0, which will be interpreted as a zero-terminated
|
|
370
|
-
* string. Returns string.
|
|
371
|
-
*
|
|
372
|
-
* @param addr
|
|
373
|
-
* @param len
|
|
374
|
-
*/
|
|
375
366
|
getString(addr, len = 0) {
|
|
376
367
|
this.ensureMemory();
|
|
377
368
|
return this.utf8Decoder.decode(this.u8.subarray(addr, len > 0 ? addr + len : this.u8.indexOf(0, addr)));
|
|
378
369
|
}
|
|
379
|
-
/**
|
|
380
|
-
* Encodes given string as UTF-8 and writes it to WASM memory starting at
|
|
381
|
-
* `addr`. By default the string will be zero-terminated and only `maxBytes`
|
|
382
|
-
* will be written. Returns the number of bytes written (excluding final
|
|
383
|
-
* sentinel, if any).
|
|
384
|
-
*
|
|
385
|
-
* @remarks
|
|
386
|
-
* An error will be thrown if the encoded string doesn't fully fit into the
|
|
387
|
-
* designated memory region (also note that there might need to be space for
|
|
388
|
-
* the additional sentinel/termination byte).
|
|
389
|
-
*
|
|
390
|
-
* @param str
|
|
391
|
-
* @param addr
|
|
392
|
-
* @param maxBytes
|
|
393
|
-
* @param terminate
|
|
394
|
-
*/
|
|
395
370
|
setString(str, addr, maxBytes, terminate = true) {
|
|
396
371
|
this.ensureMemory();
|
|
397
372
|
maxBytes = Math.min(maxBytes, this.u8.length - addr);
|
package/include/wasmapi.h
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
#pragma once
|
|
2
2
|
|
|
3
|
+
#include "wasmapi_types.h"
|
|
4
|
+
|
|
3
5
|
#ifdef __cplusplus
|
|
4
6
|
extern "C" {
|
|
5
7
|
#endif
|
|
@@ -7,70 +9,72 @@ extern "C" {
|
|
|
7
9
|
#include <stddef.h>
|
|
8
10
|
#include <stdint.h>
|
|
9
11
|
|
|
10
|
-
// Declares an
|
|
11
|
-
// The prefix is only used for the C side,
|
|
12
|
-
#define
|
|
12
|
+
// Declares an extern symbol from named import module
|
|
13
|
+
// The prefix is only used for the C side, _not_ for exported NAME
|
|
14
|
+
#define WASMAPI_IMPORT(MODULE, TYPE, NAME, PREFIX) \
|
|
13
15
|
extern __attribute__((import_module(MODULE), import_name(#NAME))) TYPE PREFIX##NAME
|
|
14
16
|
|
|
15
17
|
// Same as EMSCRIPTEN_KEEP_ALIVE, ensures symbol will be exported
|
|
16
|
-
#define
|
|
18
|
+
#define WASMAPI_KEEP __attribute__((used))
|
|
17
19
|
|
|
18
20
|
// Generate malloc/free wrappers only if explicitly enabled by defining this
|
|
19
21
|
// symbol. If undefined some function stubs are exported.
|
|
20
22
|
#ifdef WASMAPI_MALLOC
|
|
21
23
|
|
|
22
24
|
#include <stdlib.h>
|
|
23
|
-
size_t
|
|
25
|
+
size_t WASMAPI_KEEP _wasm_allocate(size_t numBytes) {
|
|
24
26
|
return (size_t)malloc(numBytes);
|
|
25
27
|
}
|
|
26
|
-
void
|
|
28
|
+
void WASMAPI_KEEP _wasm_free(size_t addr, size_t numBytes) {
|
|
27
29
|
free((void*)addr);
|
|
28
30
|
}
|
|
29
31
|
|
|
30
32
|
#else
|
|
31
33
|
|
|
32
|
-
size_t
|
|
34
|
+
size_t WASMAPI_KEEP _wasm_allocate(size_t numBytes) {
|
|
33
35
|
return 0;
|
|
34
36
|
}
|
|
35
|
-
void
|
|
37
|
+
void WASMAPI_KEEP _wasm_free(size_t addr, size_t numBytes) {
|
|
36
38
|
}
|
|
37
39
|
|
|
38
40
|
#endif // WASMAPI_MALLOC
|
|
39
41
|
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
42
|
+
WASMAPI_IMPORT("wasmapi", void, printI8, wasm_)(int8_t x);
|
|
43
|
+
WASMAPI_IMPORT("wasmapi", void, printU8, wasm_)(uint8_t x);
|
|
44
|
+
WASMAPI_IMPORT("wasmapi", void, printU8Hex, wasm_)(uint8_t x);
|
|
45
|
+
WASMAPI_IMPORT("wasmapi", void, printI16, wasm_)(int16_t x);
|
|
46
|
+
WASMAPI_IMPORT("wasmapi", void, printU16, wasm_)(uint16_t x);
|
|
47
|
+
WASMAPI_IMPORT("wasmapi", void, printU16Hex, wasm_)(uint16_t x);
|
|
48
|
+
WASMAPI_IMPORT("wasmapi", void, printI32, wasm_)(int32_t x);
|
|
49
|
+
WASMAPI_IMPORT("wasmapi", void, printU32, wasm_)(uint32_t x);
|
|
50
|
+
WASMAPI_IMPORT("wasmapi", void, printU32Hex, wasm_)(uint32_t x);
|
|
51
|
+
WASMAPI_IMPORT("wasmapi", void, printI64, wasm_)(int64_t x);
|
|
52
|
+
WASMAPI_IMPORT("wasmapi", void, printU64, wasm_)(uint64_t x);
|
|
53
|
+
WASMAPI_IMPORT("wasmapi", void, printU64Hex, wasm_)(uint64_t x);
|
|
54
|
+
WASMAPI_IMPORT("wasmapi", void, printF32, wasm_)(float x);
|
|
55
|
+
WASMAPI_IMPORT("wasmapi", void, printF64, wasm_)(double x);
|
|
56
|
+
|
|
57
|
+
WASMAPI_IMPORT("wasmapi", void, printHexdump, wasm_)(const void* addr, size_t len);
|
|
58
|
+
|
|
59
|
+
WASMAPI_IMPORT("wasmapi", void, _printI8Array, wasm)(const int8_t* addr, size_t len);
|
|
60
|
+
WASMAPI_IMPORT("wasmapi", void, _printU8Array, wasm)(const uint8_t* addr, size_t len);
|
|
61
|
+
WASMAPI_IMPORT("wasmapi", void, _printI16Array, wasm)(const int16_t* addr, size_t len);
|
|
62
|
+
WASMAPI_IMPORT("wasmapi", void, _printU16Array, wasm)(const uint16_t* addr, size_t len);
|
|
63
|
+
WASMAPI_IMPORT("wasmapi", void, _printI32Array, wasm)(const int32_t* addr, size_t len);
|
|
64
|
+
WASMAPI_IMPORT("wasmapi", void, _printU32Array, wasm)(const uint32_t* addr, size_t len);
|
|
65
|
+
WASMAPI_IMPORT("wasmapi", void, _printI64Array, wasm)(const int64_t* addr, size_t len);
|
|
66
|
+
WASMAPI_IMPORT("wasmapi", void, _printU64Array, wasm)(const uint64_t* addr, size_t len);
|
|
67
|
+
WASMAPI_IMPORT("wasmapi", void, _printF32Array, wasm)(const float* addr, size_t len);
|
|
68
|
+
WASMAPI_IMPORT("wasmapi", void, _printF64Array, wasm)(const double* addr, size_t len);
|
|
69
|
+
|
|
70
|
+
WASMAPI_IMPORT("wasmapi", void, printStrZ, wasm_)(const char* addr);
|
|
71
|
+
WASMAPI_IMPORT("wasmapi", void, _printStr, wasm)(const char* addr, size_t len);
|
|
72
|
+
|
|
73
|
+
WASMAPI_IMPORT("wasmapi", void, debug, wasm_)(void);
|
|
74
|
+
WASMAPI_IMPORT("wasmapi", void, panic, wasm_)(const char*, size_t len);
|
|
75
|
+
|
|
76
|
+
WASMAPI_IMPORT("wasmapi", double, timer, wasm_)(void);
|
|
77
|
+
WASMAPI_IMPORT("wasmapi", uint64_t, epoch, wasm_)(void);
|
|
74
78
|
|
|
75
79
|
void wasm_printPtr(const void* ptr) {
|
|
76
80
|
wasm_printU32Hex((size_t)ptr);
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#pragma once
|
|
2
|
+
|
|
3
|
+
#ifdef __cplusplus
|
|
4
|
+
extern "C" {
|
|
5
|
+
#endif
|
|
6
|
+
|
|
7
|
+
#include <stddef.h>
|
|
8
|
+
|
|
9
|
+
typedef char* WASM_StringPtr;
|
|
10
|
+
typedef const char* WASM_ConstStringPtr;
|
|
11
|
+
|
|
12
|
+
typedef void* WASM_OpaquePtr;
|
|
13
|
+
typedef const void* WASM_ConstOpaquePtr;
|
|
14
|
+
|
|
15
|
+
// slice-based string
|
|
16
|
+
typedef struct { WASM_StringPtr ptr; size_t len; } WASM_String;
|
|
17
|
+
typedef struct { WASM_ConstStringPtr ptr; size_t len; } WASM_ConstString;
|
|
18
|
+
|
|
19
|
+
// defines slice wrapper structs for given pointer types
|
|
20
|
+
#define WASMAPI_SLICE(PREFIX, NAME, PTR, CONSTPTR) \
|
|
21
|
+
typedef struct { PTR* ptr; size_t len; } PREFIX##NAME##Slice; \
|
|
22
|
+
typedef struct { CONSTPTR* ptr; size_t len; } PREFIX##Const##NAME##Slice;
|
|
23
|
+
|
|
24
|
+
// slice of string slices
|
|
25
|
+
WASMAPI_SLICE(WASM_, String, WASM_String, WASM_ConstString)
|
|
26
|
+
|
|
27
|
+
// slice of string pointers
|
|
28
|
+
WASMAPI_SLICE(WASM_, StringPtr, WASM_StringPtr, WASM_ConstStringPtr)
|
|
29
|
+
|
|
30
|
+
// slice of opaque pointers
|
|
31
|
+
WASMAPI_SLICE(WASM_, OpaquePtr, WASM_OpaquePtr, WASM_ConstOpaquePtr)
|
|
32
|
+
|
|
33
|
+
#ifdef __cplusplus
|
|
34
|
+
}
|
|
35
|
+
#endif
|
package/index.d.ts
CHANGED
|
@@ -1,10 +1,5 @@
|
|
|
1
1
|
export * from "./api.js";
|
|
2
2
|
export * from "./bridge.js";
|
|
3
|
-
export * from "./codegen.js";
|
|
4
|
-
export * from "./codegen/c11.js";
|
|
5
|
-
export * from "./codegen/typescript.js";
|
|
6
|
-
export * from "./codegen/utils.js";
|
|
7
|
-
export * from "./codegen/zig.js";
|
|
8
3
|
export * from "./object-index.js";
|
|
9
4
|
export * from "./pointer.js";
|
|
10
5
|
export * from "./string.js";
|