@gtkx/runtime 1.0.0-rc.4 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (133) hide show
  1. package/README.md +5 -5
  2. package/dist/application-class.d.ts +32 -0
  3. package/dist/application-class.d.ts.map +1 -0
  4. package/dist/application-class.js +68 -0
  5. package/dist/application-class.js.map +1 -0
  6. package/dist/arg.d.ts +2 -1
  7. package/dist/arg.d.ts.map +1 -1
  8. package/dist/arg.js +2 -1
  9. package/dist/arg.js.map +1 -1
  10. package/dist/bind.d.ts +11 -0
  11. package/dist/bind.d.ts.map +1 -1
  12. package/dist/bind.js +11 -0
  13. package/dist/bind.js.map +1 -1
  14. package/dist/callback.d.ts +2 -1
  15. package/dist/callback.d.ts.map +1 -1
  16. package/dist/callback.js +118 -20
  17. package/dist/callback.js.map +1 -1
  18. package/dist/closure.d.ts +26 -0
  19. package/dist/closure.d.ts.map +1 -0
  20. package/dist/closure.js +99 -0
  21. package/dist/closure.js.map +1 -0
  22. package/dist/descriptors.d.ts +74 -3
  23. package/dist/descriptors.d.ts.map +1 -1
  24. package/dist/descriptors.js +44 -0
  25. package/dist/descriptors.js.map +1 -1
  26. package/dist/error.d.ts +1 -0
  27. package/dist/error.d.ts.map +1 -1
  28. package/dist/error.js.map +1 -1
  29. package/dist/fn.d.ts +19 -2
  30. package/dist/fn.d.ts.map +1 -1
  31. package/dist/fn.js +64 -27
  32. package/dist/fn.js.map +1 -1
  33. package/dist/index.d.ts +14 -6
  34. package/dist/index.d.ts.map +1 -1
  35. package/dist/index.js +12 -4
  36. package/dist/index.js.map +1 -1
  37. package/dist/internal.d.ts +1 -0
  38. package/dist/internal.d.ts.map +1 -1
  39. package/dist/internal.js +1 -0
  40. package/dist/internal.js.map +1 -1
  41. package/dist/library.d.ts +2 -1
  42. package/dist/library.d.ts.map +1 -1
  43. package/dist/library.js +2 -1
  44. package/dist/library.js.map +1 -1
  45. package/dist/lifecycle.d.ts +43 -11
  46. package/dist/lifecycle.d.ts.map +1 -1
  47. package/dist/lifecycle.js +62 -19
  48. package/dist/lifecycle.js.map +1 -1
  49. package/dist/listeners.d.ts.map +1 -1
  50. package/dist/listeners.js +3 -1
  51. package/dist/listeners.js.map +1 -1
  52. package/dist/mixin.d.ts +3 -2
  53. package/dist/mixin.d.ts.map +1 -1
  54. package/dist/mixin.js +13 -1
  55. package/dist/mixin.js.map +1 -1
  56. package/dist/native-value.d.ts.map +1 -1
  57. package/dist/native-value.js +4 -3
  58. package/dist/native-value.js.map +1 -1
  59. package/dist/object.d.ts +34 -6
  60. package/dist/object.d.ts.map +1 -1
  61. package/dist/object.js +89 -25
  62. package/dist/object.js.map +1 -1
  63. package/dist/param-spec.d.ts +11 -0
  64. package/dist/param-spec.d.ts.map +1 -0
  65. package/dist/param-spec.js +149 -0
  66. package/dist/param-spec.js.map +1 -0
  67. package/dist/promisify.d.ts.map +1 -1
  68. package/dist/promisify.js +3 -2
  69. package/dist/promisify.js.map +1 -1
  70. package/dist/properties.d.ts +38 -7
  71. package/dist/properties.d.ts.map +1 -1
  72. package/dist/properties.js +305 -42
  73. package/dist/properties.js.map +1 -1
  74. package/dist/register-class.d.ts +117 -12
  75. package/dist/register-class.d.ts.map +1 -1
  76. package/dist/register-class.js +108 -76
  77. package/dist/register-class.js.map +1 -1
  78. package/dist/registry.d.ts +69 -19
  79. package/dist/registry.d.ts.map +1 -1
  80. package/dist/registry.js +70 -26
  81. package/dist/registry.js.map +1 -1
  82. package/dist/signal.d.ts +9 -2
  83. package/dist/signal.d.ts.map +1 -1
  84. package/dist/signal.js +63 -7
  85. package/dist/signal.js.map +1 -1
  86. package/dist/type.d.ts +2 -1
  87. package/dist/type.d.ts.map +1 -1
  88. package/dist/type.js +2 -0
  89. package/dist/type.js.map +1 -1
  90. package/dist/value.d.ts +8 -1
  91. package/dist/value.d.ts.map +1 -1
  92. package/dist/value.js +42 -13
  93. package/dist/value.js.map +1 -1
  94. package/dist/vfunc-call.d.ts +37 -0
  95. package/dist/vfunc-call.d.ts.map +1 -0
  96. package/dist/vfunc-call.js +168 -0
  97. package/dist/vfunc-call.js.map +1 -0
  98. package/dist/vfunc-seeds.d.ts +12 -0
  99. package/dist/vfunc-seeds.d.ts.map +1 -0
  100. package/dist/vfunc-seeds.js +19 -0
  101. package/dist/vfunc-seeds.js.map +1 -0
  102. package/dist/vfunc.d.ts +8 -0
  103. package/dist/vfunc.d.ts.map +1 -0
  104. package/dist/vfunc.js +23 -0
  105. package/dist/vfunc.js.map +1 -0
  106. package/package.json +4 -4
  107. package/src/application-class.ts +116 -0
  108. package/src/arg.ts +2 -1
  109. package/src/bind.ts +11 -0
  110. package/src/callback.ts +162 -22
  111. package/src/closure.ts +136 -0
  112. package/src/descriptors.ts +85 -24
  113. package/src/error.ts +1 -0
  114. package/src/fn.ts +92 -31
  115. package/src/index.ts +21 -6
  116. package/src/internal.ts +1 -0
  117. package/src/library.ts +2 -1
  118. package/src/lifecycle.ts +90 -23
  119. package/src/listeners.ts +3 -1
  120. package/src/mixin.ts +15 -2
  121. package/src/native-value.ts +5 -3
  122. package/src/object.ts +128 -34
  123. package/src/param-spec.ts +235 -0
  124. package/src/promisify.ts +4 -2
  125. package/src/properties.ts +458 -45
  126. package/src/register-class.ts +326 -109
  127. package/src/registry.ts +138 -40
  128. package/src/signal.ts +84 -14
  129. package/src/type.ts +3 -1
  130. package/src/value.ts +55 -10
  131. package/src/vfunc-call.ts +243 -0
  132. package/src/vfunc-seeds.ts +36 -0
  133. package/src/vfunc.ts +31 -0
