# HG changeset patch # User Atul Varma # Date 1245737140 25200 # Node ID cfdbaadf21b8efc60ead3a67a306170c30954aa1 # Parent 193ff16b013a11931fed70e34cca4f6e6c78de9d made heap dumping examine object constructors. diff -r 193ff16b013a -r cfdbaadf21b8 spidermonkey-playground.cpp --- 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(), diff -r 193ff16b013a -r cfdbaadf21b8 tcb.js --- 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());