Author: [email protected]
Date: Mon Jan 26 07:09:44 2009
New Revision: 1157

Modified:
    branches/bleeding_edge/src/heap.cc
    branches/bleeding_edge/test/cctest/test-api.cc

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

Review URL: http://codereview.chromium.org/18591

Modified: branches/bleeding_edge/src/heap.cc
==============================================================================
--- branches/bleeding_edge/src/heap.cc  (original)
+++ branches/bleeding_edge/src/heap.cc  Mon Jan 26 07:09:44 2009
@@ -1724,9 +1724,12 @@


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

Modified: branches/bleeding_edge/test/cctest/test-api.cc
==============================================================================
--- branches/bleeding_edge/test/cctest/test-api.cc      (original)
+++ branches/bleeding_edge/test/cctest/test-api.cc      Mon Jan 26 07:09:44 2009
@@ -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_EQ(42, value->Int32Value());
+  context1->Exit();
+
+  // Dispose the contexts to allow them to be garbage collected.
+  context0.Dispose();
+  context1.Dispose();
+}

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

Reply via email to