almost a real repl
This commit is contained in:
33
runcode.ts
Normal file
33
runcode.ts
Normal file
@ -0,0 +1,33 @@
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const G: any = {};
|
||||
const context = new Proxy(G, {
|
||||
get: (target, prop) => {
|
||||
return target[prop];
|
||||
},
|
||||
set: (target, prop, value) => {
|
||||
target[prop] = value;
|
||||
return true;
|
||||
},
|
||||
has: () => {
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
export const runCode = (code: string) => {
|
||||
try {
|
||||
new Function(code);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
const fn = new Function("context", `
|
||||
with (context) {
|
||||
${code}
|
||||
}
|
||||
`);
|
||||
return fn(context);
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
export const addToContext = (name: string, value: any) => {
|
||||
G[name] = value;
|
||||
}
|
Reference in New Issue
Block a user