Reviewers: Kasper Lund,

Description:
Make sure that the prototype of the initial map is created in the
right context.


Please review this at http://codereview.chromium.org/18591

SVN Base: http://v8.googlecode.com/svn/branches/bleeding_edge/

Affected files:
   M     src/heap.cc
   M     test/cctest/test-api.cc


Index: test/cctest/test-api.cc
===================================================================
--- test/cctest/test-api.cc     (revision 1154)
+++ test/cctest/test-api.cc     (working copy)
@@ -5607,3 +5607,36 @@
      CompileRun("for (var j = 0; j < 10; j++) RegExp('')");
    }
  }
+
+
+// Test that cross-context new calls use the context of the callee to
+// create the new JavaScript object.
+THREADED_TEST(CrossContextNew) {
+  v8::HandleScope scope;
+  v8::Persistent<Context> context0 = Context::New();
+  v8::Persistent<Context> context1 = Context::New();
+
+  // Allow cross-domain access.
+  Local<String> token = v8_str("<security token>");
+  context0->SetSecurityToken(token);
+  context1->SetSecurityToken(token);
+
+  // Set an 'x' property on the Object prototype and define a
+  // constructor function in context0.
+  context0->Enter();
+  CompileRun("Object.prototype.x = 42; function C() {};");
+  context0->Exit();
+
+  // Call the constructor function from context0 and check that the
+  // result has the 'x' property.
+  context1->Enter();
+  context1->Global()->Set(v8_str("other"), context0->Global());
+  Local<Value> value = CompileRun("var instance = new other.C();  
instance.x");
+  CHECK(value->IsInt32());
+  CHECK(value->Int32Value() == 42);
+  context1->Exit();
+
+  // Dispose the contexts to allow them to be garbage collected.
+  context0.Dispose();
+  context1.Dispose();
+}
Index: src/heap.cc
===================================================================
--- src/heap.cc (revision 1154)
+++ src/heap.cc (working copy)
@@ -1724,9 +1724,11 @@


  Object* Heap::AllocateFunctionPrototype(JSFunction* function) {
-  // Allocate the prototype.
+  // Allocate the prototype.  Make sure to use the object function
+  // from the function's context, since the function can be from a
+  // different context.
    Object* prototype =
-       
AllocateJSObject(Top::context()->global_context()->object_function());
+       
AllocateJSObject(function->context()->global_context()->object_function());
    if (prototype->IsFailure()) return prototype;
    // When creating the prototype for the function we must set its
    // constructor to the function.



--~--~---------~--~----~------------~-------~--~----~
v8-dev mailing list
[email protected]
http://groups.google.com/group/v8-dev
-~----------~----~----~----~------~----~------~--~---

Reply via email to