@gjsify/node-gi 0.13.0 → 0.20.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/index.d.ts CHANGED
@@ -49,28 +49,62 @@ export function getConstantValue(namespace: string, name: string): unknown;
49
49
  */
50
50
  export function getEnumValues(namespace: string, name: string): Record<string, number>;
51
51
 
52
+ /**
53
+ * For an enum type registered as a GError domain (e.g. `Gio.IOErrorEnum`), report
54
+ * its domain quark name + numeric quark; `null` for a plain enum.
55
+ */
56
+ export function getErrorDomain(
57
+ namespace: string,
58
+ name: string,
59
+ ): { name: string; quark: number } | null;
60
+
61
+ /**
62
+ * Register the L1 GLib.Error factory the engine calls when a GI invoke fails, so
63
+ * a failed sync call throws a real `GLib.Error` (instanceof, with `.matches()`).
64
+ */
65
+ export function setErrorBuilder(
66
+ builder: (domainName: string, domainQuark: number, code: number, message: string) => Error,
67
+ ): void;
68
+
52
69
  /** Prepend a directory to the GIRepository typelib search path. */
53
70
  export function prependSearchPath(path: string): void;
54
71
 
55
72
  /**
56
73
  * Invoke a namespace-level GObject-Introspection function (not an instance
57
- * method) with IN-only primitive/string arguments. Returns the marshalled
58
- * return value. Milestone 1: numbers, booleans and strings.
74
+ * method) with primitive/string/object args. OUT and INOUT parameters are
75
+ * supported for fundamentals (numbers/booleans), strings (utf8/filename) and
76
+ * GObjects/enums/flags; only IN/INOUT args are passed in `args`.
77
+ *
78
+ * The return follows the GJS convention: the function's own return value (when
79
+ * non-void) followed by each OUT/INOUT value in argument order — a single value
80
+ * is returned bare, several as an Array, none as `undefined`. Compound OUT types
81
+ * (arrays, GList/GHashTable, structs) are a later milestone and throw a clear
82
+ * "not yet supported" error.
59
83
  */
60
84
  export function callFunction(namespace: string, functionName: string, args?: unknown[]): unknown;
61
85
 
62
86
  /**
63
- * Invoke an instance method on a GObject handle with IN-only
64
- * primitive/string/object/enum args. The method is resolved against the
87
+ * Invoke an instance method on a GObject handle with primitive/string/object/enum
88
+ * args, including OUT/INOUT parameters. The method is resolved against the
65
89
  * instance's introspection type (own + implemented-interface methods, then up
66
- * the parent chain). The Node twin of `obj.method(...)`.
90
+ * the parent chain). The Node twin of `obj.method(...)`. See {@link callFunction}
91
+ * for the OUT/INOUT return-tuple convention.
67
92
  */
68
93
  export function callMethod(handle: GObjectHandle, methodName: string, args?: unknown[]): unknown;
69
94
 
95
+ /**
96
+ * `true` iff {@link callMethod} would resolve an invocable instance method for
97
+ * `methodName` (literal introspected name or snake_case alias, walking the same
98
+ * own/interface/parent chain). Feature detection — never throws for a merely
99
+ * unknown name; a non-introspectable instance reports `false`.
100
+ */
101
+ export function hasMethod(handle: GObjectHandle, methodName: string): boolean;
102
+
70
103
  /**
71
104
  * Invoke a type-level constructor/static function (e.g. `Gio.File.new_for_path`,
72
105
  * `Gtk.Label.new`) — a function found on a type but taking no instance. The Node
73
- * twin of `Ns.Class.method(...)`.
106
+ * twin of `Ns.Class.method(...)`. OUT/INOUT params follow {@link callFunction}'s
107
+ * return-tuple convention.
74
108
  */
