summaryrefslogtreecommitdiff
path: root/modules/core/overrides/GLib.js
blob: ed6005ba67544eaa5429671d5e5703156a831694 (plain)
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
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2011 Giovanni Campagna

const ByteArray = imports.byteArray;
const {GLib, GjsPrivate} = imports.gi;
const {log_set_writer_func, log_set_writer_default} = GjsPrivate;

const SIMPLE_TYPES = ['b', 'y', 'n', 'q', 'i', 'u', 'x', 't', 'h', 'd', 's', 'o', 'g'];

function _readSingleType(signature, forceSimple) {
    let char = signature.shift();
    let isSimple = false;

    if (!SIMPLE_TYPES.includes(char)) {
        if (forceSimple)
            throw new TypeError('Invalid GVariant signature (a simple type was expected)');
    } else {
        isSimple = true;
    }

    if (char === 'm' || char === 'a')
        return [char].concat(_readSingleType(signature, false));
    if (char === '{') {
        let key = _readSingleType(signature, true);
        let val = _readSingleType(signature, false);
        let close = signature.shift();
        if (close !== '}')
            throw new TypeError('Invalid GVariant signature for type DICT_ENTRY (expected "}"');
        return [char].concat(key, val, close);
    }
    if (char === '(') {
        let res = [char];
        while (true) {
            if (signature.length === 0)
                throw new TypeError('Invalid GVariant signature for type TUPLE (expected ")")');
            let next = signature[0];
            if (next === ')') {
                signature.shift();
                return res.concat(next);
            }
            let el = _readSingleType(signature);
            res = res.concat(el);
        }
    }

    // Valid types are simple types, arrays, maybes, tuples, dictionary entries and variants
    if (!isSimple && char !== 'v')
        throw new TypeError(`Invalid GVariant signature (${char} is not a valid type)`);

    return [char];
}

function _packVariant(signature, value) {
    if (signature.length === 0)
        throw new TypeError('GVariant signature cannot be empty');

    let char = signature.shift();
    switch (char) {
    case 'b':
        return GLib.Variant.new_boolean(value);
    case 'y':
        return GLib.Variant.new_byte(value);
    case 'n':
        return GLib.Variant.new_int16(value);
    case 'q':
        return GLib.Variant.new_uint16(value);
    case 'i':
        return GLib.Variant.new_int32(value);
    case 'u':
        return GLib.Variant.new_uint32(value);
    case 'x':
        return GLib.Variant.new_int64(value);
    case 't':
        return GLib.Variant.new_uint64(value);
    case 'h':
        return GLib.Variant.new_handle(value);
    case 'd':
        return GLib.Variant.new_double(value);
    case 's':
        return GLib.Variant.new_string(value);
    case 'o':
        return GLib.Variant.new_object_path(value);
    case 'g':
        return GLib.Variant.new_signature(value);
    case 'v':
        return GLib.Variant.new_variant(value);
    case 'm':
        if (value !== null) {
            return GLib.Variant.new_maybe(null, _packVariant(signature, value));
        } else {
            return GLib.Variant.new_maybe(new GLib.VariantType(
                _readSingleType(signature, false).join('')), null);
        }
    case 'a': {
        let arrayType = _readSingleType(signature, false);
        if (arrayType[0] === 's') {
            // special case for array of strings
            return GLib.Variant.new_strv(value);
        }
        if (arrayType[0] === 'y') {
            // special case for array of bytes
            let bytes;
            if (typeof value === 'string') {
                let byteArray = ByteArray.fromString(value);
                if (byteArray[byteArray.length - 1] !== 0)
                    byteArray = Uint8Array.of(...byteArray, 0);
                bytes = ByteArray.toGBytes(byteArray);
            } else {
                bytes = new GLib.Bytes(value);
            }
            return GLib.Variant.new_from_bytes(new GLib.VariantType('ay'),
                bytes, true);
        }

        let arrayValue = [];
        if (arrayType[0] === '{') {
            // special case for dictionaries
            for (let key in value) {
                let copy = [].concat(arrayType);
                let child = _packVariant(copy, [key, value[key]]);
                arrayValue.push(child);
            }
        } else {
            for (let i = 0; i < value.length; i++) {
                let copy = [].concat(arrayType);
                let child = _packVariant(copy, value[i]);
                arrayValue.push(child);
            }
        }
        return GLib.Variant.new_array(new GLib.VariantType(arrayType.join('')), arrayValue);
    }

    case '(': {
        let children = [];
        for (let i = 0; i < value.length; i++) {
            let next = signature[0];
            if (next === ')')
                break;
            children.push(_packVariant(signature, value[i]));
        }

        if (signature[0] !== ')')
            throw new TypeError('Invalid GVariant signature for type TUPLE (expected ")")');
        signature.shift();
        return GLib.Variant.new_tuple(children);
    }
    case '{': {
        let key = _packVariant(signature, value[0]);
        let child = _packVariant(signature, value[1]);

        if (signature[0] !== '}')
            throw new TypeError('Invalid GVariant signature for type DICT_ENTRY (expected "}")');
        signature.shift();

        return GLib.Variant.new_dict_entry(key, child);
    }
    default:
        throw new TypeError(`Invalid GVariant signature (unexpected character ${char})`);
    }
}

