@gtkx/runtime 1.5.0 → 2.0.0-beta.1

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 (101) hide show
  1. package/README.md +5 -5
  2. package/dist/application-class.d.ts +1 -1
  3. package/dist/application-class.d.ts.map +1 -1
  4. package/dist/application-class.js +1 -2
  5. package/dist/application-class.js.map +1 -1
  6. package/dist/arg.d.ts +4 -1
  7. package/dist/arg.d.ts.map +1 -1
  8. package/dist/arg.js.map +1 -1
  9. package/dist/bind.d.ts.map +1 -1
  10. package/dist/bind.js +1 -9
  11. package/dist/bind.js.map +1 -1
  12. package/dist/callback.d.ts +6 -1
  13. package/dist/callback.d.ts.map +1 -1
  14. package/dist/callback.js +33 -21
  15. package/dist/callback.js.map +1 -1
  16. package/dist/closure.d.ts +3 -1
  17. package/dist/closure.d.ts.map +1 -1
  18. package/dist/closure.js +17 -6
  19. package/dist/closure.js.map +1 -1
  20. package/dist/descriptors.d.ts +36 -36
  21. package/dist/descriptors.d.ts.map +1 -1
  22. package/dist/descriptors.js +65 -26
  23. package/dist/descriptors.js.map +1 -1
  24. package/dist/element-metadata.d.ts +9 -0
  25. package/dist/element-metadata.d.ts.map +1 -0
  26. package/dist/element-metadata.js +14 -0
  27. package/dist/element-metadata.js.map +1 -0
  28. package/dist/fn.d.ts +8 -2
  29. package/dist/fn.d.ts.map +1 -1
  30. package/dist/fn.js +43 -13
  31. package/dist/fn.js.map +1 -1
  32. package/dist/folded-lengths.d.ts +2 -1
  33. package/dist/folded-lengths.d.ts.map +1 -1
  34. package/dist/folded-lengths.js +17 -7
  35. package/dist/folded-lengths.js.map +1 -1
  36. package/dist/index.d.ts +4 -26
  37. package/dist/index.d.ts.map +1 -1
  38. package/dist/index.js +4 -26
  39. package/dist/index.js.map +1 -1
  40. package/dist/internal.d.ts +5 -2
  41. package/dist/internal.d.ts.map +1 -1
  42. package/dist/internal.js +5 -2
  43. package/dist/internal.js.map +1 -1
  44. package/dist/native-value.d.ts +9 -1
  45. package/dist/native-value.d.ts.map +1 -1
  46. package/dist/native-value.js +41 -5
  47. package/dist/native-value.js.map +1 -1
  48. package/dist/properties.d.ts +1 -1
  49. package/dist/properties.d.ts.map +1 -1
  50. package/dist/properties.js +2 -11
  51. package/dist/properties.js.map +1 -1
  52. package/dist/register-class.d.ts +45 -249
  53. package/dist/register-class.d.ts.map +1 -1
  54. package/dist/register-class.js +38 -1
  55. package/dist/register-class.js.map +1 -1
  56. package/dist/registry.d.ts +26 -3
  57. package/dist/registry.d.ts.map +1 -1
  58. package/dist/registry.js +75 -20
  59. package/dist/registry.js.map +1 -1
  60. package/dist/signal.d.ts.map +1 -1
  61. package/dist/signal.js +9 -22
  62. package/dist/signal.js.map +1 -1
  63. package/dist/t.d.ts +5 -21
  64. package/dist/t.d.ts.map +1 -1
  65. package/dist/t.js +1 -5
  66. package/dist/t.js.map +1 -1
  67. package/dist/type.d.ts +10 -1
  68. package/dist/type.d.ts.map +1 -1
  69. package/dist/type.js +21 -2
  70. package/dist/type.js.map +1 -1
  71. package/dist/value.d.ts.map +1 -1
  72. package/dist/value.js +26 -4
  73. package/dist/value.js.map +1 -1
  74. package/dist/variant.d.ts +2 -6
  75. package/dist/variant.d.ts.map +1 -1
  76. package/dist/variant.js.map +1 -1
  77. package/dist/vfunc-call.d.ts.map +1 -1
  78. package/dist/vfunc-call.js +2 -11
  79. package/dist/vfunc-call.js.map +1 -1
  80. package/package.json +15 -6
  81. package/src/application-class.ts +2 -2
  82. package/src/arg.ts +4 -1
  83. package/src/bind.ts +1 -12
  84. package/src/callback.ts +53 -29
  85. package/src/closure.ts +31 -6
  86. package/src/descriptors.ts +126 -44
  87. package/src/element-metadata.ts +24 -0
  88. package/src/fn.ts +64 -15
  89. package/src/folded-lengths.ts +23 -10
  90. package/src/index.ts +5 -24
  91. package/src/internal.ts +9 -2
  92. package/src/native-value.ts +54 -5
  93. package/src/properties.ts +3 -16
  94. package/src/register-class.ts +104 -249
  95. package/src/registry.ts +110 -19
  96. package/src/signal.ts +9 -34
  97. package/src/t.ts +5 -21
  98. package/src/type.ts +34 -1
  99. package/src/value.ts +37 -5
  100. package/src/variant.ts +2 -6
  101. package/src/vfunc-call.ts +3 -16
