Browse Source

Make evalcx work like it's supposed to.

1. Move the context->Enter() call so that the global obj is available for writing.
2. On success, copy the modified global out to the sandbox object.
3. Don't copy functions in either direction.  They have scope and closures, and make for craziness when trying to keep contexts separate.
4. Only do the ->ToObject->Clone() on objects, so that simple values stay simple.
5. Update the test so that it tests all this stuff.
v0.7.4-release
isaacs 15 years ago
committed by Ryan Dahl
parent
commit
943b2c61a8
  1. 24
      src/node.cc
  2. 17
      test/simple/test-eval-cx.js

24
src/node.cc

@ -862,6 +862,9 @@ Handle<Value> EvalCX(const Arguments& args) {
// Create the new context // Create the new context
Persistent<Context> context = Context::New(); Persistent<Context> context = Context::New();
// Enter and compile script
context->Enter();
// Copy objects from global context, to our brand new context // Copy objects from global context, to our brand new context
Handle<Array> keys = sandbox->GetPropertyNames(); Handle<Array> keys = sandbox->GetPropertyNames();
@ -869,11 +872,12 @@ Handle<Value> EvalCX(const Arguments& args) {
for (i = 0; i < keys->Length(); i++) { for (i = 0; i < keys->Length(); i++) {
Handle<String> key = keys->Get(Integer::New(i))->ToString(); Handle<String> key = keys->Get(Integer::New(i))->ToString();
Handle<Value> value = sandbox->Get(key); Handle<Value> value = sandbox->Get(key);
context->Global()->Set(key, value->ToObject()->Clone()); if (value->IsFunction()) continue;
if (value->IsObject()) {
value = value->ToObject()->Clone();
}
context->Global()->Set(key, value);
} }
// Enter and compile script
context->Enter();
// Catch errors // Catch errors
TryCatch try_catch; TryCatch try_catch;
@ -887,6 +891,18 @@ Handle<Value> EvalCX(const Arguments& args) {
result = script->Run(); result = script->Run();
if (result.IsEmpty()) { if (result.IsEmpty()) {
result = ThrowException(try_catch.Exception()); result = ThrowException(try_catch.Exception());
} else {
// success! copy changes back onto the sandbox object.
keys = context->Global()->GetPropertyNames();
for (i = 0; i < keys->Length(); i++) {
Handle<String> key = keys->Get(Integer::New(i))->ToString();
Handle<Value> value = context->Global()->Get(key);
if (value->IsFunction()) continue;
if (value->IsObject()) {
value = value->ToObject()->Clone();
}
sandbox->Set(key, value);
}
} }
} }

17
test/simple/test-eval-cx.js

@ -14,14 +14,19 @@ process.evalcx('hello = 2');
assert.equal(5, hello); assert.equal(5, hello);
code = "foo = 1; bar = 2;"; code = "foo = 1;"
foo = 2; + "bar = 2;"
obj = { foo : 0 }; + "if (baz !== 3) throw new Error('test fail');"
process.evalcx(code, obj); + "quux.pwned = true;";
/* TODO? foo = 2;
var quux = { pwned : false };
obj = { foo : 0, baz : 3, quux : quux };
var baz = process.evalcx(code, obj);
assert.equal(1, obj.foo); assert.equal(1, obj.foo);
assert.equal(2, obj.bar); assert.equal(2, obj.bar);
*/ assert.equal(obj.quux.pwned, true);
assert.equal(quux.pwned, false);
assert.notEqual(quux, obj.quux);
assert.equal(2, foo); assert.equal(2, foo);

Loading…
Cancel
Save