Revision: 5890
Author: [email protected]
Date: Thu Nov 25 00:04:12 2010
Log: Expose a method for getting JSObject constructor name
Review URL: http://codereview.chromium.org/5256004
http://code.google.com/p/v8/source/detail?r=5890

Modified:
 /branches/bleeding_edge/include/v8.h
 /branches/bleeding_edge/src/api.cc
 /branches/bleeding_edge/src/heap-profiler.cc
 /branches/bleeding_edge/src/objects.cc
 /branches/bleeding_edge/src/profile-generator.cc
 /branches/bleeding_edge/src/profile-generator.h
 /branches/bleeding_edge/test/cctest/test-api.cc

=======================================
--- /branches/bleeding_edge/include/v8.h        Mon Nov 15 05:23:30 2010
+++ /branches/bleeding_edge/include/v8.h        Thu Nov 25 00:04:12 2010
@@ -1539,6 +1539,11 @@
    */
   V8EXPORT Local<String> ObjectProtoToString();

+  /**
+ * Returns the name of the function invoked as a constructor for this object.
+   */
+  V8EXPORT Local<String> GetConstructorName();
+
   /** Gets the number of internal fields for this Object. */
   V8EXPORT int InternalFieldCount();
   /** Gets the value in an internal field. */
=======================================
--- /branches/bleeding_edge/src/api.cc  Mon Nov 22 06:00:40 2010
+++ /branches/bleeding_edge/src/api.cc  Thu Nov 25 00:04:12 2010
@@ -2449,6 +2449,15 @@
     }
   }
 }
+
+
+Local<String> v8::Object::GetConstructorName() {
+ ON_BAILOUT("v8::Object::GetConstructorName()", return Local<v8::String>());
+  ENTER_V8;
+  i::Handle<i::JSObject> self = Utils::OpenHandle(this);
+  i::Handle<i::String> name(self->constructor_name());
+  return Utils::ToLocal(name);
+}


 bool v8::Object::Delete(v8::Handle<String> key) {
=======================================
--- /branches/bleeding_edge/src/heap-profiler.cc        Wed Nov 24 02:47:18 2010
+++ /branches/bleeding_edge/src/heap-profiler.cc        Thu Nov 25 00:04:12 2010
@@ -69,7 +69,8 @@
JSObjectsCluster Clusterizer::Clusterize(HeapObject* obj, bool fine_grain) {
   if (obj->IsJSObject()) {
     JSObject* js_obj = JSObject::cast(obj);
-    String* constructor = JSObject::cast(js_obj)->constructor_name();
+    String* constructor = GetConstructorNameForHeapProfile(
+        JSObject::cast(js_obj));
     // Differentiate Object and Array instances.
     if (fine_grain && (constructor == Heap::Object_symbol() ||
                        constructor == Heap::Array_symbol())) {
@@ -714,7 +715,7 @@

 static void PrintProducerStackTrace(Object* obj, void* trace) {
   if (!obj->IsJSObject()) return;
-  String* constructor = JSObject::cast(obj)->constructor_name();
+ String* constructor = GetConstructorNameForHeapProfile(JSObject::cast(obj));
   SmartPointer<char> s_name(
       constructor->ToCString(DISALLOW_NULLS, ROBUST_STRING_TRAVERSAL));
   LOG(HeapSampleJSProducerEvent(GetConstructorName(*s_name),
@@ -886,7 +887,8 @@
     return JSObjectsCluster(String::cast(object));
   } else {
     JSObject* js_obj = JSObject::cast(object);
-    String* constructor = JSObject::cast(js_obj)->constructor_name();
+    String* constructor = GetConstructorNameForHeapProfile(
+        JSObject::cast(js_obj));
     return JSObjectsCluster(constructor, object);
   }
 }
=======================================
--- /branches/bleeding_edge/src/objects.cc      Tue Nov 16 00:01:45 2010
+++ /branches/bleeding_edge/src/objects.cc      Thu Nov 25 00:04:12 2010
@@ -1166,9 +1166,6 @@


 String* JSObject::constructor_name() {
-  if (IsJSFunction()) {
-    return Heap::closure_symbol();
-  }
   if (map()->constructor()->IsJSFunction()) {
     JSFunction* constructor = JSFunction::cast(map()->constructor());
     String* name = String::cast(constructor->shared()->name());
=======================================
--- /branches/bleeding_edge/src/profile-generator.cc Wed Nov 24 02:47:18 2010 +++ /branches/bleeding_edge/src/profile-generator.cc Thu Nov 25 00:04:12 2010
@@ -1295,8 +1295,8 @@
   } else if (object->IsJSObject()) {
     return AddEntry(object,
                     HeapEntry::kObject,
-                    collection_->GetName(
-                        JSObject::cast(object)->constructor_name()),
+                    collection_->GetName(GetConstructorNameForHeapProfile(
+                        JSObject::cast(object))),
                     children_count,
                     retainers_count);
   } else if (object->IsString()) {
@@ -2768,6 +2768,12 @@
     sorted_entries->Add(p);
   sorted_entries->Sort(SortUsingEntryValue);
 }
+
+
+String* GetConstructorNameForHeapProfile(JSObject* object) {
+  if (object->IsJSFunction()) return Heap::closure_symbol();
+  return object->constructor_name();
+}

 } }  // namespace v8::internal

=======================================
--- /branches/bleeding_edge/src/profile-generator.h     Wed Nov 24 02:47:18 2010
+++ /branches/bleeding_edge/src/profile-generator.h     Thu Nov 25 00:04:12 2010
@@ -1073,6 +1073,9 @@
   DISALLOW_COPY_AND_ASSIGN(HeapSnapshotJSONSerializer);
 };

+
+String* GetConstructorNameForHeapProfile(JSObject* object);
+
 } }  // namespace v8::internal

 #endif  // ENABLE_LOGGING_AND_PROFILING
=======================================
--- /branches/bleeding_edge/test/cctest/test-api.cc     Tue Nov 23 03:46:36 2010
+++ /branches/bleeding_edge/test/cctest/test-api.cc     Thu Nov 25 00:04:12 2010
@@ -7817,6 +7817,31 @@
   value = object.As<v8::Object>()->ObjectProtoToString();
   CHECK(value->IsString() && value->Equals(v8_str("[object Object]")));
 }
+
+
+THREADED_TEST(ObjectGetConstructorName) {
+  v8::HandleScope scope;
+  LocalContext context;
+  v8_compile("function Parent() {};"
+             "function Child() {};"
+             "Child.prototype = new Parent();"
+             "var outer = { inner: function() { } };"
+             "var p = new Parent();"
+             "var c = new Child();"
+             "var x = new outer.inner();")->Run();
+
+  Local<v8::Value> p = context->Global()->Get(v8_str("p"));
+  CHECK(p->IsObject() && p->ToObject()->GetConstructorName()->Equals(
+      v8_str("Parent")));
+
+  Local<v8::Value> c = context->Global()->Get(v8_str("c"));
+  CHECK(c->IsObject() && c->ToObject()->GetConstructorName()->Equals(
+      v8_str("Child")));
+
+  Local<v8::Value> x = context->Global()->Get(v8_str("x"));
+  CHECK(x->IsObject() && x->ToObject()->GetConstructorName()->Equals(
+      v8_str("outer.inner")));
+}


 bool ApiTestFuzzer::fuzzing_ = false;

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

Reply via email to