@@ -11,11 +11,13 @@ import {
11
11
  } from "@gtkx/native";
12
12
  import {
13
13
  type ArrayDescriptor,
14
+ type BoxedDescriptor,
14
15
  boxedT,
15
16
  type CallbackDescriptor,
16
17
  type FundamentalDescriptor,
17
18
  type HashTableDescriptor,
18
19
  isGtypeDescriptor,
20
+ type ObjectDescriptor,
19
21
  refT,
20
22
  type StructDescriptor,
21
23
  } from "./descriptors.js";
@@ -71,6 +73,17 @@ function boxedFromNative(descriptor: Descriptor, value: unknown): unknown {
71
73
 
72
74
  const handle = value as ExternalObject<Handle>;
73
75
  const type = resolveDescriptorType(descriptor);
76
+ const registered = resolveWrapperClass(type);
77
+
78
+ if (registered !== null) {
79
+ return wrapHandle(handle, registered);
80
+ }
81
+
82
+ const fallback = (descriptor as BoxedDescriptor).fallbackClass?.();
83
+
84
+ if (fallback !== undefined) {
85
+ return wrapHandle(handle, fallback);
86
+ }
74
87
 
75
88
  return wrapHandle(handle, getWrapperClass(type));
76
89
  }
@@ -81,8 +94,19 @@ function fundamentalWrapperClass(descriptor: FundamentalDescriptor, handle: Exte
81
94
  }
82
95
 
83
96
  const declaredType = resolveDescriptorType(descriptor);
97
+ const registered = resolveWrapperClass(getType(handle, declaredType));
98
+
99
+ if (registered !== null) {
100
+ return registered;
101
+ }
102
+
103
+ const fallback = descriptor.fallbackClass?.();
104
+
105
+ if (fallback !== undefined) {
106
+ return fallback;
107
+ }
84
108
 
85
- return resolveWrapperClass(getType(handle, declaredType)) ?? getWrapperClass(declaredType);
109
+ return getWrapperClass(declaredType);
86
110
  }
87
111
 