75
109
  export function callStaticMethod(
76
110
  namespace: string,
@@ -79,6 +113,14 @@ export function callStaticMethod(
79
113
  args?: unknown[],
80
114
  ): unknown;
81
115
 
116
+ /**
117
+ * Construct a boxed/plain struct instance — the `new <Struct>()` path (GJS
118
+ * gi/boxed.cpp parity). A struct WITH a 'new' constructor routes to it with
119
+ * `args` (e.g. `GLib.MainLoop`); a struct WITHOUT one and ZERO args is
120
+ * zero-allocated (`Graphene.Rect`, `Gdk.RGBA`); args without a 'new' throw.
121
+ */
122
+ export function constructStruct(namespace: string, typeName: string, args?: unknown[]): unknown;
123
+
82
124
  /**
83
125
  * Opaque handle to a live GObject instance, owned by node-gi and released when
84
126
  * the handle is garbage-collected. Pass it back to {@link getProperty} /
@@ -136,8 +178,8 @@ export interface SignalSpec {
136
178
  * GObject vfunc name (e.g. `"constructed"`) to the JS function that overrides it.
137
179
  * The override is invoked as a method on the instance — `this` is the GObject
138
180
  * handle — with the vfunc's declared arguments as JS arguments, and its return is
139
- * marshalled back to C. Chain-up to the parent vfunc lands in a later milestone,
140
- * so an override fully replaces the inherited implementation.
181
+ * marshalled back to C. An override can chain up to the parent implementation via
182
+ * {@link callParentVfunc} (the engine half of `super.vfunc_<name>(...)`).
141
183
  */
142
184
  export type VFuncMap = Record<string, (this: GObjectHandle, ...args: unknown[]) => unknown>;
143
185
 
@@ -146,6 +188,20 @@ export interface RegisterClassOptions {
146
188
  properties?: PropertySpec[];
147
189
  signals?: SignalSpec[];
148
190
  vfuncs?: VFuncMap;
191
+ /**
192
+ * A Gtk.Widget composite template. Either inline UI-XML (a `Uint8Array`/Buffer
193
+ * or a string) installed via `gtk_widget_class_set_template`, or a
194
+ * `"resource:///…"` path string installed via
195
+ * `gtk_widget_class_set_template_from_resource`. Installed in the subtype's
196
+ * `class_init`; `gtk_widget_init_template` runs at construction.
197
+ */
198
+ template?: Uint8Array | string;
199
+ /** `gtk_widget_class_set_css_name` for the subtype (optional). */
200
+ cssName?: string;
201
+ /** Template child ids to bind + expose publicly (via `gtk_widget_get_template_child`). */
202
+ children?: string[];
203
+ /** Template child ids to bind as internal children + expose privately. */
204
+ internalChildren?: string[];
149
205
  }
150
206
 
151
207
  /**
@@ -154,8 +210,8 @@ export interface RegisterClassOptions {
154
210
  * type handle. `options` installs custom properties (backed by a per-instance
155
211
  * value store), signals, and vfunc overrides (an ffi closure written into the new
156
212
  * type's class vtable) in the new type's `class_init` — the Node twin of (the
157
- * engine half of) GJS's `GObject.registerClass`. vfunc chain-up to the parent
158
- * implementation lands in a later milestone.
213
+ * engine half of) GJS's `GObject.registerClass`. Each vfunc override captures the
214
+ * parent vtable pointer it displaces, so it can chain up via {@link callParentVfunc}.
159
215
  */
160
216
  export function registerClass(
161
217
  name: string,
@@ -164,12 +220,38 @@ export function registerClass(
164
220
  options?: RegisterClassOptions,
165
221
  ): TypeHandle;
166
222
 
223
+ /**
224
+ * Chain up to the parent implementation of an overridden vfunc — the engine half
225
+ * of `super.vfunc_<name>(...)`. Invokes the function that was in the instance
226
+ * type's vtable slot BEFORE the {@link registerClass} override was installed (the
227
+ * C default, or a JS override further up the chain), passing `handle` as the
228
+ * instance and `args` as the vfunc's declared IN arguments; returns the marshalled
229
+ * vfunc return (or `undefined` for a void vfunc). Throws if no overridden vfunc by
230
+ * that name owns a slot on the instance's type, or if the vfunc declares any
231
+ * OUT/INOUT argument (chain-up of those is not yet supported — a catchable throw,
232
+ * never a crash).
233
+ */
234
+ export function callParentVfunc(
235
+ handle: GObjectHandle,
236
+ vfuncName: string,
237
+ args?: unknown[],
238
+ ): unknown;
239
+
167
240
  /**
168
241
  * Construct a GObject of a registered type handle (from {@link registerClass})
169
- * with optional construct/settable properties.
242
+ * with optional construct/settable properties. For a templated type the engine
243
+ * runs `gtk_widget_init_template` on the new instance before returning it.
170
244
  */
171
245
  export function constructType(typeHandle: TypeHandle, props?: Record<string, unknown>): GObjectHandle;
172
246
 
247
+ /**
248
+ * Resolve a composite-template child (bound via {@link RegisterClassOptions.children}
249
+ * / {@link RegisterClassOptions.internalChildren}) by id on a templated instance —
250
+ * `gtk_widget_get_template_child(widget, G_OBJECT_TYPE(widget), name)`. Returns the
251
+ * child as a wrapped GObject handle (borrowed; toggle-ref bridged) or `null`.
252
+ */
253
+ export function getTemplateChild(handle: GObjectHandle, name: string): GObjectHandle | null;
254
+
173
255
  /** Read a GObject property. */
174
256
  export function getProperty(handle: GObjectHandle, name: string): unknown;
175
257
 
@@ -186,6 +268,22 @@ export function hasProperty(handle: GObjectHandle, name: string): boolean;
186
268
  /** The runtime GType name of a GObject handle (e.g. "GSimpleAction"). */
187
269
  export function getTypeName(handle: GObjectHandle): string;
188
270
 
271
+ /**
272
+ * The runtime GType of an introspected registered type, as an opaque node-gi
273
+ * GType handle ({@link TypeHandle} — a tag-distinct External carrying the GType,
274
+ * NOT a number/pointer). The L1 layer surfaces it as a lazy `Ns.Type.$gtype`
275
+ * getter and feeds it to GType-typed GI arguments (`GObject.type_ensure`,
276
+ * `g_param_spec_object`'s value type, …). Returns `null` for an unknown name.
277
+ */
278
+ export function getGType(namespace: string, name: string): TypeHandle | null;
279
+
280
+ /**
281
+ * Whether a GObject handle's GType is-a `namespace.typeName` (g_type_is_a — also
282
+ * true when the type implements an interface). Used by the L1 wrapper to pick the
283
+ * right `Gio._promisify` registration when two classes promisify a same-named method.
284
+ */
285
+ export function isInstanceOf(handle: GObjectHandle, namespace: string, typeName: string): boolean;
286
+
189
287
  /**
190
288
  * Whether `value` is one of node-gi's GObject-instance handles (tag-checked, no
191
289
  * dereference). Lets the L1 wrapper wrap object-typed return values for chaining
@@ -200,6 +298,15 @@ export function isGObjectHandle(value: unknown): boolean;
200
298
  */
201
299
  export type BoxedHandle = { readonly __nodeGiBoxed: unique symbol };
202
300
 
301
+ /**
302
+ * Allocate a fresh, zero-initialised GValue and return it as a boxed handle (GType
303
+ * `G_TYPE_VALUE`, owned → freed on GC). GObject.Value has no `g_value_new()`, so the
304
+ * L1 layer (gi.js `makeValueClass`) uses this to build a `new GObject.Value()` and
305
+ * then drives `.init(gtype)` / `.set_*` / `.get_*` / `.copy` / `.unset` through the
306
+ * boxed-method path.
307
+ */
308
+ export function newGValue(): BoxedHandle;
309
+
203
310
  /**
204
311
  * Invoke an instance method on a boxed/struct handle (e.g. `mainLoop.run()` /
205
312
  * `mainLoop.quit()`). The method is resolved against the boxed GType's
@@ -214,14 +321,121 @@ export function callBoxedMethod(handle: BoxedHandle, methodName: string, args?:
214
321
  */
215
322
  export function isBoxedHandle(value: unknown): boolean;
216
323
 
324
+ /**
325
+ * Classify a name on a boxed/struct/union handle: `0` neither, `1` method,
326
+ * `2` field. Methods take priority (gjs renames a colliding field to `_name`), so
327
+ * L1 wrapBoxed uses this to route method-dispatch vs field get/set.
328
+ */
329
+ export function boxedMemberKind(handle: BoxedHandle, name: string): 0 | 1 | 2;
330
+
331
+ /**
332
+ * Read a named field of a boxed/struct/union handle (`simpleStruct.long_`). Throws
333
+ * if `name` is not a field, or the field is unreadable / unsupported.
334
+ */
335
+ export function getBoxedField(handle: BoxedHandle, name: string): unknown;
336
+
337
+ /**
338
+ * Write a simple-typed field of a boxed/struct/union handle (`union.long_ = 5`).
339
+ * Throws if `name` is not a field, or the field is unwritable / unsupported.
340
+ */
341
+ export function setBoxedField(handle: BoxedHandle, name: string, value: unknown): void;
342
+
343
+ /**
344
+ * The boxed handle's GType name (e.g. `"GBytes"`), or `null` when it carries no
345
+ * registered GType. Lets L1 attach type-specific conveniences (GLib.Bytes.toArray).
346
+ */
347
+ export function boxedTypeName(value: unknown): string | null;
348
+
349
+ /**
350
+ * Opaque handle to a GObject.ParamSpec (a GObject fundamental, ref-counted via
351
+ * `g_param_spec_ref` / `unref`, released on GC). A `notify` handler's second arg
352
+ * and a GParamSpec-typed value surface as one; the L1 wrapper adds the
353
+ * name/nick/blurb/flags/value_type/owner_type/default_value ergonomics.
354
+ */
355
+ export type ParamSpecHandle = { readonly __nodeGiParamSpec: unique symbol };
356
+
357
+ /**
358
+ * Whether `value` is a node-gi GParamSpec handle (tag-checked, no dereference).
359
+ */
360
+ export function isParamSpecHandle(value: unknown): boolean;
361
+
362
+ /**
363
+ * Read a GParamSpec accessor by name: `name` | `nick` | `blurb` | `flags` |
364
+ * `valueType` | `ownerType` | `defaultValue`. Backs the L1 GObject.ParamSpec
365
+ * getters + get_name()/get_nick()/get_blurb()/get_default_value().
366
+ */
367
+ export function paramSpecProp(handle: ParamSpecHandle, which: string): unknown;
368
+
369
+ /**
370
+ * Opaque handle to a GLib.Variant (a boxed handle tagged with the GVariant
371
+ * GType). Reference-counted via GVariant's (de)floating refcount and released on
372
+ * GC. Pass it to {@link variantUnpack} / {@link variantGetTypeString}.
373
+ */
374
+ export type VariantHandle = BoxedHandle & { readonly __nodeGiVariant: unique symbol };
375
+
376
+ /**
377
+ * Build a GVariant from a GVariant type signature + JS value, returning an owned
378
+ * GLib.Variant handle (the floating ref is sunk; released on GC). The recursive
379
+ * native packer behind `new GLib.Variant(signature, value)`. Supports the basics
380
+ * `b y n q i u x t h d s o g`, `v`, `m*` maybe, `a*` arrays (incl. `as`, `ay`,
381
+ * `a{..}` dicts), `(...)` tuples and `{kv}` dict-entries.
382
+ */
383
+ export function variantNew(signature: string, value?: unknown): VariantHandle;
384
+
385
+ /**
386
+ * Unpack a GLib.Variant handle to a JS value. `deep` unpacks container children
387
+ * (a nested `v` stays a Variant unless `recursive`); `recursive` additionally
388
+ * unwraps nested `v` variants to plain JS. Drives the L1 `.unpack()` (deep=false),
389
+ * `.deepUnpack()` (deep=true) and `.recursiveUnpack()` (deep=true, recursive=true).
390
+ */
391
+ export function variantUnpack(handle: VariantHandle, deep?: boolean, recursive?: boolean): unknown;
392
+
393
+ /** The GVariant type string of a GLib.Variant handle (e.g. `"a{sv}"`). */
394
+ export function variantGetTypeString(handle: VariantHandle): string;
395
+
396
+ /**
397
+ * Whether `value` is one of node-gi's GLib.Variant handles (a boxed handle tagged
398
+ * with the GVariant GType; tag-checked, no dereference).
399
+ */
400
+ export function isVariantHandle(value: unknown): boolean;
401
+
217
402
  /**
218
403
  * Attach the libuv-backed GSource to the default GLib main context so a blocking
219
404
  * GLib main loop (`GLib.MainLoop.run()`, `Gio.Application.run()`) keeps Node's
220
405
  * timers, promises and I/O alive — the Node twin of GJS running the GLib loop as
221
- * the process loop. Idempotent and harmless until a GLib loop actually runs.
406
+ * the process loop. Also arms the uv-driven GLib auto-pump for the non-blocking
407
+ * case: pending GLib sources (Gio async completions, GLib timeouts/idles, DBus)
408
+ * dispatch from Node's own event loop, so async gi:// code needs no explicit
409
+ * mainloop. Idempotent.
222
410
  */
223
411
  export function startMainLoop(): void;
224
412
 
413
+ /**
414
+ * Iterate the default GLib main context once, dispatching any ready sources.
415
+ * Pure GLib (no libuv) — the portable main-loop primitive on Bun/Deno, driven
416
+ * from a JS timer via the L1 `startMainContextPump`. Returns whether a source
417
+ * was dispatched.
418
+ */
419
+ export function iterateMainContext(mayBlock?: boolean): boolean;
420
+
421
+ /**
422
+ * Drain ready GLib sources + re-arm the auto-pump's libuv wake-ups now (no-op
423
+ * unless {@link startMainLoop} armed the pump on this env). The L1 layer wires
424
+ * this to `process.on('beforeExit')` to bootstrap an otherwise-empty libuv loop;
425
+ * rarely needed directly.
426
+ */
427
+ export function pumpKick(): void;
428
+
429
+ /**
430
+ * Register the runtime-native microtask drain (Bun: `bun:jsc` drainMicrotasks;
431
+ * Deno: core.runMicrotasks) the engine invokes after each OUTERMOST
432
+ * loop-dispatched GLib→JS callback returns, so Promise continuations (async
433
+ * DBus replies, `await` chains) drain during a blocking GLib loop. Registered
434
+ * automatically at addon load on Bun/Deno; never registered on Node (its
435
+ * napi_make_callback performs the checkpoint natively).
436
+ */
437
+ export function setMicrotaskDrain(drain: () => void): void;
438
+
225
439
  /**
226
440
  * Connect a JS callback to a GObject signal; returns a handler id. The callback
227
441
  * receives the signal arguments (the emitter instance is not passed in this
@@ -240,29 +454,109 @@ export function emitSignal(handle: GObjectHandle, signalName: string, args?: unk
240
454
  /** Disconnect a previously connected signal handler. */
241
455
  export function disconnectSignal(handle: GObjectHandle, handlerId: number): void;
242
456
 
457
+ /**
458
+ * Register the L1 resolver mapping a Gtk.Template `<signal handler="…">` handler
459
+ * name to the instance's bound JS method (engine template-callback scope).
460
+ */
461
+ export function setTemplateCallbackResolver(
462
+ resolver: (
463
+ handle: GObjectHandle,
464
+ handlerName: string,
465
+ ) => ((...args: unknown[]) => unknown) | undefined,
466
+ ): void;
467
+
468
+ /**
469
+ * Install (or clear, with `null`) the structured-log writer function backing
470
+ * GLib.log_set_writer_func. The writer receives (logLevel, fields) where each
471
+ * field value is a Uint8Array of the field bytes (or null for an empty field),
472
+ * and returns a GLib.LogWriterOutput. NOTE: GLib permits at most ONE install per
473
+ * process (a second aborts in GLib).
474
+ */
475
+ export function logSetWriterFunc(
476
+ writer: ((logLevel: number, fields: Record<string, Uint8Array | null>) => number) | null,
477
+ ): void;
478
+
479
+ /** Route structured logs back to the default writer (GLib.log_set_writer_default). */
480
+ export function logSetWriterDefault(): void;
481
+
482
+ /**
483
+ * g_object_bind_property_full with JS transform functions (backs
484
+ * GObject.Object.prototype.bind_property_full). Each transform is
485
+ * `(binding, sourceValue) => [ok: boolean, targetValue]`. Returns the GBinding handle.
486
+ */
487
+ export function bindPropertyFull(
488
+ source: GObjectHandle,
489
+ sourceProperty: string,
490
+ target: GObjectHandle,
491
+ targetProperty: string,
492
+ flags: number,
493
+ transformTo: ((...args: unknown[]) => unknown) | null,
494
+ transformFrom: ((...args: unknown[]) => unknown) | null,
495
+ ): GObjectHandle;
496
+
497
+ /**
498
+ * g_binding_group_bind_full with JS transform functions (backs
499
+ * GObject.BindingGroup.bind_full). Same transform contract as {@link bindPropertyFull}.
500
+ */
501
+ export function bindingGroupBindFull(
502
+ group: GObjectHandle,
503
+ sourceProperty: string,
504
+ target: GObjectHandle,
505
+ targetProperty: string,
506
+ flags: number,
507
+ transformTo: ((...args: unknown[]) => unknown) | null,
508
+ transformFrom: ((...args: unknown[]) => unknown) | null,
509
+ ): void;
510
+
243
511
  declare const native: {
244
512
  requireNamespace: typeof requireNamespace;
245
513
  listInfoNames: typeof listInfoNames;
246
514
  findInfo: typeof findInfo;
247
515
  getConstantValue: typeof getConstantValue;
248
516
  getEnumValues: typeof getEnumValues;
517
+ getErrorDomain: typeof getErrorDomain;
518
+ setErrorBuilder: typeof setErrorBuilder;
249
519
  prependSearchPath: typeof prependSearchPath;
250
520
  callFunction: typeof callFunction;
251
521
  callMethod: typeof callMethod;
522
+ hasMethod: typeof hasMethod;
252
523
  callStaticMethod: typeof callStaticMethod;
524
+ constructStruct: typeof constructStruct;
253
525
  newObject: typeof newObject;
254
526
  registerClass: typeof registerClass;
255
527
  constructType: typeof constructType;
528
+ getTemplateChild: typeof getTemplateChild;
256
529
  getProperty: typeof getProperty;
257
530
  setProperty: typeof setProperty;
258
531
  hasProperty: typeof hasProperty;
259
532
  getTypeName: typeof getTypeName;
533
+ getGType: typeof getGType;
534
+ isInstanceOf: typeof isInstanceOf;
260
535
  isGObjectHandle: typeof isGObjectHandle;
536
+ newGValue: typeof newGValue;
261
537
  callBoxedMethod: typeof callBoxedMethod;
262
538
  isBoxedHandle: typeof isBoxedHandle;
539
+ boxedMemberKind: typeof boxedMemberKind;
540
+ getBoxedField: typeof getBoxedField;
541
+ setBoxedField: typeof setBoxedField;
542
+ boxedTypeName: typeof boxedTypeName;
543
+ isParamSpecHandle: typeof isParamSpecHandle;
544
+ paramSpecProp: typeof paramSpecProp;
545
+ variantNew: typeof variantNew;
546
+ variantUnpack: typeof variantUnpack;
547
+ variantGetTypeString: typeof variantGetTypeString;
548
+ isVariantHandle: typeof isVariantHandle;
263
549
  startMainLoop: typeof startMainLoop;
550
+ iterateMainContext: typeof iterateMainContext;
551
+ pumpKick: typeof pumpKick;
552
+ setMicrotaskDrain: typeof setMicrotaskDrain;
264
553
  connectSignal: typeof connectSignal;
265
554
  emitSignal: typeof emitSignal;
266
555
  disconnectSignal: typeof disconnectSignal;
556
+ setTemplateCallbackResolver: typeof setTemplateCallbackResolver;
557
+ logSetWriterFunc: typeof logSetWriterFunc;
558
+ logSetWriterDefault: typeof logSetWriterDefault;
559
+ bindPropertyFull: typeof bindPropertyFull;
560
+ bindingGroupBindFull: typeof bindingGroupBindFull;
267
561
  };
268
562
  export default native;