@ubjs/wasm 0.31.0-6 → 0.31.0-8
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/dist/core/src/async-entry.d.ts +14 -0
- package/dist/core/src/async-entry.js +72 -0
- package/dist/core/src/call.d.ts +11 -6
- package/dist/core/src/call.js +27 -0
- package/dist/core/src/callback.d.ts +7 -11
- package/dist/core/src/callback.js +12 -9
- package/dist/core/src/ffi-type.d.ts +1 -111
- package/dist/core/src/ffi-type.js +8 -22
- package/dist/core/src/future.d.ts +5 -1
- package/dist/core/src/future.js +3 -1
- package/dist/core/src/marshal.d.ts +2 -5
- package/dist/core/src/module.d.ts +9 -14
- package/dist/core/src/module.js +25 -15
- package/package.json +2 -2
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { type AsyncFunctionDef, type FunctionDef } from "@ubjs/core";
|
|
2
|
+
import { type DispatchContext } from "./call.js";
|
|
3
|
+
export declare function hasAsyncEntry(def: FunctionDef): def is FunctionDef & {
|
|
4
|
+
async: AsyncFunctionDef;
|
|
5
|
+
};
|
|
6
|
+
/** Registers a future-starting export as one Promise-returning entry:
|
|
7
|
+
* `(...args, status, signal)` starts the future synchronously (lowered
|
|
8
|
+
* RustBuffer args alias wasm memory and must be consumed before any
|
|
9
|
+
* await), drives it to READY, completes into `status`, frees it, and
|
|
10
|
+
* resolves with the lowered value — a RustBuffer result as an owned copy
|
|
11
|
+
* with its wasm allocation already freed. */
|
|
12
|
+
export declare function buildAsyncEntry(ctx: DispatchContext, exports: WebAssembly.Exports, fnName: string, def: FunctionDef & {
|
|
13
|
+
async: AsyncFunctionDef;
|
|
14
|
+
}): (...args: any[]) => Promise<unknown>;
|
|
@@ -0,0 +1,72 @@
|
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/
|
|
5
|
+
*/
|
|
6
|
+
// An async function as one entry the caller awaits. The player runs UniFFI's
|
|
7
|
+
// poll loop here, so a caller behind a channel pays one round trip per await
|
|
8
|
+
// rather than one per poll.
|
|
9
|
+
import { uniffiAsyncEntry, } from "@ubjs/core";
|
|
10
|
+
import { copyAndFreeRustBufferView, specializeExport, } from "./call.js";
|
|
11
|
+
import { FfiType } from "./ffi-type.js";
|
|
12
|
+
import { CONT_DEF, RUST_FUTURE_CONTINUATION } from "./future.js";
|
|
13
|
+
// UniFFI's future exports have a fixed ABI, so these shapes are not in the
|
|
14
|
+
// table.
|
|
15
|
+
const POLL_DEF = {
|
|
16
|
+
args: [
|
|
17
|
+
FfiType.Handle,
|
|
18
|
+
FfiType.Callback(RUST_FUTURE_CONTINUATION),
|
|
19
|
+
FfiType.Handle,
|
|
20
|
+
],
|
|
21
|
+
ret: FfiType.Void,
|
|
22
|
+
hasRustCallStatus: false,
|
|
23
|
+
};
|
|
24
|
+
const HANDLE_ONLY_DEF = {
|
|
25
|
+
args: [FfiType.Handle],
|
|
26
|
+
ret: FfiType.Void,
|
|
27
|
+
hasRustCallStatus: false,
|
|
28
|
+
};
|
|
29
|
+
export function hasAsyncEntry(def) {
|
|
30
|
+
return Boolean(def.async);
|
|
31
|
+
}
|
|
32
|
+
/** `poll` takes the continuation as a `Callback` arg. UniFFI fixes that
|
|
33
|
+
* callback's shape, so the player's own wins over any shape the table
|
|
34
|
+
* carries under the same name. */
|
|
35
|
+
function specializePoll(ctx, exports, name) {
|
|
36
|
+
// Async functions share poll exports: copy the callbacks only on a miss.
|
|
37
|
+
const cached = ctx.specialized.get(name);
|
|
38
|
+
if (cached)
|
|
39
|
+
return cached;
|
|
40
|
+
const callbackDefs = new Map(ctx.callbackDefs);
|
|
41
|
+
callbackDefs.set(RUST_FUTURE_CONTINUATION, CONT_DEF);
|
|
42
|
+
return specializeExport({ ...ctx, callbackDefs }, exports, name, POLL_DEF);
|
|
43
|
+
}
|
|
44
|
+
/** Registers a future-starting export as one Promise-returning entry:
|
|
45
|
+
* `(...args, status, signal)` starts the future synchronously (lowered
|
|
46
|
+
* RustBuffer args alias wasm memory and must be consumed before any
|
|
47
|
+
* await), drives it to READY, completes into `status`, frees it, and
|
|
48
|
+
* resolves with the lowered value — a RustBuffer result as an owned copy
|
|
49
|
+
* with its wasm allocation already freed. */
|
|
50
|
+
export function buildAsyncEntry(ctx, exports, fnName, def) {
|
|
51
|
+
const start = specializeExport(ctx, exports, fnName, def);
|
|
52
|
+
// `complete` is synchronous, so sharing its scratch is safe.
|
|
53
|
+
const complete = specializeExport(ctx, exports, def.async.complete, {
|
|
54
|
+
args: [FfiType.Handle],
|
|
55
|
+
ret: def.async.ret,
|
|
56
|
+
hasRustCallStatus: true,
|
|
57
|
+
});
|
|
58
|
+
const fns = {
|
|
59
|
+
poll: specializePoll(ctx, exports, def.async.poll),
|
|
60
|
+
cancel: specializeExport(ctx, exports, def.async.cancel, HANDLE_ONLY_DEF),
|
|
61
|
+
free: specializeExport(ctx, exports, def.async.free, HANDLE_ONLY_DEF),
|
|
62
|
+
complete: (rustFuture, status) => {
|
|
63
|
+
const value = complete(rustFuture, status);
|
|
64
|
+
// A view over wasm memory would outlive this call, and the next wasm
|
|
65
|
+
// memory growth would detach it: hand back a copy and free the original.
|
|
66
|
+
return value instanceof Uint8Array
|
|
67
|
+
? copyAndFreeRustBufferView(ctx, value)
|
|
68
|
+
: value;
|
|
69
|
+
},
|
|
70
|
+
};
|
|
71
|
+
return uniffiAsyncEntry(def.args.length, start, fns);
|
|
72
|
+
}
|
package/dist/core/src/call.d.ts
CHANGED
|
@@ -1,13 +1,9 @@
|
|
|
1
1
|
import type { Memory } from "./memory.js";
|
|
2
2
|
import type { Scratch } from "./scratch.js";
|
|
3
3
|
import { type StructLayout } from "./marshal.js";
|
|
4
|
-
import type { FfiTypeDesc } from "./ffi-type.js";
|
|
5
4
|
import type { CallbackDef } from "./callback.js";
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
ret: FfiTypeDesc;
|
|
9
|
-
hasRustCallStatus: boolean;
|
|
10
|
-
}
|
|
5
|
+
import { type FunctionDef } from "@ubjs/core";
|
|
6
|
+
export type { FunctionDef };
|
|
11
7
|
export interface DispatchContext {
|
|
12
8
|
memory: Memory;
|
|
13
9
|
scratch: Scratch;
|
|
@@ -17,6 +13,7 @@ export interface DispatchContext {
|
|
|
17
13
|
free: (ptr: number, size: number, align: number) => void;
|
|
18
14
|
installCallback: (fn: (...args: any[]) => any, def: CallbackDef) => number;
|
|
19
15
|
useJit: boolean;
|
|
16
|
+
specialized: Map<string, (...jsArgs: any[]) => any>;
|
|
20
17
|
}
|
|
21
18
|
/**
|
|
22
19
|
* Probe for `new Function` availability. Returns true if we can JIT-compile
|
|
@@ -25,6 +22,7 @@ export interface DispatchContext {
|
|
|
25
22
|
* lifetime of the module.
|
|
26
23
|
*/
|
|
27
24
|
export declare function canUseFunctionConstructor(): boolean;
|
|
25
|
+
export declare function copyAndFreeRustBufferView(ctx: DispatchContext, view: Uint8Array): Uint8Array;
|
|
28
26
|
export declare function getCapacityHint(view: Uint8Array): number | undefined;
|
|
29
27
|
/**
|
|
30
28
|
* Build a JIT-compiled dispatcher for a function whose arg/return types are
|
|
@@ -43,3 +41,10 @@ export declare function getCapacityHint(view: Uint8Array): number | undefined;
|
|
|
43
41
|
*/
|
|
44
42
|
export declare function buildJitDispatcher(ctx: DispatchContext, exportFn: Function, def: FunctionDef, fnName: string): ((...args: any[]) => unknown) | undefined;
|
|
45
43
|
export declare function specializeFunction(ctx: DispatchContext, exportFn: Function, def: FunctionDef, fnName?: string): (...jsArgs: any[]) => any;
|
|
44
|
+
/**
|
|
45
|
+
* Specializes the wasm export `name` to `shape` once per registration. A
|
|
46
|
+
* name fixes its shape, so one dispatcher serves every caller: async
|
|
47
|
+
* functions with the same return type share their `rust_future_*` exports,
|
|
48
|
+
* and each specialization reserves scratch for good.
|
|
49
|
+
*/
|
|
50
|
+
export declare function specializeExport(ctx: DispatchContext, exports: WebAssembly.Exports, name: string, shape: FunctionDef): (...jsArgs: any[]) => any;
|
package/dist/core/src/call.js
CHANGED
|
@@ -131,6 +131,15 @@ function copyAndFreeRustBuffer(ctx, rb) {
|
|
|
131
131
|
}
|
|
132
132
|
return bytes;
|
|
133
133
|
}
|
|
134
|
+
// The same for a view `viewRustBufferHandoff` returned, for a RustBuffer
|
|
135
|
+
// result that must outlive the next wasm memory growth.
|
|
136
|
+
export function copyAndFreeRustBufferView(ctx, view) {
|
|
137
|
+
const copy = view.slice();
|
|
138
|
+
const cap = getCapacityHint(view) ?? view.byteLength;
|
|
139
|
+
if (cap > 0)
|
|
140
|
+
ctx.free(view.byteOffset, cap, 1);
|
|
141
|
+
return copy;
|
|
142
|
+
}
|
|
134
143
|
// Records a view's Rust-side `capacity`, which `rustbuffer_free` must pass
|
|
135
144
|
// back to satisfy the allocator's `Layout` contract.
|
|
136
145
|
//
|
|
@@ -474,3 +483,21 @@ export function specializeFunction(ctx, exportFn, def, fnName = "dispatch") {
|
|
|
474
483
|
}
|
|
475
484
|
return buildInterpretedDispatcher(ctx, exportFn, def);
|
|
476
485
|
}
|
|
486
|
+
/**
|
|
487
|
+
* Specializes the wasm export `name` to `shape` once per registration. A
|
|
488
|
+
* name fixes its shape, so one dispatcher serves every caller: async
|
|
489
|
+
* functions with the same return type share their `rust_future_*` exports,
|
|
490
|
+
* and each specialization reserves scratch for good.
|
|
491
|
+
*/
|
|
492
|
+
export function specializeExport(ctx, exports, name, shape) {
|
|
493
|
+
let fn = ctx.specialized.get(name);
|
|
494
|
+
if (fn === undefined) {
|
|
495
|
+
const exportFn = exports[name];
|
|
496
|
+
if (typeof exportFn !== "function") {
|
|
497
|
+
throw new Error(`register: wasm export "${name}" not found`);
|
|
498
|
+
}
|
|
499
|
+
fn = specializeFunction(ctx, exportFn, shape, name);
|
|
500
|
+
ctx.specialized.set(name, fn);
|
|
501
|
+
}
|
|
502
|
+
return fn;
|
|
503
|
+
}
|
|
@@ -1,11 +1,10 @@
|
|
|
1
|
-
import type { FfiTypeDesc } from "./ffi-type.js";
|
|
2
1
|
import type { Memory } from "./memory.js";
|
|
2
|
+
import { type CallbackDef } from "@ubjs/core";
|
|
3
|
+
export type { CallbackDef };
|
|
3
4
|
import { type StructLayout } from "./marshal.js";
|
|
4
|
-
export interface
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
hasRustCallStatus: boolean;
|
|
8
|
-
outReturn?: boolean;
|
|
5
|
+
export interface RegistrationContext {
|
|
6
|
+
callbackDefs: Map<string, CallbackDef>;
|
|
7
|
+
structs: Map<string, StructLayout>;
|
|
9
8
|
}
|
|
10
9
|
/**
|
|
11
10
|
* Per-instance dispatch table. `module.ts` owns one of these and routes the
|
|
@@ -34,10 +33,7 @@ export declare class CallbackTable {
|
|
|
34
33
|
constructor(memory: Memory, hostExports: WebAssembly.Exports,
|
|
35
34
|
/** Async compilation is not implemented; `false` is reserved for it. */
|
|
36
35
|
compileSync?: boolean);
|
|
37
|
-
setRegistrationContext(ctx:
|
|
38
|
-
callbackDefs: Map<string, CallbackDef>;
|
|
39
|
-
structs: Map<string, StructLayout>;
|
|
40
|
-
}): void;
|
|
36
|
+
setRegistrationContext(ctx: RegistrationContext): void;
|
|
41
37
|
/**
|
|
42
38
|
* Implements `env.__ubrn_dispatch`. Trampolines prepend their shape id and
|
|
43
39
|
* forward everything else verbatim.
|
|
@@ -57,7 +53,7 @@ export declare class CallbackTable {
|
|
|
57
53
|
*/
|
|
58
54
|
registerContinuationShape(shapeId: number, fn: (...args: any[]) => any): void;
|
|
59
55
|
private fnSlotCache;
|
|
60
|
-
installCallbackFunction(fn: (...args: any[]) => any, def: CallbackDef): number;
|
|
56
|
+
installCallbackFunction(fn: (...args: any[]) => any, def: CallbackDef, regCtx?: RegistrationContext | undefined): number;
|
|
61
57
|
private makeDispatchRoute;
|
|
62
58
|
/**
|
|
63
59
|
* Define (or look up, if already defined) the trampoline for a callback
|
|
@@ -344,7 +344,10 @@ export class CallbackTable {
|
|
|
344
344
|
// Keyed by signature as well as identity: one function installed for two
|
|
345
345
|
// signatures needs two slots, or Rust's `call_indirect` traps.
|
|
346
346
|
fnSlotCache = new WeakMap();
|
|
347
|
-
|
|
347
|
+
// `regCtx` is the registering crate's tables: a callback's Callback-typed
|
|
348
|
+
// args and struct returns resolve against them, not against whichever
|
|
349
|
+
// crate registered last on a module hosting several.
|
|
350
|
+
installCallbackFunction(fn, def, regCtx = this.registrationContext) {
|
|
348
351
|
const sigKey = this.signatureKey(def);
|
|
349
352
|
let bySig = this.fnSlotCache.get(fn);
|
|
350
353
|
const cached = bySig?.get(sigKey);
|
|
@@ -374,7 +377,7 @@ export class CallbackTable {
|
|
|
374
377
|
// dispatcher unwraps the result and writes it back through those
|
|
375
378
|
// pointers. Free/clone (no `outReturn`/`hasRustCallStatus`) and the
|
|
376
379
|
// future continuation route through unchanged.
|
|
377
|
-
const route = this.makeDispatchRoute(fn, def);
|
|
380
|
+
const route = this.makeDispatchRoute(fn, def, regCtx);
|
|
378
381
|
// Routed through the continuation path so all wasm args (including the
|
|
379
382
|
// u64 instance handle in vtable methods) are forwarded verbatim — the JS
|
|
380
383
|
// closure does its own handle-to-instance lift.
|
|
@@ -393,7 +396,7 @@ export class CallbackTable {
|
|
|
393
396
|
bySig.set(sigKey, slot);
|
|
394
397
|
return slot;
|
|
395
398
|
}
|
|
396
|
-
makeDispatchRoute(fn, def) {
|
|
399
|
+
makeDispatchRoute(fn, def, regCtx) {
|
|
397
400
|
const userArgCount = def.args.length;
|
|
398
401
|
const memory = this.memory;
|
|
399
402
|
const alloc = this.hostExports.__ubrn_alloc;
|
|
@@ -412,11 +415,11 @@ export class CallbackTable {
|
|
|
412
415
|
// Direct passthrough — free/clone, the future continuation, etc.
|
|
413
416
|
return fn;
|
|
414
417
|
}
|
|
415
|
-
const installCallback = (cbFn, cbDef) => this.installCallbackFunction(cbFn, cbDef);
|
|
416
|
-
const writeRetCtx =
|
|
418
|
+
const installCallback = (cbFn, cbDef) => this.installCallbackFunction(cbFn, cbDef, regCtx);
|
|
419
|
+
const writeRetCtx = regCtx
|
|
417
420
|
? {
|
|
418
|
-
structs:
|
|
419
|
-
callbackDefs:
|
|
421
|
+
structs: regCtx.structs,
|
|
422
|
+
callbackDefs: regCtx.callbackDefs,
|
|
420
423
|
installCallback,
|
|
421
424
|
}
|
|
422
425
|
: undefined;
|
|
@@ -425,8 +428,8 @@ export class CallbackTable {
|
|
|
425
428
|
alloc,
|
|
426
429
|
free,
|
|
427
430
|
table: this.hostExports.__indirect_function_table,
|
|
428
|
-
callbackDefs:
|
|
429
|
-
structs:
|
|
431
|
+
callbackDefs: regCtx?.callbackDefs,
|
|
432
|
+
structs: regCtx?.structs,
|
|
430
433
|
};
|
|
431
434
|
// `hasStatus` also decides where the FFI return value lives: sync vtable
|
|
432
435
|
// methods wrap it in a `UniffiResult` (`result.pointee`), async ones
|
|
@@ -1,111 +1 @@
|
|
|
1
|
-
export type FfiTypeDesc
|
|
2
|
-
tag: "UInt8";
|
|
3
|
-
} | {
|
|
4
|
-
tag: "Int8";
|
|
5
|
-
} | {
|
|
6
|
-
tag: "UInt16";
|
|
7
|
-
} | {
|
|
8
|
-
tag: "Int16";
|
|
9
|
-
} | {
|
|
10
|
-
tag: "UInt32";
|
|
11
|
-
} | {
|
|
12
|
-
tag: "Int32";
|
|
13
|
-
} | {
|
|
14
|
-
tag: "UInt64";
|
|
15
|
-
} | {
|
|
16
|
-
tag: "Int64";
|
|
17
|
-
} | {
|
|
18
|
-
tag: "Float32";
|
|
19
|
-
} | {
|
|
20
|
-
tag: "Float64";
|
|
21
|
-
} | {
|
|
22
|
-
tag: "Handle";
|
|
23
|
-
} | {
|
|
24
|
-
tag: "RustBuffer";
|
|
25
|
-
} | {
|
|
26
|
-
tag: "ForeignBytes";
|
|
27
|
-
} | {
|
|
28
|
-
tag: "RustCallStatus";
|
|
29
|
-
} | {
|
|
30
|
-
tag: "VoidPointer";
|
|
31
|
-
} | {
|
|
32
|
-
tag: "Void";
|
|
33
|
-
} | {
|
|
34
|
-
tag: "Callback";
|
|
35
|
-
name: string;
|
|
36
|
-
} | {
|
|
37
|
-
tag: "Struct";
|
|
38
|
-
name: string;
|
|
39
|
-
} | {
|
|
40
|
-
tag: "Reference";
|
|
41
|
-
inner: FfiTypeDesc;
|
|
42
|
-
} | {
|
|
43
|
-
tag: "MutReference";
|
|
44
|
-
inner: FfiTypeDesc;
|
|
45
|
-
};
|
|
46
|
-
export declare const FfiType: {
|
|
47
|
-
UInt8: {
|
|
48
|
-
readonly tag: "UInt8";
|
|
49
|
-
};
|
|
50
|
-
Int8: {
|
|
51
|
-
readonly tag: "Int8";
|
|
52
|
-
};
|
|
53
|
-
UInt16: {
|
|
54
|
-
readonly tag: "UInt16";
|
|
55
|
-
};
|
|
56
|
-
Int16: {
|
|
57
|
-
readonly tag: "Int16";
|
|
58
|
-
};
|
|
59
|
-
UInt32: {
|
|
60
|
-
readonly tag: "UInt32";
|
|
61
|
-
};
|
|
62
|
-
Int32: {
|
|
63
|
-
readonly tag: "Int32";
|
|
64
|
-
};
|
|
65
|
-
UInt64: {
|
|
66
|
-
readonly tag: "UInt64";
|
|
67
|
-
};
|
|
68
|
-
Int64: {
|
|
69
|
-
readonly tag: "Int64";
|
|
70
|
-
};
|
|
71
|
-
Float32: {
|
|
72
|
-
readonly tag: "Float32";
|
|
73
|
-
};
|
|
74
|
-
Float64: {
|
|
75
|
-
readonly tag: "Float64";
|
|
76
|
-
};
|
|
77
|
-
Handle: {
|
|
78
|
-
readonly tag: "Handle";
|
|
79
|
-
};
|
|
80
|
-
RustBuffer: {
|
|
81
|
-
readonly tag: "RustBuffer";
|
|
82
|
-
};
|
|
83
|
-
ForeignBytes: {
|
|
84
|
-
readonly tag: "ForeignBytes";
|
|
85
|
-
};
|
|
86
|
-
RustCallStatus: {
|
|
87
|
-
readonly tag: "RustCallStatus";
|
|
88
|
-
};
|
|
89
|
-
VoidPointer: {
|
|
90
|
-
readonly tag: "VoidPointer";
|
|
91
|
-
};
|
|
92
|
-
Void: {
|
|
93
|
-
readonly tag: "Void";
|
|
94
|
-
};
|
|
95
|
-
Callback: (name: string) => {
|
|
96
|
-
readonly tag: "Callback";
|
|
97
|
-
readonly name: string;
|
|
98
|
-
};
|
|
99
|
-
Struct: (name: string) => {
|
|
100
|
-
readonly tag: "Struct";
|
|
101
|
-
readonly name: string;
|
|
102
|
-
};
|
|
103
|
-
Reference: (inner: FfiTypeDesc) => {
|
|
104
|
-
readonly tag: "Reference";
|
|
105
|
-
readonly inner: FfiTypeDesc;
|
|
106
|
-
};
|
|
107
|
-
MutReference: (inner: FfiTypeDesc) => {
|
|
108
|
-
readonly tag: "MutReference";
|
|
109
|
-
readonly inner: FfiTypeDesc;
|
|
110
|
-
};
|
|
111
|
-
};
|
|
1
|
+
export { FfiType, type FfiTypeDesc } from "@ubjs/core";
|
|
@@ -1,22 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
Int64: { tag: "Int64" },
|
|
10
|
-
Float32: { tag: "Float32" },
|
|
11
|
-
Float64: { tag: "Float64" },
|
|
12
|
-
Handle: { tag: "Handle" },
|
|
13
|
-
RustBuffer: { tag: "RustBuffer" },
|
|
14
|
-
ForeignBytes: { tag: "ForeignBytes" },
|
|
15
|
-
RustCallStatus: { tag: "RustCallStatus" },
|
|
16
|
-
VoidPointer: { tag: "VoidPointer" },
|
|
17
|
-
Void: { tag: "Void" },
|
|
18
|
-
Callback: (name) => ({ tag: "Callback", name }),
|
|
19
|
-
Struct: (name) => ({ tag: "Struct", name }),
|
|
20
|
-
Reference: (inner) => ({ tag: "Reference", inner }),
|
|
21
|
-
MutReference: (inner) => ({ tag: "MutReference", inner }),
|
|
22
|
-
};
|
|
1
|
+
/*
|
|
2
|
+
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
3
|
+
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
4
|
+
* file, You can obtain one at http://mozilla.org/MPL/2.0/
|
|
5
|
+
*/
|
|
6
|
+
// The vocabulary lives in @ubjs/core so every player and channel shares one
|
|
7
|
+
// definition; this module only keeps the old import path working.
|
|
8
|
+
export { FfiType } from "@ubjs/core";
|
|
@@ -1,4 +1,8 @@
|
|
|
1
|
-
import type { CallbackTable } from "./callback.js";
|
|
1
|
+
import type { CallbackDef, CallbackTable } from "./callback.js";
|
|
2
|
+
/** The name UniFFI gives the continuation in `FfiType.Callback(...)`. */
|
|
3
|
+
export declare const RUST_FUTURE_CONTINUATION = "RustFutureContinuationCallback";
|
|
4
|
+
/** The single shape used by every `RustFutureContinuationCallback`. */
|
|
5
|
+
export declare const CONT_DEF: CallbackDef;
|
|
2
6
|
export declare class FutureRegistry {
|
|
3
7
|
private callbacks;
|
|
4
8
|
private nextData;
|
package/dist/core/src/future.js
CHANGED
|
@@ -16,8 +16,10 @@
|
|
|
16
16
|
// looks the resolver up by cookie rather than by the usual handle, and
|
|
17
17
|
// calls it with `code`.
|
|
18
18
|
import { FfiType } from "./ffi-type.js";
|
|
19
|
+
/** The name UniFFI gives the continuation in `FfiType.Callback(...)`. */
|
|
20
|
+
export const RUST_FUTURE_CONTINUATION = "RustFutureContinuationCallback";
|
|
19
21
|
/** The single shape used by every `RustFutureContinuationCallback`. */
|
|
20
|
-
const CONT_DEF = {
|
|
22
|
+
export const CONT_DEF = {
|
|
21
23
|
args: [FfiType.UInt64, FfiType.Int8],
|
|
22
24
|
ret: FfiType.Void,
|
|
23
25
|
hasRustCallStatus: false,
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { Memory } from "./memory.js";
|
|
2
2
|
import type { FfiTypeDesc } from "./ffi-type.js";
|
|
3
|
-
import { RustBuffer } from "@ubjs/core";
|
|
3
|
+
import { RustBuffer, type FieldDesc } from "@ubjs/core";
|
|
4
|
+
export type { FieldDesc };
|
|
4
5
|
export declare const RUST_BUFFER_SIZE = 24;
|
|
5
6
|
export declare const RUST_CALL_STATUS_SIZE = 32;
|
|
6
7
|
export declare const FOREIGN_BYTES_SIZE = 8;
|
|
@@ -51,10 +52,6 @@ export interface ForeignBytesLike {
|
|
|
51
52
|
}
|
|
52
53
|
export declare function readForeignBytes(m: Memory, ptr: number): ForeignBytesLike;
|
|
53
54
|
export declare function writeForeignBytes(m: Memory, ptr: number, fb: ForeignBytesLike): void;
|
|
54
|
-
export interface FieldDesc {
|
|
55
|
-
name: string;
|
|
56
|
-
type: FfiTypeDesc;
|
|
57
|
-
}
|
|
58
55
|
export interface CompiledField {
|
|
59
56
|
name: string;
|
|
60
57
|
offset: number;
|
|
@@ -1,21 +1,16 @@
|
|
|
1
1
|
import { Memory } from "./memory.js";
|
|
2
2
|
import { Scratch } from "./scratch.js";
|
|
3
|
-
import {
|
|
4
|
-
import { type FunctionDef } from "./call.js";
|
|
5
|
-
import { CallbackTable, type CallbackDef } from "./callback.js";
|
|
3
|
+
import { CallbackTable } from "./callback.js";
|
|
6
4
|
import { FutureRegistry } from "./future.js";
|
|
5
|
+
import { type ModuleDefinitions } from "@ubjs/core";
|
|
6
|
+
export type { ModuleDefinitions };
|
|
7
7
|
export type WasmSource = WebAssembly.Module | ArrayBuffer | Uint8Array | Response | URL | string;
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
functions: Record<string, FunctionDef>;
|
|
15
|
-
callbacks: Record<string, CallbackDef>;
|
|
16
|
-
structs: Record<string, FieldDesc[]>;
|
|
17
|
-
}
|
|
18
|
-
export type NativeModuleInterface = Record<string, (...args: any[]) => any>;
|
|
8
|
+
/** What `registerSync` returns: a function per definition, and the buffer
|
|
9
|
+
* pair the runtime answers itself, as `@ubjs/worker`'s receiver needs. */
|
|
10
|
+
export type NativeModuleInterface = Record<string, (...args: any[]) => any> & {
|
|
11
|
+
rustbuffer_alloc(n: number): Uint8Array;
|
|
12
|
+
rustbuffer_free(view: Uint8Array): void;
|
|
13
|
+
};
|
|
19
14
|
/**
|
|
20
15
|
* Resolver for unrecognized wasm import modules. The runner side may run
|
|
21
16
|
* `wasm-bindgen-cli --target bundler` against the cdylib, leaving the
|
package/dist/core/src/module.js
CHANGED
|
@@ -5,10 +5,11 @@
|
|
|
5
5
|
*/
|
|
6
6
|
import { Memory } from "./memory.js";
|
|
7
7
|
import { Scratch } from "./scratch.js";
|
|
8
|
-
import { compileStructLayout
|
|
9
|
-
import {
|
|
8
|
+
import { compileStructLayout } from "./marshal.js";
|
|
9
|
+
import { specializeExport, getCapacityHint, canUseFunctionConstructor, } from "./call.js";
|
|
10
10
|
import { CallbackTable } from "./callback.js";
|
|
11
11
|
import { FutureRegistry } from "./future.js";
|
|
12
|
+
import { buildAsyncEntry, hasAsyncEntry } from "./async-entry.js";
|
|
12
13
|
const REQUIRED_EXPORTS = ["__ubrn_alloc", "__ubrn_free", "memory"];
|
|
13
14
|
const SCRATCH_BYTES = 64 * 1024;
|
|
14
15
|
async function buildImports(compiled, importObject, resolveModule) {
|
|
@@ -203,17 +204,13 @@ export class UniffiNativeModule {
|
|
|
203
204
|
callbackDefs,
|
|
204
205
|
alloc: this.alloc,
|
|
205
206
|
free: this.free,
|
|
206
|
-
installCallback: (fn, def) => this.callbacks.installCallbackFunction(fn, def
|
|
207
|
+
installCallback: (fn, def) => this.callbacks.installCallbackFunction(fn, def, {
|
|
208
|
+
callbackDefs,
|
|
209
|
+
structs,
|
|
210
|
+
}),
|
|
207
211
|
useJit,
|
|
212
|
+
specialized: new Map(),
|
|
208
213
|
};
|
|
209
|
-
const result = Object.create(null);
|
|
210
|
-
for (const [fnName, def] of Object.entries(definitions.functions)) {
|
|
211
|
-
const exportFn = this.exports[fnName];
|
|
212
|
-
if (typeof exportFn !== "function") {
|
|
213
|
-
throw new Error(`register: wasm export "${fnName}" not found`);
|
|
214
|
-
}
|
|
215
|
-
result[fnName] = specializeFunction(ctx, exportFn, def, fnName);
|
|
216
|
-
}
|
|
217
214
|
// JS-callable allocator pair, exposed alongside the per-function entries.
|
|
218
215
|
// A converter handed `rustbuffer_alloc` as its `lower(value, alloc)`
|
|
219
216
|
// writes its payload straight into a view over wasm memory;
|
|
@@ -240,18 +237,23 @@ export class UniffiNativeModule {
|
|
|
240
237
|
* Rust-side. After detachment, the view's byteLength becomes 0 and
|
|
241
238
|
* `rustbuffer_free(view)` will leak the original allocation.
|
|
242
239
|
*/
|
|
243
|
-
|
|
240
|
+
const rustbufferAlloc = (n) => {
|
|
244
241
|
if (n === 0)
|
|
245
242
|
return new Uint8Array(0);
|
|
246
243
|
const ptr = alloc(n, 1);
|
|
247
244
|
return new Uint8Array(memory.buffer(), ptr, n);
|
|
248
245
|
};
|
|
249
|
-
|
|
246
|
+
const rustbufferFree = (view) => {
|
|
250
247
|
const hint = getCapacityHint(view);
|
|
251
248
|
if (view.byteLength === 0 && hint === undefined)
|
|
252
249
|
return;
|
|
253
|
-
if (view.buffer
|
|
254
|
-
|
|
250
|
+
if (view.buffer !== memory.buffer()) {
|
|
251
|
+
if (view.buffer.byteLength === 0) {
|
|
252
|
+
throw new Error("rustbuffer_free: view is detached (held across a wasm memory growth?)");
|
|
253
|
+
}
|
|
254
|
+
// A copy on the JS heap, such as an async entry's RustBuffer result,
|
|
255
|
+
// whose wasm allocation is already freed.
|
|
256
|
+
return;
|
|
255
257
|
}
|
|
256
258
|
// Rust may over-allocate a returned RustBuffer, so its capacity can
|
|
257
259
|
// exceed the view's byteLength. The handoff path stashes the real
|
|
@@ -262,6 +264,14 @@ export class UniffiNativeModule {
|
|
|
262
264
|
return;
|
|
263
265
|
free(view.byteOffset, cap, 1);
|
|
264
266
|
};
|
|
267
|
+
const result = Object.create(null);
|
|
268
|
+
for (const [fnName, def] of Object.entries(definitions.functions)) {
|
|
269
|
+
result[fnName] = hasAsyncEntry(def)
|
|
270
|
+
? buildAsyncEntry(ctx, this.exports, fnName, def)
|
|
271
|
+
: specializeExport(ctx, this.exports, fnName, def);
|
|
272
|
+
}
|
|
273
|
+
result.rustbuffer_alloc = rustbufferAlloc;
|
|
274
|
+
result.rustbuffer_free = rustbufferFree;
|
|
265
275
|
return result;
|
|
266
276
|
}
|
|
267
277
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ubjs/wasm",
|
|
3
|
-
"version": "0.31.0-
|
|
3
|
+
"version": "0.31.0-8",
|
|
4
4
|
"description": "WebAssembly player runtime for uniffi-bindgen-react-native generated bindings.",
|
|
5
5
|
"homepage": "https://github.com/jhugman/uniffi-bindgen-react-native",
|
|
6
6
|
"repository": {
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"test": "node --test --import tsx './core/tests/*.test.ts'"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@ubjs/core": "^0.31.0-
|
|
38
|
+
"@ubjs/core": "^0.31.0-8"
|
|
39
39
|
},
|
|
40
40
|
"devDependencies": {
|
|
41
41
|
"@ubjs/core": "file:../../typescript",
|