Branch data Line data Source code
1 : : /* exported _init, interfaces, properties, registerClass, requires, signals */
2 : : // SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
3 : : // SPDX-FileCopyrightText: 2011 Jasper St. Pierre
4 : : // SPDX-FileCopyrightText: 2017 Philip Chimento <philip.chimento@gmail.com>, <philip@endlessm.com>
5 : :
6 : 47 : const Gi = imports._gi;
7 : 47 : const {GjsPrivate, GLib} = imports.gi;
8 : 47 : const {_checkAccessors, _registerType} = imports._common;
9 : 47 : const Legacy = imports._legacy;
10 : :
11 : 47 : let GObject;
12 : :
13 : 47 : var GTypeName = Symbol('GType name');
14 : 47 : var GTypeFlags = Symbol('GType flags');
15 : 47 : var interfaces = Symbol('GObject interfaces');
16 : 47 : var properties = Symbol('GObject properties');
17 : 47 : var requires = Symbol('GObject interface requires');
18 : 47 : var signals = Symbol('GObject signals');
19 : :
20 : : // These four will be aliased to GTK
21 : 47 : var _gtkChildren = Symbol('GTK widget template children');
22 : 47 : var _gtkCssName = Symbol('GTK widget CSS name');
23 : 47 : var _gtkInternalChildren = Symbol('GTK widget template internal children');
24 : 47 : var _gtkTemplate = Symbol('GTK widget template');
25 : :
26 : 92 : function registerClass(...args) {
27 : 92 : let klass = args[0];
28 [ + + ]: 92 : if (args.length === 2) {
29 : : // The two-argument form is the convenient syntax without ESnext
30 : : // decorators and class data properties. The first argument is an
31 : : // object with meta info such as properties and signals. The second
32 : : // argument is the class expression for the class itself.
33 : : //
34 : : // var MyClass = GObject.registerClass({
35 : : // Properties: { ... },
36 : : // Signals: { ... },
37 : : // }, class MyClass extends GObject.Object {
38 : : // _init() { ... }
39 : : // });
40 : : //
41 : : // When decorators and class data properties become part of the JS
42 : : // standard, this function can be used directly as a decorator.
43 : 63 : let metaInfo = args[0];
44 : 63 : klass = args[1];
45 [ + + ]: 63 : if ('GTypeName' in metaInfo)
46 : 6 : klass[GTypeName] = metaInfo.GTypeName;
47 [ + + ]: 63 : if ('GTypeFlags' in metaInfo)
48 : 1 : klass[GTypeFlags] = metaInfo.GTypeFlags;
49 [ + + ]: 63 : if ('Implements' in metaInfo)
50 : 25 : klass[interfaces] = metaInfo.Implements;
51 [ + + ]: 63 : if ('Properties' in metaInfo)
52 : 27 : klass[properties] = metaInfo.Properties;
53 [ + + ]: 63 : if ('Signals' in metaInfo)
54 : 8 : klass[signals] = metaInfo.Signals;
55 [ + + ]: 63 : if ('Requires' in metaInfo)
56 : 3 : klass[requires] = metaInfo.Requires;
57 [ + + ]: 63 : if ('CssName' in metaInfo)
58 : 2 : klass[_gtkCssName] = metaInfo.CssName;
59 [ + + ]: 63 : if ('Template' in metaInfo)
60 : 8 : klass[_gtkTemplate] = metaInfo.Template;
61 [ + + ]: 63 : if ('Children' in metaInfo)
62 : 7 : klass[_gtkChildren] = metaInfo.Children;
63 [ + + ]: 63 : if ('InternalChildren' in metaInfo)
64 : 7 : klass[_gtkInternalChildren] = metaInfo.InternalChildren;
65 : : }
66 : :
67 [ + + ]: 92 : if (!(klass.prototype instanceof GObject.Object) &&
68 [ + + ]: 8 : !(klass.prototype instanceof GObject.Interface)) {
69 : 10 : throw new TypeError('GObject.registerClass() used with invalid base ' +
70 : 5 : `class (is ${Object.getPrototypeOf(klass).name})`);
71 : : }
72 : :
73 [ + + ]: 87 : if ('_classInit' in klass) {
74 : 86 : klass = klass._classInit(klass);
75 : : } else {
76 : : // Lang.Class compatibility.
77 : 1 : klass = _resolveLegacyClassFunction(klass, '_classInit')(klass);
78 : : }
79 : :
80 : 80 : return klass;
81 : 80 : }
82 : :
83 : 2 : function _resolveLegacyClassFunction(klass, func) {
84 : : // Find the "least derived" class with a _classInit static function; there
85 : : // definitely is one, since this class must inherit from GObject
86 : 2 : let initclass = klass;
87 [ + + ]: 6 : while (typeof initclass[func] === 'undefined')
88 : 4 : initclass = Object.getPrototypeOf(initclass.prototype).constructor;
89 : 2 : return initclass[func];
90 : 2 : }
91 : :
92 : 86 : function _defineGType(klass, giPrototype, registeredType) {
93 : 86 : const config = {
94 : 86 : enumerable: false,
95 : 86 : configurable: false,
96 : : };
97 : :
98 : : /**
99 : : * class Example {
100 : : * // The JS object for this class' ObjectPrototype
101 : : * static [Gi.gobject_prototype_symbol] = ...
102 : : * static get $gtype () {
103 : : * return ...;
104 : : * }
105 : : * }
106 : : *
107 : : * // Equal to the same property on the constructor
108 : : * Example.prototype[Gi.gobject_prototype_symbol] = ...
109 : : */
110 : :
111 : 172 : Object.defineProperty(klass, '$gtype', {
112 : 86 : ...config,
113 : 86 : get() {
114 : 344 : return registeredType;
115 : : },
116 : : });
117 : :
118 : 172 : Object.defineProperty(klass.prototype, Gi.gobject_prototype_symbol, {
119 : 86 : ...config,
120 : 86 : writable: false,
121 : 86 : value: giPrototype,
122 : : });
123 : : }
124 : :
125 : : // Some common functions between GObject.Class and GObject.Interface
126 : :
127 : : function _createSignals(gtype, sigs) {
128 [ + + ]: 107 : for (let signalName in sigs) {
129 : 21 : let obj = sigs[signalName];
130 [ + + ]: 21 : let flags = obj.flags !== undefined ? obj.flags : GObject.SignalFlags.RUN_FIRST;
131 [ + + ]: 21 : let accumulator = obj.accumulator !== undefined ? obj.accumulator : GObject.AccumulatorType.NONE;
132 [ + + ]: 21 : let rtype = obj.return_type !== undefined ? obj.return_type : GObject.TYPE_NONE;
133 [ + + ]: 21 : let paramtypes = obj.param_types !== undefined ? obj.param_types : [];
134 : :
135 : 21 : try {
136 : 21 : obj.signal_id = Gi.signal_new(gtype, signalName, flags, accumulator, rtype, paramtypes);
137 : 0 : } catch (e) {
138 : 0 : throw new TypeError(`Invalid signal ${signalName}: ${e.message}`);
139 : : }
140 : : }
141 : : }
142 : :
143 : 2 : function _getCallerBasename() {
144 : 2 : const stackLines = new Error().stack.trim().split('\n');
145 : 2 : const lineRegex = new RegExp(/@(.+:\/\/)?(.*\/)?(.+)\.js:\d+(:[\d]+)?$/);
146 : 2 : let thisFile = null;
147 : 2 : let thisDir = null;
148 : :
149 [ + + ]: 12 : for (let line of stackLines) {
150 : 12 : let match = line.match(lineRegex);
151 [ - + ]: 12 : if (match) {
152 : 12 : let scriptDir = match[2];
153 : 12 : let scriptBasename = match[3];
154 : :
155 [ + + ]: 12 : if (!thisFile) {
156 : 2 : thisDir = scriptDir;
157 : 2 : thisFile = scriptBasename;
158 : 2 : continue;
159 : : }
160 : :
161 [ + + ][ + + ]: 10 : if (scriptDir === thisDir && scriptBasename === thisFile)
162 : 8 : continue;
163 : :
164 [ - + ][ + - ]: 2 : if (scriptDir && scriptDir.startsWith('/org/gnome/gjs/'))
165 : 0 : continue;
166 : :
167 : 2 : let basename = scriptBasename;
168 [ - + ]: 2 : if (scriptDir) {
169 : 2 : scriptDir = scriptDir.replace(/^\/|\/$/g, '');
170 : 2 : basename = `${scriptDir.split('/').reverse()[0]}_${basename}`;
171 : : }
172 : 2 : return basename;
173 : : }
174 : : }
175 : :
176 : 0 : return null;
177 : 2 : }
178 : :
179 : 87 : function _createGTypeName(klass) {
180 : 174 : const sanitizeGType = s => s.replace(/[^a-z0-9+_-]/gi, '_');
181 : :
182 [ + + ]: 87 : if (klass.hasOwnProperty(GTypeName)) {
183 : 6 : let sanitized = sanitizeGType(klass[GTypeName]);
184 [ + + ]: 6 : if (sanitized !== klass[GTypeName]) {
185 : 2 : logError(new RangeError(`Provided GType name '${klass[GTypeName]}' ` +
186 : 1 : `is not valid; automatically sanitized to '${sanitized}'`));
187 : : }
188 : 6 : return sanitized;
189 : : }
190 : :
191 : 81 : let gtypeClassName = klass.name;
192 [ + + ]: 81 : if (GObject.gtypeNameBasedOnJSPath) {
193 : 2 : let callerBasename = _getCallerBasename();
194 [ - + ]: 2 : if (callerBasename)
195 : 2 : gtypeClassName = `${callerBasename}_${gtypeClassName}`;
196 : : }
197 : :
198 [ + + ]: 81 : if (gtypeClassName === '')
199 : 3 : gtypeClassName = `anonymous_${GLib.uuid_string_random()}`;
200 : :
201 : 81 : return sanitizeGType(`Gjs_${gtypeClassName}`);
202 : 87 : }
203 : :
204 : 87 : function _propertiesAsArray(klass) {
205 : 87 : let propertiesArray = [];
206 [ + + ]: 87 : if (klass.hasOwnProperty(properties)) {
207 [ + + ]: 81 : for (let prop in klass[properties])
208 : 52 : propertiesArray.push(klass[properties][prop]);
209 : : }
210 : 87 : return propertiesArray;
211 : 87 : }
212 : :
213 : : function _copyInterfacePrototypeDescriptors(targetPrototype, sourceInterface) {
214 : 50 : Object.entries(Object.getOwnPropertyDescriptors(sourceInterface))
215 [ + - ][ + - ]: 266 : .filter(([key, descriptor]) =>
[ + - ][ + - ]
216 : : // Don't attempt to copy the constructor or toString implementations
217 [ + + ]: 216 : !['constructor', 'toString'].includes(key) &&
218 : : // Ignore properties starting with __
219 [ - + ][ + + ]: 175 : (typeof key !== 'string' || !key.startsWith('__')) &&
220 : : // Don't override an implementation on the target
221 [ + + ]: 171 : !targetPrototype.hasOwnProperty(key) &&
222 [ - + ]: 60 : descriptor &&
223 : : // Only copy if the descriptor has a getter, is a function, or is enumerable.
224 [ + - ][ # # ]: 60 : (typeof descriptor.value === 'function' || descriptor.get || descriptor.enumerable))
225 [ + - ][ + - ]: 110 : .forEach(([key, descriptor]) => {
[ + - ][ + - ]
226 : 60 : Object.defineProperty(targetPrototype, key, descriptor);
227 : : });
228 : : }
229 : :
230 : : function _interfacePresent(required, klass) {
231 [ + + ]: 17 : if (!klass[interfaces])
232 : 7 : return false;
233 [ + + ]: 10 : if (klass[interfaces].includes(required))
234 : 2 : return true; // implemented here
235 : : // Might be implemented on a parent class
236 : 8 : return _interfacePresent(required, Object.getPrototypeOf(klass));
237 : : }
238 : :
239 : 24 : function _checkInterface(iface, proto) {
240 : : // Checks for specific interfaces
241 : :
242 : : // Default vfunc_async_init() will run vfunc_init() in a thread and crash.
243 : : // Change error message when https://gitlab.gnome.org/GNOME/gjs/issues/72
244 : : // has been solved.
245 [ + + ]: 24 : if (iface.$gtype.name === 'GAsyncInitable' &&
246 [ + + ]: 1 : !Object.getOwnPropertyNames(proto).includes('vfunc_init_async'))
247 : 1 : throw new Error("It's not currently possible to implement Gio.AsyncInitable.");
248 : :
249 : : // Check that proto implements all of this interface's required interfaces.
250 : : // "proto" refers to the object's prototype (which implements the interface)
251 : : // whereas "iface.prototype" is the interface's prototype (which may still
252 : : // contain unimplemented methods.)
253 [ + + ]: 23 : if (typeof iface[requires] === 'undefined')
254 : 14 : return;
255 : :
256 : 18 : let unfulfilledReqs = iface[requires].filter(required => {
257 : : // Either the interface is not present or it is not listed before the
258 : : // interface that requires it or the class does not inherit it. This is
259 : : // so that required interfaces don't copy over properties from other
260 : : // interfaces that require them.
261 : 9 : let ifaces = proto.constructor[interfaces];
262 [ + + ]: 9 : return (!_interfacePresent(required, proto.constructor) ||
263 [ + + ]: 2 : ifaces.indexOf(required) > ifaces.indexOf(iface)) &&
264 : 7 : !(proto instanceof required);
265 : 18 : }).map(required =>
266 : : // required.name will be present on JS classes, but on introspected
267 : : // GObjects it will be the C name. The alternative is just so that
268 : : // we print something if there is garbage in Requires.
269 [ # # ]: 0 : required.name || required);
270 [ + - ]: 9 : if (unfulfilledReqs.length > 0) {
271 : 0 : throw new Error('The following interfaces must be implemented before ' +
272 : 0 : `${iface.name}: ${unfulfilledReqs.join(', ')}`);
273 : : }
274 : 14 : }
275 : :
276 : : function _checkProperties(klass) {
277 [ + + ]: 84 : if (!klass.hasOwnProperty(properties))
278 : 56 : return;
279 : :
280 [ + + ]: 79 : for (let pspec of Object.values(klass[properties]))
281 : 51 : _checkAccessors(klass.prototype, pspec, GObject);
282 : : }
283 : :
284 : 47 : function _init() {
285 : 47 : GObject = this;
286 : :
287 : 564 : function _makeDummyClass(obj, name, upperName, gtypeName, actual) {
288 : 564 : let gtype = GObject.type_from_name(gtypeName);
289 : 564 : obj[`TYPE_${upperName}`] = gtype;
290 : 564 : obj[name] = function (v) {
291 : 6 : return actual(v);
292 : : };
293 : 564 : obj[name].$gtype = gtype;
294 : : }
295 : :
296 : 47 : GObject.gtypeNameBasedOnJSPath = false;
297 : :
298 : 47 : _makeDummyClass(GObject, 'VoidType', 'NONE', 'void', function () {});
299 : 47 : _makeDummyClass(GObject, 'Char', 'CHAR', 'gchar', Number);
300 : 47 : _makeDummyClass(GObject, 'UChar', 'UCHAR', 'guchar', Number);
301 : 47 : _makeDummyClass(GObject, 'Unichar', 'UNICHAR', 'gint', String);
302 : :
303 : 47 : GObject.TYPE_BOOLEAN = GObject.type_from_name('gboolean');
304 : 47 : GObject.Boolean = Boolean;
305 : 47 : Boolean.$gtype = GObject.TYPE_BOOLEAN;
306 : :
307 : 47 : _makeDummyClass(GObject, 'Int', 'INT', 'gint', Number);
308 : 47 : _makeDummyClass(GObject, 'UInt', 'UINT', 'guint', Number);
309 : 47 : _makeDummyClass(GObject, 'Long', 'LONG', 'glong', Number);
310 : 47 : _makeDummyClass(GObject, 'ULong', 'ULONG', 'gulong', Number);
311 : 47 : _makeDummyClass(GObject, 'Int64', 'INT64', 'gint64', Number);
312 : 47 : _makeDummyClass(GObject, 'UInt64', 'UINT64', 'guint64', Number);
313 : :
314 : 47 : GObject.TYPE_ENUM = GObject.type_from_name('GEnum');
315 : 47 : GObject.TYPE_FLAGS = GObject.type_from_name('GFlags');
316 : :
317 : 47 : _makeDummyClass(GObject, 'Float', 'FLOAT', 'gfloat', Number);
318 : 47 : GObject.TYPE_DOUBLE = GObject.type_from_name('gdouble');
319 : 47 : GObject.Double = Number;
320 : 47 : Number.$gtype = GObject.TYPE_DOUBLE;
321 : :
322 : 47 : GObject.TYPE_STRING = GObject.type_from_name('gchararray');
323 : 47 : GObject.String = String;
324 : 47 : String.$gtype = GObject.TYPE_STRING;
325 : :
326 : 47 : GObject.TYPE_JSOBJECT = GObject.type_from_name('JSObject');
327 : 47 : GObject.JSObject = Object;
328 : 47 : Object.$gtype = GObject.TYPE_JSOBJECT;
329 : :
330 : 47 : GObject.TYPE_POINTER = GObject.type_from_name('gpointer');
331 : 47 : GObject.TYPE_BOXED = GObject.type_from_name('GBoxed');
332 : 47 : GObject.TYPE_PARAM = GObject.type_from_name('GParam');
333 : 47 : GObject.TYPE_INTERFACE = GObject.type_from_name('GInterface');
334 : 47 : GObject.TYPE_OBJECT = GObject.type_from_name('GObject');
335 : 47 : GObject.TYPE_VARIANT = GObject.type_from_name('GVariant');
336 : :
337 : 47 : _makeDummyClass(GObject, 'Type', 'GTYPE', 'GType', GObject.type_from_name);
338 : :
339 : 47 : GObject.ParamSpec.char = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
340 : 0 : return GObject.param_spec_char(name, nick, blurb, minimum, maximum, defaultValue, flags);
341 : : };
342 : :
343 : 47 : GObject.ParamSpec.uchar = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
344 : 0 : return GObject.param_spec_uchar(name, nick, blurb, minimum, maximum, defaultValue, flags);
345 : : };
346 : :
347 : 47 : GObject.ParamSpec.int = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
348 : 19 : return GObject.param_spec_int(name, nick, blurb, minimum, maximum, defaultValue, flags);
349 : : };
350 : :
351 : 47 : GObject.ParamSpec.uint = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
352 : 3 : return GObject.param_spec_uint(name, nick, blurb, minimum, maximum, defaultValue, flags);
353 : : };
354 : :
355 : 47 : GObject.ParamSpec.long = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
356 : 0 : return GObject.param_spec_long(name, nick, blurb, minimum, maximum, defaultValue, flags);
357 : : };
358 : :
359 : 47 : GObject.ParamSpec.ulong = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
360 : 0 : return GObject.param_spec_ulong(name, nick, blurb, minimum, maximum, defaultValue, flags);
361 : : };
362 : :
363 : 47 : GObject.ParamSpec.int64 = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
364 : 6 : return GObject.param_spec_int64(name, nick, blurb, minimum, maximum, defaultValue, flags);
365 : : };
366 : :
367 : 47 : GObject.ParamSpec.uint64 = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
368 : 5 : return GObject.param_spec_uint64(name, nick, blurb, minimum, maximum, defaultValue, flags);
369 : : };
370 : :
371 : 47 : GObject.ParamSpec.float = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
372 : 0 : return GObject.param_spec_float(name, nick, blurb, minimum, maximum, defaultValue, flags);
373 : : };
374 : :
375 : 47 : GObject.ParamSpec.boolean = function (name, nick, blurb, flags, defaultValue) {
376 : 4 : return GObject.param_spec_boolean(name, nick, blurb, defaultValue, flags);
377 : : };
378 : :
379 : 47 : GObject.ParamSpec.flags = function (name, nick, blurb, flags, flagsType, defaultValue) {
380 : 3 : return GObject.param_spec_flags(name, nick, blurb, flagsType, defaultValue, flags);
381 : : };
382 : :
383 : 47 : GObject.ParamSpec.enum = function (name, nick, blurb, flags, enumType, defaultValue) {
384 : 3 : return GObject.param_spec_enum(name, nick, blurb, enumType, defaultValue, flags);
385 : : };
386 : :
387 : 47 : GObject.ParamSpec.double = function (name, nick, blurb, flags, minimum, maximum, defaultValue) {
388 : 0 : return GObject.param_spec_double(name, nick, blurb, minimum, maximum, defaultValue, flags);
389 : : };
390 : :
391 : 47 : GObject.ParamSpec.string = function (name, nick, blurb, flags, defaultValue) {
392 : 26 : return GObject.param_spec_string(name, nick, blurb, defaultValue, flags);
393 : : };
394 : :
395 : 47 : GObject.ParamSpec.boxed = function (name, nick, blurb, flags, boxedType) {
396 : 0 : return GObject.param_spec_boxed(name, nick, blurb, boxedType, flags);
397 : : };
398 : :
399 : 47 : GObject.ParamSpec.object = function (name, nick, blurb, flags, objectType) {
400 : 5 : return GObject.param_spec_object(name, nick, blurb, objectType, flags);
401 : : };
402 : :
403 : 47 : GObject.ParamSpec.jsobject = function (name, nick, blurb, flags) {
404 : 5 : return GObject.param_spec_boxed(name, nick, blurb, Object.$gtype, flags);
405 : : };
406 : :
407 : 47 : GObject.ParamSpec.param = function (name, nick, blurb, flags, paramType) {
408 : 0 : return GObject.param_spec_param(name, nick, blurb, paramType, flags);
409 : : };
410 : :
411 : 47 : GObject.ParamSpec.override = Gi.override_property;
412 : :
413 : 94 : Object.defineProperties(GObject.ParamSpec.prototype, {
414 : 47 : 'name': {
415 : 47 : configurable: false,
416 : 47 : enumerable: false,
417 : 47 : get() {
418 : 112 : return this.get_name();
419 : : },
420 : : },
421 : 47 : '_nick': {
422 : 47 : configurable: false,
423 : 47 : enumerable: false,
424 : 47 : get() {
425 : 9 : return this.get_nick();
426 : : },
427 : : },
428 : 47 : 'nick': {
429 : 47 : configurable: false,
430 : 47 : enumerable: false,
431 : 47 : get() {
432 : 2 : return this.get_nick();
433 : : },
434 : : },
435 : 47 : '_blurb': {
436 : 47 : configurable: false,
437 : 47 : enumerable: false,
438 : 47 : get() {
439 : 9 : return this.get_blurb();
440 : : },
441 : : },
442 : 47 : 'blurb': {
443 : 47 : configurable: false,
444 : 47 : enumerable: false,
445 : 47 : get() {
446 : 2 : return this.get_blurb();
447 : : },
448 : : },
449 : 47 : 'default_value': {
450 : 47 : configurable: false,
451 : 47 : enumerable: false,
452 : 47 : get() {
453 : 12 : return this.get_default_value();
454 : : },
455 : : },
456 : 47 : 'flags': {
457 : 47 : configurable: false,
458 : 47 : enumerable: false,
459 : 47 : get() {
460 : 110 : return GjsPrivate.param_spec_get_flags(this);
461 : : },
462 : : },
463 : 47 : 'value_type': {
464 : 47 : configurable: false,
465 : 47 : enumerable: false,
466 : 47 : get() {
467 : 2 : return GjsPrivate.param_spec_get_value_type(this);
468 : : },
469 : : },
470 : 47 : 'owner_type': {
471 : 47 : configurable: false,
472 : 47 : enumerable: false,
473 : 47 : get() {
474 : 0 : return GjsPrivate.param_spec_get_owner_type(this);
475 : : },
476 : : },
477 : : });
478 : :
479 : 47 : let {GObjectMeta, GObjectInterface} = Legacy.defineGObjectLegacyObjects(GObject);
480 : 47 : GObject.Class = GObjectMeta;
481 : 47 : GObject.Interface = GObjectInterface;
482 : 47 : GObject.Object.prototype.__metaclass__ = GObject.Class;
483 : :
484 : : // For compatibility with Lang.Class... we need a _construct
485 : : // or the Lang.Class constructor will fail.
486 : 47 : GObject.Object.prototype._construct = function (...args) {
487 [ # # ][ # # ]: 0 : this._init(...args);
488 : 0 : return this;
489 : : };
490 : :
491 : 47 : GObject.registerClass = registerClass;
492 : :
493 [ + + ]: 58 : GObject.Object.new = function (gtype, props = {}) {
494 : 11 : const constructor = Gi.lookupConstructor(gtype);
495 : :
496 [ + - ]: 11 : if (!constructor)
497 : 0 : throw new Error(`Constructor for gtype ${gtype} not found`);
498 : 11 : return new constructor(props);
499 : 7 : };
500 : :
501 : 56 : GObject.Object.new_with_properties = function (gtype, names, values) {
502 [ + + ][ + + ]: 9 : if (!Array.isArray(names) || !Array.isArray(values))
503 : 3 : throw new Error('new_with_properties takes two arrays (names, values)');
504 [ + + ]: 6 : if (names.length !== values.length)
505 : 1 : throw new Error('Arrays passed to new_with_properties must be the same length');
506 : :
507 : 10 : const props = Object.fromEntries(names.map((name, ix) => [name, values[ix]]));
508 : 5 : return GObject.Object.new(gtype, props);
509 : 3 : };
510 : :
511 : 47 : GObject.Object._classInit = function (klass) {
512 : 84 : _checkProperties(klass);
513 : :
514 [ + + ]: 84 : if (_registerType in klass)
515 : 83 : klass[_registerType]();
516 : : else
517 : 1 : _resolveLegacyClassFunction(klass, _registerType).call(klass);
518 : :
519 : 77 : return klass;
520 : : };
521 : :
522 : : // For backwards compatibility only. Use instanceof instead.
523 : 47 : GObject.Object.implements = function (iface) {
524 [ - + ]: 2 : if (iface.$gtype)
525 : 2 : return GObject.type_is_a(this, iface.$gtype);
526 : 0 : return false;
527 : : };
528 : :
529 : 84 : function registerGObjectType() {
530 : 84 : let klass = this;
531 : :
532 : 84 : let gtypename = _createGTypeName(klass);
533 [ + + ]: 84 : let gflags = klass.hasOwnProperty(GTypeFlags) ? klass[GTypeFlags] : 0;
534 [ + + ]: 84 : let gobjectInterfaces = klass.hasOwnProperty(interfaces) ? klass[interfaces] : [];
535 : 84 : let propertiesArray = _propertiesAsArray(klass);
536 : 84 : let parent = Object.getPrototypeOf(klass);
537 [ + + ]: 84 : let gobjectSignals = klass.hasOwnProperty(signals) ? klass[signals] : [];
538 : :
539 : : // Default to the GObject-specific prototype, fallback on the JS prototype for GI native classes.
540 [ + + ]: 84 : const parentPrototype = parent.prototype[Gi.gobject_prototype_symbol] ?? parent.prototype;
541 : :
542 [ + - ][ + - ]: 168 : const [giPrototype, registeredType] = Gi.register_type_with_class(
[ + - ][ + - ]
543 : 84 : klass, parentPrototype, gtypename, gflags,
544 : 84 : gobjectInterfaces, propertiesArray);
545 : :
546 : 83 : _defineGType(klass, giPrototype, registeredType);
547 : 83 : _createSignals(klass.$gtype, gobjectSignals);
548 : :
549 : : // Reverse the interface array to give the last required interface precedence over the first.
550 [ + + ]: 83 : const requiredInterfaces = [...gobjectInterfaces].reverse();
551 : 83 : requiredInterfaces.forEach(iface =>
552 : 25 : _copyInterfacePrototypeDescriptors(klass, iface));
553 : 83 : requiredInterfaces.forEach(iface =>
554 : 25 : _copyInterfacePrototypeDescriptors(klass.prototype, iface.prototype));
555 : :
556 : 83 : Object.getOwnPropertyNames(klass.prototype)
557 [ + + ]: 404 : .filter(name => name.startsWith('vfunc_') || name.startsWith('on_'))
558 : 149 : .forEach(name => {
559 : 66 : let descr = Object.getOwnPropertyDescriptor(klass.prototype, name);
560 [ + - ]: 66 : if (typeof descr.value !== 'function')
561 : 0 : return;
562 : :
563 : 66 : let func = klass.prototype[name];
564 : :
565 [ + + ]: 66 : if (name.startsWith('vfunc_')) {
566 : 60 : giPrototype[Gi.hook_up_vfunc_symbol](name.slice(6), func);
567 [ - + ]: 6 : } else if (name.startsWith('on_')) {
568 : 12 : let id = GObject.signal_lookup(name.slice(3).replace('_', '-'),
569 : 6 : klass.$gtype);
570 [ - + ]: 6 : if (id !== 0) {
571 : 20 : GObject.signal_override_class_closure(id, klass.$gtype, function (...argArray) {
572 : 14 : let emitter = argArray.shift();
573 : :
574 : 14 : return func.apply(emitter, argArray);
575 : 14 : });
576 : : }
577 : : }
578 : 0 : });
579 : :
580 : 79 : gobjectInterfaces.forEach(iface =>
581 : 24 : _checkInterface(iface, klass.prototype));
582 : :
583 : : // Lang.Class parent classes don't support static inheritance
584 [ + - ]: 78 : if (!('implements' in klass))
585 : 0 : klass.implements = GObject.Object.implements;
586 : : }
587 : :
588 : 94 : Object.defineProperty(GObject.Object, _registerType, {
589 : 47 : value: registerGObjectType,
590 : 47 : writable: false,
591 : 47 : configurable: false,
592 : 47 : enumerable: false,
593 : : });
594 : :
595 : : function interfaceInstanceOf(instance) {
596 [ + + ][ + + ]: 18 : if (instance && typeof instance === 'object' &&
597 [ + + ]: 12 : GObject.Interface.prototype.isPrototypeOf(this.prototype))
598 : 12 : return GObject.type_is_a(instance, this);
599 : :
600 : 6 : return false;
601 : : }
602 : :
603 : 3 : function registerInterfaceType() {
604 : 3 : let klass = this;
605 : :
606 : 3 : let gtypename = _createGTypeName(klass);
607 [ - + ]: 3 : let gobjectInterfaces = klass.hasOwnProperty(requires) ? klass[requires] : [];
608 : 3 : let props = _propertiesAsArray(klass);
609 [ + + ]: 3 : let gobjectSignals = klass.hasOwnProperty(signals) ? klass[signals] : [];
610 : :
611 [ + - ][ + - ]: 6 : const [giPrototype, registeredType] = Gi.register_interface_with_class(klass, gtypename, gobjectInterfaces,
[ + - ][ + - ]
612 : 3 : props);
613 : :
614 : 3 : _defineGType(klass, giPrototype, registeredType);
615 : 3 : _createSignals(klass.$gtype, gobjectSignals);
616 : :
617 : 6 : Object.defineProperty(klass, Symbol.hasInstance, {
618 : 3 : value: interfaceInstanceOf,
619 : : });
620 : :
621 : 3 : return klass;
622 : 3 : }
623 : :
624 : 94 : Object.defineProperty(GObject.Interface, _registerType, {
625 : 47 : value: registerInterfaceType,
626 : 47 : writable: false,
627 : 47 : configurable: false,
628 : 47 : enumerable: false,
629 : : });
630 : :
631 : 47 : GObject.Interface._classInit = function (klass) {
632 [ - + ]: 3 : if (_registerType in klass)
633 : 3 : klass[_registerType]();
634 : : else
635 : 0 : _resolveLegacyClassFunction(klass, _registerType).call(klass);
636 : :
637 : 3 : Object.getOwnPropertyNames(klass.prototype)
638 : 9 : .filter(key => key !== 'constructor')
639 : 3 : .concat(Object.getOwnPropertySymbols(klass.prototype))
640 : 9 : .forEach(key => {
641 : 6 : let descr = Object.getOwnPropertyDescriptor(klass.prototype, key);
642 : :
643 : : // Create wrappers on the interface object so that generics work (e.g.
644 : : // SomeInterface.some_function(this, blah) instead of
645 : : // SomeInterface.prototype.some_function.call(this, blah)
646 [ + + ]: 6 : if (typeof descr.value === 'function') {
647 : 3 : let interfaceProto = klass.prototype; // capture in closure
648 : 7 : klass[key] = function (thisObj, ...args) {
649 [ + - ]: 4 : return interfaceProto[key].call(thisObj, ...args);
650 : : };
651 : : }
652 : :
653 : 6 : Object.defineProperty(klass.prototype, key, descr);
654 : : });
655 : :
656 : 3 : return klass;
657 : : };
658 : :
659 : : /**
660 : : * Use this to signify a function that must be overridden in an
661 : : * implementation of the interface.
662 : : */
663 [ - + ]: 48 : GObject.NotImplementedError = class NotImplementedError extends Error {
664 : 47 : get name() {
665 : 0 : return 'NotImplementedError';
666 : : }
667 : : };
668 : :
669 : : // These will be copied in the Gtk overrides
670 : : // Use __X__ syntax to indicate these variables should not be used publicly.
671 : :
672 : 47 : GObject.__gtkCssName__ = _gtkCssName;
673 : 47 : GObject.__gtkTemplate__ = _gtkTemplate;
674 : 47 : GObject.__gtkChildren__ = _gtkChildren;
675 : 47 : GObject.__gtkInternalChildren__ = _gtkInternalChildren;
676 : :
677 : : // Expose GObject static properties for ES6 classes
678 : :
679 : 47 : GObject.GTypeName = GTypeName;
680 : 47 : GObject.requires = requires;
681 : 47 : GObject.interfaces = interfaces;
682 : 47 : GObject.properties = properties;
683 : 47 : GObject.signals = signals;
684 : :
685 : : // Replacement for non-introspectable g_object_set()
686 : 47 : GObject.Object.prototype.set = function (params) {
687 : 1 : Object.assign(this, params);
688 : : };
689 : :
690 : 48 : GObject.Object.prototype.bind_property_full = function (...args) {
691 [ + + ]: 1 : return GjsPrivate.g_object_bind_property_full(this, ...args);
692 : : };
693 : :
694 [ - + ]: 47 : if (GObject.BindingGroup !== undefined) {
695 : 48 : GObject.BindingGroup.prototype.bind_full = function (...args) {
696 [ + + ]: 1 : return GjsPrivate.g_binding_group_bind_full(this, ...args);
697 : : };
698 : : }
699 : :
700 : : // fake enum for signal accumulators, keep in sync with gi/object.c
701 : 47 : GObject.AccumulatorType = {
702 : 47 : NONE: 0,
703 : 47 : FIRST_WINS: 1,
704 : 47 : TRUE_HANDLED: 2,
705 : : };
706 : :
707 : 47 : GObject.Object.prototype.disconnect = function (id) {
708 : 5 : return GObject.signal_handler_disconnect(this, id);
709 : : };
710 : 47 : GObject.Object.prototype.block_signal_handler = function (id) {
711 : 0 : return GObject.signal_handler_block(this, id);
712 : : };
713 : 47 : GObject.Object.prototype.unblock_signal_handler = function (id) {
714 : 0 : return GObject.signal_handler_unblock(this, id);
715 : : };
716 : 47 : GObject.Object.prototype.stop_emission_by_name = function (detailedName) {
717 : 0 : return GObject.signal_stop_emission_by_name(this, detailedName);
718 : : };
719 : :
720 : : // A simple workaround if you have a class with .connect, .disconnect or .emit
721 : : // methods (such as Gio.Socket.connect or NMClient.Device.disconnect)
722 : : // The original g_signal_* functions are not introspectable anyway, because
723 : : // we need our own handling of signal argument marshalling
724 : 47 : GObject.signal_connect = function (object, name, handler) {
725 : 2 : return GObject.Object.prototype.connect.call(object, name, handler);
726 : : };
727 : 47 : GObject.signal_connect_after = function (object, name, handler) {
728 : 0 : return GObject.Object.prototype.connect_after.call(object, name, handler);
729 : : };
730 : 50 : GObject.signal_emit_by_name = function (object, ...nameAndArgs) {
731 : 3 : return GObject.Object.prototype.emit.apply(object, nameAndArgs);
732 : : };
733 : :
734 : : // Replacements for signal_handler_find() and similar functions, which can't
735 : : // work normally since we connect private closures
736 : 47 : GObject._real_signal_handler_find = GObject.signal_handler_find;
737 : 47 : GObject._real_signal_handlers_block_matched = GObject.signal_handlers_block_matched;
738 : 47 : GObject._real_signal_handlers_unblock_matched = GObject.signal_handlers_unblock_matched;
739 : 47 : GObject._real_signal_handlers_disconnect_matched = GObject.signal_handlers_disconnect_matched;
740 : :
741 : : /**
742 : : * Finds the first signal handler that matches certain selection criteria.
743 : : * The criteria are passed as properties of a match object.
744 : : * The match object has to be non-empty for successful matches.
745 : : * If no handler was found, a falsy value is returned.
746 : : *
747 : : * @function
748 : : * @param {GObject.Object} instance - the instance owning the signal handler
749 : : * to be found.
750 : : * @param {object} match - a properties object indicating whether to match
751 : : * by signal ID, detail, or callback function.
752 : : * @param {string} [match.signalId] - signal the handler has to be connected
753 : : * to.
754 : : * @param {string} [match.detail] - signal detail the handler has to be
755 : : * connected to.
756 : : * @param {Function} [match.func] - the callback function the handler will
757 : : * invoke.
758 : : * @returns {number | bigint | object | null} A valid non-0 signal handler ID for
759 : : * a successful match.
760 : : */
761 : 47 : GObject.signal_handler_find = function (instance, match) {
762 : : // For backwards compatibility
763 [ + - ]: 12 : if (arguments.length === 7)
764 : : // eslint-disable-next-line prefer-rest-params
765 [ # # ][ # # ]: 0 : return GObject._real_signal_handler_find(...arguments);
766 : 12 : return instance[Gi.signal_find_symbol](match);
767 : : };
768 : : /**
769 : : * Blocks all handlers on an instance that match certain selection criteria.
770 : : * The criteria are passed as properties of a match object.
771 : : * The match object has to have at least `func` for successful matches.
772 : : * If no handlers were found, 0 is returned, the number of blocked handlers
773 : : * otherwise.
774 : : *
775 : : * @function
776 : : * @param {GObject.Object} instance - the instance owning the signal handler
777 : : * to be found.
778 : : * @param {object} match - a properties object indicating whether to match
779 : : * by signal ID, detail, or callback function.
780 : : * @param {string} [match.signalId] - signal the handler has to be connected
781 : : * to.
782 : : * @param {string} [match.detail] - signal detail the handler has to be
783 : : * connected to.
784 : : * @param {Function} match.func - the callback function the handler will
785 : : * invoke.
786 : : * @returns {number} The number of handlers that matched.
787 : : */
788 : 47 : GObject.signal_handlers_block_matched = function (instance, match) {
789 : : // For backwards compatibility
790 [ + - ]: 3 : if (arguments.length === 7)
791 : : // eslint-disable-next-line prefer-rest-params
792 [ # # ][ # # ]: 0 : return GObject._real_signal_handlers_block_matched(...arguments);
793 : 3 : return instance[Gi.signals_block_symbol](match);
794 : : };
795 : : /**
796 : : * Unblocks all handlers on an instance that match certain selection
797 : : * criteria.
798 : : * The criteria are passed as properties of a match object.
799 : : * The match object has to have at least `func` for successful matches.
800 : : * If no handlers were found, 0 is returned, the number of unblocked
801 : : * handlers otherwise.
802 : : * The match criteria should not apply to any handlers that are not
803 : : * currently blocked.
804 : : *
805 : : * @function
806 : : * @param {GObject.Object} instance - the instance owning the signal handler
807 : : * to be found.
808 : : * @param {object} match - a properties object indicating whether to match
809 : : * by signal ID, detail, or callback function.
810 : : * @param {string} [match.signalId] - signal the handler has to be connected
811 : : * to.
812 : : * @param {string} [match.detail] - signal detail the handler has to be
813 : : * connected to.
814 : : * @param {Function} match.func - the callback function the handler will
815 : : * invoke.
816 : : * @returns {number} The number of handlers that matched.
817 : : */
818 : 47 : GObject.signal_handlers_unblock_matched = function (instance, match) {
819 : : // For backwards compatibility
820 [ + - ]: 3 : if (arguments.length === 7)
821 : : // eslint-disable-next-line prefer-rest-params
822 [ # # ][ # # ]: 0 : return GObject._real_signal_handlers_unblock_matched(...arguments);
823 : 3 : return instance[Gi.signals_unblock_symbol](match);
824 : : };
825 : : /**
826 : : * Disconnects all handlers on an instance that match certain selection
827 : : * criteria.
828 : : * The criteria are passed as properties of a match object.
829 : : * The match object has to have at least `func` for successful matches.
830 : : * If no handlers were found, 0 is returned, the number of disconnected
831 : : * handlers otherwise.
832 : : *
833 : : * @function
834 : : * @param {GObject.Object} instance - the instance owning the signal handler
835 : : * to be found.
836 : : * @param {object} match - a properties object indicating whether to match
837 : : * by signal ID, detail, or callback function.
838 : : * @param {string} [match.signalId] - signal the handler has to be connected
839 : : * to.
840 : : * @param {string} [match.detail] - signal detail the handler has to be
841 : : * connected to.
842 : : * @param {Function} match.func - the callback function the handler will
843 : : * invoke.
844 : : * @returns {number} The number of handlers that matched.
845 : : */
846 : 47 : GObject.signal_handlers_disconnect_matched = function (instance, match) {
847 : : // For backwards compatibility
848 [ + - ]: 3 : if (arguments.length === 7)
849 : : // eslint-disable-next-line prefer-rest-params
850 [ # # ][ # # ]: 0 : return GObject._real_signal_handlers_disconnect_matched(...arguments);
851 : 3 : return instance[Gi.signals_disconnect_symbol](match);
852 : : };
853 : :
854 : : // Also match the macros used in C APIs, even though they're not introspected
855 : :
856 : : /**
857 : : * Blocks all handlers on an instance that match `func`.
858 : : *
859 : : * @function
860 : : * @param {GObject.Object} instance - the instance to block handlers from.
861 : : * @param {Function} func - the callback function the handler will invoke.
862 : : * @returns {number} The number of handlers that matched.
863 : : */
864 : 47 : GObject.signal_handlers_block_by_func = function (instance, func) {
865 : 3 : return instance[Gi.signals_block_symbol]({func});
866 : : };
867 : : /**
868 : : * Unblocks all handlers on an instance that match `func`.
869 : : *
870 : : * @function
871 : : * @param {GObject.Object} instance - the instance to unblock handlers from.
872 : : * @param {Function} func - the callback function the handler will invoke.
873 : : * @returns {number} The number of handlers that matched.
874 : : */
875 : 47 : GObject.signal_handlers_unblock_by_func = function (instance, func) {
876 : 3 : return instance[Gi.signals_unblock_symbol]({func});
877 : : };
878 : : /**
879 : : * Disconnects all handlers on an instance that match `func`.
880 : : *
881 : : * @function
882 : : * @param {GObject.Object} instance - the instance to remove handlers from.
883 : : * @param {Function} func - the callback function the handler will invoke.
884 : : * @returns {number} The number of handlers that matched.
885 : : */
886 : 47 : GObject.signal_handlers_disconnect_by_func = function (instance, func) {
887 : 3 : return instance[Gi.signals_disconnect_symbol]({func});
888 : : };
889 : 47 : GObject.signal_handlers_disconnect_by_data = function () {
890 : 1 : throw new Error('GObject.signal_handlers_disconnect_by_data() is not \
891 : : introspectable. Use GObject.signal_handlers_disconnect_by_func() instead.');
892 : : };
893 : : }
|