mirror of
https://github.com/talwrii/plugin-repl.git
synced 2026-07-22 11:40:27 +00:00
35 lines
762 B
JavaScript
35 lines
762 B
JavaScript
function evalInScope (s, context) {
|
|
let _result;
|
|
context["_s"] = s
|
|
with (makeScope(context)) {
|
|
if (/^ *\{/.test(_s)) {
|
|
// object literal
|
|
_result = eval("(" + _s + ")")
|
|
} else {
|
|
_result = eval(_s)
|
|
}
|
|
|
|
return _result
|
|
}
|
|
}
|
|
|
|
function runInScope(f, context) {
|
|
let _result;
|
|
context["f"] = f
|
|
with (makeScope(context)) {
|
|
_result = f()
|
|
return _result
|
|
}
|
|
}
|
|
|
|
|
|
function makeScope (target) {
|
|
return new Proxy(target, {
|
|
has(target, prop) { return true; },
|
|
get(target, prop) { return (prop in target ? target : window)[prop]; }
|
|
});
|
|
}
|
|
|
|
exports["evalInScope"] = evalInScope
|
|
exports["makeScope"] = makeScope
|
|
exports["runInScope"] = runInScope
|