function _unpackVariant(variant, deep, recursive = false) {
    switch (String.fromCharCode(variant.classify())) {
    case 'b':
        return variant.get_boolean();
    case 'y':
        return variant.get_byte();
    case 'n':
        return variant.get_int16();
    case 'q':
        return variant.get_uint16();
    case 'i':
        return variant.get_int32();
    case 'u':
        return variant.get_uint32();
    case 'x':
        return variant.get_int64();
    case 't':
        return variant.get_uint64();
    case 'h':
        return variant.get_handle();
    case 'd':
        return variant.get_double();
    case 'o':
    case 'g':
    case 's':
        // g_variant_get_string has length as out argument
        return variant.get_string()[0];
    case 'v': {
        const ret = variant.get_variant();
        if (deep && recursive && ret instanceof GLib.Variant)
            return _unpackVariant(ret, deep, recursive);
        return ret;
    }
    case 'm': {
        let val = variant.get_maybe();
        if (deep && val)
            return _unpackVariant(val, deep, recursive);
        else
            return val;
    }
    case 'a':
        if (variant.is_of_type(new GLib.VariantType('a{?*}'))) {
            // special case containers
            let ret = { };
            let nElements = variant.n_children();
            for (let i = 0; i < nElements; i++) {
                // always unpack the dictionary entry, and always unpack
                // the key (or it cannot be added as a key)
                let val = _unpackVariant(variant.get_child_value(i), deep,
                    recursive);
                let key;
                if (!deep)
                    key = _unpackVariant(val[0], true);
                else
                    key = val[0];
                ret[key] = val[1];
            }
            return ret;
        }
        if (variant.is_of_type(new GLib.VariantType('ay'))) {
            // special case byte arrays
            return variant.get_data_as_bytes().toArray();
        }

        // fall through
    case '(':
    case '{': {
        let ret = [];
        let nElements = variant.n_children();
        for (let i = 0; i < nElements; i++) {
            let val = variant.get_child_value(i);
            if (deep)
                ret.push(_unpackVariant(val, deep, recursive));
            else
                ret.push(val);
        }
        return ret;
    }
    }

    throw new Error('Assertion failure: this code should not be reached');
}

function _notIntrospectableError(funcName, replacement) {
    return new Error(`${funcName} is not introspectable. Use ${replacement} instead.`);
}

function _warnNotIntrospectable(funcName, replacement) {
    logError(_notIntrospectableError(funcName, replacement));
}

function _escapeCharacterSetChars(char) {
    if ('-^]\\'.includes(char))
        return `\\${char}`;
    return char;
}

// For convenience in property min or max values, since GLib.MAXINT64 and
// friends will log a warning when used
GLib.MAXINT64_BIGINT = 0x7fff_ffff_ffff_ffffn;
GLib.MININT64_BIGINT = -GLib.MAXINT64_BIGINT - 1n;
GLib.MAXUINT64_BIGINT = 0xffff_ffff_ffff_ffffn;

