Created
October 18, 2022 10:48
-
-
Save semlinker/15511bb4450f41fc54b21858fa87f970 to your computer and use it in GitHub Desktop.
Proxy API usage scenarios —— Sandbox
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sandbox(code) { | |
code = "with (sandbox) {" + code + "}"; | |
const fn = new Function("sandbox", code); | |
return function (sandbox) { | |
const sandboxProxy = new Proxy(sandbox, { | |
has(target, key) { | |
return true; | |
}, | |
get(target, key) { | |
if (key === Symbol.unscopables) return undefined; | |
return target[key]; | |
}, | |
}); | |
return fn(sandboxProxy); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment