MCPcopy Create free account
hub / github.com/microsoft/vscode-cpptools / createSandbox

Function createSandbox

Extension/src/Utility/Sandbox/sandbox.ts:14–37  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

12 * Creates a reusable safe-eval sandbox to execute code in.
13 */
14export function createSandbox(): <T>(code: string, context?: any) => T {
15 const sandbox = createContext({});
16 return (code: string, context?: any) => {
17 const response = `SAFE_EVAL_${Math.floor(Math.random() * 1000000)}`;
18 sandbox[response] = {};
19 if (context) {
20 Object.keys(context).forEach((key) => sandbox[key] = context[key]);
21 runInContext(
22 `try { ${response} = ${code} } catch (e) { ${response} = undefined }`,
23 sandbox
24 );
25 for (const key of Object.keys(context)) {
26 delete sandbox[key];
27 }
28 } else {
29 try {
30 runInContext(`${response} = ${code}`, sandbox);
31 } catch {
32 sandbox[response] = undefined;
33 }
34 }
35 return sandbox[response];
36 };
37}
38
39export const safeEval = createSandbox();
40

Callers 1

sandbox.tsFile · 0.85

Calls 2

keysMethod · 0.80
forEachMethod · 0.45

Tested by

no test coverage detected