// small HACK: we add a matches() method to standard Errors so that
// you can do "if (e.matches(Ns.FooError, Ns.FooError.SOME_CODE))"
// without checking instanceof
Error.prototype.matches = function () {
    return false;
};

// Guard against domains that aren't valid quarks and would lead
// to a crash
const quarkToString = GLib.quark_to_string;
const realNewLiteral = GLib.Error.new_literal;
GLib.Error.new_literal = function (domain, code, message) {
    if (quarkToString(domain) === null)
        throw new TypeError(`Error.new_literal: ${domain} is not a valid domain`);
    return realNewLiteral(domain, code, message);
};

GLib.Variant._new_internal = function (sig, value) {
    let signature = Array.prototype.slice.call(sig);

    let variant = _packVariant(signature, value);
    if (signature.length !== 0)
        throw new TypeError('Invalid GVariant signature (more than one single complete type)');

    return variant;
};

// Deprecate version of new GLib.Variant()
GLib.Variant.new = function (sig, value) {
    return new GLib.Variant(sig, value);
};
GLib.Variant.prototype.unpack = function () {
    return _unpackVariant(this, false);
};
GLib.Variant.prototype.deepUnpack = function () {
    return _unpackVariant(this, true);
};
// backwards compatibility alias
GLib.Variant.prototype.deep_unpack = GLib.Variant.prototype.deepUnpack;

// Note: discards type information, if the variant contains any 'v' types
GLib.Variant.prototype.recursiveUnpack = function () {
    return _unpackVariant(this, true, true);
};

GLib.Variant.prototype.toString = function () {
    return `[object variant of type "${GLib.get_type_string()}"]`;
};

GLib.Bytes.prototype.toArray = function () {
    return imports._byteArrayNative.fromGBytes(this);
};

GLib.log_structured =
    /**
     * @param {string} logDomain
     * @param {GLib.LogLevelFlags} logLevel
     * @param {Record<string, unknown>} stringFields
     * @returns {void}
     */
    function log_structured(logDomain, logLevel, stringFields) {
        /** @type {Record<string, GLib.Variant>} */
        let fields = {};

        for (let key in stringFields) {
            const field = stringFields[key];

            if (field instanceof Uint8Array) {
                fields[key] = new GLib.Variant('ay', field);
            } else if (typeof field === 'string') {
                fields[key] = new GLib.Variant('s', field);
            } else if (field instanceof GLib.Variant) {
                // GLib.log_variant converts all Variants that are
                // not 'ay' or 's' type to strings by printing
                // them.
                //
                // https://gitlab.gnome.org/GNOME/glib/-/blob/a380bfdf93cb3bfd3cd4caedc0127c4e5717545b/glib/gmessages.c#L1894
                fields[key] = field;
            } else {
                throw new TypeError(`Unsupported value ${field}, log_structured supports GLib.Variant, Uint8Array, and string values.`);
            }
        }

        GLib.log_variant(logDomain, logLevel, new GLib.Variant('a{sv}', fields));
    };

GLib.log_set_writer_func_variant = function (...args) {
    log_set_writer_func(...args);
};

GLib.log_set_writer_default = function (...args) {
    log_set_writer_default(...args);
};

GLib.log_set_writer_func = function (writer_func) {
    if (typeof writer_func !== 'function') {
        log_set_writer_func(writer_func);
    } else {
        log_set_writer_func(function (logLevel, stringFields) {
            const stringFieldsObj = {...stringFields.recursiveUnpack()};
            return writer_func(logLevel, stringFieldsObj);
        });
    }
};

GLib.VariantDict.prototype.lookup = function (key, variantType = null, deep = false) {
    if (typeof variantType === 'string')
        variantType = new GLib.VariantType(variantType);

    const variant = this.lookup_value(key, variantType);
    if (variant === null)
        return null;
    return _unpackVariant(variant, deep);
};

