more precise evaluator
This commit is contained in:
@ -1,31 +1,48 @@
|
|||||||
import type { Editor } from './main';
|
import type { Editor } from './main';
|
||||||
import type { File } from './AppSettings';
|
import type { File } from './AppSettings';
|
||||||
|
|
||||||
/* This mode of evaluation can only work if the whole buffer is evaluated at once */
|
const delay = (ms: number) => new Promise((resolve, reject) => setTimeout(() => reject(new Error('Operation took too long')), ms));
|
||||||
export const tryEvaluate = (application: Editor, code: File): void => {
|
|
||||||
let isValidCode: boolean;
|
|
||||||
try {
|
|
||||||
Function(`with (this) {try{${code.candidate}} catch (e) {console.log(e)}};`).call(application.api)
|
|
||||||
code.evaluations++;
|
|
||||||
isValidCode = true;
|
|
||||||
} catch (error) {
|
|
||||||
Function(`with (this) {try{${code.committed}} catch (e) {console.log(e)}};`).call(application.api)
|
|
||||||
code.evaluations++;
|
|
||||||
isValidCode = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (isValidCode) {
|
const tryCatchWrapper = (application: Editor, code: string): Promise<boolean> => {
|
||||||
|
return new Promise((resolve, reject) => {
|
||||||
|
try {
|
||||||
|
Function(`with (this) {try{${code}} catch (e) {console.log(e)}};`).call(application.api);
|
||||||
|
resolve(true);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
resolve(false);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
export const tryEvaluate = async (application: Editor, code: File, timeout = 5000): Promise<void> => {
|
||||||
|
try {
|
||||||
|
const isCodeValid = await Promise.race([tryCatchWrapper(application, code.candidate), delay(timeout)]);
|
||||||
|
code.evaluations++;
|
||||||
|
|
||||||
|
if (isCodeValid) {
|
||||||
code.committed = code.candidate;
|
code.committed = code.candidate;
|
||||||
} else {
|
} else {
|
||||||
evaluate(application, code);
|
await evaluate(application, code, timeout);
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const evaluate = (application: Editor, code: File): void => {
|
export const evaluate = async (application: Editor, code: File, timeout = 5000): Promise<void> => {
|
||||||
Function(`with (this) {try{${code.committed}} catch (e) {console.log(e)}};`).call(application.api)
|
try {
|
||||||
|
await Promise.race([tryCatchWrapper(application, code.committed), delay(timeout)]);
|
||||||
code.evaluations++;
|
code.evaluations++;
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export const evaluateCommand = (application: Editor, command: string): void => {
|
export const evaluateCommand = async (application: Editor, command: string, timeout = 5000): Promise<void> => {
|
||||||
Function(`with (this) {try{${command}} catch (e) {console.log(e)}};`).call(application.api)
|
try {
|
||||||
|
await Promise.race([tryCatchWrapper(application, command), delay(timeout)]);
|
||||||
|
} catch (error) {
|
||||||
|
console.log(error);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user