@gtkx/runtime 0.0.1 → 1.0.0-rc.2
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/LICENSE +373 -0
- package/README.md +171 -3
- package/dist/arg.d.ts +13 -0
- package/dist/arg.d.ts.map +1 -0
- package/dist/arg.js +6 -0
- package/dist/arg.js.map +1 -0
- package/dist/bind.d.ts +5 -0
- package/dist/bind.d.ts.map +1 -0
- package/dist/bind.js +19 -0
- package/dist/bind.js.map +1 -0
- package/dist/callback.d.ts +13 -0
- package/dist/callback.d.ts.map +1 -0
- package/dist/callback.js +86 -0
- package/dist/callback.js.map +1 -0
- package/dist/descriptors.d.ts +158 -0
- package/dist/descriptors.d.ts.map +1 -0
- package/dist/descriptors.js +135 -0
- package/dist/descriptors.js.map +1 -0
- package/dist/error.d.ts +24 -0
- package/dist/error.d.ts.map +1 -0
- package/dist/error.js +33 -0
- package/dist/error.js.map +1 -0
- package/dist/exit-hook.d.ts +2 -0
- package/dist/exit-hook.d.ts.map +1 -0
- package/dist/exit-hook.js +3 -0
- package/dist/exit-hook.js.map +1 -0
- package/dist/fn.d.ts +10 -0
- package/dist/fn.d.ts.map +1 -0
- package/dist/fn.js +90 -0
- package/dist/fn.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/internal.d.ts +5 -0
- package/dist/internal.d.ts.map +1 -0
- package/dist/internal.js +5 -0
- package/dist/internal.js.map +1 -0
- package/dist/library.d.ts +8 -0
- package/dist/library.d.ts.map +1 -0
- package/dist/library.js +14 -0
- package/dist/library.js.map +1 -0
- package/dist/lifecycle.d.ts +42 -0
- package/dist/lifecycle.d.ts.map +1 -0
- package/dist/lifecycle.js +70 -0
- package/dist/lifecycle.js.map +1 -0
- package/dist/listeners.d.ts +36 -0
- package/dist/listeners.d.ts.map +1 -0
- package/dist/listeners.js +85 -0
- package/dist/listeners.js.map +1 -0
- package/dist/mixin.d.ts +13 -0
- package/dist/mixin.d.ts.map +1 -0
- package/dist/mixin.js +49 -0
- package/dist/mixin.js.map +1 -0
- package/dist/native-value.d.ts +13 -0
- package/dist/native-value.d.ts.map +1 -0
- package/dist/native-value.js +105 -0
- package/dist/native-value.js.map +1 -0
- package/dist/object.d.ts +32 -0
- package/dist/object.d.ts.map +1 -0
- package/dist/object.js +66 -0
- package/dist/object.js.map +1 -0
- package/dist/promisify.d.ts +21 -0
- package/dist/promisify.d.ts.map +1 -0
- package/dist/promisify.js +39 -0
- package/dist/promisify.js.map +1 -0
- package/dist/properties.d.ts +14 -0
- package/dist/properties.d.ts.map +1 -0
- package/dist/properties.js +79 -0
- package/dist/properties.js.map +1 -0
- package/dist/register-class.d.ts +25 -0
- package/dist/register-class.d.ts.map +1 -0
- package/dist/register-class.js +178 -0
- package/dist/register-class.js.map +1 -0
- package/dist/registry.d.ts +69 -0
- package/dist/registry.d.ts.map +1 -0
- package/dist/registry.js +186 -0
- package/dist/registry.js.map +1 -0
- package/dist/signal.d.ts +35 -0
- package/dist/signal.d.ts.map +1 -0
- package/dist/signal.js +95 -0
- package/dist/signal.js.map +1 -0
- package/dist/t.d.ts +50 -0
- package/dist/t.d.ts.map +1 -0
- package/dist/t.js +49 -0
- package/dist/t.js.map +1 -0
- package/dist/tuple.d.ts +7 -0
- package/dist/tuple.d.ts.map +1 -0
- package/dist/tuple.js +26 -0
- package/dist/tuple.js.map +1 -0
- package/dist/type.d.ts +88 -0
- package/dist/type.d.ts.map +1 -0
- package/dist/type.js +194 -0
- package/dist/type.js.map +1 -0
- package/dist/value.d.ts +29 -0
- package/dist/value.d.ts.map +1 -0
- package/dist/value.js +292 -0
- package/dist/value.js.map +1 -0
- package/package.json +54 -13
- package/src/arg.ts +15 -0
- package/src/bind.ts +31 -0
- package/src/callback.ts +149 -0
- package/src/descriptors.ts +350 -0
- package/src/error.ts +58 -0
- package/src/exit-hook.ts +3 -0
- package/src/fn.ts +139 -0
- package/src/index.ts +59 -0
- package/src/internal.ts +12 -0
- package/src/library.ts +17 -0
- package/src/lifecycle.ts +101 -0
- package/src/listeners.ts +112 -0
- package/src/mixin.ts +73 -0
- package/src/native-value.ts +135 -0
- package/src/object.ts +95 -0
- package/src/promisify.ts +67 -0
- package/src/properties.ts +115 -0
- package/src/register-class.ts +305 -0
- package/src/registry.ts +311 -0
- package/src/signal.ts +192 -0
- package/src/t.ts +90 -0
- package/src/tuple.ts +37 -0
- package/src/type.ts +290 -0
- package/src/value.ts +430 -0
|
@@ -0,0 +1,350 @@
|
|
|
1
|
+
import type { ArrayKind, Descriptor, Ownership } from "@gtkx/native";
|
|
2
|
+
import type { AnyClass } from "@gtkx/utils";
|
|
3
|
+
|
|
4
|
+
type Int8Descriptor = Extract<Descriptor, { kind: "int8" }>;
|
|
5
|
+
type Uint8Descriptor = Extract<Descriptor, { kind: "uint8" }>;
|
|
6
|
+
type Int16Descriptor = Extract<Descriptor, { kind: "int16" }>;
|
|
7
|
+
type Uint16Descriptor = Extract<Descriptor, { kind: "uint16" }>;
|
|
8
|
+
type Int32Descriptor = Extract<Descriptor, { kind: "int32" }>;
|
|
9
|
+
type Uint32Descriptor = Extract<Descriptor, { kind: "uint32" }>;
|
|
10
|
+
type Int64Descriptor = Extract<Descriptor, { kind: "int64" }>;
|
|
11
|
+
type Uint64Descriptor = Extract<Descriptor, { kind: "uint64" }>;
|
|
12
|
+
type BigInt64Descriptor = Extract<Descriptor, { kind: "bigint64" }>;
|
|
13
|
+
type BigUint64Descriptor = Extract<Descriptor, { kind: "biguint64" }>;
|
|
14
|
+
type Float32Descriptor = Extract<Descriptor, { kind: "float32" }>;
|
|
15
|
+
type Float64Descriptor = Extract<Descriptor, { kind: "float64" }>;
|
|
16
|
+
type EnumDescriptor = Extract<Descriptor, { kind: "enum" }>;
|
|
17
|
+
type FlagsDescriptor = Extract<Descriptor, { kind: "flags" }>;
|
|
18
|
+
type BooleanDescriptor = Extract<Descriptor, { kind: "boolean" }>;
|
|
19
|
+
type StringDescriptor = Extract<Descriptor, { kind: "string" }>;
|
|
20
|
+
type ObjectDescriptor = Extract<Descriptor, { kind: "object" }>;
|
|
21
|
+
type UnicharDescriptor = Extract<Descriptor, { kind: "unichar" }>;
|
|
22
|
+
type VoidDescriptor = Extract<Descriptor, { kind: "void" }>;
|
|
23
|
+
type BufferDescriptor = Extract<Descriptor, { kind: "buffer" }>;
|
|
24
|
+
type BoxedDescriptor = Extract<Descriptor, { kind: "boxed" }>;
|
|
25
|
+
type StructDescriptor = Extract<Descriptor, { kind: "struct" }> & { wrapperClass?: AnyClass };
|
|
26
|
+
type FundamentalDescriptor = Extract<Descriptor, { kind: "fundamental" }> & { wrapperClass?: AnyClass };
|
|
27
|
+
type ArrayDescriptor = Extract<Descriptor, { kind: "array" }>;
|
|
28
|
+
type HashTableDescriptor = Extract<Descriptor, { kind: "hashtable" }>;
|
|
29
|
+
type CallbackDescriptor = Extract<Descriptor, { kind: "callback" }>;
|
|
30
|
+
type RefDescriptor = Extract<Descriptor, { kind: "ref" }>;
|
|
31
|
+
type TypeDescriptor = BigUint64Descriptor & { type: true };
|
|
32
|
+
|
|
33
|
+
type BoxedOptions = {
|
|
34
|
+
callerAllocated?: boolean;
|
|
35
|
+
inline?: boolean;
|
|
36
|
+
ownership?: Ownership;
|
|
37
|
+
sharedLibrary?: string;
|
|
38
|
+
getTypeFnName?: string;
|
|
39
|
+
freeFnName?: string;
|
|
40
|
+
size?: number;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
type CallbackOptions = {
|
|
44
|
+
hasDestroy?: boolean;
|
|
45
|
+
userDataIndex?: number;
|
|
46
|
+
scope?: CallbackDescriptor["scope"];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
type ArrayOptions = {
|
|
50
|
+
elementSize?: number | undefined;
|
|
51
|
+
sizeParamIndex?: number | undefined;
|
|
52
|
+
fixedSize?: number | undefined;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
type FundamentalOptions = {
|
|
56
|
+
ownership?: Ownership;
|
|
57
|
+
typeName?: string;
|
|
58
|
+
wrapperClass?: AnyClass;
|
|
59
|
+
inline?: boolean;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
type StructOptions = {
|
|
63
|
+
callerAllocated?: boolean;
|
|
64
|
+
inline?: boolean;
|
|
65
|
+
size?: number;
|
|
66
|
+
wrapperClass?: AnyClass;
|
|
67
|
+
};
|
|
68
|
+
|
|
69
|
+
const int8T: Int8Descriptor = { kind: "int8" };
|
|
70
|
+
const uint8T: Uint8Descriptor = { kind: "uint8" };
|
|
71
|
+
const int16T: Int16Descriptor = { kind: "int16" };
|
|
72
|
+
const uint16T: Uint16Descriptor = { kind: "uint16" };
|
|
73
|
+
const int32T: Int32Descriptor = { kind: "int32" };
|
|
74
|
+
const uint32T: Uint32Descriptor = { kind: "uint32" };
|
|
75
|
+
const int64T: Int64Descriptor = { kind: "int64" };
|
|
76
|
+
const uint64T: Uint64Descriptor = { kind: "uint64" };
|
|
77
|
+
const bigint64T: BigInt64Descriptor = { kind: "bigint64" };
|
|
78
|
+
const biguint64T: BigUint64Descriptor = { kind: "biguint64" };
|
|
79
|
+
const gtypeT: TypeDescriptor = { kind: "biguint64", type: true };
|
|
80
|
+
const float32T: Float32Descriptor = { kind: "float32" };
|
|
81
|
+
const float64T: Float64Descriptor = { kind: "float64" };
|
|
82
|
+
const booleanT: BooleanDescriptor = { kind: "boolean" };
|
|
83
|
+
const voidT: VoidDescriptor = { kind: "void" };
|
|
84
|
+
const unicharT: UnicharDescriptor = { kind: "unichar" };
|
|
85
|
+
const bufferT: BufferDescriptor = { kind: "buffer" };
|
|
86
|
+
|
|
87
|
+
const stringT = (ownership: Ownership = "borrowed", length?: number): StringDescriptor =>
|
|
88
|
+
length === undefined ? { kind: "string", ownership } : { kind: "string", ownership, length };
|
|
89
|
+
|
|
90
|
+
const objectT = (ownership: Ownership = "borrowed"): ObjectDescriptor => ({ kind: "object", ownership });
|
|
91
|
+
|
|
92
|
+
const refT = (innerDescriptor: Descriptor, isInout = false): RefDescriptor =>
|
|
93
|
+
isInout ? { kind: "ref", innerDescriptor, inout: true } : { kind: "ref", innerDescriptor };
|
|
94
|
+
|
|
95
|
+
const hashTableT = (
|
|
96
|
+
keyDescriptor: Descriptor,
|
|
97
|
+
valueDescriptor: Descriptor,
|
|
98
|
+
ownership: Ownership = "borrowed",
|
|
99
|
+
): HashTableDescriptor => ({
|
|
100
|
+
kind: "hashtable",
|
|
101
|
+
keyDescriptor,
|
|
102
|
+
valueDescriptor,
|
|
103
|
+
ownership,
|
|
104
|
+
});
|
|
105
|
+
|
|
106
|
+
const enumT = (sharedLibrary: string, typeFnName: string, isSigned: boolean): EnumDescriptor => ({
|
|
107
|
+
kind: "enum",
|
|
108
|
+
sharedLibrary,
|
|
109
|
+
getTypeFnName: typeFnName,
|
|
110
|
+
signed: isSigned,
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const flagsT = (sharedLibrary: string, typeFnName: string, isSigned: boolean): FlagsDescriptor => ({
|
|
114
|
+
kind: "flags",
|
|
115
|
+
sharedLibrary,
|
|
116
|
+
getTypeFnName: typeFnName,
|
|
117
|
+
signed: isSigned,
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
const applyBoxedNames = (result: BoxedDescriptor, options: BoxedOptions): void => {
|
|
121
|
+
if (options.sharedLibrary !== undefined) {
|
|
122
|
+
result.sharedLibrary = options.sharedLibrary;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
if (options.getTypeFnName !== undefined) {
|
|
126
|
+
result.getTypeFnName = options.getTypeFnName;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (options.freeFnName !== undefined) {
|
|
130
|
+
result.freeFnName = options.freeFnName;
|
|
131
|
+
}
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
const applyBoxedOptions = (result: BoxedDescriptor, options: BoxedOptions): void => {
|
|
135
|
+
applyBoxedNames(result, options);
|
|
136
|
+
|
|
137
|
+
if (options.callerAllocated) {
|
|
138
|
+
result.callerAllocated = true;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (options.inline) {
|
|
142
|
+
result.inline = true;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
if (options.size !== undefined) {
|
|
146
|
+
result.size = options.size;
|
|
147
|
+
}
|
|
148
|
+
};
|
|
149
|
+
|
|
150
|
+
const boxedT = (typeName: string, options: BoxedOptions = {}): BoxedDescriptor => {
|
|
151
|
+
const result: BoxedDescriptor = {
|
|
152
|
+
kind: "boxed",
|
|
153
|
+
ownership: options.ownership ?? "borrowed",
|
|
154
|
+
typeName,
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
applyBoxedOptions(result, options);
|
|
158
|
+
|
|
159
|
+
return result;
|
|
160
|
+
};
|
|
161
|
+
|
|
162
|
+
const structT = (ownership: Ownership = "borrowed", options: StructOptions = {}): StructDescriptor => {
|
|
163
|
+
const result: StructDescriptor = { kind: "struct", ownership };
|
|
164
|
+
|
|
165
|
+
if (options.size !== undefined) {
|
|
166
|
+
result.size = options.size;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
if (options.wrapperClass !== undefined) {
|
|
170
|
+
result.wrapperClass = options.wrapperClass;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
if (options.callerAllocated) {
|
|
174
|
+
result.callerAllocated = true;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
if (options.inline) {
|
|
178
|
+
result.inline = true;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
return result;
|
|
182
|
+
};
|
|
183
|
+
|
|
184
|
+
const fundamentalT = (
|
|
185
|
+
sharedLibrary: string,
|
|
186
|
+
refFnName: string,
|
|
187
|
+
unrefFnName: string,
|
|
188
|
+
options: FundamentalOptions = {},
|
|
189
|
+
): FundamentalDescriptor => {
|
|
190
|
+
const ownership = options.ownership ?? "borrowed";
|
|
191
|
+
const result: FundamentalDescriptor = { kind: "fundamental", ownership, sharedLibrary, refFnName, unrefFnName };
|
|
192
|
+
|
|
193
|
+
if (options.typeName !== undefined) {
|
|
194
|
+
result.typeName = options.typeName;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
if (options.wrapperClass !== undefined) {
|
|
198
|
+
result.wrapperClass = options.wrapperClass;
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
if (options.inline) {
|
|
202
|
+
result.inline = true;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
return result;
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
const arrayT = (
|
|
209
|
+
itemDescriptor: Descriptor,
|
|
210
|
+
arrayKind: ArrayKind = "array",
|
|
211
|
+
ownership: Ownership = "borrowed",
|
|
212
|
+
options?: ArrayOptions,
|
|
213
|
+
): ArrayDescriptor => {
|
|
214
|
+
const result: ArrayDescriptor = { kind: "array", itemDescriptor, arrayKind, ownership };
|
|
215
|
+
|
|
216
|
+
if (options?.elementSize !== undefined) {
|
|
217
|
+
result.elementSize = options.elementSize;
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
if (options?.sizeParamIndex !== undefined) {
|
|
221
|
+
result.sizeParamIndex = options.sizeParamIndex;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
if (options?.fixedSize !== undefined) {
|
|
225
|
+
result.fixedSize = options.fixedSize;
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
return result;
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
const listT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
|
|
232
|
+
arrayT(itemDescriptor, "glist", ownership);
|
|
233
|
+
|
|
234
|
+
const slistT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
|
|
235
|
+
arrayT(itemDescriptor, "gslist", ownership);
|
|
236
|
+
|
|
237
|
+
const ptrArrayT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
|
|
238
|
+
arrayT(itemDescriptor, "gptrarray", ownership);
|
|
239
|
+
|
|
240
|
+
const gArrayT = (
|
|
241
|
+
itemDescriptor: Descriptor,
|
|
242
|
+
ownership: Ownership = "borrowed",
|
|
243
|
+
elementSize?: number,
|
|
244
|
+
): ArrayDescriptor =>
|
|
245
|
+
arrayT(itemDescriptor, "garray", ownership, elementSize === undefined ? undefined : { elementSize });
|
|
246
|
+
|
|
247
|
+
const byteArrayT = (ownership: Ownership = "borrowed"): ArrayDescriptor =>
|
|
248
|
+
arrayT(uint8T, "gbytearray", ownership);
|
|
249
|
+
|
|
250
|
+
const sizedArrayT = (
|
|
251
|
+
itemDescriptor: Descriptor,
|
|
252
|
+
sizeParamIndex: number,
|
|
253
|
+
ownership: Ownership = "borrowed",
|
|
254
|
+
elementSize?: number,
|
|
255
|
+
): ArrayDescriptor => arrayT(itemDescriptor, "sized", ownership, { sizeParamIndex, elementSize });
|
|
256
|
+
|
|
257
|
+
const fixedArrayT = (
|
|
258
|
+
itemDescriptor: Descriptor,
|
|
259
|
+
fixedSize: number,
|
|
260
|
+
ownership: Ownership = "borrowed",
|
|
261
|
+
elementSize?: number,
|
|
262
|
+
): ArrayDescriptor => arrayT(itemDescriptor, "fixed", ownership, { fixedSize, elementSize });
|
|
263
|
+
|
|
264
|
+
const callbackT = (
|
|
265
|
+
argDescriptors: Descriptor[],
|
|
266
|
+
returnDescriptor: Descriptor,
|
|
267
|
+
options?: CallbackOptions,
|
|
268
|
+
): CallbackDescriptor => {
|
|
269
|
+
const result: CallbackDescriptor = { kind: "callback", argDescriptors, returnDescriptor };
|
|
270
|
+
|
|
271
|
+
if (options?.hasDestroy !== undefined) {
|
|
272
|
+
result.hasDestroy = options.hasDestroy;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
if (options?.userDataIndex !== undefined) {
|
|
276
|
+
result.userDataIndex = options.userDataIndex;
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
if (options?.scope !== undefined) {
|
|
280
|
+
result.scope = options.scope;
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
return result;
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
export {
|
|
287
|
+
int8T,
|
|
288
|
+
uint8T,
|
|
289
|
+
int16T,
|
|
290
|
+
uint16T,
|
|
291
|
+
int32T,
|
|
292
|
+
uint32T,
|
|
293
|
+
int64T,
|
|
294
|
+
uint64T,
|
|
295
|
+
bigint64T,
|
|
296
|
+
biguint64T,
|
|
297
|
+
gtypeT,
|
|
298
|
+
float32T,
|
|
299
|
+
float64T,
|
|
300
|
+
booleanT,
|
|
301
|
+
voidT,
|
|
302
|
+
unicharT,
|
|
303
|
+
bufferT,
|
|
304
|
+
stringT,
|
|
305
|
+
objectT,
|
|
306
|
+
refT,
|
|
307
|
+
hashTableT,
|
|
308
|
+
enumT,
|
|
309
|
+
flagsT,
|
|
310
|
+
boxedT,
|
|
311
|
+
structT,
|
|
312
|
+
fundamentalT,
|
|
313
|
+
arrayT,
|
|
314
|
+
listT,
|
|
315
|
+
slistT,
|
|
316
|
+
ptrArrayT,
|
|
317
|
+
gArrayT,
|
|
318
|
+
byteArrayT,
|
|
319
|
+
sizedArrayT,
|
|
320
|
+
fixedArrayT,
|
|
321
|
+
callbackT,
|
|
322
|
+
type Int8Descriptor,
|
|
323
|
+
type Uint8Descriptor,
|
|
324
|
+
type Int16Descriptor,
|
|
325
|
+
type Uint16Descriptor,
|
|
326
|
+
type Int32Descriptor,
|
|
327
|
+
type Uint32Descriptor,
|
|
328
|
+
type Int64Descriptor,
|
|
329
|
+
type Uint64Descriptor,
|
|
330
|
+
type BigInt64Descriptor,
|
|
331
|
+
type BigUint64Descriptor,
|
|
332
|
+
type Float32Descriptor,
|
|
333
|
+
type Float64Descriptor,
|
|
334
|
+
type EnumDescriptor,
|
|
335
|
+
type FlagsDescriptor,
|
|
336
|
+
type BooleanDescriptor,
|
|
337
|
+
type StringDescriptor,
|
|
338
|
+
type ObjectDescriptor,
|
|
339
|
+
type UnicharDescriptor,
|
|
340
|
+
type VoidDescriptor,
|
|
341
|
+
type BufferDescriptor,
|
|
342
|
+
type BoxedDescriptor,
|
|
343
|
+
type StructDescriptor,
|
|
344
|
+
type FundamentalDescriptor,
|
|
345
|
+
type ArrayDescriptor,
|
|
346
|
+
type HashTableDescriptor,
|
|
347
|
+
type CallbackDescriptor,
|
|
348
|
+
type RefDescriptor,
|
|
349
|
+
type TypeDescriptor,
|
|
350
|
+
};
|
package/src/error.ts
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import type { ExternalObject, Handle, Ref } from "@gtkx/native";
|
|
2
|
+
import { getWrapperClass, wrapHandle } from "./registry.js";
|
|
3
|
+
import { getErrorType, isTypedClass } from "./type.js";
|
|
4
|
+
|
|
5
|
+
type ErrorLike = Error & {
|
|
6
|
+
domain: number;
|
|
7
|
+
code: number;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* An error enum object carrying its member codes plus an `instanceof` check that
|
|
12
|
+
* matches wrapped GLib errors belonging to a specific error domain.
|
|
13
|
+
*/
|
|
14
|
+
type ErrorDomain<T extends Record<string, number>> = T & {
|
|
15
|
+
[Symbol.hasInstance]: (value: unknown) => value is ErrorLike;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
function checkError(error: Ref): void {
|
|
19
|
+
if (error.value === null) {
|
|
20
|
+
return;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
const gerror = wrapHandle(error.value as ExternalObject<Handle>, getWrapperClass(getErrorType())) as ErrorLike;
|
|
24
|
+
const callSite = new Error("gtkx call site");
|
|
25
|
+
const callerFrames = (callSite.stack ?? "").split("\n").slice(2);
|
|
26
|
+
gerror.stack = [`${gerror.name}: ${gerror.message}`, ...callerFrames].join("\n");
|
|
27
|
+
throw gerror;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const isError = (value: unknown): value is ErrorLike => isTypedClass(value) && value.__type__ === getErrorType();
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Builds an error domain enum object from its members and a lazy resolver for the
|
|
34
|
+
* domain quark, giving it an `instanceof` check that matches wrapped GLib errors of
|
|
35
|
+
* that domain.
|
|
36
|
+
*
|
|
37
|
+
* @param resolveDomain Called once, on first `instanceof` check, to obtain the domain quark.
|
|
38
|
+
* @param members The error code enum members to expose on the returned object.
|
|
39
|
+
*/
|
|
40
|
+
function createErrorDomain<const T extends Record<string, number>>(
|
|
41
|
+
resolveDomain: () => number,
|
|
42
|
+
members: T,
|
|
43
|
+
): ErrorDomain<T> {
|
|
44
|
+
let domain: number | undefined;
|
|
45
|
+
|
|
46
|
+
const hasInstance = (value: unknown): value is ErrorLike => {
|
|
47
|
+
domain ??= resolveDomain();
|
|
48
|
+
|
|
49
|
+
return isError(value) && value.domain === domain;
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
const enumObject: Record<string, unknown> = { ...members };
|
|
53
|
+
Object.defineProperty(enumObject, Symbol.hasInstance, { value: hasInstance });
|
|
54
|
+
|
|
55
|
+
return enumObject as ErrorDomain<T>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export { checkError, createErrorDomain, type ErrorDomain };
|
package/src/exit-hook.ts
ADDED
package/src/fn.ts
ADDED
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
import type { Descriptor, Ref } from "@gtkx/native";
|
|
2
|
+
import { type Arg, isCallerAllocatedArg, isInoutArg, isOutputArg, isRefArg } from "./arg.js";
|
|
3
|
+
import { bind } from "./bind.js";
|
|
4
|
+
import { wrapCallbackValue } from "./callback.js";
|
|
5
|
+
import { boxedT, refT } from "./descriptors.js";
|
|
6
|
+
import { checkError } from "./error.js";
|
|
7
|
+
import { LIB } from "./library.js";
|
|
8
|
+
import { fromNative } from "./native-value.js";
|
|
9
|
+
import { getHandle } from "./registry.js";
|
|
10
|
+
import { packTupleResult } from "./tuple.js";
|
|
11
|
+
|
|
12
|
+
type FnSpec = {
|
|
13
|
+
args: Arg[];
|
|
14
|
+
returns: Descriptor;
|
|
15
|
+
throws?: boolean;
|
|
16
|
+
};
|
|
17
|
+
|
|
18
|
+
type ArgSpec = {
|
|
19
|
+
arg: Arg;
|
|
20
|
+
isRef: boolean;
|
|
21
|
+
isCallerAllocated: boolean;
|
|
22
|
+
consumesInput: boolean;
|
|
23
|
+
inputIndex: number;
|
|
24
|
+
isOutParam: boolean;
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
const buildNativeArgTypes = (args: Arg[], canThrow: boolean): Descriptor[] => {
|
|
28
|
+
const nativeArgTypes = args.map((argSpec) =>
|
|
29
|
+
argSpec.direction !== undefined && argSpec.callerAllocated !== true ? refT(argSpec.type) : argSpec.type,
|
|
30
|
+
);
|
|
31
|
+
|
|
32
|
+
if (canThrow) {
|
|
33
|
+
nativeArgTypes.push(
|
|
34
|
+
refT(boxedT("GError", { ownership: "full", sharedLibrary: LIB, getTypeFnName: "g_error_get_type" })),
|
|
35
|
+
);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return nativeArgTypes;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const buildArgSpecs = (args: Arg[]): ArgSpec[] => {
|
|
42
|
+
let inputCursor = 0;
|
|
43
|
+
|
|
44
|
+
return args.map((arg) => {
|
|
45
|
+
const isRef = isRefArg(arg);
|
|
46
|
+
const isConsumesInput = !isRef || isInoutArg(arg);
|
|
47
|
+
const isOutParam = isOutputArg(arg) && arg.consumed !== true;
|
|
48
|
+
|
|
49
|
+
return {
|
|
50
|
+
arg,
|
|
51
|
+
isRef,
|
|
52
|
+
isCallerAllocated: isCallerAllocatedArg(arg),
|
|
53
|
+
consumesInput: isConsumesInput,
|
|
54
|
+
inputIndex: isConsumesInput ? inputCursor++ : -1,
|
|
55
|
+
isOutParam,
|
|
56
|
+
};
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const resolveCallerAllocated = (inputs: unknown[], inputIndex: number): unknown => {
|
|
61
|
+
const wrapper = inputs[inputIndex];
|
|
62
|
+
|
|
63
|
+
return wrapper == null ? wrapper : getHandle(wrapper);
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
const buildRefValue = (isInputConsumed: boolean, inputs: unknown[], inputIndex: number): Ref => ({
|
|
67
|
+
value: isInputConsumed ? inputs[inputIndex] : null,
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
const buildNativeValue = (spec: ArgSpec, inputs: unknown[]): unknown => {
|
|
71
|
+
const { arg, isRef, isCallerAllocated, consumesInput, inputIndex } = spec;
|
|
72
|
+
|
|
73
|
+
if (isCallerAllocated) {
|
|
74
|
+
return resolveCallerAllocated(inputs, inputIndex);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (isRef) {
|
|
78
|
+
return buildRefValue(consumesInput, inputs, inputIndex);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
if (arg.type.kind === "callback") {
|
|
82
|
+
return wrapCallbackValue(arg.type, inputs[inputIndex]);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return inputs[inputIndex];
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
const buildNativeValues = (plans: ArgSpec[], inputs: unknown[]): unknown[] =>
|
|
89
|
+
plans.map((plan) => buildNativeValue(plan, inputs));
|
|
90
|
+
|
|
91
|
+
const readOutParams = (plans: ArgSpec[], inputs: unknown[], nativeValues: unknown[]): unknown[] => {
|
|
92
|
+
const outParams: unknown[] = [];
|
|
93
|
+
|
|
94
|
+
for (const [index, { arg, isCallerAllocated, inputIndex, isOutParam }] of plans.entries()) {
|
|
95
|
+
if (!isOutParam) {
|
|
96
|
+
continue;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
outParams.push(
|
|
100
|
+
isCallerAllocated ? inputs[inputIndex] : fromNative(arg.type, (nativeValues[index] as Ref).value),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return outParams;
|
|
105
|
+
};
|
|
106
|
+
|
|
107
|
+
function fn(sharedLibrary: string, symbol: string, spec: FnSpec): (...inputs: unknown[]) => unknown {
|
|
108
|
+
const { args, returns: returnDescriptor, throws = false } = spec;
|
|
109
|
+
const nativeArgTypes = buildNativeArgTypes(args, throws);
|
|
110
|
+
const nativeFn = bind(sharedLibrary, symbol, nativeArgTypes, returnDescriptor);
|
|
111
|
+
const hasPrimary = returnDescriptor.kind !== "void";
|
|
112
|
+
const plans = buildArgSpecs(args);
|
|
113
|
+
|
|
114
|
+
const shape = (inputs: unknown[], nativeValues: unknown[], nativeResult: unknown): unknown => {
|
|
115
|
+
const primary = hasPrimary ? fromNative(returnDescriptor, nativeResult) : undefined;
|
|
116
|
+
|
|
117
|
+
return packTupleResult(readOutParams(plans, inputs, nativeValues), primary, hasPrimary);
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
if (throws) {
|
|
121
|
+
return (...inputs) => {
|
|
122
|
+
const nativeValues = buildNativeValues(plans, inputs);
|
|
123
|
+
const errorRef: Ref = { value: null };
|
|
124
|
+
nativeValues.push(errorRef);
|
|
125
|
+
const nativeResult = nativeFn(...nativeValues);
|
|
126
|
+
checkError(errorRef);
|
|
127
|
+
|
|
128
|
+
return shape(inputs, nativeValues, nativeResult);
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
return (...inputs) => {
|
|
133
|
+
const nativeValues = buildNativeValues(plans, inputs);
|
|
134
|
+
|
|
135
|
+
return shape(inputs, nativeValues, nativeFn(...nativeValues));
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export { fn };
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import "./exit-hook.js";
|
|
2
|
+
|
|
3
|
+
export { createErrorDomain, type ErrorDomain } from "./error.js";
|
|
4
|
+
export { type ApplicationLike, onExit, quit, quitApplication, runApplication } from "./lifecycle.js";
|
|
5
|
+
export { offSignal, onceSignal, onSignal } from "./listeners.js";
|
|
6
|
+
export { installMixins, type Mixin } from "./mixin.js";
|
|
7
|
+
export { fromNative } from "./native-value.js";
|
|
8
|
+
export { getObjectProperty, newObjectWithProperties, setObjectProperty } from "./object.js";
|
|
9
|
+
export { type FinishResult, promisify } from "./promisify.js";
|
|
10
|
+
export { registerClass } from "./register-class.js";
|
|
11
|
+
export {
|
|
12
|
+
getHandle,
|
|
13
|
+
getInstanceType,
|
|
14
|
+
getWrapperClass,
|
|
15
|
+
registerInterface,
|
|
16
|
+
registerWrapperClass,
|
|
17
|
+
type StaticBase,
|
|
18
|
+
setHandle,
|
|
19
|
+
tryGetHandle,
|
|
20
|
+
wrapHandle,
|
|
21
|
+
} from "./registry.js";
|
|
22
|
+
export { connectSignal, emitSignal, getSignalBaseName, type SignalHandler } from "./signal.js";
|
|
23
|
+
export { t } from "./t.js";
|
|
24
|
+
export {
|
|
25
|
+
resolveType,
|
|
26
|
+
TYPE_BOOLEAN,
|
|
27
|
+
TYPE_BOXED,
|
|
28
|
+
TYPE_CHAR,
|
|
29
|
+
TYPE_DOUBLE,
|
|
30
|
+
TYPE_ENUM,
|
|
31
|
+
TYPE_FLAGS,
|
|
32
|
+
TYPE_FLOAT,
|
|
33
|
+
TYPE_GTYPE,
|
|
34
|
+
TYPE_INT,
|
|
35
|
+
TYPE_INT64,
|
|
36
|
+
TYPE_INTERFACE,
|
|
37
|
+
TYPE_INVALID,
|
|
38
|
+
TYPE_LONG,
|
|
39
|
+
TYPE_NONE,
|
|
40
|
+
TYPE_OBJECT,
|
|
41
|
+
TYPE_PARAM,
|
|
42
|
+
TYPE_POINTER,
|
|
43
|
+
TYPE_STRING,
|
|
44
|
+
TYPE_UCHAR,
|
|
45
|
+
TYPE_UINT,
|
|
46
|
+
TYPE_UINT64,
|
|
47
|
+
TYPE_ULONG,
|
|
48
|
+
TYPE_UNICHAR,
|
|
49
|
+
TYPE_VARIANT,
|
|
50
|
+
type TypedClass,
|
|
51
|
+
typeFromName,
|
|
52
|
+
typeInterfaces,
|
|
53
|
+
typeIsA,
|
|
54
|
+
typeName,
|
|
55
|
+
typeParent,
|
|
56
|
+
valueIsA,
|
|
57
|
+
} from "./type.js";
|
|
58
|
+
export { getBoxedValue, setBoxedValue } from "./value.js";
|
|
59
|
+
export { alloc, type ExternalObject, type Handle, read, setWrapper, write } from "@gtkx/native";
|
package/src/internal.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export { checkError, createErrorDomain } from "./error.js";
|
|
2
|
+
export { registerClassType, resolveWrapperClass, wrapHandle } from "./registry.js";
|
|
3
|
+
export { resolveType } from "./type.js";
|
|
4
|
+
export {
|
|
5
|
+
fromValue,
|
|
6
|
+
getBoxedValue,
|
|
7
|
+
getValueType,
|
|
8
|
+
inoutValueForBoxedDescriptor,
|
|
9
|
+
newValueForDescriptor,
|
|
10
|
+
outValueForBoxedDescriptor,
|
|
11
|
+
toValue,
|
|
12
|
+
} from "./value.js";
|
package/src/library.ts
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { type BoxedDescriptor, boxedT, type FundamentalDescriptor, fundamentalT } from "./descriptors.js";
|
|
2
|
+
|
|
3
|
+
const LIB = "libgobject-2.0.so.0,libglib-2.0.so.0";
|
|
4
|
+
const VALUE_SIZE = 24;
|
|
5
|
+
const VALUE_T: BoxedDescriptor = boxedT("GValue", { sharedLibrary: LIB, getTypeFnName: "g_value_get_type" });
|
|
6
|
+
|
|
7
|
+
const PARAM_T: FundamentalDescriptor = fundamentalT(LIB, "g_param_spec_ref", "g_param_spec_unref", {
|
|
8
|
+
ownership: "borrowed",
|
|
9
|
+
typeName: "GParam",
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
const VARIANT_T: FundamentalDescriptor = fundamentalT(LIB, "g_variant_ref", "g_variant_unref", {
|
|
13
|
+
ownership: "borrowed",
|
|
14
|
+
typeName: "GVariant",
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
export { LIB, VALUE_SIZE, VALUE_T, PARAM_T, VARIANT_T };
|