Reviewers: Mads Ager,

Message:
Mads,

may you have a look?

I used it for my experiments. But yesterday Aaron (cc'ed) independently showed
an interest in such an API.

Description:
Introduce v8::Object::CreationContext method.

That allows to find out a global context in which the object
was created.


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

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

Affected files:
  M include/v8.h
  M src/api.cc
  M test/cctest/test-api.cc


Index: include/v8.h
diff --git a/include/v8.h b/include/v8.h
index 62d1085c20e6c4a7665c6e4559a8198816b56812..9cc20b45b5534e65f910e81e412d4c3d570617a3 100644
--- a/include/v8.h
+++ b/include/v8.h
@@ -1653,6 +1653,11 @@ class Object : public Value {
   V8EXPORT Local<Object> Clone();

   /**
+   * Returns a context in which the object was created.
+   */
+  V8EXPORT Local<Context> CreationContext();
+
+  /**
    * Set the backing store of the indexed properties to be managed by the
* embedding layer. Access to the indexed properties will follow the rules
    * spelled out in CanvasPixelArray.
Index: src/api.cc
diff --git a/src/api.cc b/src/api.cc
index 6c04687df5be900adf8f559778848fca0052d2b1..bc4fed2acd2722ddfba5ef2cd6219b6d1717e186 100644
--- a/src/api.cc
+++ b/src/api.cc
@@ -2879,6 +2879,33 @@ Local<v8::Object> v8::Object::Clone() {
 }


+static i::Context* creationContext(i::JSObject* object) {
+  i::Object* constructor = object->map()->constructor();
+  i::JSFunction* function;
+  if (!constructor->IsJSFunction()) {
+    // API functions have null as a constructor,
+    // but any JSFunction knows its context immediately.
+    ASSERT(object->IsJSFunction() &&
+           i::JSFunction::cast(object)->shared()->IsApiFunction());
+    function = i::JSFunction::cast(object);
+  } else {
+    function = i::JSFunction::cast(constructor);
+  }
+  return function->context()->global_context();
+}
+
+
+Local<v8::Context> v8::Object::CreationContext() {
+  i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
+  ON_BAILOUT(isolate,
+             "v8::Object::CreationContext()", return Local<v8::Context>());
+  ENTER_V8(isolate);
+  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+  i::Context* context = creationContext(*self);
+  return Utils::ToLocal(i::Handle<i::Context>(context));
+}
+
+
 int v8::Object::GetIdentityHash() {
   i::Isolate* isolate = Utils::OpenHandle(this)->GetIsolate();
   ON_BAILOUT(isolate, "v8::Object::GetIdentityHash()", return 0);
Index: test/cctest/test-api.cc
diff --git a/test/cctest/test-api.cc b/test/cctest/test-api.cc
index 33d505eaaa2640c1cb5018c5e60c521602a46a72..da9fed9b488d0ca04a641ac81f9c72eee38bb8fd 100644
--- a/test/cctest/test-api.cc
+++ b/test/cctest/test-api.cc
@@ -13629,3 +13629,40 @@ TEST(DefinePropertyPostDetach) {
   context->DetachGlobal();
   define_property->Call(proxy, 0, NULL);
 }
+
+
+THREADED_TEST(CreationContext) {
+  HandleScope handle_scope;
+  Persistent<Context> context1 = Context::New();
+  Persistent<Context> context2 = Context::New();
+
+  Local<Object> object1;
+  {
+    Context::Scope scope(context1);
+    object1 = Object::New();
+  }
+
+  Local<Object> object2;
+  {
+    Context::Scope scope(context2);
+    object2 = Object::New();
+  }
+
+  CHECK(object1->CreationContext() == context1);
+  CHECK(object2->CreationContext() == context2);
+
+  {
+    Context::Scope scope(context1);
+    CHECK(object1->CreationContext() == context1);
+    CHECK(object2->CreationContext() == context2);
+  }
+
+  {
+    Context::Scope scope(context2);
+    CHECK(object1->CreationContext() == context1);
+    CHECK(object2->CreationContext() == context2);
+  }
+
+  context1.Dispose();
+  context2.Dispose();
+}


--
v8-dev mailing list
v8-dev@googlegroups.com
http://groups.google.com/group/v8-dev

Reply via email to