bug 494645 - new "-r" option to the dis function disassemble recursively. r=mrbkap
authorIgor Bukanov <igor@mir2.org>
Wed, 03 Jun 2009 20:44:20 +0200
changeset 28924 265f98384b1facd018c8e673a1ed8a35c1148fe5
parent 28923 a16ed38ff63adb79c3afe94b86903604f9553b6a
child 28925 2ad658e9f42ad5e92d0fe3439c464c21f1d32ce3
push id7330
push userrsayre@mozilla.com
push dateWed, 03 Jun 2009 20:56:50 +0000
treeherdermozilla-central@054daaef53c9 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersmrbkap
bugs494645
milestone1.9.2a1pre
bug 494645 - new "-r" option to the dis function disassemble recursively. r=mrbkap
js/src/shell/js.cpp
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -1667,70 +1667,91 @@ TryNotes(JSContext *cx, JSScript *script
         JS_ASSERT(tn->kind < JS_ARRAY_LENGTH(TryNoteNames));
         fprintf(gOutFile, " %-7s %6u %8u %8u\n",
                 TryNoteNames[tn->kind], tn->stackDepth,
                 tn->start, tn->start + tn->length);
     } while (++tn != tnlimit);
     return JS_TRUE;
 }
 
+static bool
+DisassembleValue(JSContext *cx, jsval v, bool lines, bool recursive)
+{
+    JSScript *script = ValueToScript(cx, v);
+    if (!script)
+        return false;
+    if (VALUE_IS_FUNCTION(cx, v)) {
+        JSFunction *fun = JS_ValueToFunction(cx, v);
+        if (fun && (fun->flags & ~7U)) {
+            uint16 flags = fun->flags;
+            fputs("flags:", stdout);
+
+#define SHOW_FLAG(flag) if (flags & JSFUN_##flag) fputs(" " #flag, stdout);
+
+            SHOW_FLAG(LAMBDA);
+            SHOW_FLAG(SETTER);
+            SHOW_FLAG(GETTER);
+            SHOW_FLAG(BOUND_METHOD);
+            SHOW_FLAG(HEAVYWEIGHT);
+            SHOW_FLAG(THISP_STRING);
+            SHOW_FLAG(THISP_NUMBER);
+            SHOW_FLAG(THISP_BOOLEAN);
+            SHOW_FLAG(EXPR_CLOSURE);
+            SHOW_FLAG(TRACEABLE);
+
+#undef SHOW_FLAG
+
+            if (FUN_NULL_CLOSURE(fun))
+                fputs(" NULL_CLOSURE", stdout);
+            else if (FUN_FLAT_CLOSURE(fun))
+                fputs(" FLAT_CLOSURE", stdout);
+            putchar('\n');
+        }
+    }
+
+    if (!js_Disassemble(cx, script, lines, stdout))
+        return false;
+    SrcNotes(cx, script);
+    TryNotes(cx, script);
+
+    if (recursive && script->objectsOffset != 0) {
+        JSObjectArray *objects = JS_SCRIPT_OBJECTS(script);
+        for (uintN i = 0; i != objects->length; ++i) {
+            JSObject *obj = objects->vector[i];
+            if (HAS_FUNCTION_CLASS(obj)) {
+                putchar('\n');
+                if (!DisassembleValue(cx, OBJECT_TO_JSVAL(obj),
+                                      lines, recursive)) {
+                    return false;
+                }
+            }
+        }
+    }
+    return true;
+}
+
 static JSBool
 Disassemble(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 {
-    JSBool lines;
-    uintN i;
-    JSScript *script;
-
-    if (argc > 0 &&
-        JSVAL_IS_STRING(argv[0]) &&
-        !strcmp(JS_GetStringBytes(JSVAL_TO_STRING(argv[0])), "-l")) {
-        lines = JS_TRUE;
+    bool lines = false, recursive = false;
+
+    while (argc > 0 && JSVAL_IS_STRING(argv[0])) {
+        const char *bytes = JS_GetStringBytes(JSVAL_TO_STRING(argv[0]));
+        lines = !strcmp(bytes, "-l");
+        recursive = !strcmp(bytes, "-r");
+        if (!lines && !recursive)
+            break;
         argv++, argc--;
-    } else {
-        lines = JS_FALSE;
     }
-    for (i = 0; i < argc; i++) {
-        script = ValueToScript(cx, argv[i]);
-        if (!script)
-            return JS_FALSE;
-        if (VALUE_IS_FUNCTION(cx, argv[i])) {
-            JSFunction *fun = JS_ValueToFunction(cx, argv[i]);
-            if (fun && (fun->flags & ~7U)) {
-                uint16 flags = fun->flags;
-                fputs("flags:", stdout);
-
-#define SHOW_FLAG(flag) if (flags & JSFUN_##flag) fputs(" " #flag, stdout);
-
-                SHOW_FLAG(LAMBDA);
-                SHOW_FLAG(SETTER);
-                SHOW_FLAG(GETTER);
-                SHOW_FLAG(BOUND_METHOD);
-                SHOW_FLAG(HEAVYWEIGHT);
-                SHOW_FLAG(THISP_STRING);
-                SHOW_FLAG(THISP_NUMBER);
-                SHOW_FLAG(THISP_BOOLEAN);
-                SHOW_FLAG(EXPR_CLOSURE);
-                SHOW_FLAG(TRACEABLE);
-
-#undef SHOW_FLAG
-
-                if (FUN_NULL_CLOSURE(fun))
-                    fputs(" NULL_CLOSURE", stdout);
-                else if (FUN_FLAT_CLOSURE(fun))
-                    fputs(" FLAT_CLOSURE", stdout);
-                putchar('\n');
-            }
-        }
-
-        if (!js_Disassemble(cx, script, lines, stdout))
-            return JS_FALSE;
-        SrcNotes(cx, script);
-        TryNotes(cx, script);
+
+    for (uintN i = 0; i < argc; i++) {
+        if (!DisassembleValue(cx, argv[i], lines, recursive))
+            return false;
     }
-    return JS_TRUE;
+    return true;
 }
 
 static JSBool
 DisassFile(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 {
     JSString *str;
     const char *filename;
     JSScript *script;