aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ScriptingGlobals.ts
blob: f151acd812d11edb53ab3249b3685c3f8e1cb966 (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
import * as ts from "typescript";
export { ts };

export namespace ScriptingGlobals {
    export function add(global: { name: string }): void;
    export function add(name: string, global: any): void;
    export function add(global: { name: string }, decription?: string, params?: string): void;
    export function add(first: any, second?: any, third?: string) {
        let n: any;
        let obj: any;

        if (second !== undefined) {
            if (typeof first === "string") {
                n = first;
                obj = second;
            } else {
                obj = first;
                n = first.name;
                _scriptingDescriptions[n] = second;
                if (third !== undefined) {
                    _scriptingParams[n] = third;
                }
            }
        } else if (first && typeof first.name === "string") {
            n = first.name;
            obj = first;
        } else {
            throw new Error("Must either register an object with a name, or give a name and an object");
        }
        if (n === undefined || n === "undefined") {
            return false;
        } else if (_scriptingGlobals.hasOwnProperty(n)) {
            throw new Error(`Global with name ${n} is already registered, choose another name`);
        }
        _scriptingGlobals[n] = obj;
    }
    export function makeMutableGlobalsCopy(globals?: { [name: string]: any }) {
        return { ..._scriptingGlobals, ...(globals || {}) };
    }

    export function setScriptingGlobals(globals: { [key: string]: any }) {
        scriptingGlobals = globals;
    }

    export function removeGlobal(name: string) {
        if (getGlobals().includes(name)) {
            delete _scriptingGlobals[name];
            if (_scriptingDescriptions[name]) {
                delete _scriptingDescriptions[name];
            }
            if (_scriptingParams[name]) {
                delete _scriptingParams[name];
            }
            return true;
        }
        return false;
    }

    export function resetScriptingGlobals() { scriptingGlobals = _scriptingGlobals; }

    // const types = Object.keys(ts.SyntaxKind).map(kind => ts.SyntaxKind[kind]);
    export function printNodeType(node: any, indentation = "") { console.log(indentation + ts.SyntaxKind[node.kind]); }

    export function getGlobals() { return Object.keys(_scriptingGlobals); }

    export function getGlobalObj() { return _scriptingGlobals; }

    export function getDescriptions() { return _scriptingDescriptions; }

    export function getParameters() { return _scriptingParams; }
}

export function scriptingGlobal(constructor: { new(...args: any[]): any }) {
    ScriptingGlobals.add(constructor);
}

const _scriptingGlobals: { [name: string]: any } = {};
export let scriptingGlobals: { [name: string]: any } = _scriptingGlobals;
const _scriptingDescriptions: { [name: string]: any } = {};
const _scriptingParams: { [name: string]: any } = {};