Improving repl more
This commit is contained in:
16
runcode.ts
16
runcode.ts
@ -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;
|
||||
|
Reference in New Issue
Block a user