@ubjs/wasm 0.31.0-5
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/README.md +60 -0
- package/dist/browser/src/index.d.ts +14 -0
- package/dist/browser/src/index.js +26 -0
- package/dist/core/src/call.d.ts +45 -0
- package/dist/core/src/call.js +476 -0
- package/dist/core/src/callback.d.ts +95 -0
- package/dist/core/src/callback.js +562 -0
- package/dist/core/src/ffi-type.d.ts +111 -0
- package/dist/core/src/ffi-type.js +22 -0
- package/dist/core/src/future.d.ts +22 -0
- package/dist/core/src/future.js +64 -0
- package/dist/core/src/index.d.ts +2 -0
- package/dist/core/src/index.js +7 -0
- package/dist/core/src/marshal.d.ts +84 -0
- package/dist/core/src/marshal.js +210 -0
- package/dist/core/src/memory.d.ts +26 -0
- package/dist/core/src/memory.js +75 -0
- package/dist/core/src/module.d.ts +48 -0
- package/dist/core/src/module.js +267 -0
- package/dist/core/src/scratch.d.ts +40 -0
- package/dist/core/src/scratch.js +81 -0
- package/dist/core/src/trampoline.d.ts +9 -0
- package/dist/core/src/trampoline.js +153 -0
- package/dist/node/src/index.d.ts +19 -0
- package/dist/node/src/index.js +35 -0
- package/package.json +47 -0
|
@@ -0,0 +1,111 @@
|
|
|
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
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
export const FfiType = {
|
|
2
|
+
UInt8: { tag: "UInt8" },
|
|
3
|
+
Int8: { tag: "Int8" },
|
|
4
|
+
UInt16: { tag: "UInt16" },
|
|
5
|
+
Int16: { tag: "Int16" },
|
|
6
|
+
UInt32: { tag: "UInt32" },
|
|
7
|
+
Int32: { tag: "Int32" },
|
|
8
|
+
UInt64: { tag: "UInt64" },
|
|
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
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { CallbackTable } from "./callback.js";
|
|
2
|
+
export declare class FutureRegistry {
|
|
3
|
+
private callbacks;
|
|
4
|
+
private nextData;
|
|
5
|
+
private resolvers;
|
|
6
|
+
private contShapeId;
|
|
7
|
+
constructor(callbacks: CallbackTable);
|
|
8
|
+
/**
|
|
9
|
+
* Compile (once) the continuation trampoline and install it at
|
|
10
|
+
* `tableIndex` in the host's indirect function table. Subsequent calls
|
|
11
|
+
* reuse the cached shape but re-install at whichever table index the
|
|
12
|
+
* caller asks for. Returns `tableIndex` for convenience.
|
|
13
|
+
*/
|
|
14
|
+
installContinuation(tableIndex: number): number;
|
|
15
|
+
/**
|
|
16
|
+
* Allocate a fresh continuation cookie and record the resolver to be
|
|
17
|
+
* invoked when the host wasm calls the continuation trampoline with that
|
|
18
|
+
* cookie. The returned `bigint` is the `data` argument the caller should
|
|
19
|
+
* thread through to the Rust-side `rust_future_poll(...)`-style export.
|
|
20
|
+
*/
|
|
21
|
+
allocateContinuation(resolver: (code: number) => void): bigint;
|
|
22
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
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
|
+
// JS-side registry for `RustFutureContinuationCallback`, UniFFI's poll-style
|
|
7
|
+
// async ABI.
|
|
8
|
+
//
|
|
9
|
+
// * The continuation has a *fixed* `(data: u64, code: i8)` signature, so one
|
|
10
|
+
// trampoline serves every future in the cdylib.
|
|
11
|
+
// * `data` is **not** a UniFFI handle. It is an opaque u64 cookie the player
|
|
12
|
+
// allocates per await and the host wasm passes back unchanged when the
|
|
13
|
+
// future settles.
|
|
14
|
+
// * Each await allocates a cookie and records `cookie -> resolver`. When the
|
|
15
|
+
// trampoline fires, the continuation route in `CallbackTable.dispatch`
|
|
16
|
+
// looks the resolver up by cookie rather than by the usual handle, and
|
|
17
|
+
// calls it with `code`.
|
|
18
|
+
import { FfiType } from "./ffi-type.js";
|
|
19
|
+
/** The single shape used by every `RustFutureContinuationCallback`. */
|
|
20
|
+
const CONT_DEF = {
|
|
21
|
+
args: [FfiType.UInt64, FfiType.Int8],
|
|
22
|
+
ret: FfiType.Void,
|
|
23
|
+
hasRustCallStatus: false,
|
|
24
|
+
};
|
|
25
|
+
export class FutureRegistry {
|
|
26
|
+
callbacks;
|
|
27
|
+
nextData = 1n;
|
|
28
|
+
resolvers = new Map();
|
|
29
|
+
contShapeId = -1;
|
|
30
|
+
constructor(callbacks) {
|
|
31
|
+
this.callbacks = callbacks;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Compile (once) the continuation trampoline and install it at
|
|
35
|
+
* `tableIndex` in the host's indirect function table. Subsequent calls
|
|
36
|
+
* reuse the cached shape but re-install at whichever table index the
|
|
37
|
+
* caller asks for. Returns `tableIndex` for convenience.
|
|
38
|
+
*/
|
|
39
|
+
installContinuation(tableIndex) {
|
|
40
|
+
if (this.contShapeId < 0) {
|
|
41
|
+
this.contShapeId = this.callbacks.defineShape("__rust_future_continuation", CONT_DEF);
|
|
42
|
+
this.callbacks.registerContinuationShape(this.contShapeId, (data, code) => {
|
|
43
|
+
const resolver = this.resolvers.get(data);
|
|
44
|
+
if (!resolver)
|
|
45
|
+
return;
|
|
46
|
+
this.resolvers.delete(data);
|
|
47
|
+
resolver(code);
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
this.callbacks.installAt(tableIndex, this.contShapeId);
|
|
51
|
+
return tableIndex;
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Allocate a fresh continuation cookie and record the resolver to be
|
|
55
|
+
* invoked when the host wasm calls the continuation trampoline with that
|
|
56
|
+
* cookie. The returned `bigint` is the `data` argument the caller should
|
|
57
|
+
* thread through to the Rust-side `rust_future_poll(...)`-style export.
|
|
58
|
+
*/
|
|
59
|
+
allocateContinuation(resolver) {
|
|
60
|
+
const data = this.nextData++;
|
|
61
|
+
this.resolvers.set(data, resolver);
|
|
62
|
+
return data;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
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
|
+
export { FfiType } from "./ffi-type.js";
|
|
7
|
+
export { UniffiNativeModule, } from "./module.js";
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Memory } from "./memory.js";
|
|
2
|
+
import type { FfiTypeDesc } from "./ffi-type.js";
|
|
3
|
+
import { RustBuffer } from "@ubjs/core";
|
|
4
|
+
export declare const RUST_BUFFER_SIZE = 24;
|
|
5
|
+
export declare const RUST_CALL_STATUS_SIZE = 32;
|
|
6
|
+
export declare const FOREIGN_BYTES_SIZE = 8;
|
|
7
|
+
export interface RustBufferLike {
|
|
8
|
+
capacity: bigint;
|
|
9
|
+
len: bigint;
|
|
10
|
+
dataPtr: number;
|
|
11
|
+
}
|
|
12
|
+
export declare function readRustBuffer(m: Memory, ptr: number): RustBufferLike;
|
|
13
|
+
export declare function writeRustBuffer(m: Memory, ptr: number, rb: RustBufferLike): void;
|
|
14
|
+
/**
|
|
15
|
+
* Write a 24-byte `RustBuffer` struct at `ptr` (`capacity = len = bytes
|
|
16
|
+
* .byteLength`, `dataPtr` pointing at the payload bytes inside wasm memory).
|
|
17
|
+
*
|
|
18
|
+
* Two payload paths:
|
|
19
|
+
*
|
|
20
|
+
* 1. **Wasm-aliased view (`lower(value, rustbuffer_alloc)`):** the converter
|
|
21
|
+
* has already written its bytes directly into a wasm allocation obtained
|
|
22
|
+
* from `rustbuffer_alloc(n)` — `bytes.buffer` is the wasm linear memory
|
|
23
|
+
* and `bytes.byteOffset` is the `dataPtr`. We just forward
|
|
24
|
+
* `(byteOffset, byteLength)` into the struct.
|
|
25
|
+
*
|
|
26
|
+
* 2. **JS-owned `Uint8Array`:** allocate a fresh wasm buffer of the right
|
|
27
|
+
* size and copy bytes in.
|
|
28
|
+
*
|
|
29
|
+
* Used on every wasm2 lower path that hands a `Uint8Array` to Rust through
|
|
30
|
+
* a `RustBuffer` slot — top-level args, callback args/returns,
|
|
31
|
+
* `RustCallStatus.errorBuf`, and `RustBuffer` fields inside structs.
|
|
32
|
+
* Ownership of the payload bytes transfers to Rust, so the caller does
|
|
33
|
+
* not free `dataPtr`.
|
|
34
|
+
*
|
|
35
|
+
* Throws if `bytes` is a detached view. A detached view is unrecoverable
|
|
36
|
+
* (we cannot read its contents to copy them, and the source buffer is no
|
|
37
|
+
* longer valid wasm memory either). It indicates a contract violation —
|
|
38
|
+
* a wasm-aliased view was held across an operation that grew wasm memory.
|
|
39
|
+
*/
|
|
40
|
+
export declare function writeRustBufferPayload(m: Memory, alloc: (size: number, align: number) => number, ptr: number, bytes: Uint8Array): void;
|
|
41
|
+
export declare const RCS_ERROR_BUF_OFF = 8;
|
|
42
|
+
export interface RustCallStatusLike {
|
|
43
|
+
code: number;
|
|
44
|
+
errorBuf: RustBufferLike;
|
|
45
|
+
}
|
|
46
|
+
export declare function readRustCallStatus(m: Memory, ptr: number): RustCallStatusLike;
|
|
47
|
+
export declare function writeRustCallStatusZero(m: Memory, ptr: number): void;
|
|
48
|
+
export interface ForeignBytesLike {
|
|
49
|
+
len: number;
|
|
50
|
+
dataPtr: number;
|
|
51
|
+
}
|
|
52
|
+
export declare function readForeignBytes(m: Memory, ptr: number): ForeignBytesLike;
|
|
53
|
+
export declare function writeForeignBytes(m: Memory, ptr: number, fb: ForeignBytesLike): void;
|
|
54
|
+
export interface FieldDesc {
|
|
55
|
+
name: string;
|
|
56
|
+
type: FfiTypeDesc;
|
|
57
|
+
}
|
|
58
|
+
export interface CompiledField {
|
|
59
|
+
name: string;
|
|
60
|
+
offset: number;
|
|
61
|
+
type: FfiTypeDesc;
|
|
62
|
+
shape: ScalarShape;
|
|
63
|
+
}
|
|
64
|
+
export interface StructLayout {
|
|
65
|
+
size: number;
|
|
66
|
+
fields: CompiledField[];
|
|
67
|
+
read: (m: Memory, ptr: number) => Record<string, unknown>;
|
|
68
|
+
write: (m: Memory, ptr: number, value: Record<string, unknown>) => void;
|
|
69
|
+
}
|
|
70
|
+
export interface ScalarShape {
|
|
71
|
+
size: number;
|
|
72
|
+
align: number;
|
|
73
|
+
read: (m: Memory, p: number) => unknown;
|
|
74
|
+
write: (m: Memory, p: number, v: any) => void;
|
|
75
|
+
}
|
|
76
|
+
export declare function compileStructLayout(fields: FieldDesc[]): StructLayout;
|
|
77
|
+
/**
|
|
78
|
+
* Lift a `RustBufferLike` view into a `RustBuffer` that aliases the wasm
|
|
79
|
+
* `Memory.buffer()` directly — no intermediate copy. Use this on the hot
|
|
80
|
+
* path for record-shaped types so lift/lower can read/write straight out
|
|
81
|
+
* of wasm memory. The returned `RustBuffer` is only valid until the next
|
|
82
|
+
* memory growth or boundary crossing that may detach the buffer.
|
|
83
|
+
*/
|
|
84
|
+
export declare function liftRustBufferOverMemory(m: Memory, rb: RustBufferLike): RustBuffer;
|
|
@@ -0,0 +1,210 @@
|
|
|
1
|
+
import { RustBuffer } from "@ubjs/core";
|
|
2
|
+
export const RUST_BUFFER_SIZE = 24; // capacity:u64, len:u64, dataPtr:u32 (+pad)
|
|
3
|
+
export const RUST_CALL_STATUS_SIZE = 32; // code:i8 + pad + RustBuffer
|
|
4
|
+
export const FOREIGN_BYTES_SIZE = 8; // len:i32, dataPtr:u32
|
|
5
|
+
const RB_CAPACITY_OFF = 0;
|
|
6
|
+
const RB_LEN_OFF = 8;
|
|
7
|
+
const RB_DATAPTR_OFF = 16;
|
|
8
|
+
export function readRustBuffer(m, ptr) {
|
|
9
|
+
return {
|
|
10
|
+
capacity: m.readU64(ptr + RB_CAPACITY_OFF),
|
|
11
|
+
len: m.readU64(ptr + RB_LEN_OFF),
|
|
12
|
+
dataPtr: m.readU32(ptr + RB_DATAPTR_OFF),
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
export function writeRustBuffer(m, ptr, rb) {
|
|
16
|
+
m.writeU64(ptr + RB_CAPACITY_OFF, rb.capacity);
|
|
17
|
+
m.writeU64(ptr + RB_LEN_OFF, rb.len);
|
|
18
|
+
m.writeU32(ptr + RB_DATAPTR_OFF, rb.dataPtr);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Write a 24-byte `RustBuffer` struct at `ptr` (`capacity = len = bytes
|
|
22
|
+
* .byteLength`, `dataPtr` pointing at the payload bytes inside wasm memory).
|
|
23
|
+
*
|
|
24
|
+
* Two payload paths:
|
|
25
|
+
*
|
|
26
|
+
* 1. **Wasm-aliased view (`lower(value, rustbuffer_alloc)`):** the converter
|
|
27
|
+
* has already written its bytes directly into a wasm allocation obtained
|
|
28
|
+
* from `rustbuffer_alloc(n)` — `bytes.buffer` is the wasm linear memory
|
|
29
|
+
* and `bytes.byteOffset` is the `dataPtr`. We just forward
|
|
30
|
+
* `(byteOffset, byteLength)` into the struct.
|
|
31
|
+
*
|
|
32
|
+
* 2. **JS-owned `Uint8Array`:** allocate a fresh wasm buffer of the right
|
|
33
|
+
* size and copy bytes in.
|
|
34
|
+
*
|
|
35
|
+
* Used on every wasm2 lower path that hands a `Uint8Array` to Rust through
|
|
36
|
+
* a `RustBuffer` slot — top-level args, callback args/returns,
|
|
37
|
+
* `RustCallStatus.errorBuf`, and `RustBuffer` fields inside structs.
|
|
38
|
+
* Ownership of the payload bytes transfers to Rust, so the caller does
|
|
39
|
+
* not free `dataPtr`.
|
|
40
|
+
*
|
|
41
|
+
* Throws if `bytes` is a detached view. A detached view is unrecoverable
|
|
42
|
+
* (we cannot read its contents to copy them, and the source buffer is no
|
|
43
|
+
* longer valid wasm memory either). It indicates a contract violation —
|
|
44
|
+
* a wasm-aliased view was held across an operation that grew wasm memory.
|
|
45
|
+
*/
|
|
46
|
+
export function writeRustBufferPayload(m, alloc, ptr, bytes) {
|
|
47
|
+
const len = bytes.byteLength;
|
|
48
|
+
let dataPtr = 0;
|
|
49
|
+
if (len > 0) {
|
|
50
|
+
if (bytes.buffer === m.buffer()) {
|
|
51
|
+
// Already aliased to wasm memory — no realloc, no copy.
|
|
52
|
+
dataPtr = bytes.byteOffset;
|
|
53
|
+
}
|
|
54
|
+
else if (bytes.buffer.byteLength === 0) {
|
|
55
|
+
// The source view's ArrayBuffer was detached, almost certainly because
|
|
56
|
+
// wasm memory grew between `rustbuffer_alloc(n)` and this call. We
|
|
57
|
+
// cannot recover: the bytes are unreadable.
|
|
58
|
+
throw new Error("writeRustBufferPayload: source view is detached — wasm memory grew " +
|
|
59
|
+
"between rustbuffer_alloc(n) and the FFI call. The view must not " +
|
|
60
|
+
"be held across any operation that can grow wasm memory.");
|
|
61
|
+
}
|
|
62
|
+
else {
|
|
63
|
+
// JS-owned `Uint8Array` — allocate wasm memory and copy.
|
|
64
|
+
dataPtr = alloc(len, 1);
|
|
65
|
+
m.writeBytes(dataPtr, bytes);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
const blen = BigInt(len);
|
|
69
|
+
writeRustBuffer(m, ptr, { capacity: blen, len: blen, dataPtr });
|
|
70
|
+
}
|
|
71
|
+
const RCS_CODE_OFF = 0;
|
|
72
|
+
export const RCS_ERROR_BUF_OFF = 8;
|
|
73
|
+
export function readRustCallStatus(m, ptr) {
|
|
74
|
+
return {
|
|
75
|
+
code: m.readU8(ptr + RCS_CODE_OFF),
|
|
76
|
+
errorBuf: readRustBuffer(m, ptr + RCS_ERROR_BUF_OFF),
|
|
77
|
+
};
|
|
78
|
+
}
|
|
79
|
+
export function writeRustCallStatusZero(m, ptr) {
|
|
80
|
+
// Status is exactly 32 bytes; write zeros via two u64 + one u64 + alignment.
|
|
81
|
+
for (let i = 0; i < RUST_CALL_STATUS_SIZE; i += 8) {
|
|
82
|
+
m.writeU64(ptr + i, 0n);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
const FB_LEN_OFF = 0;
|
|
86
|
+
const FB_DATAPTR_OFF = 4;
|
|
87
|
+
export function readForeignBytes(m, ptr) {
|
|
88
|
+
return {
|
|
89
|
+
len: m.readI32(ptr + FB_LEN_OFF),
|
|
90
|
+
dataPtr: m.readU32(ptr + FB_DATAPTR_OFF),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
export function writeForeignBytes(m, ptr, fb) {
|
|
94
|
+
m.writeI32(ptr + FB_LEN_OFF, fb.len);
|
|
95
|
+
m.writeU32(ptr + FB_DATAPTR_OFF, fb.dataPtr);
|
|
96
|
+
}
|
|
97
|
+
function shapeFor(type) {
|
|
98
|
+
switch (type.tag) {
|
|
99
|
+
case "UInt8":
|
|
100
|
+
case "Int8":
|
|
101
|
+
return {
|
|
102
|
+
size: 1,
|
|
103
|
+
align: 1,
|
|
104
|
+
read: (m, p) => m.readU8(p),
|
|
105
|
+
write: (m, p, v) => m.writeU8(p, v),
|
|
106
|
+
};
|
|
107
|
+
case "UInt32":
|
|
108
|
+
case "Int32":
|
|
109
|
+
case "VoidPointer":
|
|
110
|
+
case "Callback":
|
|
111
|
+
case "Reference":
|
|
112
|
+
case "MutReference":
|
|
113
|
+
return {
|
|
114
|
+
size: 4,
|
|
115
|
+
align: 4,
|
|
116
|
+
read: (m, p) => m.readU32(p),
|
|
117
|
+
write: (m, p, v) => m.writeU32(p, v),
|
|
118
|
+
};
|
|
119
|
+
case "UInt64":
|
|
120
|
+
case "Int64":
|
|
121
|
+
// UniFFI handles are u64 (registry slots); reading/writing them as 32-bit
|
|
122
|
+
// would silently drop the high half — see e.g.
|
|
123
|
+
// `ForeignFutureDroppedCallbackStruct` which has a `handle: Handle` field.
|
|
124
|
+
case "Handle":
|
|
125
|
+
return {
|
|
126
|
+
size: 8,
|
|
127
|
+
align: 8,
|
|
128
|
+
read: (m, p) => m.readU64(p),
|
|
129
|
+
write: (m, p, v) => m.writeU64(p, v),
|
|
130
|
+
};
|
|
131
|
+
case "Float32":
|
|
132
|
+
return {
|
|
133
|
+
size: 4,
|
|
134
|
+
align: 4,
|
|
135
|
+
read: (m, p) => m.readF32(p),
|
|
136
|
+
write: (m, p, v) => m.writeF32(p, v),
|
|
137
|
+
};
|
|
138
|
+
case "Float64":
|
|
139
|
+
return {
|
|
140
|
+
size: 8,
|
|
141
|
+
align: 8,
|
|
142
|
+
read: (m, p) => m.readF64(p),
|
|
143
|
+
write: (m, p, v) => m.writeF64(p, v),
|
|
144
|
+
};
|
|
145
|
+
case "RustBuffer":
|
|
146
|
+
return {
|
|
147
|
+
size: RUST_BUFFER_SIZE,
|
|
148
|
+
align: 8,
|
|
149
|
+
read: (m, p) => readRustBuffer(m, p),
|
|
150
|
+
write: (m, p, v) => writeRustBuffer(m, p, v),
|
|
151
|
+
};
|
|
152
|
+
case "ForeignBytes":
|
|
153
|
+
return {
|
|
154
|
+
size: FOREIGN_BYTES_SIZE,
|
|
155
|
+
align: 4,
|
|
156
|
+
read: (m, p) => readForeignBytes(m, p),
|
|
157
|
+
write: (m, p, v) => writeForeignBytes(m, p, v),
|
|
158
|
+
};
|
|
159
|
+
case "RustCallStatus":
|
|
160
|
+
return {
|
|
161
|
+
size: RUST_CALL_STATUS_SIZE,
|
|
162
|
+
align: 8,
|
|
163
|
+
read: (m, p) => readRustCallStatus(m, p),
|
|
164
|
+
write: () => {
|
|
165
|
+
throw new Error("RustCallStatus is not directly writable");
|
|
166
|
+
},
|
|
167
|
+
};
|
|
168
|
+
default:
|
|
169
|
+
throw new Error(`shapeFor: nested type ${type.tag} not yet supported in struct fields`);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const align = (off, a) => (off + (a - 1)) & ~(a - 1);
|
|
173
|
+
export function compileStructLayout(fields) {
|
|
174
|
+
const compiled = [];
|
|
175
|
+
let off = 0;
|
|
176
|
+
let maxAlign = 1;
|
|
177
|
+
for (const f of fields) {
|
|
178
|
+
const shape = shapeFor(f.type);
|
|
179
|
+
off = align(off, shape.align);
|
|
180
|
+
compiled.push({ name: f.name, offset: off, type: f.type, shape });
|
|
181
|
+
off += shape.size;
|
|
182
|
+
if (shape.align > maxAlign)
|
|
183
|
+
maxAlign = shape.align;
|
|
184
|
+
}
|
|
185
|
+
const size = align(off, maxAlign);
|
|
186
|
+
return {
|
|
187
|
+
size,
|
|
188
|
+
fields: compiled,
|
|
189
|
+
read(m, ptr) {
|
|
190
|
+
const out = {};
|
|
191
|
+
for (const c of compiled)
|
|
192
|
+
out[c.name] = c.shape.read(m, ptr + c.offset);
|
|
193
|
+
return out;
|
|
194
|
+
},
|
|
195
|
+
write(m, ptr, value) {
|
|
196
|
+
for (const c of compiled)
|
|
197
|
+
c.shape.write(m, ptr + c.offset, value[c.name]);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Lift a `RustBufferLike` view into a `RustBuffer` that aliases the wasm
|
|
203
|
+
* `Memory.buffer()` directly — no intermediate copy. Use this on the hot
|
|
204
|
+
* path for record-shaped types so lift/lower can read/write straight out
|
|
205
|
+
* of wasm memory. The returned `RustBuffer` is only valid until the next
|
|
206
|
+
* memory growth or boundary crossing that may detach the buffer.
|
|
207
|
+
*/
|
|
208
|
+
export function liftRustBufferOverMemory(m, rb) {
|
|
209
|
+
return RustBuffer.fromWasmMemory(m.buffer(), rb.dataPtr, Number(rb.len));
|
|
210
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
export declare class Memory {
|
|
2
|
+
private memory;
|
|
3
|
+
private cachedDv;
|
|
4
|
+
private cachedU8;
|
|
5
|
+
constructor(memory: WebAssembly.Memory);
|
|
6
|
+
/** Always-current DataView. Cheap if memory has not grown. */
|
|
7
|
+
dv(): DataView;
|
|
8
|
+
/** Always-current Uint8Array. Cheap if memory has not grown. */
|
|
9
|
+
view(): Uint8Array;
|
|
10
|
+
buffer(): ArrayBuffer;
|
|
11
|
+
readU8(ptr: number): number;
|
|
12
|
+
readU32(ptr: number): number;
|
|
13
|
+
readI32(ptr: number): number;
|
|
14
|
+
readU64(ptr: number): bigint;
|
|
15
|
+
readF32(ptr: number): number;
|
|
16
|
+
readF64(ptr: number): number;
|
|
17
|
+
writeU8(ptr: number, v: number): void;
|
|
18
|
+
writeU32(ptr: number, v: number): void;
|
|
19
|
+
writeI32(ptr: number, v: number): void;
|
|
20
|
+
writeU64(ptr: number, v: bigint): void;
|
|
21
|
+
writeF32(ptr: number, v: number): void;
|
|
22
|
+
writeF64(ptr: number, v: number): void;
|
|
23
|
+
/** Returns a fresh `Uint8Array` copy of `[ptr, ptr+len)`. */
|
|
24
|
+
readBytes(ptr: number, len: number): Uint8Array;
|
|
25
|
+
writeBytes(ptr: number, src: Uint8Array): void;
|
|
26
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
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
|
+
export class Memory {
|
|
7
|
+
memory;
|
|
8
|
+
cachedDv;
|
|
9
|
+
cachedU8;
|
|
10
|
+
constructor(memory) {
|
|
11
|
+
this.memory = memory;
|
|
12
|
+
this.cachedDv = new DataView(memory.buffer);
|
|
13
|
+
this.cachedU8 = new Uint8Array(memory.buffer);
|
|
14
|
+
}
|
|
15
|
+
/** Always-current DataView. Cheap if memory has not grown. */
|
|
16
|
+
dv() {
|
|
17
|
+
if (this.cachedDv.buffer !== this.memory.buffer) {
|
|
18
|
+
this.cachedDv = new DataView(this.memory.buffer);
|
|
19
|
+
}
|
|
20
|
+
return this.cachedDv;
|
|
21
|
+
}
|
|
22
|
+
/** Always-current Uint8Array. Cheap if memory has not grown. */
|
|
23
|
+
view() {
|
|
24
|
+
if (this.cachedU8.buffer !== this.memory.buffer) {
|
|
25
|
+
this.cachedU8 = new Uint8Array(this.memory.buffer);
|
|
26
|
+
}
|
|
27
|
+
return this.cachedU8;
|
|
28
|
+
}
|
|
29
|
+
buffer() {
|
|
30
|
+
return this.memory.buffer;
|
|
31
|
+
}
|
|
32
|
+
readU8(ptr) {
|
|
33
|
+
return this.dv().getUint8(ptr);
|
|
34
|
+
}
|
|
35
|
+
readU32(ptr) {
|
|
36
|
+
return this.dv().getUint32(ptr, true);
|
|
37
|
+
}
|
|
38
|
+
readI32(ptr) {
|
|
39
|
+
return this.dv().getInt32(ptr, true);
|
|
40
|
+
}
|
|
41
|
+
readU64(ptr) {
|
|
42
|
+
return this.dv().getBigUint64(ptr, true);
|
|
43
|
+
}
|
|
44
|
+
readF32(ptr) {
|
|
45
|
+
return this.dv().getFloat32(ptr, true);
|
|
46
|
+
}
|
|
47
|
+
readF64(ptr) {
|
|
48
|
+
return this.dv().getFloat64(ptr, true);
|
|
49
|
+
}
|
|
50
|
+
writeU8(ptr, v) {
|
|
51
|
+
this.dv().setUint8(ptr, v);
|
|
52
|
+
}
|
|
53
|
+
writeU32(ptr, v) {
|
|
54
|
+
this.dv().setUint32(ptr, v, true);
|
|
55
|
+
}
|
|
56
|
+
writeI32(ptr, v) {
|
|
57
|
+
this.dv().setInt32(ptr, v, true);
|
|
58
|
+
}
|
|
59
|
+
writeU64(ptr, v) {
|
|
60
|
+
this.dv().setBigUint64(ptr, v, true);
|
|
61
|
+
}
|
|
62
|
+
writeF32(ptr, v) {
|
|
63
|
+
this.dv().setFloat32(ptr, v, true);
|
|
64
|
+
}
|
|
65
|
+
writeF64(ptr, v) {
|
|
66
|
+
this.dv().setFloat64(ptr, v, true);
|
|
67
|
+
}
|
|
68
|
+
/** Returns a fresh `Uint8Array` copy of `[ptr, ptr+len)`. */
|
|
69
|
+
readBytes(ptr, len) {
|
|
70
|
+
return this.view().slice(ptr, ptr + len);
|
|
71
|
+
}
|
|
72
|
+
writeBytes(ptr, src) {
|
|
73
|
+
this.view().set(src, ptr);
|
|
74
|
+
}
|
|
75
|
+
}
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { Memory } from "./memory.js";
|
|
2
|
+
import { Scratch } from "./scratch.js";
|
|
3
|
+
import { type FieldDesc } from "./marshal.js";
|
|
4
|
+
import { type FunctionDef } from "./call.js";
|
|
5
|
+
import { CallbackTable, type CallbackDef } from "./callback.js";
|
|
6
|
+
import { FutureRegistry } from "./future.js";
|
|
7
|
+
export type WasmSource = WebAssembly.Module | ArrayBuffer | Uint8Array | Response | URL | string;
|
|
8
|
+
export interface ModuleDefinitions {
|
|
9
|
+
symbols: {
|
|
10
|
+
rustbuffer_alloc: string;
|
|
11
|
+
rustbuffer_free: string;
|
|
12
|
+
rustbuffer_from_bytes: string;
|
|
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>;
|
|
19
|
+
/**
|
|
20
|
+
* Resolver for unrecognized wasm import modules. The runner side may run
|
|
21
|
+
* `wasm-bindgen-cli --target bundler` against the cdylib, leaving the
|
|
22
|
+
* rewritten wasm with imports against `./<crate>_bg.js`. The env-specific
|
|
23
|
+
* entry (node/browser) supplies a resolver that dynamic-imports those
|
|
24
|
+
* relative module paths and hands the named exports back. Any module the
|
|
25
|
+
* resolver can't satisfy is filled with throwing stubs so calling into a
|
|
26
|
+
* truly-unwired path fails loudly rather than silently no-op'ing.
|
|
27
|
+
*/
|
|
28
|
+
export type ImportResolver = (moduleName: string) => Promise<Record<string, unknown> | undefined>;
|
|
29
|
+
export declare class UniffiNativeModule {
|
|
30
|
+
readonly memory: Memory;
|
|
31
|
+
readonly scratch: Scratch;
|
|
32
|
+
readonly exports: WebAssembly.Exports;
|
|
33
|
+
readonly instance: WebAssembly.Instance;
|
|
34
|
+
readonly callbacks: CallbackTable;
|
|
35
|
+
readonly futures: FutureRegistry;
|
|
36
|
+
readonly alloc: (size: number, align: number) => number;
|
|
37
|
+
readonly free: (ptr: number, size: number, align: number) => void;
|
|
38
|
+
private constructor();
|
|
39
|
+
static open(source: WasmSource, options?: {
|
|
40
|
+
resolveModule?: ImportResolver;
|
|
41
|
+
}): Promise<UniffiNativeModule>;
|
|
42
|
+
register(definitions: ModuleDefinitions, opts?: {
|
|
43
|
+
disableJit?: boolean;
|
|
44
|
+
}): Promise<NativeModuleInterface>;
|
|
45
|
+
registerSync(definitions: ModuleDefinitions, opts?: {
|
|
46
|
+
disableJit?: boolean;
|
|
47
|
+
}): NativeModuleInterface;
|
|
48
|
+
}
|