diff --git a/src/node.cc b/src/node.cc index 25f249fa7b..8cb5e64ed8 100644 --- a/src/node.cc +++ b/src/node.cc @@ -846,6 +846,53 @@ Handle DLOpen(const v8::Arguments& args) { return Undefined(); } +// evalcx(code, sandbox={}) +// Executes code in a new context +Handle EvalCX(const Arguments& args) { + HandleScope scope; + + Local code = args[0]->ToString(); + Local sandbox = args.Length() > 1 ? args[1]->ToObject() + : Object::New(); + // Create the new context + Persistent context = Context::New(); + + // Copy objects from global context, to our brand new context + Handle keys = sandbox->GetPropertyNames(); + + int i; + for (i = 0; i < keys->Length(); i++) { + Handle key = keys->Get(Integer::New(i))->ToString(); + Handle value = sandbox->Get(key); + context->Global()->Set(key, value->ToObject()->Clone()); + } + + // Enter and compile script + context->Enter(); + + // Catch errors + TryCatch try_catch; + + Local