aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/ScriptingGlobals.ts
diff options
context:
space:
mode:
authorNaafiyan Ahmed <naafiyan@gmail.com>2022-03-17 12:17:27 -0400
committerGitHub <noreply@github.com>2022-03-17 12:17:27 -0400
commit5c874c6829d9957696dfe61014173b6800c864df (patch)
tree23b7206679d500fc039a4302255cf3d6e17b47d3 /src/client/util/ScriptingGlobals.ts
parenta299d1766aa7f4bf3e6bbc3032a39b6b5ae28b4b (diff)
parent5a385e46937a2f79d557b4ee929e78ba78aca8bf (diff)
Merge pull request #55 from brown-dash/speedups2
Speedups2
Diffstat (limited to 'src/client/util/ScriptingGlobals.ts')
-rw-r--r--src/client/util/ScriptingGlobals.ts81
1 files changed, 81 insertions, 0 deletions
diff --git a/src/client/util/ScriptingGlobals.ts b/src/client/util/ScriptingGlobals.ts
new file mode 100644
index 000000000..f151acd81
--- /dev/null
+++ b/src/client/util/ScriptingGlobals.ts
@@ -0,0 +1,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 } = {}; \ No newline at end of file