Improving repl more

This commit is contained in:
dylan
2023-05-04 20:14:48 -07:00
parent dca54e76ec
commit 2a7003b443
7 changed files with 77 additions and 38 deletions

View File

@ -1,5 +1,7 @@
// deno-lint-ignore no-explicit-any
const G: any = {};
const G: any = {
eval: eval,
};
const context = new Proxy(G, {
get: (target, prop) => {
return target[prop];
@ -27,6 +29,18 @@ export const runCode = (code: string) => {
return fn(context);
}
export const evalCode = (code: string) => {
try {
return runCode(`return eval("(${code.replaceAll('"', '\\"')})");`);
} catch (err) {
if (err.name === "SyntaxError") {
return runCode(`return eval("${code.replaceAll('"', '\\"')}");`);
} else {
throw err;
}
}
}
// deno-lint-ignore no-explicit-any
export const addToContext = (name: string, value: any) => {
G[name] = value;