@slatedb/uniffi 0.12.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/README.md +211 -0
- package/index.d.ts +1 -0
- package/index.js +7 -0
- package/package.json +47 -0
- package/prebuilds/darwin-arm64/libslatedb_uniffi.dylib +0 -0
- package/prebuilds/darwin-x64/libslatedb_uniffi.dylib +0 -0
- package/prebuilds/linux-arm64-gnu/libslatedb_uniffi.so +0 -0
- package/prebuilds/linux-x64-gnu/libslatedb_uniffi.so +0 -0
- package/prebuilds/win32-arm64/slatedb_uniffi.dll +0 -0
- package/prebuilds/win32-x64/slatedb_uniffi.dll +0 -0
- package/runtime/async-rust-call.d.ts +86 -0
- package/runtime/async-rust-call.js +217 -0
- package/runtime/callbacks.d.ts +122 -0
- package/runtime/callbacks.js +392 -0
- package/runtime/errors.d.ts +120 -0
- package/runtime/errors.js +274 -0
- package/runtime/ffi-converters.d.ts +100 -0
- package/runtime/ffi-converters.js +758 -0
- package/runtime/ffi-types.d.ts +120 -0
- package/runtime/ffi-types.js +456 -0
- package/runtime/handle-map.d.ts +35 -0
- package/runtime/handle-map.js +137 -0
- package/runtime/objects.d.ts +68 -0
- package/runtime/objects.js +469 -0
- package/runtime/rust-call.d.ts +77 -0
- package/runtime/rust-call.js +233 -0
- package/slatedb-ffi.d.ts +732 -0
- package/slatedb-ffi.js +11480 -0
- package/slatedb.d.ts +1393 -0
- package/slatedb.js +5873 -0
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { DuplicateHandleError, StaleHandleError } from "./errors.js";
|
|
2
|
+
import { normalizeHandle, normalizeUInt64 } from "./ffi-types.js";
|
|
3
|
+
|
|
4
|
+
export const INVALID_HANDLE = 0n;
|
|
5
|
+
export const defaultUniffiHandle = INVALID_HANDLE;
|
|
6
|
+
export const FIRST_FOREIGN_HANDLE = 1n;
|
|
7
|
+
export const FOREIGN_HANDLE_STEP = 2n;
|
|
8
|
+
|
|
9
|
+
function requireHandle(handle, label = "handle") {
|
|
10
|
+
const normalized = normalizeHandle(handle);
|
|
11
|
+
if (normalized == null || normalized === INVALID_HANDLE) {
|
|
12
|
+
throw new TypeError(`${label} must be a non-zero UniFFI handle.`);
|
|
13
|
+
}
|
|
14
|
+
return normalized;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
function normalizeHandleStep(step) {
|
|
18
|
+
const normalized = normalizeUInt64(step ?? FOREIGN_HANDLE_STEP);
|
|
19
|
+
if (normalized == null || normalized === 0n) {
|
|
20
|
+
throw new TypeError("handleStep must be a positive integer.");
|
|
21
|
+
}
|
|
22
|
+
return normalized;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
export class UniffiHandleMap {
|
|
26
|
+
constructor({
|
|
27
|
+
firstHandle = FIRST_FOREIGN_HANDLE,
|
|
28
|
+
handleStep = FOREIGN_HANDLE_STEP,
|
|
29
|
+
} = {}) {
|
|
30
|
+
this._map = new Map();
|
|
31
|
+
this._nextHandle = requireHandle(firstHandle, "firstHandle");
|
|
32
|
+
this._handleStep = normalizeHandleStep(handleStep);
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
_allocateHandle() {
|
|
36
|
+
const handle = this._nextHandle;
|
|
37
|
+
this._nextHandle += this._handleStep;
|
|
38
|
+
return handle;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
insert(value) {
|
|
42
|
+
let handle = this._allocateHandle();
|
|
43
|
+
while (this._map.has(handle)) {
|
|
44
|
+
handle = this._allocateHandle();
|
|
45
|
+
}
|
|
46
|
+
this._map.set(handle, value);
|
|
47
|
+
return handle;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
register(handle, value) {
|
|
51
|
+
const normalized = requireHandle(handle);
|
|
52
|
+
if (this._map.has(normalized)) {
|
|
53
|
+
throw new DuplicateHandleError(normalized);
|
|
54
|
+
}
|
|
55
|
+
this._map.set(normalized, value);
|
|
56
|
+
return normalized;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
get(handle) {
|
|
60
|
+
const normalized = requireHandle(handle);
|
|
61
|
+
if (!this._map.has(normalized)) {
|
|
62
|
+
throw new StaleHandleError(normalized);
|
|
63
|
+
}
|
|
64
|
+
return this._map.get(normalized);
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
peek(handle) {
|
|
68
|
+
const normalized = normalizeHandle(handle);
|
|
69
|
+
if (normalized == null || normalized === INVALID_HANDLE) {
|
|
70
|
+
return undefined;
|
|
71
|
+
}
|
|
72
|
+
return this._map.get(normalized);
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
clone(handle) {
|
|
76
|
+
return this.insert(this.get(handle));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
remove(handle) {
|
|
80
|
+
const normalized = normalizeHandle(handle);
|
|
81
|
+
if (normalized == null || normalized === INVALID_HANDLE) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
const value = this._map.get(normalized);
|
|
86
|
+
if (this._map.has(normalized)) {
|
|
87
|
+
this._map.delete(normalized);
|
|
88
|
+
return value;
|
|
89
|
+
}
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
take(handle) {
|
|
94
|
+
const normalized = requireHandle(handle);
|
|
95
|
+
if (!this._map.has(normalized)) {
|
|
96
|
+
throw new StaleHandleError(normalized);
|
|
97
|
+
}
|
|
98
|
+
const value = this._map.get(normalized);
|
|
99
|
+
this._map.delete(normalized);
|
|
100
|
+
return value;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
has(handle) {
|
|
104
|
+
const normalized = normalizeHandle(handle);
|
|
105
|
+
return normalized != null
|
|
106
|
+
&& normalized !== INVALID_HANDLE
|
|
107
|
+
&& this._map.has(normalized);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
clear() {
|
|
111
|
+
this._map.clear();
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
keys() {
|
|
115
|
+
return this._map.keys();
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
values() {
|
|
119
|
+
return this._map.values();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
entries() {
|
|
123
|
+
return this._map.entries();
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
[Symbol.iterator]() {
|
|
127
|
+
return this.entries();
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
get size() {
|
|
131
|
+
return this._map.size;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
export function createHandleMap(options = undefined) {
|
|
136
|
+
return new UniffiHandleMap(options);
|
|
137
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ByteReader, ByteWriter } from "./ffi-converters.js";
|
|
2
|
+
|
|
3
|
+
export declare const uniffiTypeNameSymbol: unique symbol;
|
|
4
|
+
export declare const uniffiObjectFactorySymbol: unique symbol;
|
|
5
|
+
export declare const uniffiObjectHandleSymbol: unique symbol;
|
|
6
|
+
export declare const uniffiObjectDestroyedSymbol: unique symbol;
|
|
7
|
+
export declare const UNIFFI_OBJECT_HANDLE_SIZE: 8;
|
|
8
|
+
|
|
9
|
+
export interface UniffiObjectFactoryOptions<T extends object = UniffiObjectBase> {
|
|
10
|
+
typeName?: string;
|
|
11
|
+
createInstance?: () => T;
|
|
12
|
+
cloneFreeUsesUniffiHandle?: boolean | null;
|
|
13
|
+
cloneHandle?: ((handle: unknown, value: T) => unknown) | null;
|
|
14
|
+
cloneHandleGeneric?: ((handle: unknown, value: T) => unknown) | null;
|
|
15
|
+
cloneHandleRawExternal?: ((handle: unknown, value: T) => unknown) | null;
|
|
16
|
+
freeHandle?: ((handle: unknown, value?: T) => void) | null;
|
|
17
|
+
freeHandleGeneric?: ((handle: unknown, value?: T) => void) | null;
|
|
18
|
+
freeHandleRawExternal?: ((handle: unknown, value?: T) => void) | null;
|
|
19
|
+
handleType?: unknown | (() => unknown) | null;
|
|
20
|
+
serializeHandle?: ((handle: unknown, value: T) => bigint | number) | null;
|
|
21
|
+
deserializeHandle?: ((handle: bigint | number) => unknown) | null;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export declare class UniffiObjectBase {
|
|
25
|
+
readonly [uniffiTypeNameSymbol]?: string;
|
|
26
|
+
readonly [uniffiObjectHandleSymbol]?: unknown;
|
|
27
|
+
readonly [uniffiObjectDestroyedSymbol]?: boolean;
|
|
28
|
+
get uniffiTypeName(): string;
|
|
29
|
+
get uniffiHandle(): unknown;
|
|
30
|
+
uniffiDestroy(): boolean;
|
|
31
|
+
dispose(): boolean;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export declare class UniffiObjectFactory<T extends object = UniffiObjectBase> {
|
|
35
|
+
readonly typeName: string;
|
|
36
|
+
constructor(options?: UniffiObjectFactoryOptions<T>);
|
|
37
|
+
attach(instance: T, handle: unknown): T;
|
|
38
|
+
bless(instance: T, handle: unknown): T;
|
|
39
|
+
create(handle: unknown): T;
|
|
40
|
+
createGenericAbi(handle: unknown): T;
|
|
41
|
+
createRawExternal(handle: unknown): T;
|
|
42
|
+
handle(value: T): unknown;
|
|
43
|
+
peekHandle(value: unknown): unknown;
|
|
44
|
+
cloneHandle(value: T): unknown;
|
|
45
|
+
destroy(value: unknown): boolean;
|
|
46
|
+
isInstance(value: unknown): value is T;
|
|
47
|
+
isDestroyed(value: unknown): boolean;
|
|
48
|
+
usesRawExternal(value: unknown): boolean;
|
|
49
|
+
serializeHandle(value: T): bigint;
|
|
50
|
+
deserializeHandle(serialized: bigint | number): unknown;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export declare class FfiConverterObject<T extends object = UniffiObjectBase> {
|
|
54
|
+
readonly factory: UniffiObjectFactory<T>;
|
|
55
|
+
constructor(factory: UniffiObjectFactory<T>);
|
|
56
|
+
lower(value: T): bigint;
|
|
57
|
+
lift(handle: bigint | number): T;
|
|
58
|
+
write(value: T, writer: ByteWriter): void;
|
|
59
|
+
read(reader: ByteReader): T;
|
|
60
|
+
allocationSize(value?: T): 8;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export declare function createObjectFactory<T extends object = UniffiObjectBase>(
|
|
64
|
+
options?: UniffiObjectFactoryOptions<T>,
|
|
65
|
+
): UniffiObjectFactory<T>;
|
|
66
|
+
export declare function createObjectConverter<T extends object = UniffiObjectBase>(
|
|
67
|
+
factory: UniffiObjectFactory<T>,
|
|
68
|
+
): FfiConverterObject<T>;
|
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
import koffi from "koffi";
|
|
2
|
+
import { UnexpectedNullPointer } from "./errors.js";
|
|
3
|
+
import { isNullPointer, normalizeUInt64, pointerAddress } from "./ffi-types.js";
|
|
4
|
+
|
|
5
|
+
const uniffiFinalizerTokenSymbol = Symbol("uniffi.finalizerToken");
|
|
6
|
+
const uniffiObjectGenericAbiSymbol = Symbol("uniffi.objectGenericAbi");
|
|
7
|
+
const uniffiObjectRawExternalSymbol = Symbol("uniffi.objectRawExternal");
|
|
8
|
+
|
|
9
|
+
export const uniffiTypeNameSymbol = Symbol("uniffi.typeName");
|
|
10
|
+
export const uniffiObjectFactorySymbol = Symbol("uniffi.objectFactory");
|
|
11
|
+
export const uniffiObjectHandleSymbol = Symbol("uniffi.objectHandle");
|
|
12
|
+
export const uniffiObjectDestroyedSymbol = Symbol("uniffi.objectDestroyed");
|
|
13
|
+
export const UNIFFI_OBJECT_HANDLE_SIZE = 8;
|
|
14
|
+
|
|
15
|
+
function requireObjectInstance(value, label = "UniFFI object") {
|
|
16
|
+
if (typeof value !== "object" || value == null) {
|
|
17
|
+
throw new TypeError(`${label} instances must be non-null objects.`);
|
|
18
|
+
}
|
|
19
|
+
return value;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
function requireHandle(handle, label = "UniFFI handle") {
|
|
23
|
+
if (handle == null || isNullPointer(handle)) {
|
|
24
|
+
throw new UnexpectedNullPointer(`${label} is null or has already been destroyed.`);
|
|
25
|
+
}
|
|
26
|
+
return handle;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function storedHandle(value) {
|
|
30
|
+
const handle = value?.[uniffiObjectHandleSymbol];
|
|
31
|
+
if (handle == null || isNullPointer(handle)) {
|
|
32
|
+
return undefined;
|
|
33
|
+
}
|
|
34
|
+
return handle;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function objectFactory(value) {
|
|
38
|
+
const factory = value?.[uniffiObjectFactorySymbol];
|
|
39
|
+
if (
|
|
40
|
+
factory == null
|
|
41
|
+
|| typeof factory.create !== "function"
|
|
42
|
+
|| typeof factory.destroy !== "function"
|
|
43
|
+
|| typeof factory.handle !== "function"
|
|
44
|
+
) {
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
return factory;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
function resolveHandleType(handleType) {
|
|
51
|
+
return typeof handleType === "function"
|
|
52
|
+
? handleType()
|
|
53
|
+
: handleType;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export class UniffiObjectBase {
|
|
57
|
+
get uniffiTypeName() {
|
|
58
|
+
return this[uniffiTypeNameSymbol] ?? this.constructor?.name ?? "UniFFI object";
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
get uniffiHandle() {
|
|
62
|
+
const factory = objectFactory(this);
|
|
63
|
+
if (factory == null) {
|
|
64
|
+
throw new TypeError(`${this.uniffiTypeName} is not attached to a UniFFI object factory.`);
|
|
65
|
+
}
|
|
66
|
+
return factory.handle(this);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
uniffiDestroy() {
|
|
70
|
+
const factory = objectFactory(this);
|
|
71
|
+
if (factory == null) {
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
return factory.destroy(this);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
dispose() {
|
|
78
|
+
return this.uniffiDestroy();
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export class UniffiObjectFactory {
|
|
83
|
+
constructor({
|
|
84
|
+
typeName = "UniFFI object",
|
|
85
|
+
createInstance = () => Object.create(UniffiObjectBase.prototype),
|
|
86
|
+
cloneFreeUsesUniffiHandle = false,
|
|
87
|
+
cloneHandle = undefined,
|
|
88
|
+
cloneHandleGeneric = undefined,
|
|
89
|
+
cloneHandleRawExternal = undefined,
|
|
90
|
+
freeHandle = undefined,
|
|
91
|
+
freeHandleGeneric = undefined,
|
|
92
|
+
freeHandleRawExternal = undefined,
|
|
93
|
+
handleType = undefined,
|
|
94
|
+
serializeHandle = undefined,
|
|
95
|
+
deserializeHandle = undefined,
|
|
96
|
+
} = {}) {
|
|
97
|
+
this.typeName = typeName;
|
|
98
|
+
this._createInstance = createInstance;
|
|
99
|
+
this._cloneFreeUsesUniffiHandle = cloneFreeUsesUniffiHandle === true;
|
|
100
|
+
this._cloneHandle = cloneHandle;
|
|
101
|
+
this._cloneHandleGeneric = cloneHandleGeneric;
|
|
102
|
+
this._cloneHandleRawExternal = cloneHandleRawExternal;
|
|
103
|
+
this._freeHandle = freeHandle;
|
|
104
|
+
this._freeHandleGeneric = freeHandleGeneric;
|
|
105
|
+
this._freeHandleRawExternal = freeHandleRawExternal;
|
|
106
|
+
this._handleType = handleType;
|
|
107
|
+
this._serializeHandle = serializeHandle;
|
|
108
|
+
this._deserializeHandle = deserializeHandle;
|
|
109
|
+
this._registry =
|
|
110
|
+
typeof FinalizationRegistry === "undefined"
|
|
111
|
+
|| (
|
|
112
|
+
typeof freeHandle !== "function"
|
|
113
|
+
&& typeof freeHandleGeneric !== "function"
|
|
114
|
+
&& typeof freeHandleRawExternal !== "function"
|
|
115
|
+
)
|
|
116
|
+
? null
|
|
117
|
+
: new FinalizationRegistry((record) => {
|
|
118
|
+
try {
|
|
119
|
+
const useRawExternal =
|
|
120
|
+
record.rawExternal === true
|
|
121
|
+
&& typeof this._freeHandleRawExternal === "function";
|
|
122
|
+
const freeHandle =
|
|
123
|
+
record.genericAbi === true
|
|
124
|
+
? this._freeHandleGeneric
|
|
125
|
+
: useRawExternal
|
|
126
|
+
? this._freeHandleRawExternal
|
|
127
|
+
: this._freeHandle;
|
|
128
|
+
if (typeof freeHandle === "function") {
|
|
129
|
+
freeHandle(this._cloneFreeHandle(record.handle, {
|
|
130
|
+
genericAbi: record.genericAbi === true,
|
|
131
|
+
rawExternal: useRawExternal,
|
|
132
|
+
}));
|
|
133
|
+
}
|
|
134
|
+
} catch {
|
|
135
|
+
// Finalizers must not surface user-visible errors.
|
|
136
|
+
}
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
_unregisterFinalizer(instance) {
|
|
141
|
+
const token = instance[uniffiFinalizerTokenSymbol];
|
|
142
|
+
if (this._registry != null && token != null) {
|
|
143
|
+
this._registry.unregister(token);
|
|
144
|
+
}
|
|
145
|
+
instance[uniffiFinalizerTokenSymbol] = null;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
_registerFinalizer(instance, handle) {
|
|
149
|
+
if (this._registry == null) {
|
|
150
|
+
instance[uniffiFinalizerTokenSymbol] = null;
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const token = {};
|
|
155
|
+
instance[uniffiFinalizerTokenSymbol] = token;
|
|
156
|
+
this._registry.register(instance, handle, token);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
_markDestroyed(instance) {
|
|
160
|
+
instance[uniffiObjectHandleSymbol] = null;
|
|
161
|
+
instance[uniffiObjectDestroyedSymbol] = true;
|
|
162
|
+
instance[uniffiFinalizerTokenSymbol] = null;
|
|
163
|
+
instance[uniffiObjectGenericAbiSymbol] = false;
|
|
164
|
+
instance[uniffiObjectRawExternalSymbol] = false;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
_normalizeHandle(handle) {
|
|
168
|
+
if (handle == null) {
|
|
169
|
+
return handle;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
if (typeof handle === "bigint" || typeof handle === "number") {
|
|
173
|
+
return normalizeUInt64(handle);
|
|
174
|
+
}
|
|
175
|
+
return handle;
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
_coerceHandle(handle) {
|
|
179
|
+
if (handle == null) {
|
|
180
|
+
return handle;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
const handleType = resolveHandleType(this._handleType);
|
|
184
|
+
if (handleType == null) {
|
|
185
|
+
return this._normalizeHandle(handle);
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const normalizedHandle = this._normalizeHandle(handle);
|
|
189
|
+
if (typeof normalizedHandle === "bigint" || typeof normalizedHandle === "number") {
|
|
190
|
+
return koffi.decode(new BigUint64Array([normalizeUInt64(normalizedHandle)]), handleType);
|
|
191
|
+
}
|
|
192
|
+
return koffi.as(normalizedHandle, handleType);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
_cloneFreeHandle(handle, { genericAbi = false, rawExternal = false } = {}) {
|
|
196
|
+
if (handle == null) {
|
|
197
|
+
return handle;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (genericAbi || rawExternal || this._cloneFreeUsesUniffiHandle) {
|
|
201
|
+
return this._normalizeHandle(handle);
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return this._coerceHandle(handle);
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
attach(instance, handle, options = undefined) {
|
|
208
|
+
const normalizedHandle = requireHandle(
|
|
209
|
+
this._normalizeHandle(handle),
|
|
210
|
+
`${this.typeName} handle`,
|
|
211
|
+
);
|
|
212
|
+
const genericAbi = options?.genericAbi === true;
|
|
213
|
+
const rawExternal = options?.rawExternal === true;
|
|
214
|
+
const target = requireObjectInstance(instance, this.typeName);
|
|
215
|
+
|
|
216
|
+
if (storedHandle(target) != null && target[uniffiObjectDestroyedSymbol] !== true) {
|
|
217
|
+
throw new TypeError(`${this.typeName} is already bound to a live UniFFI handle.`);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
Object.defineProperties(target, {
|
|
221
|
+
[uniffiTypeNameSymbol]: {
|
|
222
|
+
configurable: true,
|
|
223
|
+
value: this.typeName,
|
|
224
|
+
writable: true,
|
|
225
|
+
},
|
|
226
|
+
[uniffiObjectFactorySymbol]: {
|
|
227
|
+
configurable: true,
|
|
228
|
+
value: this,
|
|
229
|
+
},
|
|
230
|
+
[uniffiObjectHandleSymbol]: {
|
|
231
|
+
configurable: true,
|
|
232
|
+
value: normalizedHandle,
|
|
233
|
+
writable: true,
|
|
234
|
+
},
|
|
235
|
+
[uniffiObjectDestroyedSymbol]: {
|
|
236
|
+
configurable: true,
|
|
237
|
+
value: false,
|
|
238
|
+
writable: true,
|
|
239
|
+
},
|
|
240
|
+
[uniffiObjectGenericAbiSymbol]: {
|
|
241
|
+
configurable: true,
|
|
242
|
+
value: genericAbi,
|
|
243
|
+
writable: true,
|
|
244
|
+
},
|
|
245
|
+
[uniffiObjectRawExternalSymbol]: {
|
|
246
|
+
configurable: true,
|
|
247
|
+
value: rawExternal,
|
|
248
|
+
writable: true,
|
|
249
|
+
},
|
|
250
|
+
[uniffiFinalizerTokenSymbol]: {
|
|
251
|
+
configurable: true,
|
|
252
|
+
value: null,
|
|
253
|
+
writable: true,
|
|
254
|
+
},
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
this._registerFinalizer(target, {
|
|
258
|
+
genericAbi,
|
|
259
|
+
rawExternal,
|
|
260
|
+
handle: normalizedHandle,
|
|
261
|
+
});
|
|
262
|
+
return target;
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
bless(instance, handle) {
|
|
266
|
+
return this.attach(instance, handle);
|
|
267
|
+
}
|
|
268
|
+
|
|
269
|
+
create(handle) {
|
|
270
|
+
return this.attach(this._createInstance(), handle);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
createGenericAbi(handle) {
|
|
274
|
+
return this.attach(this._createInstance(), handle, {
|
|
275
|
+
genericAbi: true,
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
createRawExternal(handle) {
|
|
280
|
+
const rawHandle = requireHandle(
|
|
281
|
+
this._normalizeHandle(handle),
|
|
282
|
+
`${this.typeName} handle`,
|
|
283
|
+
);
|
|
284
|
+
const adoptedHandle =
|
|
285
|
+
typeof this._cloneHandleRawExternal === "function"
|
|
286
|
+
? requireHandle(
|
|
287
|
+
this._cloneHandleRawExternal(rawHandle),
|
|
288
|
+
`${this.typeName} adopted handle`,
|
|
289
|
+
)
|
|
290
|
+
: rawHandle;
|
|
291
|
+
if (
|
|
292
|
+
adoptedHandle !== rawHandle
|
|
293
|
+
&& typeof this._freeHandleRawExternal === "function"
|
|
294
|
+
) {
|
|
295
|
+
this._freeHandleRawExternal(rawHandle);
|
|
296
|
+
}
|
|
297
|
+
return this.attach(this._createInstance(), adoptedHandle, {
|
|
298
|
+
rawExternal: true,
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
handle(value) {
|
|
303
|
+
if (!this.isInstance(value)) {
|
|
304
|
+
throw new TypeError(`${this.typeName} expected an instance created by this factory.`);
|
|
305
|
+
}
|
|
306
|
+
const rawHandle = requireHandle(
|
|
307
|
+
this._normalizeHandle(storedHandle(value)),
|
|
308
|
+
`${this.typeName} handle`,
|
|
309
|
+
);
|
|
310
|
+
return this.usesGenericAbi(value)
|
|
311
|
+
? rawHandle
|
|
312
|
+
: this._coerceHandle(rawHandle);
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
peekHandle(value) {
|
|
316
|
+
if (!this.isInstance(value) || this.isDestroyed(value)) {
|
|
317
|
+
return undefined;
|
|
318
|
+
}
|
|
319
|
+
return storedHandle(value);
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
cloneHandle(value) {
|
|
323
|
+
const rawHandle = requireHandle(
|
|
324
|
+
this._normalizeHandle(storedHandle(value)),
|
|
325
|
+
`${this.typeName} handle`,
|
|
326
|
+
);
|
|
327
|
+
const genericAbi = this.usesGenericAbi(value);
|
|
328
|
+
const useRawExternal =
|
|
329
|
+
this.usesRawExternal(value)
|
|
330
|
+
&& typeof this._cloneHandleRawExternal === "function";
|
|
331
|
+
const handle = this._cloneFreeHandle(rawHandle, {
|
|
332
|
+
genericAbi,
|
|
333
|
+
rawExternal: useRawExternal,
|
|
334
|
+
});
|
|
335
|
+
const cloneHandle =
|
|
336
|
+
genericAbi
|
|
337
|
+
? this._cloneHandleGeneric
|
|
338
|
+
: useRawExternal
|
|
339
|
+
? this._cloneHandleRawExternal
|
|
340
|
+
: this._cloneHandle;
|
|
341
|
+
if (typeof cloneHandle !== "function") {
|
|
342
|
+
return handle;
|
|
343
|
+
}
|
|
344
|
+
const clonedHandle = requireHandle(
|
|
345
|
+
cloneHandle(handle, value),
|
|
346
|
+
`${this.typeName} cloned handle`,
|
|
347
|
+
);
|
|
348
|
+
if (genericAbi) {
|
|
349
|
+
return clonedHandle;
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
if (useRawExternal) {
|
|
353
|
+
return this._cloneFreeUsesUniffiHandle
|
|
354
|
+
? pointerAddress(clonedHandle)
|
|
355
|
+
: this._coerceHandle(clonedHandle);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
return clonedHandle;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
destroy(value) {
|
|
362
|
+
if (!this.isInstance(value) || this.isDestroyed(value)) {
|
|
363
|
+
return false;
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
const rawHandle = requireHandle(
|
|
367
|
+
this._normalizeHandle(storedHandle(value)),
|
|
368
|
+
`${this.typeName} handle`,
|
|
369
|
+
);
|
|
370
|
+
const genericAbi = this.usesGenericAbi(value);
|
|
371
|
+
const useRawExternal =
|
|
372
|
+
this.usesRawExternal(value)
|
|
373
|
+
&& typeof this._freeHandleRawExternal === "function";
|
|
374
|
+
const handle = this._cloneFreeHandle(rawHandle, {
|
|
375
|
+
genericAbi,
|
|
376
|
+
rawExternal: useRawExternal,
|
|
377
|
+
});
|
|
378
|
+
|
|
379
|
+
const freeHandle =
|
|
380
|
+
genericAbi
|
|
381
|
+
? this._freeHandleGeneric
|
|
382
|
+
: useRawExternal
|
|
383
|
+
? this._freeHandleRawExternal
|
|
384
|
+
: this._freeHandle;
|
|
385
|
+
if (typeof freeHandle === "function") {
|
|
386
|
+
freeHandle(handle, value);
|
|
387
|
+
}
|
|
388
|
+
this._unregisterFinalizer(value);
|
|
389
|
+
this._markDestroyed(value);
|
|
390
|
+
return true;
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
isInstance(value) {
|
|
394
|
+
return objectFactory(value) === this;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
isDestroyed(value) {
|
|
398
|
+
return this.isInstance(value)
|
|
399
|
+
&& (value[uniffiObjectDestroyedSymbol] === true || storedHandle(value) == null);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
usesGenericAbi(value) {
|
|
403
|
+
return this.isInstance(value) && value[uniffiObjectGenericAbiSymbol] === true;
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
usesRawExternal(value) {
|
|
407
|
+
return this.isInstance(value) && value[uniffiObjectRawExternalSymbol] === true;
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
serializeHandle(value) {
|
|
411
|
+
const rawHandle = requireHandle(
|
|
412
|
+
this._normalizeHandle(storedHandle(value)),
|
|
413
|
+
`${this.typeName} handle`,
|
|
414
|
+
);
|
|
415
|
+
if (typeof this._serializeHandle === "function") {
|
|
416
|
+
return normalizeUInt64(this._serializeHandle(this.handle(value), value));
|
|
417
|
+
}
|
|
418
|
+
return pointerAddress(rawHandle);
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
deserializeHandle(serialized) {
|
|
422
|
+
const normalizedHandle = normalizeUInt64(serialized);
|
|
423
|
+
if (typeof this._deserializeHandle === "function") {
|
|
424
|
+
return requireHandle(
|
|
425
|
+
this._normalizeHandle(this._deserializeHandle(normalizedHandle)),
|
|
426
|
+
`${this.typeName} handle`,
|
|
427
|
+
);
|
|
428
|
+
}
|
|
429
|
+
return requireHandle(
|
|
430
|
+
this._normalizeHandle(normalizedHandle),
|
|
431
|
+
`${this.typeName} handle`,
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
export class FfiConverterObject {
|
|
437
|
+
constructor(factory) {
|
|
438
|
+
this.factory = factory;
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
lower(value) {
|
|
442
|
+
return this.factory.cloneHandle(value);
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
lift(handle) {
|
|
446
|
+
return this.factory.create(handle);
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
write(value, writer) {
|
|
450
|
+
writer.writeUInt64(this.factory.serializeHandle(value));
|
|
451
|
+
}
|
|
452
|
+
|
|
453
|
+
read(reader) {
|
|
454
|
+
const handle = this.factory.deserializeHandle(reader.readUInt64());
|
|
455
|
+
return this.factory.createGenericAbi(handle);
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
allocationSize() {
|
|
459
|
+
return UNIFFI_OBJECT_HANDLE_SIZE;
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
export function createObjectFactory(options = undefined) {
|
|
464
|
+
return new UniffiObjectFactory(options);
|
|
465
|
+
}
|
|
466
|
+
|
|
467
|
+
export function createObjectConverter(factory) {
|
|
468
|
+
return new FfiConverterObject(factory);
|
|
469
|
+
}
|