summaryrefslogtreecommitdiff
path: root/modules/script/lang.js
blob: 98082995fdc0265cf15a46d511e14141b96a831d (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
/* -*- mode: js; indent-tabs-mode: nil; -*- */
/* exported bind, copyProperties, copyPublicProperties, countProperties, Class,
getMetaClass, Interface */
// SPDX-License-Identifier: MIT OR LGPL-2.0-or-later
// SPDX-FileCopyrightText: 2008 litl, LLC

// Utilities that are "meta-language" things like manipulating object props

var {Class, Interface, getMetaClass} = imports._legacy;

function countProperties(obj) {
    let count = 0;
    for (let unusedProperty in obj)
        count += 1;
    return count;
}

function getPropertyDescriptor(obj, property) {
    if (obj.hasOwnProperty(property))
        return Object.getOwnPropertyDescriptor(obj, property);
    return getPropertyDescriptor(Object.getPrototypeOf(obj), property);
}

function _copyProperty(source, dest, property) {
    let descriptor = getPropertyDescriptor(source, property);
    Object.defineProperty(dest, property, descriptor);
}

function copyProperties(source, dest) {
    for (let property in source)
        _copyProperty(source, dest, property);
}

function copyPublicProperties(source, dest) {
    for (let property in source) {
        if (typeof property === 'string' && property.startsWith('_'))
            continue;
        else
            _copyProperty(source, dest, property);
    }
}

/**
 * Binds obj to callback. Makes it possible to refer to "obj"
 * using this within the callback.
 *
 * @param {object} obj the object to bind
 * @param {Function} callback callback to bind obj in
 * @param {*} bindArguments additional arguments to the callback
 * @returns {Function} a new callback
 */
function bind(obj, callback, ...bindArguments) {
    if (typeof obj !== 'object') {
        throw new Error(`first argument to Lang.bind() must be an object, not ${
            typeof obj}`);
    }

    if (typeof callback !== 'function') {
        throw new Error(`second argument to Lang.bind() must be a function, not ${
            typeof callback}`);
    }

    // Use ES5 Function.prototype.bind, but only if not passing any bindArguments,
    // because ES5 has them at the beginning, not at the end
    if (arguments.length === 2)
        return callback.bind(obj);

    let me = obj;
    return function (...args) {
        args = args.concat(bindArguments);
        return callback.apply(me, args);
    };
}