88
112
  function fundamentalFromNative(descriptor: FundamentalDescriptor, value: unknown): unknown {
@@ -203,7 +227,11 @@ function fromNative(descriptor: Descriptor, value: unknown): unknown {
203
227
 
204
228
  switch (descriptor.kind) {
205
229
  case "object": {
206
- return descriptor.isCallScoped === true ? wrapCallScopedObject(value) : wrapObject(value);
230
+ const fallbackClass = (descriptor as ObjectDescriptor).fallbackClass;
231
+
232
+ return descriptor.isCallScoped === true
233
+ ? wrapCallScopedObject(value, fallbackClass)
234
+ : wrapObject(value, fallbackClass);
207
235
  }
208
236
  case "struct": {
209
237
  return wrapHandle(value as ExternalObject<Handle> | null, (descriptor as StructDescriptor).wrapperClass);
@@ -227,12 +255,33 @@ function collectionToNative(descriptor: ArrayDescriptor, value: unknown): unknow
227
255
  return value == null ? null : mapCollection(descriptor, value, toNative);
228
256
  }
229
257
 
230
- function hashTableToNative(descriptor: HashTableDescriptor, value: unknown): unknown {
258
+ /**
259
+ * Normalises the JavaScript form of a hash table argument into its entry list. A `Map` is the
260
+ * expected form and any other iterable of pairs is accepted; anything else is rejected, a plain
261
+ * object above all, which `Array.from` would quietly flatten into an empty table.
262
+ *
263
+ * @param value The JavaScript value passed for a hash table argument.
264
+ */
265
+ function toHashTableEntries(value: unknown): [unknown, unknown][] | null {
231
266
  if (value == null) {
232
267
  return null;
233
268
  }
234
269
 
235
- return [...(value as Map<unknown, unknown>)].map(([key, val]): [unknown, unknown] => [
270
+ if (typeof (value as Partial<Iterable<unknown>>)[Symbol.iterator] !== "function") {
271
+ throw new TypeError("A hash table argument must be a Map or an iterable of [key, value] pairs");
272
+ }
273
+
274
+ return [...(value as Iterable<[unknown, unknown]>)];
275
+ }
276
+
277
+ function hashTableToNative(descriptor: HashTableDescriptor, value: unknown): unknown {
278
+ const entries = toHashTableEntries(value);
279
+
280
+ if (entries === null) {
281
+ return null;
282
+ }
283
+
284
+ return entries.map(([key, val]): [unknown, unknown] => [
236
285
  toNative(descriptor.keyDescriptor, key),
237
286
  toNative(descriptor.valueDescriptor, val),
238
287
  ]);
@@ -274,4 +323,4 @@ function toNative(descriptor: Descriptor, value: unknown): unknown {
274
323
  }
275
324
  }
276
325
 
277
- export { fromNative, toNative };
326
+ export { fromNative, toHashTableEntries, toNative };
package/src/properties.ts CHANGED
@@ -1,6 +1,5 @@
1
1
  import type { ExternalObject, Handle, RegisterClassProperty } from "@gtkx/native";
2
- import type { AnyClass } from "@gtkx/utils";
3
- import { camelCase, kebabCase, toCamelIdentifier } from "@gtkx/utils";
2
+ import { type AnyClass, camelCase, kebabCase, toCamelIdentifier } from "@gtkx/utils";
4
3
  import { bind } from "./bind.js";
5
4
  import { biguint64T, fundamentalT, refT, sizedArrayT, stringT, structT, uint32T, voidT } from "./descriptors.js";
6
5
  import { LIB, PARAM_T, VALUE_T } from "./library.js";
@@ -347,21 +346,9 @@ function lookupCoercionCheck(gtype: bigint, name: string): PropertyCheck | null
347
346
  }
348
347
 
349
348
  function coercionCheckFor(gtype: bigint, name: string): PropertyCheck | null {
350
- let checks = coercionChecks.get(gtype);
349
+ const checks = coercionChecks.getOrInsertComputed(gtype, () => new Map<string, PropertyCheck | null>());
351
350
 
352
- if (checks === undefined) {
353
- checks = new Map();
354
- coercionChecks.set(gtype, checks);
355
- }
356
-
357
- let check = checks.get(name);
358
-
359
- if (check === undefined) {
360
- check = lookupCoercionCheck(gtype, name);
361
- checks.set(name, check);
362
- }
363
-
364
- return check;
351
+ return checks.getOrInsertComputed(name, () => lookupCoercionCheck(gtype, name));
365
352
  }
366
353
 
367
354
  function truncateToWhole(check: PropertyCheck, value: number): number {
@@ -45,111 +45,67 @@ import {
45
45
  TYPE_NONE,
46
46
  type TypedClass,
47
47
  typeFundamental,
48
+ typeInterfacePrerequisites,
48
49
  typeInterfaces,
49
50
  typeIsA,
50
51
  typeName,
51
52
  } from "./type.js";
52
53
  import { findClassVfuncDescriptor, findInterfaceVfuncDescriptor } from "./vfunc.js";
53
54
 
54
- /**
55
- * A generated interface value, such as `Gio.ListModel`, in the form {@link registerClass} takes it.
56
- * `__impl__` exists only in the type system: it carries the interface's `Impl` type, of which a class
57
- * has to match every member it declares itself, which rejects a class value and a member whose
58
- * signature does not fit the slot it fills. Leaving a slot to the interface is not rejected,
59
- * because `registerClass` does not reject it either: every member of an `Impl` type is optional,
60
- * and the `Partial` holds that open whatever the type declares. The `object` beside it keeps
61
- * TypeScript from also demanding one member in common. An interface that introspection describes
62
- * no vtable for carries `unknown`, since it has no slot a class could fill.
63
- */
55
+ /** A generated interface value, such as `Gio.ListModel`, accepted by {@link registerClass}. */
64
56
  type Interface<TImpl> = AnyClass & {
65
57
  /** Type-level slot holding the interface's `Impl` type; no value ever carries it. */
66
58
  __impl__: (impl: Partial<TImpl> & object) => void;
67
59
  };
68
60
 
69
- /**
70
- * One key of `RegisterClassOptions.properties` with every underscore turned into the dash a canonical
71
- * GObject property name separates its words with.
72
- */
61
+ /** Converts underscores in a property name to canonical dashes. */
73
62
  type Dashed<TName extends string> = TName extends `${infer THead}_${infer TTail}`
74
63
  ? Dashed<`${THead}-${TTail}`>
75
64
  : TName;
76
65
 
77
- /**
78
- * One dashed key in the camelCase spelling the accessors {@link registerClass} installs carry, which is
79
- * the spelling the hooks that address a property by name take.
80
- */
66
+ /** Converts a dashed property name to its JavaScript spelling. */
81
67
  type Camelized<TName extends string> = TName extends `${infer THead}-${infer TTail}`
82
68
  ? `${THead}${Capitalize<Camelized<TTail>>}`
83
69
  : TName;
84
70
 
85
- /**
86
- * The names a registered class carries in its property map: every key of
87
- * `RegisterClassOptions.properties` in camelCase, whichever spelling it was written in.
88
- * A `properties` object given a type of its own rather than left to inference has `string` for its key
89
- * type and names nothing, because a name only known as `string` addresses no member in particular.
90
- */
71
+ /** Property-map keys in the camelCase spelling hooks use. */
91
72
  type InstalledNames<TProperties> = string extends keyof TProperties
92
73
  ? never
93
74
  : Camelized<Dashed<keyof TProperties & string>>;
94
75
 
95
- /**
96
- * The base names the signal surface takes for the signals `RegisterClassOptions.signals` declares:
97
- * each name as written, plus its canonical spelling with underscores turned into dashes, which
98
- * GObject knows the signal by too.
99
- */
76
+ /** Declared signal names in their original and canonical spellings. */
100
77
  type DeclaredSignalBase<TSignals> = (keyof TSignals & string) | Dashed<keyof TSignals & string>;
101
- /**
102
- * The names `connect` and `emit` take for one of the signals `RegisterClassOptions.signals`
103
- * declares: each spelling of each name, plus its detailed form for a signal emitted with a
104
- * `::detail` suffix.
105
- */
78
+ /** Declared signal names, including detailed forms. */
106
79
  type DeclaredSignalName<TSignals> = DeclaredSignalBase<TSignals> | `${DeclaredSignalBase<TSignals>}::${string}`;
107
80
 
108
- /**
109
- * The `connect` and `emit` signatures instances gain for the signals
110
- * `RegisterClassOptions.signals` declares, widening the inherited ones, which take only the names
111
- * introspection knows. A class declaring no signals gains nothing, since no name reaches the
112
- * added signatures.
113
- */
81
+ /** Signal methods added for declared names. */
114
82
  type DeclaredSignalMethods<TSignals> = {
115
- /**
116
- * Type-level map from declared signal name to handler signature, feeding the hooks that
117
- * address a signal by name, such as `useSignal` from `@gtkx/react`; no value ever carries it.
118
- */
83
+ /** Type-level map used by signal hooks. */
119
84
  __signals__?: Record<DeclaredSignalBase<TSignals>, (...args: never[]) => unknown>;
120
- /** Connects a handler to a signal `RegisterClassOptions.signals` declared. */
85
+ /** Connects a handler. */
121
86
  connect(
122
87
  signal: DeclaredSignalName<TSignals>,
123
88
  handler: (...args: never[]) => unknown,
124
89
  isAfter?: boolean,
125
90
  ): number;
126
- /** Emits a signal `RegisterClassOptions.signals` declared. */
91
+ /** Emits a signal. */
127
92
  emit(sigName: DeclaredSignalName<TSignals>, ...args: unknown[]): unknown;
128
- /** Connects a handler to a signal `RegisterClassOptions.signals` declared, for `off` to take off. */
93
+ /** Connects a removable handler. */
129
94
  on(sigName: DeclaredSignalName<TSignals>, callback: (...args: unknown[]) => unknown, isAfter?: boolean): unknown;
130
- /** Connects a handler to a signal `RegisterClassOptions.signals` declared for one emission. */
95
+ /** Connects a one-shot handler. */
131
96
  once(sigName: DeclaredSignalName<TSignals>, callback: (...args: unknown[]) => unknown, isAfter?: boolean): unknown;
132
- /** Disconnects a handler `on` or `once` connected to a signal `RegisterClassOptions.signals` declared. */
97
+ /** Disconnects a handler. */
133
98
  off(sigName: DeclaredSignalName<TSignals>, callback: (...args: unknown[]) => unknown): unknown;
134
99
  };
135
100
 
136
- /**
137
- * An instance of a registered class: everything the class itself declares, plus the property map the
138
- * hooks that address a property by name, such as `useProperty` from `@gtkx/react`, read the installed
139
- * names off. Each name is typed with the value type the class declares for the member of that name,
140
- * so a property the class does not `declare` contributes nothing.
141
- */
101
+ /** A registered instance with declared signal methods and property metadata. */
142
102
  type RegisteredInstance<TInstance, TProperties, TSignals> = TInstance &
143
103
  DeclaredSignalMethods<TSignals> & {
144
104
  /** Type-level map from installed property name to value type; no value ever carries it. */
145
105
  __properties__: Pick<TInstance, InstalledNames<TProperties> & keyof TInstance>;
146
106
  };
147
107
 
148
- /**
149
- * The construct signature and prototype a registered class carries, both giving
150
- * {@link RegisteredInstance}. The signature stays abstract for as long as the class itself is, so
151
- * registering an abstract base leaves it as impossible to construct as it was.
152
- */
108
+ /** A registered class's construct signature and prototype. */
153
109
  type RegisteredConstructor<TClass, TArgs extends unknown[], TInstance> = {
154
110
  /** Object the class's instances inherit from. */
155
111
  prototype: TInstance;
@@ -161,45 +117,24 @@ type RegisteredConstructor<TClass, TArgs extends unknown[], TInstance> = {
161
117
  type RegisteredParts<TClass, TArgs extends unknown[], TInstance> = Omit<TClass, "prototype"> &
162
118
  RegisteredConstructor<TClass, TArgs, TInstance>;
163
119
 
164
- /**
165
- * The class {@link registerClass} hands back: the same class, with the same statics, whose instances
166
- * carry the properties `RegisterClassOptions.properties` installed and take the signals
167
- * `RegisterClassOptions.signals` declares by name. Binding the call to a name, rather than
168
- * discarding it, is what carries those names into the type system.
169
- */
120
+ /** The registered class with its declared properties and signals. */
170
121
  type RegisteredClass<TClass extends AnyClass, TProperties, TSignals> =
171
122
  TClass extends abstract new (...args: infer TArgs) => infer TInstance
172
123
  ? RegisteredParts<TClass, TArgs, RegisteredInstance<TInstance, TProperties, TSignals>>
173
124
  : never;
174
125
 
175
- /**
176
- * A GType in the form `RegisterClassOptions.signals` takes one: the numeric GType itself, such as
177
- * `TYPE_STRING` from `@gtkx/gi/gobject`, or a class carrying one, which is any generated wrapper
178
- * class and any class an earlier {@link registerClass} call registered.
179
- */
126
+ /** A numeric GType or a generated or registered wrapper class. */
180
127
  type SignalGType = bigint | AnyClass<TypedClass>;
181
128
 
182
- /**
183
- * One signal `RegisterClassOptions.signals` creates on the new type, sitting under the signal's
184
- * name. Every part is optional: `{}` declares a signal with no arguments and no return value that
185
- * runs its handlers in the default `RUN_FIRST` stage.
186
- */
129
+ /** A signal installed by {@link registerClass}. */
187
130
  type SignalSpec = {
188
- /**
189
- * `GObject.SignalFlags` bit mask for the signal, defaulting to `RUN_FIRST`. `DETAILED` lets
190
- * handlers connect to and emissions name a `::detail` suffix.
191
- */
131
+ /** `GObject.SignalFlags` bit mask, defaulting to `RUN_FIRST`. */
192
132
  flags?: number;
193
- /** GType of each argument an emission carries, defaulting to none. */
133
+ /** Argument GTypes, defaulting to none. */
194
134
  paramTypes?: SignalGType[];
195
- /** GType of the value an emission returns, defaulting to none. */
135
+ /** Return GType, defaulting to none. */
196
136
  returnType?: SignalGType;
197
- /**
198
- * How the emission combines what its handlers return, limited to the two accumulators GObject
199
- * ships: `"first-wins"` stops the emission at the first handler and keeps its result, and
200
- * `"true-handled"` runs handlers until one returns `true`, which requires a boolean
201
- * `returnType`. Without one, every handler runs and the last result stands.
202
- */
137
+ /** Combines handler results; omitted means the last result wins. */
203
138
  accumulator?: "first-wins" | "true-handled";
204
139
  };
205
140
 
@@ -209,129 +144,29 @@ type RegisterClassOptions<
209
144
  TProperties extends Record<string, PropertySpec>,
210
145
  TSignals extends Record<string, SignalSpec>,
211
146
  > = {
212
- /**
213
- * Name to register the new GType under, defaulting to the class's own name. Either way the
214
- * name has to be a valid GType name: at least three characters, starting with a letter or
215
- * underscore, the rest letters, digits, `-`, `_` or `+`. Any other name throws a `TypeError`.
216
- */
147
+ /** Registered GType name, defaulting to the class name. */
217
148
  typeName?: string;
218
- /**
219
- * Registers the new GType abstract, the way `G_TYPE_FLAG_ABSTRACT` marks a C type: the class
220
- * still serves as a parent for further registered subclasses, which instantiate as usual, but
221
- * constructing it directly throws, whether from JavaScript or from a native caller.
222
- */
149
+ /** Prevents direct construction while allowing registered subclasses. */
223
150
  abstract?: boolean;
224
- /**
225
- * Name instances of the new type carry in CSS, applied through `gtk_widget_class_set_css_name`
226
- * from inside the type's `class_init`, so every instance is born with it, wherever it is
227
- * created from. Requires the class to extend `Gtk.Widget`; registering a non-widget with a
228
- * `cssName` throws.
229
- */
151
+ /** CSS node name for a `Gtk.Widget` subclass. */
230
152
  cssName?: string;
231
153
  /**
232
- * Hook run once, synchronously, while `registerClass` registers the type, after its
233
- * `class_init` has installed the vfuncs, properties and signals declared here. It receives the
234
- * new type's class struct wrapped in its generated GTypeStruct wrapper, so class-level setup
235
- * calls such as `Gtk.WidgetClass.installAction`, `Gtk.WidgetClass.addShortcut` and
236
- * `Gtk.WidgetClass.setLayoutManagerType` have somewhere to land. The wrapper serves the
237
- * members of every struct in the parent chain on one object: a widget subclass sees
238
- * `Gtk.WidgetClass` and `GObject.ObjectClass` members alike, so the parameter can be declared
239
- * as whichever of those types the hook needs. The class struct belongs to the type system for
240
- * the life of the process, so keeping the wrapper around past the hook is safe, if rarely
241
- * useful. An exception the hook throws propagates out of `registerClass`, with the type
242
- * already registered: GObject offers no way to unregister a static type.
154
+ * Runs once after registration with the wrapped class struct. An exception propagates after
155
+ * the static GType has already been registered and cannot be undone.
243
156
  */
244
157
  classInit?(typeStruct: object): void;
245
- /**
246
- * Interfaces the new type implements on top of the ones it inherits, given as the interface values
247
- * themselves, such as `Gio.ListModel`. Their vtable slots are filled from the `vfunc`-prefixed methods on
248
- * the class's prototype chain, each of which has to match the interface's `Impl` type, such as
249
- * `Gio.ListModelImpl`.
250
- */
158
+ /** Interfaces implemented through matching `vfunc` methods. */
251
159
  implements?: Interface<TInstance>[];
252
160
  /**
253
- * Properties to install on the new type, keyed by the name JavaScript addresses each one by and
254
- * valued with the `GObject.ParamSpec` describing it. A key is read in camelCase however it is
255
- * written, so `dewPoint`, `dew_point` and `dew-point` all name the same member, and the ParamSpec
256
- * has to carry a name that reads back as that same member, such as `dew-point`, or registration
257
- * throws: the ParamSpec's name is the one GObject emits `notify` with, and a name the key does
258
- * not spell reaches nothing that listens for it. A word starting with a digit is a word of its
259
- * own on either side of that reading, so `level2Depth` takes a ParamSpec named `level-2-depth`
260
- * as readily as one named `level2-depth`, the way `WebKit.Settings` names
261
- * `enable-2d-canvas-acceleration` for its `enable2dCanvasAcceleration` member. An uppercase
262
- * letter in the ParamSpec's own name is refused, since GObject notifies under that spelling
263
- * and nothing else reads it back. Every property gains prototype accessors, one for the key as
264
- * written, one for it with dashes turned into underscores and one for it in camelCase, each
265
- * unless the class already defines that name. They serve the value from storage of their own on
266
- * the instance, which is also what the type's `get_property` and `set_property` slots read and
267
- * write, so a value set from JavaScript, from `g_object_set_property` and at construction all
268
- * land in the same place.
269
- *
270
- * A write the ParamSpec would refuse throws rather than reaching GObject, which reports such a
271
- * write as a GLib critical and drops it: a `TypeError` for a read-only or construct-only
272
- * property and for a value of a type the property cannot hold, and a `RangeError` for a value
273
- * the ParamSpec rejects. The same checks run over a value handed to the constructor, where
274
- * a construct-only property is the one that is writable. An accepted write emits one `notify`,
275
- * which a `freeze_notify` batch collects; a write of the value the property already holds is
276
- * dropped and emits none.
277
- *
278
- * `null` and `undefined` both mean NULL, and mean it only where the ParamSpec's own type holds
279
- * NULL, which is a string, string-array, boxed, object, interface, param, variant or pointer
280
- * property. Such a property holds the `null` either spelling wrote, so the member, the type's
281
- * `get_property` slot and `g_object_get_property` serve the same thing, and writing the other
282
- * spelling over it emits no `notify`. Every other property, so every integer, floating-point,
283
- * boolean, enum, flags and GType one, refuses both with the same `TypeError` it refuses a
284
- * string with, and keeps the value it already holds. It refuses them for what it holds rather
285
- * than for its range, whatever that range is: the type is checked before the range, so a
286
- * `gint` whose range excludes 0 answers a nullish with that same `TypeError` and never with
287
- * the `RangeError` that names the value GObject would put in its place. The one place
288
- * `undefined` means something else is the constructor, which reads it as the property not
289
- * being given at all and leaves it at the ParamSpec's default, so a property is never handed a
290
- * value it cannot serve back.
291
- *
292
- * A floating-point property takes every JavaScript number, `NaN` and both infinities
293
- * included, and its ParamSpec alone rules on which of them the range admits: a `gdouble`
294
- * bounded by `-Infinity` and `Infinity` holds either infinity, and a magnitude a bounded
295
- * one excludes, like any `NaN`, comes back as the `RangeError` that names what GObject
296
- * would put in its place rather than as the `TypeError` a type the property cannot hold
297
- * earns. A `gfloat` property holds what GObject narrows the double to, so it serves `0.1`
298
- * back as `0.10000000149011612` and a finite magnitude no `gfloat` reaches as an infinity,
299
- * which the range then rules on in turn. That narrowing belongs to the property alone: the
300
- * same magnitude written to a `gfloat` through a generated binding, a signal argument or a
301
- * closure return is refused outright rather than narrowed.
302
- *
303
- * A generated property of a wrapped type answers a nullish differently, and the two halves of
304
- * the API disagree here: that property marshals what it is written through its descriptor
305
- * rather than through the checks above, so `new Gtk.Label({ widthRequest: null })` and a later
306
- * write of `null` to that member both land 0, where a `gint` installed here refuses both.
307
- *
308
- * A class that defines the camelCase member itself owns the property: its own accessor decides
309
- * what a write means, the other spellings forward to it, and the type's property slots read
310
- * and write it rather than the generated storage.
161
+ * ParamSpecs keyed by their JavaScript property names. Keys and ParamSpec names must canonicalize
162
+ * to the same member. Writes validate flags, types, and ranges and notify only on changes.
163
+ * Nullish values require a nullable GType; constructor `undefined` means omitted. An existing
164
+ * camelCase accessor remains authoritative.
311
165
  */
312
166
  properties?: TProperties;
313
167
  /**
314
- * Signals to create on the new type, keyed by signal name and valued with the
315
- * {@link SignalSpec} describing each one. A name has to start with a lowercase letter, continue
316
- * in lowercase letters, digits, `-` and `_`, and be new to the type: one an ancestor type or a
317
- * listed interface already carries throws, and so does one carrying an uppercase letter, which
318
- * GObject would carry under that exact spelling, out of reach of both its dashed spelling and
319
- * its `on<SignalName>` default handler. The two word separators spell the same signal, so a
320
- * signal declared as `data_changed` is connected to and emitted as `data-changed` too.
321
- *
322
- * Instances connect and emit by name through the same `connect`, `on`, `once`, `off` and
323
- * `emit` surface inherited signals use: `registerClass` wraps `connect` and `emit` on the
324
- * class's prototype to serve the declared names, unless the class defines the member itself,
325
- * and hands every other name to the inherited implementation. A handler receives the
326
- * emission's arguments without the leading emitter, matching a generated signal, and what it
327
- * returns becomes the emission's return value when the signal declares one.
328
- *
329
- * The declared parameter GTypes rule the emission: `emit` takes exactly one argument per
330
- * declared parameter, throwing a `TypeError` for any other count, and converts each argument
331
- * into a `GValue` of the declared type, throwing for a value that type cannot hold. The signals
332
- * are created with no class closure of their own, but a method named `on<SignalName>`
333
- * becomes the signal's default handler, the way every `on`-prefixed method that names a
334
- * signal the type carries does; see {@link registerClass}.
168
+ * New canonical signal names and specs. Declared GTypes validate emissions, and a matching
169
+ * `on<SignalName>` method becomes the default handler.
335
170
  */
336
171
  signals?: TSignals;
337
172
  };
@@ -381,62 +216,25 @@ const PROPERTY_VFUNC_SPECS: PropertyVfuncSpec[] = [
381
216
  ];
382
217
 
383
218
  const PROPERTY_VFUNC_NAMES: Set<string> = new Set(PROPERTY_VFUNC_SPECS.map((spec) => spec.methodName));
219
+ const VFUNC_METHOD_PATTERN = /^vfunc[A-Z0-9]/;
384
220
  const TYPE_NAME_PATTERN = /^[A-Za-z_][A-Za-z0-9\-_+]{2,}$/;
385
221
  const UPPER_CASE_PATTERN = /[A-Z]/;
386
222
  const SIGNAL_OVERRIDE_PATTERN = /^on[A-Z]/;
387
223
 
388
224
  /**
389
- * Registers a subclass of a wrapper class as a new GType, wiring up any class and interface
390
- * virtual functions it overrides, both for the interfaces it inherits and for the ones
391
- * `RegisterClassOptions.implements` names.
392
- *
393
- * Throws when the class does not extend a registered wrapper class, when it has no derivable type
394
- * name or the name is not a valid GType name, when an entry in
395
- * `RegisterClassOptions.implements` is not a registered interface, when a
396
- * listed interface has a prerequisite that neither the parent type nor another listed interface meets,
397
- * when the list names `Gio.AsyncInitable` as an interface the parent type does not already
398
- * implement and no method on the chain fills `vfuncInitAsync`, since the default `init_async`
399
- * would run `vfuncInit` on a worker thread, when an entry in
400
- * `RegisterClassOptions.properties` names its `GObject.ParamSpec` something the key it sits under
401
- * does not spell, when an entry in `RegisterClassOptions.signals` carries an invalid name, a name
402
- * spelled with an uppercase letter rather than dashed, a name the type already knows, a GType
403
- * that cannot hold a value, or an accumulator the spec does not admit, and when
404
- * `RegisterClassOptions.cssName` is
405
- * given for a class that does not extend `Gtk.Widget`. An exception thrown by
406
- * `RegisterClassOptions.classInit` also propagates, after the type has already been registered.
407
- *
408
- * A slot is filled from the `vfunc`-prefixed methods on the class's prototype chain, up to but not
409
- * including the registered ancestor the class extends, so a method an intermediate base class
410
- * declares fills a slot the same way one the class itself declares does. A slot nothing on that
411
- * chain fills is left untouched.
412
- *
413
- * Declare every slot as a method: a class field holding a function, such as `vfuncGetNItems = () => 1`,
414
- * is assigned to each instance after registration and never reaches the vtable.
415
- *
416
- * A method named `on<SignalName>` — the signal's name in camelCase after the `on`, so `onClicked`
417
- * for `clicked` and `onItemsChanged` for `items-changed` — becomes that signal's default handler
418
- * when the type carries the signal, whether an ancestor type or an implemented interface brings it
419
- * or `RegisterClassOptions.signals` declares it. The method is installed as a class-closure
420
- * override, so it runs on every emission, on the instances a native caller creates included, in
421
- * the stage the signal's flags name rather than alongside connected handlers. It receives the
422
- * emission's arguments without the leading emitter, with `this` bound to the emitter, and what it
423
- * returns becomes the emission's result when the signal declares one. The same discovery walks the
424
- * prototype chain vfunc discovery walks, and a subclass registering its own `on<SignalName>`
425
- * replaces the handler for its instances, where `super.on<SignalName>()` reaches the replaced one.
426
- * An `on`-prefixed method naming no signal the type carries is left alone as the ordinary method
427
- * it is.
225
+ * Registers a wrapper subclass as a GType with discovered vfuncs, properties, signals, and interfaces.
226
+ * Vfuncs must be prototype methods; function-valued fields never reach the vtable. A matching
227
+ * `on<SignalName>` method becomes the signal's default handler.
428
228
  *
429
- * An override of `vfuncConstructed` runs from inside the base constructor, before JavaScript
430
- * installs the subclass's field initializers and runs its constructor body, so a field still
431
- * reads `undefined` there and reading a `#private` field throws. Declare state the override
432
- * touches without an initializer, and assign private state from the constructor body after
433
- * `super()`. An instance a native caller creates, through `GObject.newv` or `Gtk.Builder`,
434
- * never runs the subclass constructor at all, so its declared fields stay uninitialized for
435
- * the object's whole life.
229
+ * `vfuncConstructed` runs before field initializers and the constructor body. Native-created
230
+ * instances never run the JavaScript constructor, so native-required state belongs in properties
231
+ * or `vfuncConstructed`. A new `Gio.AsyncInitable` must implement `vfuncInitAsync`.
436
232
  *
437
- * @param klass The subclass to register.
438
- * @param options What the new GType gains beyond the vtable slots the class overrides.
439
- * @returns The same class, now registered, with every name in `options.properties` in its property map.
233
+ * @param klass The wrapper subclass to register.
234
+ * @param options Additional GType configuration.
235
+ * @returns The registered class with declared properties and signals in its type.
236
+ * @throws For invalid parents, names, interfaces, vfuncs, properties, signals, or widget options.
237
+ * A `classInit` exception propagates after the static type has been registered and cannot be undone.
440
238
  */
441
239
  function registerClass<
442
240
  T extends AnyClass,
@@ -456,6 +254,7 @@ function registerClass(klass: AnyClass, options: AnyRegisterClassOptions = {}):
456
254
 
457
255
  const name = resolveTypeName(klass, options);
458
256
  const declaredTypes = resolveInterfaceTypes(klass, options.implements ?? []);
257
+ assertInterfacePrerequisites(klass, parentType, declaredTypes);
459
258
  const adoptedTypes = declaredTypes.filter((gtype) => !typeIsA(parentType, gtype));
460
259
  const properties = options.properties ?? {};
461
260
  const signals = resolveDeclaredSignals(klass, options.signals ?? {});
@@ -471,6 +270,7 @@ function registerClass(klass: AnyClass, options: AnyRegisterClassOptions = {}):
471
270
 
472
271
  const claimedMethodNames = new Set(classVfuncs.map((vfunc) => vfunc.methodName));
473
272
  const interfaceBindings = discoverInterfaceBindings(methods, parentType, declaredTypes, claimedMethodNames);
273
+ assertClaimedVfuncs(klass, methods, claimedMethodNames, interfaceBindings);
474
274
 
475
275
  const nativeOptions = withNativeSignals(
476
276
  toNativeOptions(classVfuncs, interfaceBindings, properties, options),
@@ -677,6 +477,61 @@ function wrapVfunc(
677
477
  );
678
478
  }
679
479
 
480
+ const claimedVfuncNames = (
481
+ claimedMethodNames: Set<string>,
482
+ interfaceBindings: InterfaceVfuncBinding[],
483
+ ): Set<string> => {
484
+ const claimed = new Set(claimedMethodNames);
485
+
486
+ for (const binding of interfaceBindings) {
487
+ for (const vfunc of binding.vfuncs) {
488
+ claimed.add(vfunc.methodName);
489
+ }
490
+ }
491
+
492
+ return claimed;
493
+ };
494
+
495
+ const isPrerequisiteMet = (parentType: bigint, declaredTypes: bigint[], prerequisite: bigint): boolean =>
496
+ typeIsA(parentType, prerequisite) || declaredTypes.includes(prerequisite);
497
+
498
+ function assertPrerequisitesFor(klass: AnyClass, parentType: bigint, declaredTypes: bigint[], iface: bigint): void {
499
+ for (const prerequisite of typeInterfacePrerequisites(iface)) {
500
+ if (!isPrerequisiteMet(parentType, declaredTypes, prerequisite)) {
501
+ throw new TypeError(
502
+ `registerClass: ${klass.name} does not meet prerequisite ` +
503
+ `'${typeName(prerequisite) ?? String(prerequisite)}' of interface ` +
504
+ `'${typeName(iface) ?? String(iface)}'`,
505
+ );
506
+ }
507
+ }
508
+ }
509
+
510
+ function assertInterfacePrerequisites(klass: AnyClass, parentType: bigint, declaredTypes: bigint[]): void {
511
+ for (const iface of declaredTypes) {
512
+ assertPrerequisitesFor(klass, parentType, declaredTypes, iface);
513
+ }
514
+ }
515
+
516
+ function assertClaimedVfuncs(
517
+ klass: AnyClass,
518
+ methods: MethodTable,
519
+ claimedMethodNames: Set<string>,
520
+ interfaceBindings: InterfaceVfuncBinding[],
521
+ ): void {
522
+ const claimed = claimedVfuncNames(claimedMethodNames, interfaceBindings);
523
+
524
+ for (const methodName of methods.keys()) {
525
+ if (VFUNC_METHOD_PATTERN.test(methodName) && !claimed.has(methodName)) {
526
+ throw new Error(
527
+ `registerClass: ${klass.name}.${methodName} matches no vtable slot on any ancestor ` +
528
+ "or implemented interface, so the override would never be called; check the name against " +
529
+ "the parent class's virtual methods",
530
+ );
531
+ }
532
+ }
533
+ }
534
+
680
535
  function discoverInterfaceBindings(
681
536
  methods: MethodTable,
682
537
  parentGtype: bigint,