@gtkx/runtime 1.0.0-rc.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.
Files changed (116) hide show
  1. package/dist/arg.d.ts +6 -5
  2. package/dist/arg.d.ts.map +1 -1
  3. package/dist/arg.js +5 -4
  4. package/dist/arg.js.map +1 -1
  5. package/dist/bind.d.ts +3 -2
  6. package/dist/bind.d.ts.map +1 -1
  7. package/dist/bind.js +5 -3
  8. package/dist/bind.js.map +1 -1
  9. package/dist/callback.d.ts +3 -3
  10. package/dist/callback.d.ts.map +1 -1
  11. package/dist/callback.js +51 -33
  12. package/dist/callback.js.map +1 -1
  13. package/dist/descriptors.d.ts +67 -64
  14. package/dist/descriptors.d.ts.map +1 -1
  15. package/dist/descriptors.js +92 -60
  16. package/dist/descriptors.js.map +1 -1
  17. package/dist/error.d.ts +5 -6
  18. package/dist/error.d.ts.map +1 -1
  19. package/dist/error.js +10 -6
  20. package/dist/error.js.map +1 -1
  21. package/dist/exit-hook.d.ts +2 -0
  22. package/dist/exit-hook.d.ts.map +1 -0
  23. package/dist/exit-hook.js +3 -0
  24. package/dist/exit-hook.js.map +1 -0
  25. package/dist/fn.d.ts +2 -2
  26. package/dist/fn.d.ts.map +1 -1
  27. package/dist/fn.js +26 -15
  28. package/dist/fn.js.map +1 -1
  29. package/dist/index.d.ts +3 -2
  30. package/dist/index.d.ts.map +1 -1
  31. package/dist/index.js +2 -1
  32. package/dist/index.js.map +1 -1
  33. package/dist/library.d.ts +6 -5
  34. package/dist/library.d.ts.map +1 -1
  35. package/dist/library.js +6 -5
  36. package/dist/library.js.map +1 -1
  37. package/dist/lifecycle.d.ts +10 -9
  38. package/dist/lifecycle.d.ts.map +1 -1
  39. package/dist/lifecycle.js +38 -23
  40. package/dist/lifecycle.js.map +1 -1
  41. package/dist/listeners.d.ts +8 -8
  42. package/dist/listeners.d.ts.map +1 -1
  43. package/dist/listeners.js +28 -15
  44. package/dist/listeners.js.map +1 -1
  45. package/dist/mixin.d.ts +5 -11
  46. package/dist/mixin.d.ts.map +1 -1
  47. package/dist/mixin.js +24 -14
  48. package/dist/mixin.js.map +1 -1
  49. package/dist/native-value.d.ts +3 -2
  50. package/dist/native-value.d.ts.map +1 -1
  51. package/dist/native-value.js +75 -42
  52. package/dist/native-value.js.map +1 -1
  53. package/dist/object.d.ts +4 -3
  54. package/dist/object.d.ts.map +1 -1
  55. package/dist/object.js +8 -5
  56. package/dist/object.js.map +1 -1
  57. package/dist/promisify.d.ts +9 -1
  58. package/dist/promisify.d.ts.map +1 -1
  59. package/dist/promisify.js +18 -15
  60. package/dist/promisify.js.map +1 -1
  61. package/dist/properties.d.ts +14 -0
  62. package/dist/properties.d.ts.map +1 -0
  63. package/dist/properties.js +79 -0
  64. package/dist/properties.js.map +1 -0
  65. package/dist/register-class.d.ts +10 -2
  66. package/dist/register-class.d.ts.map +1 -1
  67. package/dist/register-class.js +83 -29
  68. package/dist/register-class.js.map +1 -1
  69. package/dist/registry.d.ts +23 -21
  70. package/dist/registry.d.ts.map +1 -1
  71. package/dist/registry.js +54 -35
  72. package/dist/registry.js.map +1 -1
  73. package/dist/signal.d.ts +7 -6
  74. package/dist/signal.d.ts.map +1 -1
  75. package/dist/signal.js +19 -12
  76. package/dist/signal.js.map +1 -1
  77. package/dist/t.d.ts +2 -2
  78. package/dist/t.d.ts.map +1 -1
  79. package/dist/t.js +2 -1
  80. package/dist/t.js.map +1 -1
  81. package/dist/tuple.d.ts +3 -2
  82. package/dist/tuple.d.ts.map +1 -1
  83. package/dist/tuple.js +7 -4
  84. package/dist/tuple.js.map +1 -1
  85. package/dist/type.d.ts +48 -41
  86. package/dist/type.d.ts.map +1 -1
  87. package/dist/type.js +107 -87
  88. package/dist/type.js.map +1 -1
  89. package/dist/value.d.ts +19 -10
  90. package/dist/value.d.ts.map +1 -1
  91. package/dist/value.js +164 -99
  92. package/dist/value.js.map +1 -1
  93. package/package.json +4 -6
  94. package/src/arg.ts +7 -5
  95. package/src/bind.ts +13 -4
  96. package/src/callback.ts +97 -35
  97. package/src/descriptors.ts +227 -85
  98. package/src/error.ts +18 -9
  99. package/src/exit-hook.ts +3 -0
  100. package/src/fn.ts +53 -22
  101. package/src/index.ts +4 -2
  102. package/src/library.ts +7 -5
  103. package/src/lifecycle.ts +54 -26
  104. package/src/listeners.ts +46 -18
  105. package/src/mixin.ts +39 -14
  106. package/src/native-value.ts +103 -46
  107. package/src/object.ts +19 -7
  108. package/src/promisify.ts +42 -15
  109. package/src/properties.ts +115 -0
  110. package/src/register-class.ts +157 -30
  111. package/src/registry.ts +144 -54
  112. package/src/signal.ts +53 -13
  113. package/src/t.ts +3 -1
  114. package/src/tuple.ts +16 -4
  115. package/src/type.ts +183 -93
  116. package/src/value.ts +231 -108