@@ -1,43 +1,75 @@
1
1
  import type { ArrayKind, Descriptor, Ownership } from "@gtkx/native";
2
2
  import type { AnyClass } from "@gtkx/utils";
3
3
 
4
+ /** Descriptor variant for a `gint8`. */
4
5
  type Int8Descriptor = Extract<Descriptor, { kind: "int8" }>;
6
+ /** Descriptor variant for a `guint8`. */
5
7
  type Uint8Descriptor = Extract<Descriptor, { kind: "uint8" }>;
8
+ /** Descriptor variant for a `gint16`. */
6
9
  type Int16Descriptor = Extract<Descriptor, { kind: "int16" }>;
10
+ /** Descriptor variant for a `guint16`. */
7
11
  type Uint16Descriptor = Extract<Descriptor, { kind: "uint16" }>;
12
+ /** Descriptor variant for a `gint32`. */
8
13
  type Int32Descriptor = Extract<Descriptor, { kind: "int32" }>;
14
+ /** Descriptor variant for a `guint32`. */
9
15
  type Uint32Descriptor = Extract<Descriptor, { kind: "uint32" }>;
16
+ /** Descriptor variant for a `gint64` marshalled as a number. */
10
17
  type Int64Descriptor = Extract<Descriptor, { kind: "int64" }>;
18
+ /** Descriptor variant for a `guint64` marshalled as a number. */
11
19
  type Uint64Descriptor = Extract<Descriptor, { kind: "uint64" }>;
20
+ /** Descriptor variant for a `gint64` marshalled as a bigint. */
12
21
  type BigInt64Descriptor = Extract<Descriptor, { kind: "bigint64" }>;
22
+ /** Descriptor variant for a `guint64` marshalled as a bigint. */
13
23
  type BigUint64Descriptor = Extract<Descriptor, { kind: "biguint64" }>;
24
+ /** Descriptor variant for a `gfloat`. */
14
25
  type Float32Descriptor = Extract<Descriptor, { kind: "float32" }>;
26
+ /** Descriptor variant for a `gdouble`. */
15
27
  type Float64Descriptor = Extract<Descriptor, { kind: "float64" }>;
28
+ /** Descriptor variant for an enumeration, carrying the library and `get_type` symbol its GType comes from. */
16
29
  type EnumDescriptor = Extract<Descriptor, { kind: "enum" }>;
30
+ /** Descriptor variant for a flags type, carrying the library and `get_type` symbol its GType comes from. */
17
31
  type FlagsDescriptor = Extract<Descriptor, { kind: "flags" }>;
32
+ /** Descriptor variant for a `gboolean`. */
18
33
  type BooleanDescriptor = Extract<Descriptor, { kind: "boolean" }>;
34
+ /** Descriptor variant for a C string. */
19
35
  type StringDescriptor = Extract<Descriptor, { kind: "string" }>;
36
+ /** Descriptor variant for a `GObject`. */
20
37
  type ObjectDescriptor = Extract<Descriptor, { kind: "object" }>;
38
+ /** Descriptor variant for a `gunichar`. */
21
39
  type UnicharDescriptor = Extract<Descriptor, { kind: "unichar" }>;
40
+ /** Descriptor variant for the absence of a value. */
22
41
  type VoidDescriptor = Extract<Descriptor, { kind: "void" }>;
