Allow top level await
This commit is contained in:
11
runcode.ts
11
runcode.ts
@@ -13,6 +13,9 @@ export const getBuiltins = () => {
|
||||
return builtins;
|
||||
}
|
||||
|
||||
// deno-lint-ignore no-explicit-any
|
||||
const AsyncFunction = (async function () {}).constructor as any;
|
||||
|
||||
addToContext("eval", eval);
|
||||
|
||||
const context = new Proxy(G, {
|
||||
@@ -31,18 +34,18 @@ const context = new Proxy(G, {
|
||||
},
|
||||
});
|
||||
|
||||
export const runCode = (code: string) => {
|
||||
export const runCode = async (code: string) => {
|
||||
try {
|
||||
new Function(code);
|
||||
new AsyncFunction(code);
|
||||
} catch (err) {
|
||||
throw err;
|
||||
}
|
||||
const fn = new Function("context", `
|
||||
const fn = new AsyncFunction("context", `
|
||||
with (context) {
|
||||
${code}
|
||||
}
|
||||
`);
|
||||
return fn(context);
|
||||
return await fn(context);
|
||||
}
|
||||
|
||||
export const evalCode = (code: string) => {
|
||||
|
||||
Reference in New Issue
Block a user