fantasy-console/runcode.ts
2023-05-03 15:17:27 -07:00

33 lines
574 B
TypeScript

// 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;
}