42
+ /** Descriptor variant for an opaque `gpointer`. */
23
43
  type BufferDescriptor = Extract<Descriptor, { kind: "buffer" }>;
44
+ /** Descriptor variant for a `GBoxed` value. */
24
45
  type BoxedDescriptor = Extract<Descriptor, { kind: "boxed" }>;
25
46
 
47
+ /** Descriptor variant for a plain C struct, extended with the class its decoded value is wrapped in. */
26
48
  type StructDescriptor = Extract<Descriptor, { kind: "struct" }> & {
27
49
  /** Class a decoded value is wrapped in; without it the wrapper comes from the value's own GType. */
28
50
  wrapperClass?: AnyClass;
29
51
  };
30
52
 
53
+ /** Descriptor variant for a ref-counted fundamental type, extended with the class its decoded value is wrapped in. */
31
54
  type FundamentalDescriptor = Extract<Descriptor, { kind: "fundamental" }> & {
32
55
  /** Class a decoded value is wrapped in; without it the wrapper comes from the type named by `typeName`. */
33
56
  wrapperClass?: AnyClass;
34
57
  };
35
58
 
59
+ /** Descriptor variant for an array of items in one of the supported container layouts. */
36
60
  type ArrayDescriptor = Extract<Descriptor, { kind: "array" }>;
61
+ /** Descriptor variant for a `GHashTable`, marshalled as an array of key/value pairs. */
37
62
  type HashTableDescriptor = Extract<Descriptor, { kind: "hashtable" }>;
63
+ /** Descriptor variant for a function pointer a JavaScript function is marshalled into. */
38
64
  type CallbackDescriptor = Extract<Descriptor, { kind: "callback" }>;
65
+ /** Descriptor variant for a pointer to another descriptor's value, for an output or inout argument. */
39
66
  type RefDescriptor = Extract<Descriptor, { kind: "ref" }>;
40
- type TypeDescriptor = BigUint64Descriptor & { type: true };
67
+
68
+ /** Descriptor for a `GType`: a `guint64` marked so it resolves to `G_TYPE_GTYPE` rather than an integer. */
69
+ type TypeDescriptor = BigUint64Descriptor & {
70
+ /** Distinguishes a GType from a plain `guint64` when the GLib type and the GValue type are resolved. */
71
+ type: true;
72
+ };
41
73
 
42
74
  /** How a boxed value is stored, and where its GType and free function are resolved from. */
