/
index.js
598 lines (472 loc) · 15.7 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
/*
* index.js
*/
const camelCase = require('lodash.camelcase')
const internal = require('./native.js')
// The bootstrap from C here contains functions and methods for each object,
// namespaced with underscores. See gi.cc for more information.
const GI = internal.Bootstrap();
// The GIRepository API is fairly poor, and contains methods on classes,
// methods on objects, and what should be methods interpreted as functions,
// because the scanner does not interpret methods on typedefs correctly.
// We extend this bootstrap'd repo to define all flags / enums, which
// are all we need to start declaring objects.
bootstrapGIRepository(GI)
function bootstrapGIRepository(GI) {
const repo = GI.Repository_get_default()
const ns = 'GIRepository'
// First, grab InfoType so we can find enums / flags.
const InfoType = makeEnum(GI.Repository_find_by_name.call(repo, ns, 'InfoType'))
// Now, define all enums / flags.
const nInfos = GI.Repository_get_n_infos.call(repo, ns)
for (let i = 0; i < nInfos; i++) {
const info = GI.Repository_get_info.call(repo, ns, i)
const name = GI.BaseInfo_get_name.call(info)
const type = GI.BaseInfo_get_type.call(info)
if (type === InfoType.ENUM || type === InfoType.FLAGS)
GI[name] = makeEnum(info)
}
}
// We also define GObject#on and friends, to keep NodeJS EventEmitter
// semantics. They are implemented in terms of the native .connect()
// and .disconnect() methods.
const GObject = internal.GetBaseClass()
extendGObject(GObject)
function extendGObject(GObject) {
GObject.prototype.on = function on(event, callback) {
defineListeners(this)
if (!this._listeners.has(event))
this._listeners.set(event, new WeakMap())
const fnMap = this._listeners.get(event)
const handlerID = this.connect(event, callback)
fnMap.set(callback, handlerID)
}
GObject.prototype.off = function off(event, callback) {
defineListeners(this)
if (!this._listeners.has(event))
return
const fnMap = this._listeners.get(event)
if (!fnMap.has(callback))
return
const handlerID = fnMap.get(callback)
this.disconnect(handlerID)
fnMap.delete(callback)
}
GObject.prototype.once = function once(event, callback) {
const newCallback = (...args) => {
callback(...args)
this.off(event, newCallback)
}
this.on(event, newCallback)
}
function defineListeners(object) {
if (object._listeners !== undefined)
return
Object.defineProperty(object, '_listeners', {
value: new Map()
})
}
}
/**
* Module cache
*/
const moduleCache = internal.GetModuleCache();
function define(object, description) {
const target = (description.isMethod || description.isProperty) ?
object.prototype : object
Object.defineProperty(target, description.name, description.property)
}
function getFunctionDescription(info) {
const fn = internal.MakeFunction(info);
const name = getInfoName(info);
const flags = GI.function_info_get_flags(info);
const isMethod = ((flags & GI.FunctionInfoFlags.IS_METHOD) != 0 &&
(flags & GI.FunctionInfoFlags.IS_CONSTRUCTOR) == 0);
return {
name,
isMethod,
property: {
configurable: true,
writable: true,
value: fn
}
}
}
function getPropertyDescription(info) {
const propertyName = getName(info)
const name = getInfoName(info)
const property = {
configurable: true,
enumerable: true,
get: propertyGetter(propertyName),
set: propertySetter(propertyName),
}
return { name, property, isProperty: true }
}
function getConstantDescription(info) {
const name = getInfoName(info)
const property = {
configurable: false,
writable: false,
enumerable: true,
value: makeConstant(info)
}
return { name, property }
}
function addFunction(object, info) {
define(object, getFunctionDescription(info))
}
function addVirtualFunction(object, info, implementor) {
const fn = internal.MakeVirtualFunction(info, implementor);
const name = getInfoName(info);
const flags = GI.function_info_get_flags(info);
const isMethod = ((flags & GI.FunctionInfoFlags.IS_METHOD) != 0 &&
(flags & GI.FunctionInfoFlags.IS_CONSTRUCTOR) == 0);
const target = (isMethod) ? object.prototype : object;
Object.defineProperty(target, name, {
configurable: true,
writable: true,
value: fn
})
}
function fieldGetter(fieldInfo) {
return function() {
return internal.StructFieldGetter(this, fieldInfo);
};
}
function fieldSetter(fieldInfo) {
return function(value) {
return internal.StructFieldSetter(this, fieldInfo, value);
};
}
function addField(object, fieldInfo) {
const flags = GI.field_info_get_flags(fieldInfo)
const writable = (flags & GI.FieldInfoFlags.WRITABLE) !== 0
const readable = (flags & GI.FieldInfoFlags.READABLE) !== 0
const name = getInfoName(fieldInfo)
Object.defineProperty(object, name, {
configurable: true,
enumerable: readable,
get: readable ? fieldGetter(fieldInfo) : undefined,
set: writable ? fieldSetter(fieldInfo) : undefined
})
}
function propertyGetter(propertyName) {
return function() {
return internal.ObjectPropertyGetter(this, propertyName);
};
}
function propertySetter(propertyName) {
return function(value) {
return internal.ObjectPropertySetter(this, propertyName, value);
};
}
function addProperty(object, info) {
define(object, getPropertyDescription(info))
}
function addConstant(object, info) {
define(object, getConstantDescription(info))
}
function makeConstant(info) {
return internal.GetConstantValue(info)
}
function makeFunction(info) {
return internal.MakeFunction(info)
}
function makeVirtualFunction(info, implementor) {
return internal.MakeVirtualFunction(info, implementor)
}
function makeEnum(info) {
const object = {}
const nValues = GI.enum_info_get_n_values(info);
for (let i = 0; i < nValues; i++) {
const valueInfo = GI.enum_info_get_value(info, i);
const valueName = getName(valueInfo).toUpperCase()
const value = GI.value_info_get_value(valueInfo)
Object.defineProperty(object, valueName, {
configurable: true,
enumerable: true,
value: value,
})
Object.defineProperty(object, value, {
configurable: true,
enumerable: false,
value: valueName,
})
}
const nMethods = GI.enum_info_get_n_methods(info)
for (let i = 0; i < nMethods; i++) {
const methodInfo = GI.enum_info_get_method(info, i)
const methodName = camelCase(getName(methodInfo))
const method = internal.MakeFunction(methodInfo)
Object.defineProperty(object, methodName, {
configurable: true,
writable: true,
value: method
})
}
return object;
}
function makeObject(info) {
const constructor = internal.MakeObjectClass(info);
loop(info, GI.object_info_get_n_properties, GI.object_info_get_property, (propertyInfo) => {
addProperty(constructor, propertyInfo)
})
loop(info, GI.object_info_get_n_methods, GI.object_info_get_method, (methodInfo) => {
addFunction(constructor, methodInfo);
})
loop(info, GI.object_info_get_n_constants, GI.object_info_get_constant, (constantInfo) => {
addConstant(constructor, constantInfo)
})
/*
* Define interface-implemented things
*/
loop(info, GI.object_info_get_n_interfaces, GI.object_info_get_interface, (interfaceInfo) => {
const interface = getInterface(interfaceInfo)
interface.properties.forEach(description => {
define(constructor, description)
})
interface.methods.forEach(description => {
define(constructor, description)
})
interface.constants.forEach(description => {
define(constructor, description)
})
})
return constructor;
}
function makeBoxed(info) {
if (getType(info) == GI.InfoType.UNION) {
return makeUnion(info)
} else {
return makeStruct(info)
}
}
function makeUnion(info) {
const constructor = internal.MakeBoxedClass(info);
const nMethods = GI.union_info_get_n_methods(info);
for (let i = 0; i < nMethods; i++) {
const methodInfo = GI.union_info_get_method(info, i);
addFunction(constructor, methodInfo);
}
const nFields = GI.union_info_get_n_fields(info);
for (let i = 0; i < nFields; i++) {
const fieldInfo = GI.union_info_get_field(info, i);
addField(constructor.prototype, fieldInfo);
}
return constructor
}
function makeStruct(info) {
const constructor = internal.MakeBoxedClass(info);
const nMethods = GI.struct_info_get_n_methods(info);
for (let i = 0; i < nMethods; i++) {
var methodInfo = GI.struct_info_get_method(info, i);
addFunction(constructor, methodInfo);
}
const nFields = GI.struct_info_get_n_fields(info);
for (let i = 0; i < nFields; i++) {
const fieldInfo = GI.struct_info_get_field(info, i);
addField(constructor.prototype, fieldInfo);
}
return constructor
}
function makeInterface(info) {
const id = getNamespace(info) + '.' + getName(info)
const constructor = Object.values({
[getInfoName(info)]: function() {
throw new Error('Cannot instantiate Interface (abstract type)')
}
})[0]
constructor.properties = []
loop(info, GI.interface_info_get_n_properties, GI.interface_info_get_property, (propInfo) => {
constructor.properties.push(getPropertyDescription(propInfo))
})
/* loop(info, GI.interface_info_get_n_vfuncs, GI.interface_info_get_vfunc, (methodInfo) => {
* addVirtualFunction(constructor, methodInfo, constructor.gtype);
* }) */
constructor.methods = []
loop(info, GI.interface_info_get_n_methods, GI.interface_info_get_method, (methodInfo) => {
constructor.methods.push(getFunctionDescription(methodInfo))
})
constructor.constants = []
loop(info, GI.interface_info_get_n_constants, GI.interface_info_get_constant, (constantInfo) => {
constructor.constants.push(getConstantDescription(constantInfo))
})
return constructor;
}
function makeInfo(info) {
const type = getType(info);
switch (type) {
case GI.InfoType.FUNCTION:
return makeFunction(info);
// case GI.InfoType.VFUNC:
// return makeVirtualFunction(info, ...args);
case GI.InfoType.STRUCT:
if (GI.struct_info_is_gtype_struct(info))
return null; // Not needed
// else fallthrough
case GI.InfoType.BOXED:
case GI.InfoType.UNION:
return makeBoxed(info);
case GI.InfoType.ENUM:
case GI.InfoType.FLAGS:
return makeEnum(info);
case GI.InfoType.OBJECT:
return makeObject(info);
case GI.InfoType.INTERFACE: // FIXME
return makeInterface(info);
case GI.InfoType.CONSTANT:
return makeConstant(info);
// INVALID_0
// SIGNAL
// VFUNC
// PROPERTY
// FIELD
// ARG
// TYPE
// UNRESOLVED
// INVALID
// CALLBACK
}
}
function getInfoName(info) {
const type = getType(info)
const name = getName(info)
switch (type) {
case GI.InfoType.FUNCTION:
case GI.InfoType.VFUNC:
return camelCase(name) // lowerCamelCase (snake_case by default)
case GI.InfoType.FIELD:
return camelCase(name) // lowerCamelCase (snake_case by default)
case GI.InfoType.PROPERTY:
return camelCase(name) // lowerCamelCase (dash-case by default)
case GI.InfoType.STRUCT:
case GI.InfoType.BOXED:
case GI.InfoType.UNION:
return name // UpperCamelCase
case GI.InfoType.ENUM:
case GI.InfoType.FLAGS:
return name // UpperCamelCase
case GI.InfoType.OBJECT:
return name // UpperCamelCase
case GI.InfoType.INTERFACE:
return name // UpperCamelCase
case GI.InfoType.CONSTANT:
return name // ALL_CAPS by default
case GI.InfoType.VALUE: // enum's value
return name.toUpperCase() // ALL_CAPS
// INVALID_0
// SIGNAL
// ARG
// TYPE
// UNRESOLVED
// INVALID
// CALLBACK
}
}
function getInterface(info) {
const name = getInfoName(info)
const namespace = getNamespace(info)
if (moduleCache[namespace][name])
return moduleCache[namespace][name]
moduleCache[namespace][name] = makeInterface(info)
return moduleCache[namespace][name]
}
// Namespace loading
/**
* Require module & version. Automatically loads dependencies.
*/
function giRequire(ns, version) {
if (moduleCache[ns])
return moduleCache[ns]
const module = moduleCache[ns] = Object.create(null)
const repo = GI.Repository_get_default()
GI.Repository_require.call(repo, ns, version || null, 0)
version = version || GI.Repository_get_version.call(repo, ns)
loadDependencies(ns, version)
const nInfos = GI.Repository_get_n_infos.call(repo, ns);
for (let i = 0; i < nInfos; i++) {
const info = GI.Repository_get_info.call(repo, ns, i);
const item = makeInfo(info);
if (item)
module[getInfoName(info)] = item
}
try {
const override = require(`./overrides/${[ns, version].join('-')}.js`)
override.apply(module)
} catch(e) {
try {
const override = require(`./overrides/${ns}.js`)
override.apply(module)
} catch(e) { /* No override */ }
}
return module
}
/**
* Loads dependencies of a library
*/
function loadDependencies(ns, version) {
const repo = GI.Repository_get_default()
const dependencies = GI.Repository_get_dependencies.call(repo, ns, version)
dependencies.forEach(dependency => {
const [name, version] = dependency.split('-')
giRequire(name, version)
})
}
/**
* Check if module version is loaded
*/
function _isLoaded(ns, version) {
var namespace = moduleCache[ns] || (moduleCache[ns] = Object.create(null));
version = version || null;
if (namespace[version])
return true;
if (version == null && namespace.length > 0) {
console.log(Object.keys(namespace)[0]);
return true;
}
return false;
}
/*
* Exports
*/
// Public API
exports.require = giRequire;
exports.startLoop = internal.StartLoop;
// Private API
exports._isLoaded = _isLoaded;
exports._cache = moduleCache;
exports._c = internal;
exports._GIRepository = GI;
exports._InfoType = GI.InfoType;
/*
* Helpers
*/
function getName(info) {
return GI.BaseInfo_get_name.call(info);
}
function getNamespace(info) {
return GI.BaseInfo_get_namespace.call(info);
}
function getType (info) {
return GI.BaseInfo_get_type.call(info);
}
function isDeprecated(info) {
return GI.BaseInfo_is_deprecated.call()
}
function deprecationWrapper(info, fn) {
const wrapper = Object.values({
[fn.name]: function() {
console.log(`[node-gtk]: ${getInfoName(info)} is deprecated`)
return fn.apply(this, arguments)
}
})[0]
return wrapper
}
function loop(info, lengthFn, itemFn, fn) {
const length = lengthFn(info)
for (let i = 0; i < length; i++) {
const item = itemFn(info, i)
fn(item, i)
}
}