aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/client/util/Scripting.ts7
-rw-r--r--src/debug/Repl.tsx58
-rw-r--r--src/new_fields/Doc.ts15
3 files changed, 76 insertions, 4 deletions
diff --git a/src/client/util/Scripting.ts b/src/client/util/Scripting.ts
index e45f61c11..beaf5cb03 100644
--- a/src/client/util/Scripting.ts
+++ b/src/client/util/Scripting.ts
@@ -41,7 +41,7 @@ export type CompileResult = CompiledScript | CompileError;
function Run(script: string | undefined, customParams: string[], diagnostics: any[], originalScript: string, options: ScriptOptions): CompileResult {
const errors = diagnostics.some(diag => diag.category === ts.DiagnosticCategory.Error);
- if (errors || !script) {
+ if ((options.typecheck !== false && errors) || !script) {
return { compiled: false, errors: diagnostics };
}
@@ -131,10 +131,11 @@ export interface ScriptOptions {
addReturn?: boolean;
params?: { [name: string]: string };
capturedVariables?: { [name: string]: Field };
+ typecheck?: boolean;
}
export function CompileScript(script: string, options: ScriptOptions = {}): CompileResult {
- const { requiredType = "", addReturn = false, params = {}, capturedVariables = {} } = options;
+ const { requiredType = "", addReturn = false, params = {}, capturedVariables = {}, typecheck = true } = options;
let host = new ScriptingCompilerHost;
let paramNames: string[] = [];
if ("this" in params || "this" in capturedVariables) {
@@ -158,7 +159,7 @@ export function CompileScript(script: string, options: ScriptOptions = {}): Comp
${addReturn ? `return ${script};` : script}
})`;
host.writeFile("file.ts", funcScript);
- host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib);
+ if (typecheck) host.writeFile('node_modules/typescript/lib/lib.d.ts', typescriptlib);
let program = ts.createProgram(["file.ts"], {}, host);
let testResult = program.emit();
let outputText = host.readFile("file.js");
diff --git a/src/debug/Repl.tsx b/src/debug/Repl.tsx
new file mode 100644
index 000000000..16aef1925
--- /dev/null
+++ b/src/debug/Repl.tsx
@@ -0,0 +1,58 @@
+import * as React from 'react';
+import * as ReactDOM from 'react-dom';
+import { observer } from 'mobx-react';
+import { observable, computed } from 'mobx';
+import { CompileScript } from '../client/util/Scripting';
+
+@observer
+class Repl extends React.Component {
+ @observable text: string = "";
+
+ @observable executedCommands: { command: string, result: any }[] = [];
+
+ onChange = (e: React.ChangeEvent<HTMLInputElement>) => {
+ this.text = e.target.value;
+ }
+
+ onKeyDown = (e: React.KeyboardEvent) => {
+ if (e.key === "Enter") {
+ const script = CompileScript(this.text, { addReturn: true, typecheck: false });
+ if (!script.compiled) {
+ this.executedCommands.push({ command: this.text, result: "Compile Error" });
+ } else {
+ const result = script.run();
+ if (result.success) {
+ this.executedCommands.push({ command: this.text, result: result.result });
+ } else {
+ this.executedCommands.push({ command: this.text, result: result.error });
+ }
+ }
+ this.text = "";
+ }
+ }
+
+ @computed
+ get commands() {
+ return this.executedCommands.map(command => {
+ return (
+ <div style={{ marginTop: "5px" }}>
+ <p>{command.command}</p>
+ <p>{JSON.stringify(command.result)}</p>
+ </div>
+ );
+ });
+ }
+
+ render() {
+ return (
+ <div>
+ <div style={{ verticalAlign: "bottom" }}>
+ {this.commands}
+ </div>
+ <input style={{ width: "100%", position: "absolute", bottom: "0px" }} value={this.text} onChange={this.onChange} onKeyDown={this.onKeyDown} />
+ </div>
+ );
+ }
+}
+
+ReactDOM.render(<Repl />, document.getElementById("root")); \ No newline at end of file
diff --git a/src/new_fields/Doc.ts b/src/new_fields/Doc.ts
index 793a83750..0c74b8f65 100644
--- a/src/new_fields/Doc.ts
+++ b/src/new_fields/Doc.ts
@@ -62,6 +62,7 @@ export class Doc extends RefField {
const doc = new Proxy<this>(this, {
set: setter,
get: getter,
+ // getPrototypeOf: (target) => Cast(target[SelfProxy].proto, Doc) || null, // TODO this might be able to replace the proto logic in getter
has: (target, key) => key in target.__fields,
ownKeys: target => Object.keys(target.__fields),
getOwnPropertyDescriptor: (target, prop) => {
@@ -69,6 +70,7 @@ export class Doc extends RefField {
return {
configurable: true,//TODO Should configurable be true?
enumerable: true,
+ value: target.__fields[prop]
};
}
return Reflect.getOwnPropertyDescriptor(target, prop);
@@ -197,6 +199,18 @@ export namespace Doc {
return Doc.GetT(doc, "isPrototype", "boolean", true) ? doc : doc.proto!;
}
+ export function allKeys(doc: Doc): string[] {
+ const results: Set<string> = new Set;
+
+ let proto: Doc | undefined = doc;
+ while (proto) {
+ Object.keys(proto).forEach(key => results.add(key));
+ proto = proto.proto;
+ }
+
+ return Array.from(results);
+ }
+
export function MakeAlias(doc: Doc) {
const proto = Object.getOwnPropertyNames(doc).indexOf("isPrototype") === -1 ? doc.proto : undefined;
@@ -246,5 +260,4 @@ export namespace Doc {
delegate.proto = doc;
return delegate;
}
- export const Prototype = Symbol("Prototype");
} \ No newline at end of file