// Prevent user code from calling GLib string manipulation functions that
// return the same string that was passed in. These can't be annotated
// properly, and will mostly crash.
// Here we provide approximate implementations of the functions so that if
// they had happened to work in the past, they will continue working, but
// log a stack trace and a suggestion of what to use instead.
// Exceptions are thrown instead for GLib.stpcpy() of which the return value
// is useless anyway and GLib.ascii_formatd() which is too complicated to
// implement here.

GLib.stpcpy = function () {
    throw _notIntrospectableError('GLib.stpcpy()', 'the + operator');
};

GLib.strstr_len = function (haystack, len, needle) {
    _warnNotIntrospectable('GLib.strstr_len()', 'String.indexOf()');
    let searchString = haystack;
    if (len !== -1)
        searchString = searchString.slice(0, len);
    const index = searchString.indexOf(needle);
    if (index === -1)
        return null;
    return haystack.slice(index);
};

GLib.strrstr = function (haystack, needle) {
    _warnNotIntrospectable('GLib.strrstr()', 'String.lastIndexOf()');
    const index = haystack.lastIndexOf(needle);
    if (index === -1)
        return null;
    return haystack.slice(index);
};

GLib.strrstr_len = function (haystack, len, needle) {
    _warnNotIntrospectable('GLib.strrstr_len()', 'String.lastIndexOf()');
    let searchString = haystack;
    if (len !== -1)
        searchString = searchString.slice(0, len);
    const index = searchString.lastIndexOf(needle);
    if (index === -1)
        return null;
    return haystack.slice(index);
};

GLib.strup = function (string) {
    _warnNotIntrospectable('GLib.strup()',
        'String.toUpperCase() or GLib.ascii_strup()');
    return string.toUpperCase();
};

GLib.strdown = function (string) {
    _warnNotIntrospectable('GLib.strdown()',
        'String.toLowerCase() or GLib.ascii_strdown()');
    return string.toLowerCase();
};

GLib.strreverse = function (string) {
    _warnNotIntrospectable('GLib.strreverse()',
        'Array.reverse() and String.join()');
    return [...string].reverse().join('');
};

GLib.ascii_dtostr = function (unused, len, number) {
    _warnNotIntrospectable('GLib.ascii_dtostr()', 'JS string conversion');
    return `${number}`.slice(0, len);
};

GLib.ascii_formatd = function () {
    throw _notIntrospectableError('GLib.ascii_formatd()',
        'Number.toExponential() and string interpolation');
};

GLib.strchug = function (string) {
    _warnNotIntrospectable('GLib.strchug()', 'String.trimStart()');
    return string.trimStart();
};

GLib.strchomp = function (string) {
    _warnNotIntrospectable('GLib.strchomp()', 'String.trimEnd()');
    return string.trimEnd();
};

// g_strstrip() is a macro and therefore doesn't even appear in the GIR
// file, but we may as well include it here since it's trivial
GLib.strstrip = function (string) {
    _warnNotIntrospectable('GLib.strstrip()', 'String.trim()');
    return string.trim();
};

GLib.strdelimit = function (string, delimiters, newDelimiter) {
    _warnNotIntrospectable('GLib.strdelimit()', 'String.replace()');

    if (delimiters === null)
        delimiters = GLib.STR_DELIMITERS;
    if (typeof newDelimiter === 'number')
        newDelimiter = String.fromCharCode(newDelimiter);

    const delimiterChars = delimiters.split('');
    const escapedDelimiterChars = delimiterChars.map(_escapeCharacterSetChars);
    const delimiterRegex = new RegExp(`[${escapedDelimiterChars.join('')}]`, 'g');
    return string.replace(delimiterRegex, newDelimiter);
};

GLib.strcanon = function (string, validChars, substitutor) {
    _warnNotIntrospectable('GLib.strcanon()', 'String.replace()');

    if (typeof substitutor === 'number')
        substitutor = String.fromCharCode(substitutor);

    const validArray = validChars.split('');
    const escapedValidArray = validArray.map(_escapeCharacterSetChars);
    const invalidRegex = new RegExp(`[^${escapedValidArray.join('')}]`, 'g');
    return string.replace(invalidRegex, substitutor);
};