43
75
  type BoxedOptions = {
@@ -59,9 +91,13 @@ type BoxedOptions = {
59
91
 
60
92
  /** How the callee takes a callback's closure, and how long that closure has to stay alive. */
61
93
  type CallbackOptions = {
62
- /** The callee also takes a `GDestroyNotify`, which frees the closure once it is done with it. */
94
+ /** The callee also takes a destroy notify, which frees the closure once it is done with it. */
63
95
  hasDestroy?: boolean;
64
- /** Position of the `user_data` argument carrying the closure; without one it can never be freed. */
96
+ /** Signature of that destroy notify; defaults to `destroyNotify`, a one-argument `GDestroyNotify`. */
97
+ destroyKind?: CallbackDescriptor["destroyKind"];
98
+ /** The callee also takes a `user_data` pointer; without one the closure can never be freed. */
99
+ hasUserData?: boolean;
100
+ /** Position of `user_data` among the callback's own arguments, dropped before the closure is called. */
65
101
  userDataIndex?: number;
66
102
  /** Lifetime of the closure; defaults to `notified` when `hasDestroy` is set and `call` otherwise. */
67
103
  scope?: CallbackDescriptor["scope"];
@@ -101,32 +137,56 @@ type StructOptions = {
101
137
  wrapperClass?: AnyClass;
102
138
  };
103
139
 
140
+ /** Descriptor for a `gint8`, marshalled as a number. */
104
141
  const int8T: Int8Descriptor = { kind: "int8" };
142
+ /** Descriptor for a `guint8`, marshalled as a number. */
105
143
  const uint8T: Uint8Descriptor = { kind: "uint8" };
144
+ /** Descriptor for a `gint16`, marshalled as a number. */
106
145
  const int16T: Int16Descriptor = { kind: "int16" };
146
+ /** Descriptor for a `guint16`, marshalled as a number. */
107
147
  const uint16T: Uint16Descriptor = { kind: "uint16" };
148
+ /** Descriptor for a `gint32`, marshalled as a number. */
108
149
  const int32T: Int32Descriptor = { kind: "int32" };
150
+ /** Descriptor for a `guint32`, marshalled as a number. */
109
151
  const uint32T: Uint32Descriptor = { kind: "uint32" };
152
+ /** Descriptor for a `gint64`, marshalled as a number and rejected outside the 2^53 safe range. */
110
153
  const int64T: Int64Descriptor = { kind: "int64" };
154
+ /** Descriptor for a `guint64`, marshalled as a number and rejected outside the 2^53 safe range. */
111
155
  const uint64T: Uint64Descriptor = { kind: "uint64" };
156
+ /** Descriptor for a `gint64`, marshalled as a bigint so the full 64-bit range survives. */
112
157
  const bigint64T: BigInt64Descriptor = { kind: "bigint64" };
158
+ /** Descriptor for a `guint64`, marshalled as a bigint so the full 64-bit range survives. */
113
159
  const biguint64T: BigUint64Descriptor = { kind: "biguint64" };
160
+ /** Descriptor for a `GType`, marshalled as a bigint and recognized as a GType by GValue conversion. */
114
161
  const gtypeT: TypeDescriptor = { kind: "biguint64", type: true };
162
+ /** Descriptor for a `gfloat`. */
115
163
  const float32T: Float32Descriptor = { kind: "float32" };
164
+ /** Descriptor for a `gdouble`. */
116
165
  const float64T: Float64Descriptor = { kind: "float64" };
166
+ /** Descriptor for a `gboolean`, marshalled as a JavaScript boolean. */
117
167
  const booleanT: BooleanDescriptor = { kind: "boolean" };
168
+ /** Descriptor for the absence of a value, used as the return descriptor of a `void` function. */
118
169
  const voidT: VoidDescriptor = { kind: "void" };
170
+ /** Descriptor for a `gunichar`, marshalled as a single-character string or a codepoint number. */
119
171
  const unicharT: UnicharDescriptor = { kind: "unichar" };
172
+ /** Descriptor for an opaque `gpointer`, taken from a typed array's memory or a numeric address. */
120
173
  const bufferT: BufferDescriptor = { kind: "buffer" };
121
174
 
175
+ /**
176
+ * Builds a descriptor for a C string, whose optional length sizes the caller-allocated buffer
177
+ * used when the string is passed by reference.
178
+ */
122
179
  const stringT = (ownership: Ownership = "borrowed", length?: number): StringDescriptor =>
123
180
  length === undefined ? { kind: "string", ownership } : { kind: "string", ownership, length };
124
181
 
182
+ /** Builds a descriptor for a `GObject`, wrapped in the class registered for its runtime GType. */
125
183
  const objectT = (ownership: Ownership = "borrowed"): ObjectDescriptor => ({ kind: "object", ownership });
126
184
 
185
+ /** Wraps a descriptor in a pointer to it, for an output or inout argument. */
127
186
  const refT = (innerDescriptor: Descriptor, isInout = false): RefDescriptor =>
128
187
  isInout ? { kind: "ref", innerDescriptor, inout: true } : { kind: "ref", innerDescriptor };
129
188
 
189
+ /** Builds a descriptor for a `GHashTable`, marshalled as an array of key/value pairs. */
130
190
  const hashTableT = (
131
191
  keyDescriptor: Descriptor,
132
192
  valueDescriptor: Descriptor,
@@ -138,6 +198,7 @@ const hashTableT = (
138
198
  ownership,
139
199
  });
140
200
 
201
+ /** Builds a descriptor for an enumeration, resolving its GType from the named `get_type` function. */
141
202
  const enumT = (sharedLibrary: string, typeFnName: string, isSigned: boolean): EnumDescriptor => ({
142
203
  kind: "enum",
143
204
  sharedLibrary,
@@ -145,6 +206,7 @@ const enumT = (sharedLibrary: string, typeFnName: string, isSigned: boolean): En
145
206
  isSigned,
146
207
  });
147
208
 
209
+ /** Builds a descriptor for a flags type, resolving its GType from the named `get_type` function. */
148
210
  const flagsT = (sharedLibrary: string, typeFnName: string, isSigned: boolean): FlagsDescriptor => ({
149
211
  kind: "flags",
150
212
  sharedLibrary,
@@ -182,6 +244,7 @@ const applyBoxedOptions = (result: BoxedDescriptor, options: BoxedOptions): void
182
244
  }
183
245
  };
184
246
 
247
+ /** Builds a descriptor for a `GBoxed` value of the named type. */
185
248
  const boxedT = (typeName: string, options: BoxedOptions = {}): BoxedDescriptor => {
186
249
  const result: BoxedDescriptor = {
187
250
  kind: "boxed",
@@ -194,6 +257,7 @@ const boxedT = (typeName: string, options: BoxedOptions = {}): BoxedDescriptor =
194
257
  return result;
195
258
  };
196
259
 
260
+ /** Builds a descriptor for a plain C struct. */
197
261
  const structT = (ownership: Ownership = "borrowed", options: StructOptions = {}): StructDescriptor => {
198
262
  const result: StructDescriptor = { kind: "struct", ownership };
199
263
 
@@ -216,6 +280,7 @@ const structT = (ownership: Ownership = "borrowed", options: StructOptions = {})
216
280
  return result;
217
281
  };
218
282
 
283
+ /** Builds a descriptor for a fundamental type whose lifetime is managed by named ref and unref functions. */
219
284
  const fundamentalT = (
220
285
  sharedLibrary: string,
221
286
  refFnName: string,
@@ -240,6 +305,7 @@ const fundamentalT = (
240
305
  return result;
241
306
  };
242
307
 
308
+ /** Builds a descriptor for an array of items in one of the supported container layouts. */
243
309
  const arrayT = (
244
310
  itemDescriptor: Descriptor,
245
311
  arrayKind: ArrayKind = "array",
@@ -263,15 +329,19 @@ const arrayT = (
263
329
  return result;
264
330
  };
265
331
 
332
+ /** Builds a descriptor for a `GList` of items. */
266
333
  const listT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
267
334
  arrayT(itemDescriptor, "glist", ownership);
268
335
 
336
+ /** Builds a descriptor for a `GSList` of items. */
269
337
  const slistT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
270
338
  arrayT(itemDescriptor, "gslist", ownership);
271
339
 
340
+ /** Builds a descriptor for a `GPtrArray` of items. */
272
341
  const ptrArrayT = (itemDescriptor: Descriptor, ownership: Ownership = "borrowed"): ArrayDescriptor =>
273
342
  arrayT(itemDescriptor, "gptrarray", ownership);
274
343
 
344
+ /** Builds a descriptor for a `GArray` of items, optionally with an explicit element size. */
275
345
  const gArrayT = (
276
346
  itemDescriptor: Descriptor,
277
347
  ownership: Ownership = "borrowed",
@@ -279,9 +349,11 @@ const gArrayT = (
279
349
  ): ArrayDescriptor =>
280
350
  arrayT(itemDescriptor, "garray", ownership, elementSize === undefined ? undefined : { elementSize });
281
351
 
352
+ /** Builds a descriptor for a `GByteArray`. */
282
353
  const byteArrayT = (ownership: Ownership = "borrowed"): ArrayDescriptor =>
283
354
  arrayT(uint8T, "gbytearray", ownership);
284
355
 
356
+ /** Builds a descriptor for a C array whose length is carried by another argument. */
285
357
  const sizedArrayT = (
286
358
  itemDescriptor: Descriptor,
287
359
  sizeParamIndex: number,
@@ -289,6 +361,7 @@ const sizedArrayT = (
289
361
  elementSize?: number,
290
362
  ): ArrayDescriptor => arrayT(itemDescriptor, "sized", ownership, { sizeParamIndex, elementSize });
291
363
 
364
+ /** Builds a descriptor for a C array of a fixed length. */
292
365
  const fixedArrayT = (
293
366
  itemDescriptor: Descriptor,
294
367
  fixedSize: number,
@@ -296,6 +369,7 @@ const fixedArrayT = (
296
369
  elementSize?: number,
297
370
  ): ArrayDescriptor => arrayT(itemDescriptor, "fixed", ownership, { fixedSize, elementSize });
298
371
 
372
+ /** Builds a descriptor for a function pointer, marshalling a JavaScript function into a native closure. */
299
373
  const callbackT = (
300
374
  argDescriptors: Descriptor[],
301
375
  returnDescriptor: Descriptor,
@@ -307,6 +381,14 @@ const callbackT = (
307
381
  result.hasDestroy = options.hasDestroy;
308
382
  }
309
383
 
384
+ if (options?.destroyKind !== undefined) {
385
+ result.destroyKind = options.destroyKind;
386
+ }
387
+
388
+ if (options?.hasUserData !== undefined) {
389
+ result.hasUserData = options.hasUserData;
390
+ }
391
+
310
392
  if (options?.userDataIndex !== undefined) {
311
393
  result.userDataIndex = options.userDataIndex;
312
394
  }
@@ -354,26 +436,6 @@ export {
354
436
  sizedArrayT,
355
437
  fixedArrayT,
356
438
  callbackT,
357
- type Int8Descriptor,
358
- type Uint8Descriptor,
359
- type Int16Descriptor,
360
- type Uint16Descriptor,
361
- type Int32Descriptor,
362
- type Uint32Descriptor,
363
- type Int64Descriptor,
364
- type Uint64Descriptor,
365
- type BigInt64Descriptor,
366
- type BigUint64Descriptor,
367
- type Float32Descriptor,
368
- type Float64Descriptor,
369
- type EnumDescriptor,
370
- type FlagsDescriptor,
371
- type BooleanDescriptor,
372
- type StringDescriptor,
373
- type ObjectDescriptor,
374
- type UnicharDescriptor,
375
- type VoidDescriptor,
376
- type BufferDescriptor,
377
439
  type BoxedDescriptor,
378
440
  type StructDescriptor,
379
441
  type FundamentalDescriptor,
@@ -381,5 +443,4 @@ export {
381
443
  type HashTableDescriptor,
382
444
  type CallbackDescriptor,
383
445
  type RefDescriptor,
384
- type TypeDescriptor,
385
446
  };
package/src/error.ts CHANGED
@@ -18,6 +18,7 @@ type ErrorLike = Error & {
18
18
  * matches wrapped GLib errors belonging to a specific error domain.
19
19
  */
20
20
  type ErrorDomain<T extends Record<string, number>> = T & {
21
+ /** Backs `instanceof`, matching a wrapped GLib error whose domain is this one. */
21
22
  [Symbol.hasInstance]: (value: unknown) => value is ErrorLike;
22
23
  };
23
24
 
package/src/fn.ts CHANGED
@@ -1,6 +1,7 @@
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";
1
+ import type { CallDescriptor, Descriptor, ExternalObject, Ref } from "@gtkx/native";
2
+ import { call, bind as nativeBind } from "@gtkx/native";
3
+ import type { RefSeeds } from "./vfunc-seeds.js";
4
+ import { type Arg, isCallerAllocatedArg, isOutputArg, isRefArg, requiresInputArg } from "./arg.js";
4
5
  import { wrapCallbackValue } from "./callback.js";
5
6
  import { boxedT, refT } from "./descriptors.js";
6
7
  import { checkError } from "./error.js";
@@ -17,10 +18,17 @@ type FnSpec = {
17
18
  returns: Descriptor;
18
19
  /** The function takes a trailing `GError**`, whose contents are thrown as an error on return. */
19
20
  canThrow?: boolean;
21
+ /**
22
+ * How many of `args` precede the callee's ellipsis, marking it variadic. Omitting it binds a
23
+ * fixed-arity call, which passes the wrong argument classes to a variadic callee on some
24
+ * architectures.
25
+ */
26
+ fixedArgCount?: number;
20
27
  };
21
28
 
22
29
  type ArgSpec = {
23
30
  arg: Arg;
31
+ index: number;
24
32
  isRef: boolean;
25
33
  isCallerAllocated: boolean;
26
34
  requiresInput: boolean;
@@ -28,6 +36,8 @@ type ArgSpec = {
28
36
  isOutParam: boolean;
29
37
  };
30
38
 
39
+ const NO_OUT_PARAMS: unknown[] = [];
40
+
31
41
  const buildNativeArgTypes = (args: Arg[], canThrow: boolean): Descriptor[] => {
32
42
  const nativeArgTypes = args.map((argSpec) =>
33
43
  argSpec.direction !== undefined && argSpec.isCallerAllocated !== true ? refT(argSpec.type) : argSpec.type,
@@ -45,13 +55,14 @@ const buildNativeArgTypes = (args: Arg[], canThrow: boolean): Descriptor[] => {
45
55
  const buildArgSpecs = (args: Arg[]): ArgSpec[] => {
46
56
  let inputCursor = 0;
47
57
 
48
- return args.map((arg) => {
58
+ return args.map((arg, index) => {
49
59
  const isRef = isRefArg(arg);
50
- const requiresInput = !isRef || isInoutArg(arg);
60
+ const requiresInput = requiresInputArg(arg);
51
61
  const isOutParam = isOutputArg(arg) && arg.isConsumed !== true;
52
62
 
53
63
  return {
54
64
  arg,
65
+ index,
55
66
  isRef,
56
67
  isCallerAllocated: isCallerAllocatedArg(arg),
57
68
  requiresInput,
@@ -67,19 +78,19 @@ const resolveCallerAllocated = (inputs: unknown[], inputIndex: number): unknown
67
78
  return wrapper == null ? wrapper : getHandle(wrapper);
68
79
  };
69
80
 
70
- const buildRefValue = (requiresInput: boolean, inputs: unknown[], inputIndex: number): Ref => ({
71
- value: requiresInput ? inputs[inputIndex] : null,
81
+ const buildRefValue = (spec: ArgSpec, inputs: unknown[], seeds: RefSeeds | undefined): Ref => ({
82
+ value: spec.requiresInput ? inputs[spec.inputIndex] : (seeds?.get(spec.index) ?? null),
72
83
  });
73
84
 
74
- const buildNativeValue = (spec: ArgSpec, inputs: unknown[]): unknown => {
75
- const { arg, isRef, isCallerAllocated, requiresInput, inputIndex } = spec;
85
+ const buildNativeValue = (spec: ArgSpec, inputs: unknown[], seeds: RefSeeds | undefined): unknown => {
86
+ const { arg, isRef, isCallerAllocated, inputIndex } = spec;
76
87
 
77
88
  if (isCallerAllocated) {
78
89
  return resolveCallerAllocated(inputs, inputIndex);
79
90
  }
80
91
 
81
92
  if (isRef) {
82
- return buildRefValue(requiresInput, inputs, inputIndex);
93
+ return buildRefValue(spec, inputs, seeds);
83
94
  }
84
95
 
85
96
  if (arg.type.kind === "callback") {
@@ -89,44 +100,79 @@ const buildNativeValue = (spec: ArgSpec, inputs: unknown[]): unknown => {
89
100
  return inputs[inputIndex];
90
101
  };
91
102
 
92
- const buildNativeValues = (plans: ArgSpec[], inputs: unknown[]): unknown[] =>
93
- plans.map((plan) => buildNativeValue(plan, inputs));
103
+ const buildNativeValues = (plans: ArgSpec[], inputs: unknown[], seeds: RefSeeds | undefined): unknown[] =>
104
+ plans.map((plan) => buildNativeValue(plan, inputs, seeds));
94
105
 
95
- const readOutParams = (plans: ArgSpec[], inputs: unknown[], nativeValues: unknown[]): unknown[] => {
96
- const outParams: unknown[] = [];
106
+ const readOutParams = (outPlans: ArgSpec[], inputs: unknown[], nativeValues: unknown[]): unknown[] => {
107
+ if (outPlans.length === 0) {
108
+ return NO_OUT_PARAMS;
109
+ }
97
110
 
98
- for (const [index, { arg, isCallerAllocated, inputIndex, isOutParam }] of plans.entries()) {
99
- if (!isOutParam) {
100
- continue;
101
- }
111
+ return Array.from(outPlans, (plan) =>
112
+ plan.isCallerAllocated
113
+ ? inputs[plan.inputIndex]
114
+ : fromNative(plan.arg.type, (nativeValues[plan.index] as Ref).value));
115
+ };
102
116
 
103
- outParams.push(
104
- isCallerAllocated ? inputs[inputIndex] : fromNative(arg.type, (nativeValues[index] as Ref).value),
105
- );
117
+ const isPassThroughPlan = (plan: ArgSpec, index: number): boolean =>
118
+ plan.inputIndex === index &&
119
+ !plan.isRef &&
120
+ !plan.isCallerAllocated &&
121
+ plan.arg.type.kind !== "callback";
122
+
123
+ const resizeInputs = (inputs: unknown[], argCount: number): unknown[] => {
124
+ while (inputs.length < argCount) {
125
+ inputs.push(undefined);
106
126
  }
107
127
 
108
- return outParams;
128
+ if (inputs.length > argCount) {
129
+ inputs.length = argCount;
130
+ }
131
+
132
+ return inputs;
109
133
  };
110
134
 
111
- function fn(sharedLibrary: string, symbol: string, spec: FnSpec): (...inputs: unknown[]) => unknown {
135
+ const directCallable = (
136
+ descriptor: ExternalObject<CallDescriptor>,
137
+ returnDescriptor: Descriptor,
138
+ argCount: number,
139
+ ): ((...inputs: unknown[]) => unknown) => {
140
+ if (returnDescriptor.kind === "void") {
141
+ return (...inputs) => {
142
+ call(descriptor, resizeInputs(inputs, argCount));
143
+ };
144
+ }
145
+
146
+ return (...inputs) => fromNative(returnDescriptor, call(descriptor, resizeInputs(inputs, argCount)));
147
+ };
148
+
149
+ function fromNativeCallable(
150
+ descriptor: ExternalObject<CallDescriptor>,
151
+ spec: FnSpec,
152
+ getRefSeeds?: () => RefSeeds | undefined,
153
+ ): (...inputs: unknown[]) => unknown {
112
154
  const { args, returns: returnDescriptor, canThrow = false } = spec;
113
- const nativeArgTypes = buildNativeArgTypes(args, canThrow);
114
- const nativeFn = bind(sharedLibrary, symbol, nativeArgTypes, returnDescriptor);
115
155
  const hasPrimary = returnDescriptor.kind !== "void";
116
156
  const plans = buildArgSpecs(args);
157
+ const outPlans = plans.filter((plan) => plan.isOutParam);
158
+ const arePassThrough = plans.every((plan, index) => isPassThroughPlan(plan, index));
159
+
160
+ if (!canThrow && arePassThrough) {
161
+ return directCallable(descriptor, returnDescriptor, plans.length);
162
+ }
117
163
 
118
164
  const shape = (inputs: unknown[], nativeValues: unknown[], nativeResult: unknown): unknown => {
119
165
  const primary = hasPrimary ? fromNative(returnDescriptor, nativeResult) : undefined;
120
166
 
121
- return packTupleResult(readOutParams(plans, inputs, nativeValues), primary, hasPrimary);
167
+ return packTupleResult(readOutParams(outPlans, inputs, nativeValues), primary, hasPrimary);
122
168
  };
123
169
 
124
170
  if (canThrow) {
125
171
  return (...inputs) => {
126
- const nativeValues = buildNativeValues(plans, inputs);
172
+ const nativeValues = buildNativeValues(plans, inputs, getRefSeeds?.());
127
173
  const errorRef: Ref = { value: null };
128
174
  nativeValues.push(errorRef);
129
- const nativeResult = nativeFn(...nativeValues);
175
+ const nativeResult = call(descriptor, nativeValues);
130
176
  checkError(errorRef);
131
177
 
132
178
  return shape(inputs, nativeValues, nativeResult);
@@ -134,10 +180,25 @@ function fn(sharedLibrary: string, symbol: string, spec: FnSpec): (...inputs: un
134
180
  }
135
181
 
136
182
  return (...inputs) => {
137
- const nativeValues = buildNativeValues(plans, inputs);
183
+ const nativeValues = buildNativeValues(plans, inputs, getRefSeeds?.());
138
184
 
139
- return shape(inputs, nativeValues, nativeFn(...nativeValues));
185
+ return shape(inputs, nativeValues, call(descriptor, nativeValues));
140
186
  };
141
187
  }
142
188
 
143
- export { fn };
189
+ /**
190
+ * Binds a symbol in a shared library to a callable that marshals its inputs and packs output
191
+ * arguments into the result. When the spec sets `canThrow`, the reported `GError` is thrown.
192
+ *
193
+ * @param sharedLibrary Shared library the symbol is looked up in.
194
+ * @param symbol Name of the C symbol to bind.
195
+ * @param spec Argument and return descriptors the call is marshalled through.
196
+ */
197
+ function fn(sharedLibrary: string, symbol: string, spec: FnSpec): (...inputs: unknown[]) => unknown {
198
+ const nativeArgTypes = buildNativeArgTypes(spec.args, spec.canThrow ?? false);
199
+ const descriptor = nativeBind(sharedLibrary, symbol, nativeArgTypes, spec.returns, spec.fixedArgCount);
200
+
201
+ return fromNativeCallable(descriptor, spec);
202
+ }
203
+
204
+ export { buildNativeArgTypes, fn, fromNativeCallable };
package/src/index.ts CHANGED
@@ -1,9 +1,13 @@
1
1
  import "./exit-hook.js";
2
2
 
3
+ /** @public */
4
+ export { type ApplicationClass, type CommandLineApplication, createApplication } from "./application-class.js";
5
+ /** @public */
6
+ export { type ClosureCallback, ClosureMarshalError, toClosure, tryToClosure } from "./closure.js";
3
7
  /** @public */
4
8
  export { createErrorDomain, type ErrorDomain } from "./error.js";
5
9
  /** @public */
6
- export { onExit, quit, quitApplication, runApplication } from "./lifecycle.js";
10
+ export { onExit, quit, quitApplication, runApplication, type RunApplicationResult } from "./lifecycle.js";
7
11
  /** @public */
8
12
  export { offSignal, onceSignal, onSignal } from "./listeners.js";
9
13
  /** @public */
@@ -11,13 +15,21 @@ export { installMixins, type Mixin } from "./mixin.js";
11
15
  /** @public */
12
16
  export { fromNative, toNative } from "./native-value.js";
13
17
  /** @public */
14
- export { getObjectProperty, newObjectWithProperties, setObjectProperty } from "./object.js";
18
+ export {
19
+ type ConstructBinding,
20
+ type ConstructBindings,
21
+ getObjectProperty,
22
+ newObjectWithProperties,
23
+ registerConstructProperties,
24
+ setObjectProperty,
25
+ } from "./object.js";
15
26
  /** @public */
16
27
  export { promisify } from "./promisify.js";
17
28
  /** @public */
18
- export { registerClass } from "./register-class.js";
29
+ export { type Interface, registerClass } from "./register-class.js";
19
30
  /** @public */
20
31
  export {
32
+ getClassType,
21
33
  getHandle,
22
34
  getInstanceType,
23
35
  getWrapperClass,
@@ -25,11 +37,10 @@ export {
25
37
  registerWrapperClass,
26
38
  type StaticBase,
27
39
  setHandle,
28
- tryGetHandle,
29
40
  wrapHandle,
30
41
  } from "./registry.js";
31
42
  /** @public */
32
- export { connectSignal, emitSignal, getSignalBaseName, type SignalHandler } from "./signal.js";
43
+ export { connectSignal, disconnectSignal, emitSignal, getSignalBaseName, type SignalHandler } from "./signal.js";
33
44
  /** @public */
34
45
  export { t } from "./t.js";
35
46
  /** @public */
@@ -70,4 +81,8 @@ export {
70
81
  /** @public */
71
82
  export { getBoxedValue, setBoxedValue } from "./value.js";
72
83
  /** @public */
73
- export { alloc, type ExternalObject, type Handle, read, setWrapper, write } from "@gtkx/native";
84
+ export { callParent, callVfunc } from "./vfunc-call.js";
85
+ /** @public */
86
+ export { alloc, type ExternalObject, type Handle, read, write } from "@gtkx/native";
87
+ /** @public */
88
+ export { type AnyClass } from "@gtkx/utils";
package/src/internal.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  export { checkError, createErrorDomain } from "./error.js";
2
2
  export { registerClassType, resolveWrapperClass, wrapHandle } from "./registry.js";
3
+ export { hasSignalListener } from "./signal.js";
3
4
  export { resolveType } from "./type.js";
4
5
  export {
5
6
  fromValue,
package/src/library.ts CHANGED
@@ -2,6 +2,7 @@ import { type BoxedDescriptor, boxedT, type FundamentalDescriptor, fundamentalT
2
2
 
3
3
  const LIB = "libgobject-2.0.so.0,libglib-2.0.so.0";
4
4
  const VALUE_SIZE = 24;
5
+ const CLOSURE_SIZE = 32;
5
6
  const VALUE_T: BoxedDescriptor = boxedT("GValue", { sharedLibrary: LIB, getTypeFnName: "g_value_get_type" });
6
7
 
7
8
  const PARAM_T: FundamentalDescriptor = fundamentalT(LIB, "g_param_spec_ref", "g_param_spec_unref", {
@@ -14,4 +15,4 @@ const VARIANT_T: FundamentalDescriptor = fundamentalT(LIB, "g_variant_ref", "g_v
14
15
  typeName: "GVariant",
15
16
  });
16
17
 
17
- export { LIB, VALUE_SIZE, VALUE_T, PARAM_T, VARIANT_T };
18
+ export { CLOSURE_SIZE, LIB, VALUE_SIZE, VALUE_T, PARAM_T, VARIANT_T };