diff memory_profiler.cpp @ 62:03078138a4cd

object info now includes information about properties.
author Atul Varma <varmaa@toolness.com>
date Thu, 25 Jun 2009 10:48:47 -0700
parents 44849dbd9017
children 4910bc49a182
line wrap: on
line diff
--- a/memory_profiler.cpp	Thu Jun 25 09:47:18 2009 -0700
+++ b/memory_profiler.cpp	Thu Jun 25 10:48:47 2009 -0700
@@ -155,6 +155,59 @@
                            NULL, NULL, JSPROP_ENUMERATE);
 }
 
+static JSBool getPropertiesInfo(JSContext *cx, JSObject *info,
+                                JSObject *target, JSContext *targetCx)
+{
+  JSObject *propInfo = JS_NewObject(cx, NULL, NULL, NULL);
+  if (propInfo == NULL) {
+    JS_ReportOutOfMemory(cx);
+    return JS_FALSE;
+  }
+
+  if (!JS_DefineProperty(cx, info, "properties", OBJECT_TO_JSVAL(propInfo),
+                         NULL, NULL, JSPROP_ENUMERATE))
+    return JS_FALSE;
+
+  JSPropertyDescArray properties;
+  if (!JS_GetPropertyDescArray(targetCx, target, &properties)) {
+    JS_ReportError(cx, "Couldn't get property desc array.");
+    return JS_FALSE;
+  }
+
+  for (int i = 0; i < properties.length; i++) {
+    jsval id = properties.array[i].id;
+    // TODO: What if the property is a number?
+    if (JSVAL_IS_STRING(id)) {
+      jsval value = properties.array[i].value;
+      if (JSVAL_IS_OBJECT(value)) {
+        JSObject *valueObj = JSVAL_TO_OBJECT(value);
+        value = INT_TO_JSVAL((unsigned int) valueObj);
+      } else if (JSVAL_IS_STRING(value)) {
+        JSString *valueStr = JS_NewUCStringCopyZ(
+          cx,
+          JS_GetStringChars(JSVAL_TO_STRING(value))
+          );
+        if (valueStr == NULL) {
+          JS_ReportOutOfMemory(cx);
+          return JS_FALSE;
+        }
+        value = STRING_TO_JSVAL(valueStr);
+      } else
+        value = JSVAL_NULL;
+
+      if (!JS_DefineProperty(cx, propInfo,
+                             JS_GetStringBytes(JSVAL_TO_STRING(id)),
+                             value,
+                             NULL,
+                             NULL,
+                             JSPROP_ENUMERATE))
+        return JS_FALSE;        
+    }
+  }
+
+  return JS_TRUE;
+}
+
 static JSBool getObjInfo(JSContext *cx, JSObject *obj, uintN argc,
                          jsval *argv, jsval *rval)
 {
@@ -203,6 +256,9 @@
     if (!getChildrenInfo(cx, info, target, targetCx))
       return JS_FALSE;
 
+    if (!getPropertiesInfo(cx, info, target, targetCx))
+      return JS_FALSE;
+
     *rval = OBJECT_TO_JSVAL(info);
   } else
     *rval = JSVAL_NULL;