From ae10a483834d2870073b5513badc5b7003bcdade Mon Sep 17 00:00:00 2001 From: Tim-Smart Date: Fri, 12 Mar 2010 21:36:00 +1300 Subject: [PATCH] Initial implementation of process.evalcx --- src/node.cc | 48 +++++++++++++++++++++++++++++++++++++ test/simple/test-eval-cx.js | 27 +++++++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 test/simple/test-eval-cx.js 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