changeset 38:cfdbaadf21b8

made heap dumping examine object constructors.
author Atul Varma <varmaa@toolness.com>
date Mon, 22 Jun 2009 23:05:40 -0700
parents 193ff16b013a
children 067371eb9601
files spidermonkey-playground.cpp tcb.js
diffstat 2 files changed, 41 insertions(+), 2 deletions(-) [+]
line wrap: on
line diff
--- a/spidermonkey-playground.cpp	Mon Jun 22 17:24:18 2009 -0700
+++ b/spidermonkey-playground.cpp	Mon Jun 22 23:05:40 2009 -0700
@@ -89,6 +89,9 @@
 
   // Whether the tracing operation is successful or failed.
   JSBool result;
+
+  // Class to look for.
+  JSClass *classp;
 };
 
 // Static singleton for tracking the state of tracing the JS heap.
@@ -114,14 +117,36 @@
         return;
       }
       entry->key = thing;
+      JSObject *obj = (JSObject *) thing;
+      JSClass *classp = JS_GET_CLASS(trc->context, obj);
+      if (classp == tracingState.classp) {
+        JSObject *constructor = JS_GetConstructor(trc->context, obj);
+        if (constructor == NULL) {
+          tracingState.result = JS_FALSE;
+          return;
+        }
+        if (JS_ObjectIsFunction(trc->context, constructor)) {
+          JSFunction *func = JS_ValueToFunction(
+            trc->context,
+            OBJECT_TO_JSVAL(constructor)
+            );
+          if (func == NULL) {
+            tracingState.result = JS_FALSE;
+            return;
+          }
+          JSString *name = JS_GetFunctionId(func);
+          if (name != NULL) {
+            const char *str = JS_GetStringBytes(name);
+            printf("function: %s\n", str);
+          }
+        }
+      }
       JS_TraceChildren(trc, thing, kind);
     }
     break;
   case JSTRACE_DOUBLE:
     break;
   case JSTRACE_STRING:
-    const char *str = JS_GetStringBytes((JSString *)thing);
-    printf("string: %s\n", str);
     break;
   }
 }
@@ -132,6 +157,18 @@
 static JSBool dumpHeap(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
                        jsval *rval)
 {
+  JSObject *object;
+
+  if (!JS_ConvertArguments(cx, argc, argv, "o", &object))
+    return JS_FALSE;
+
+  tracingState.classp = JS_GET_CLASS(cx, object);
+
+  if (tracingState.classp == NULL) {
+    JS_ReportError(cx, "Object has no class.");
+    return JS_FALSE;
+  }
+
   JSTracer tracer;
 
   if (!JS_DHashTableInit(&tracingState.visited, JS_DHashGetStubOps(),
--- a/tcb.js	Mon Jun 22 17:24:18 2009 -0700
+++ b/tcb.js	Mon Jun 22 23:05:40 2009 -0700
@@ -232,3 +232,5 @@
 if (getWrapper(wrapped) !== wrapper ||
     getWrapper(getWrapper(wrapped)) !== null)
   throw new Error("Getting the wrapper doesn't work!");
+
+dumpHeap(new Object());