aboutsummaryrefslogtreecommitdiff
path: root/src/client/util/Scripting.ts
diff options
context:
space:
mode:
Diffstat (limited to 'src/client/util/Scripting.ts')
-rw-r--r--src/client/util/Scripting.ts122
1 files changed, 15 insertions, 107 deletions
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts
index ffe60c72e..3b0a47b54 100644
--- a/src/client/util/Scripting.ts
+++ b/src/client/util/Scripting.ts
@@ -1,15 +1,15 @@
-import * as ts from "typescript";
-export { ts };
-
// export const ts = (window as any).ts;
-
// // @ts-ignore
// import * as typescriptlib from '!!raw-loader!../../../node_modules/typescript/lib/lib.d.ts'
// // @ts-ignore
// import * as typescriptes5 from '!!raw-loader!../../../node_modules/typescript/lib/lib.es5.d.ts'
-
// @ts-ignore
import * as typescriptlib from '!!raw-loader!./type_decls.d';
+import * as ts from "typescript";
+import { Doc, Field } from "../../fields/Doc";
+import { scriptingGlobals, ScriptingGlobals } from "./ScriptingGlobals";
+export { ts };
+
export interface ScriptSuccess {
success: true;
@@ -46,98 +46,6 @@ export function isCompileError(toBeDetermined: CompileResult): toBeDetermined is
return false;
}
-export namespace Scripting {
- export function addGlobal(global: { name: string }): void;
- export function addGlobal(name: string, global: any): void;
-
- export function addGlobal(global: { name: string }, decription?: string, params?: string): void;
-
- export function addGlobal(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 }) {
- Scripting.addGlobal(constructor);
-}
-
-export const _scriptingGlobals: { [name: string]: any } = {};
-let scriptingGlobals: { [name: string]: any } = _scriptingGlobals;
-const _scriptingDescriptions: { [name: string]: any } = {};
-const _scriptingParams: { [name: string]: any } = {};
function Run(script: string | undefined, customParams: string[], diagnostics: any[], originalScript: string, options: ScriptOptions): CompileResult {
const errors = diagnostics.filter(diag => diag.category === ts.DiagnosticCategory.Error);
@@ -168,19 +76,19 @@ function Run(script: string | undefined, customParams: string[], diagnostics: an
let batch: { end(): void } | undefined = undefined;
try {
if (!options.editable) {
- // batch = Doc.MakeReadOnly();
+ batch = Doc.MakeReadOnly();
}
const result = compiledFunction.apply(thisParam, params).apply(thisParam, argsArray);
if (batch) {
- //batch.end();
+ batch.end();
}
return { success: true, result };
} catch (error) {
if (batch) {
- //batch.end();
+ batch.end();
}
onError?.(script + " " + error);
return { success: false, error, result: errorVal };
@@ -246,13 +154,13 @@ export type Traverser = (node: ts.Node, indentation: string) => boolean | void;
export type TraverserParam = Traverser | { onEnter: Traverser, onLeave: Traverser };
export type Transformer = {
transformer: ts.TransformerFactory<ts.SourceFile>,
- getVars?: () => { capturedVariables: { [name: string]: any /* Field*/ } }
+ getVars?: () => { capturedVariables: { [name: string]: Field } }
};
export interface ScriptOptions {
requiredType?: string; // does function required a typed return value
addReturn?: boolean; // does the compiler automatically add a return statement
params?: { [name: string]: string }; // list of function parameters and their types
- capturedVariables?: { [name: string]: any /* Field */ }; // list of captured variables
+ capturedVariables?: { [name: string]: Field }; // list of captured variables
typecheck?: boolean; // should the compiler perform typechecking
editable?: boolean; // can the script edit Docs
traverser?: TraverserParam;
@@ -269,10 +177,10 @@ function forEachNode(node: ts.Node, onEnter: Traverser, onExit?: Traverser, inde
export function CompileScript(script: string, options: ScriptOptions = {}): CompileResult {
const { requiredType = "", addReturn = false, params = {}, capturedVariables = {}, typecheck = true } = options;
- if (options.params && !options.params.this) options.params.this = "Doc";//Doc.name;
- if (options.params && !options.params.self) options.params.self = "Doc";//Doc.name;
+ if (options.params && !options.params.this) options.params.this = Doc.name;
+ if (options.params && !options.params.self) options.params.self = Doc.name;
if (options.globals) {
- Scripting.setScriptingGlobals(options.globals);
+ ScriptingGlobals.setScriptingGlobals(options.globals);
}
const host = new ScriptingCompilerHost;
if (options.traverser) {
@@ -330,9 +238,9 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
const result = Run(outputText, paramNames, diagnostics, script, options);
if (options.globals) {
- Scripting.resetScriptingGlobals();
+ ScriptingGlobals.resetScriptingGlobals();
}
return result;
}
-Scripting.addGlobal(CompileScript); \ No newline at end of file
+ScriptingGlobals.add(CompileScript);