package/src/type.ts CHANGED
@@ -1,9 +1,10 @@
1
1
  import { type Descriptor, resolveType as nativeResolveType } from "@gtkx/native";
2
2
  import { bind } from "./bind.js";
3
3
  import {
4
- type BoxedDescriptor,
4
+ type ArrayDescriptor,
5
5
  biguint64T,
6
6
  booleanT,
7
+ type BoxedDescriptor,
7
8
  type FundamentalDescriptor,
8
9
  refT,
9
10
  sizedArrayT,
@@ -13,113 +14,144 @@ import {
13
14
  import { LIB } from "./library.js";
14
15
 
15
16
  /** Object tagged with its GLib type through a `__type__` GType field. */
16
- export type TypedClass = {
17
+ type TypedClass = {
17
18
  __type__: bigint;
18
19
  };
19
20
 
20
- const resolvedTypeCache = new Map<string, bigint>();
21
+ type ResolvableKind = "enum" | "flags" | "boxed" | "fundamental" | "array";
22
+ /** Descriptor kinds whose GType is resolved from library metadata rather than a fixed fundamental type. */
23
+ type ResolvableDescriptor = Extract<Descriptor, { kind: ResolvableKind }>;
21
24
 
25
+ const resolvedTypeCache: Map<string, bigint> = new Map();
22
26
  const gTypeFromName = bind(LIB, "g_type_from_name", [stringT("borrowed")], biguint64T);
23
27
  const gTypeIsA = bind(LIB, "g_type_is_a", [biguint64T, biguint64T], booleanT);
24
28
  const gTypeParent = bind(LIB, "g_type_parent", [biguint64T], biguint64T);
25
29
  const gTypeFundamental = bind(LIB, "g_type_fundamental", [biguint64T], biguint64T);
26
30
  const gTypeName = bind(LIB, "g_type_name", [biguint64T], stringT("borrowed"));
27
31
  const gTypeInterfaces = bind(LIB, "g_type_interfaces", [biguint64T, refT(uint32T)], sizedArrayT(biguint64T, 1, "full"));
28
-
29
- const lazyType = (name: string): (() => bigint) => {
30
- let cached: bigint | undefined;
31
- return () => {
32
- cached ??= typeFromName(name);
33
- return cached;
34
- };
35
- };
36
-
37
- export const isTypedClass = (value: unknown): value is TypedClass =>
38
- typeof value === "object" && value !== null && "__type__" in value && typeof value.__type__ === "bigint";
39
-
40
32
  /** GType tag for an invalid or uninitialized type. */
41
- export const TYPE_INVALID: bigint = 0n;
33
+ const TYPE_INVALID = 0n;
42
34
  /** GType tag for the absence of a value (`void`). */
43
- export const TYPE_NONE: bigint = typeFromName("void");
35
+ const TYPE_NONE: bigint = typeFromName("void");
44
36
  /** GType tag for the base GInterface type. */
45
- export const TYPE_INTERFACE: bigint = typeFromName("GInterface");
37
+ const TYPE_INTERFACE: bigint = typeFromName("GInterface");
46
38
  /** GType tag for `gchar` (signed 8-bit integer). */
47
- export const TYPE_CHAR: bigint = typeFromName("gchar");
39
+ const TYPE_CHAR: bigint = typeFromName("gchar");
48
40
  /** GType tag for `guchar` (unsigned 8-bit integer). */
49
- export const TYPE_UCHAR: bigint = typeFromName("guchar");
41
+ const TYPE_UCHAR: bigint = typeFromName("guchar");
50
42
  /** GType tag for `gboolean`. */
51
- export const TYPE_BOOLEAN: bigint = typeFromName("gboolean");
43
+ const TYPE_BOOLEAN: bigint = typeFromName("gboolean");
52
44
  /** GType tag for `gint`. */
53
- export const TYPE_INT: bigint = typeFromName("gint");
45
+ const TYPE_INT: bigint = typeFromName("gint");
54
46
  /** GType tag for `guint`. */
55
- export const TYPE_UINT: bigint = typeFromName("guint");
47
+ const TYPE_UINT: bigint = typeFromName("guint");
56
48
  /** GType tag for `glong`. */
57
- export const TYPE_LONG: bigint = typeFromName("glong");
49
+ const TYPE_LONG: bigint = typeFromName("glong");
58
50
  /** GType tag for `gulong`. */
59
- export const TYPE_ULONG: bigint = typeFromName("gulong");
51
+ const TYPE_ULONG: bigint = typeFromName("gulong");
60
52
  /** GType tag for `gint64`. */
61
- export const TYPE_INT64: bigint = typeFromName("gint64");
53
+ const TYPE_INT64: bigint = typeFromName("gint64");
62
54
  /** GType tag for `guint64`. */
63
- export const TYPE_UINT64: bigint = typeFromName("guint64");
55
+ const TYPE_UINT64: bigint = typeFromName("guint64");
64
56
  /** GType tag for the base GEnum type. */
65
- export const TYPE_ENUM: bigint = typeFromName("GEnum");
57
+ const TYPE_ENUM: bigint = typeFromName("GEnum");
66
58
  /** GType tag for the base GFlags type. */
67
- export const TYPE_FLAGS: bigint = typeFromName("GFlags");
59
+ const TYPE_FLAGS: bigint = typeFromName("GFlags");
68
60
  /** GType tag for `gfloat`. */
69
- export const TYPE_FLOAT: bigint = typeFromName("gfloat");
61
+ const TYPE_FLOAT: bigint = typeFromName("gfloat");
70
62
  /** GType tag for `gdouble`. */
71
- export const TYPE_DOUBLE: bigint = typeFromName("gdouble");
63
+ const TYPE_DOUBLE: bigint = typeFromName("gdouble");
72
64
  /** GType tag for `gchararray` (a nul-terminated C string). */
73
- export const TYPE_STRING: bigint = typeFromName("gchararray");
65
+ const TYPE_STRING: bigint = typeFromName("gchararray");
74
66
  /** GType tag for `gpointer` (an opaque pointer). */
75
- export const TYPE_POINTER: bigint = typeFromName("gpointer");
67
+ const TYPE_POINTER: bigint = typeFromName("gpointer");
76
68
  /** GType tag for the base GBoxed type. */
77
- export const TYPE_BOXED: bigint = typeFromName("GBoxed");
69
+ const TYPE_BOXED: bigint = typeFromName("GBoxed");
78
70
  /** GType tag for the base GParam type. */
79
- export const TYPE_PARAM: bigint = typeFromName("GParam");
71
+ const TYPE_PARAM: bigint = typeFromName("GParam");
80
72
  /** GType tag for the base GObject type. */
81
- export const TYPE_OBJECT: bigint = typeFromName("GObject");
73
+ const TYPE_OBJECT: bigint = typeFromName("GObject");
82
74
  /** GType tag for a GType value itself. */
83
- export const TYPE_GTYPE: bigint = typeFromName("GType");
75
+ const TYPE_GTYPE: bigint = typeFromName("GType");
84
76
  /** GType tag for GVariant. */
85
- export const TYPE_VARIANT: bigint = typeFromName("GVariant");
77
+ const TYPE_VARIANT: bigint = typeFromName("GVariant");
86
78
  /** GType tag for a Unicode character stored as `guint`. */
87
- export const TYPE_UNICHAR: bigint = typeFromName("guint");
88
- export const getErrorType: () => bigint = lazyType("GError");
89
- export const getStrvType: () => bigint = lazyType("GStrv");
79
+ const TYPE_UNICHAR: bigint = typeFromName("guint");
80
+ const getErrorType: () => bigint = lazyType("GError");
81
+ const getStrvType: () => bigint = lazyType("GStrv");
82
+
83
+ const PLAIN_DESCRIPTOR_TYPES: Partial<Record<Descriptor["kind"], bigint>> = {
84
+ boolean: TYPE_BOOLEAN,
85
+ string: TYPE_STRING,
86
+ int8: TYPE_INT,
87
+ int16: TYPE_INT,
88
+ int32: TYPE_INT,
89
+ uint8: TYPE_UINT,
90
+ uint16: TYPE_UINT,
91
+ uint32: TYPE_UINT,
92
+ int64: TYPE_INT64,
93
+ bigint64: TYPE_INT64,
94
+ uint64: TYPE_UINT64,
95
+ biguint64: TYPE_UINT64,
96
+ float32: TYPE_FLOAT,
97
+ float64: TYPE_DOUBLE,
98
+ object: TYPE_OBJECT,
99
+ };
100
+
101
+ const RESOLVABLE_DESCRIPTOR_KINDS: Set<Descriptor["kind"]> = new Set<ResolvableKind>([
102
+ "enum",
103
+ "flags",
104
+ "boxed",
105
+ "fundamental",
106
+ "array",
107
+ ]);
108
+
109
+ function lazyType(name: string): () => bigint {
110
+ let cached: bigint | undefined;
111
+
112
+ return () => {
113
+ cached ??= typeFromName(name);
114
+
115
+ return cached;
116
+ };
117
+ }
118
+
119
+ const isTypedClass = (value: unknown): value is TypedClass =>
120
+ typeof value === "object" && value !== null && "__type__" in value && typeof value.__type__ === "bigint";
90
121
 
91
- /** Returns whether `type` is `isAType` or descends from it. */
92
- export function typeIsA(type: bigint, isAType: bigint): boolean {
93
- return gTypeIsA(type, isAType) as boolean;
122
+ /** Returns whether `type` is `ancestorType` or descends from it. */
123
+ function typeIsA(type: bigint, ancestorType: bigint): boolean {
124
+ return gTypeIsA(type, ancestorType) as boolean;
94
125
  }
95
126
 
96
127
  /** Returns the immediate parent GType of the given type. */
97
- export function typeParent(type: bigint): bigint {
128
+ function typeParent(type: bigint): bigint {
98
129
  return gTypeParent(type) as bigint;
99
130
  }
100
131
 
101
132
  /** Returns the GTypes of the interfaces implemented by the given type. */
102
- export function typeInterfaces(type: bigint): bigint[] {
133
+ function typeInterfaces(type: bigint): bigint[] {
103
134
  const nInterfacesRef = { value: 0 };
135
+
104
136
  return gTypeInterfaces(type, nInterfacesRef) as bigint[];
105
137
  }
106
138
 
107
139
  /** Returns the GType registered under the given name, or `TYPE_INVALID` if none exists. */
108
- export function typeFromName(name: string): bigint {
140
+ function typeFromName(name: string): bigint {
109
141
  return gTypeFromName(name) as bigint;
110
142
  }
111
143
 
112
- export function typeFundamental(type: bigint): bigint {
144
+ function typeFundamental(type: bigint): bigint {
113
145
  return gTypeFundamental(type) as bigint;
114
146
  }
115
147
 
116
148
  /** Returns the registered name of a GType, or null if it has none. */
117
- export function typeName(type: bigint): string | null {
149
+ function typeName(type: bigint): string | null {
118
150
  return (gTypeName(type) ?? null) as string | null;
119
151
  }
120
152
 
121
153
  /** Returns whether `value` is a typed wrapper whose GType is or descends from `gtype`. */
122
- export function valueIsA(value: unknown, gtype: bigint): boolean {
154
+ function valueIsA(value: unknown, gtype: bigint): boolean {
123
155
  return isTypedClass(value) && typeIsA(value.__type__, gtype);
124
156
  }
125
157
 
@@ -127,74 +159,132 @@ export function valueIsA(value: unknown, gtype: bigint): boolean {
127
159
  * Resolves the GType produced by a `get_type` function in a shared library, caching
128
160
  * the result per library and function name.
129
161
  * @param sharedLibrary Path or name of the shared library exporting the function.
130
- * @param getTypeFnName Name of the `*_get_type` function to call.
162
+ * @param typeFnName Name of the `*_get_type` function to call.
131
163
  */
132
- export const resolveType = (sharedLibrary: string, getTypeFnName: string): bigint => {
133
- const key = `${sharedLibrary}:${getTypeFnName}`;
164
+ const resolveType = (sharedLibrary: string, typeFnName: string): bigint => {
165
+ const key = `${sharedLibrary}:${typeFnName}`;
134
166
  const cached = resolvedTypeCache.get(key);
135
- if (cached !== undefined) return cached;
136
- const gtype = nativeResolveType(sharedLibrary, getTypeFnName);
167
+
168
+ if (cached !== undefined) {
169
+ return cached;
170
+ }
171
+
172
+ const gtype = nativeResolveType(sharedLibrary, typeFnName);
137
173
  resolvedTypeCache.set(key, gtype);
174
+
138
175
  return gtype;
139
176
  };
140
177
 
141
- export const resolveBoxedType = (descriptor: BoxedDescriptor): bigint => {
178
+ const resolveBoxedType = (descriptor: BoxedDescriptor): bigint => {
142
179
  if (descriptor.getTypeFnName && descriptor.sharedLibrary) {
143
180
  return resolveType(descriptor.sharedLibrary, descriptor.getTypeFnName);
144
181
  }
182
+
145
183
  const gtype = typeFromName(descriptor.typeName);
184
+
146
185
  if (gtype === TYPE_INVALID) {
147
186
  throw new Error(`Cannot resolve gtype for boxed type '${descriptor.typeName}'`);
148
187
  }
188
+
149
189
  return gtype;
150
190
  };
151
191
 
152
- export const resolveFundamentalType = (descriptor: FundamentalDescriptor): bigint => {
192
+ const resolveFundamentalType = (descriptor: FundamentalDescriptor): bigint => {
153
193
  if (descriptor.typeName) {
154
194
  const gtype = typeFromName(descriptor.typeName);
155
- if (gtype !== TYPE_INVALID) return gtype;
195
+
196
+ if (gtype !== TYPE_INVALID) {
197
+ return gtype;
198
+ }
156
199
  }
157
- throw new Error(`Cannot resolve gtype for fundamental type without a typeName`);
200
+
201
+ throw new Error("Cannot resolve gtype for fundamental type without a typeName");
158
202
  };
159
203
 
160
- export function resolveDescriptorType(descriptor: Descriptor): bigint {
161
- if (descriptor.kind === "biguint64" && "type" in descriptor) return TYPE_GTYPE;
204
+ function resolveArrayType(descriptor: ArrayDescriptor): bigint {
205
+ if (descriptor.itemDescriptor.kind === "string" && descriptor.arrayKind === "array") {
206
+ return getStrvType();
207
+ }
208
+
209
+ throw new Error(`Unsupported array type ${descriptor.arrayKind} of ${descriptor.itemDescriptor.kind}`);
210
+ }
211
+
212
+ function isResolvableDescriptor(descriptor: Descriptor): descriptor is ResolvableDescriptor {
213
+ return RESOLVABLE_DESCRIPTOR_KINDS.has(descriptor.kind);
214
+ }
215
+
216
+ function resolveDescriptorType(descriptor: Descriptor): bigint {
217
+ if (descriptor.kind === "biguint64" && "type" in descriptor) {
218
+ return TYPE_GTYPE;
219
+ }
220
+
221
+ const plain = PLAIN_DESCRIPTOR_TYPES[descriptor.kind];
222
+
223
+ if (plain !== undefined) {
224
+ return plain;
225
+ }
226
+
227
+ if (!isResolvableDescriptor(descriptor)) {
228
+ throw new Error(`Unsupported type descriptor '${descriptor.kind}'`);
229
+ }
230
+
162
231
  switch (descriptor.kind) {
163
- case "boolean":
164
- return TYPE_BOOLEAN;
165
- case "string":
166
- return TYPE_STRING;
167
- case "int8":
168
- case "int16":
169
- case "int32":
170
- return TYPE_INT;
171
- case "uint8":
172
- case "uint16":
173
- case "uint32":
174
- return TYPE_UINT;
175
- case "int64":
176
- case "bigint64":
177
- return TYPE_INT64;
178
- case "uint64":
179
- case "biguint64":
180
- return TYPE_UINT64;
181
- case "float32":
182
- return TYPE_FLOAT;
183
- case "float64":
184
- return TYPE_DOUBLE;
185
- case "object":
186
- return TYPE_OBJECT;
187
232
  case "enum":
188
- case "flags":
233
+ case "flags": {
189
234
  return resolveType(descriptor.sharedLibrary, descriptor.getTypeFnName);
190
- case "boxed":
235
+ }
236
+ case "boxed": {
191
237
  return resolveBoxedType(descriptor);
192
- case "fundamental":
238
+ }
239
+ case "fundamental": {
193
240
  return resolveFundamentalType(descriptor);
194
- case "array":
195
- if (descriptor.itemDescriptor.kind === "string" && descriptor.arrayKind === "array") return getStrvType();
196
- throw new Error(`Unsupported array type ${descriptor.arrayKind} of ${descriptor.itemDescriptor.kind}`);
197
- default:
198
- throw new Error(`Unsupported type descriptor '${descriptor.kind}'`);
241
+ }
242
+ case "array": {
243
+ return resolveArrayType(descriptor);
244
+ }
199
245
  }
200
246
  }
247
+
248
+ export {
249
+ TYPE_INVALID,
250
+ TYPE_NONE,
251
+ TYPE_INTERFACE,
252
+ TYPE_CHAR,
253
+ TYPE_UCHAR,
254
+ TYPE_BOOLEAN,
255
+ TYPE_INT,
256
+ TYPE_UINT,
257
+ TYPE_LONG,
258
+ TYPE_ULONG,
259
+ TYPE_INT64,
260
+ TYPE_UINT64,
261
+ TYPE_ENUM,
262
+ TYPE_FLAGS,
263
+ TYPE_FLOAT,
264
+ TYPE_DOUBLE,
265
+ TYPE_STRING,
266
+ TYPE_POINTER,
267
+ TYPE_BOXED,
268
+ TYPE_PARAM,
269
+ TYPE_OBJECT,
270
+ TYPE_GTYPE,
271
+ TYPE_VARIANT,
272
+ TYPE_UNICHAR,
273
+ getErrorType,
274
+ getStrvType,
275
+ isResolvableDescriptor,
276
+ isTypedClass,
277
+ typeIsA,
278
+ typeParent,
279
+ typeInterfaces,
280
+ typeFromName,
281
+ typeFundamental,
282
+ typeName,
283
+ valueIsA,
284
+ resolveType,
285
+ resolveBoxedType,
286
+ resolveFundamentalType,
287
+ resolveDescriptorType,
288
+ type ResolvableDescriptor,
289
+ type TypedClass,
290
+ };