Cleanup error reporting in dis and dissrc. bug 445743, r=brendan
authorBlake Kaplan <mrbkap@gmail.com>
Mon, 04 Aug 2008 16:36:10 -0700
changeset 16377 957a867fcb33a63a29cefc5cb6c82b34d5c2ec43
parent 16376 d0010b626c42a00f8f7e35e2f8b58ebf40139cf8
child 16378 a9e76464fbcf1ab9ebf5c49a751732555de16201
push idunknown
push userunknown
push dateunknown
reviewersbrendan
bugs445743
milestone1.9.1a2pre
Cleanup error reporting in dis and dissrc. bug 445743, r=brendan
js/src/js.cpp
js/src/jsshell.msg
--- a/js/src/js.cpp
+++ b/js/src/js.cpp
@@ -1027,16 +1027,22 @@ ValueToScript(JSContext *cx, jsval v)
         JS_GET_CLASS(cx, JSVAL_TO_OBJECT(v)) == &js_ScriptClass) {
         script = (JSScript *) JS_GetPrivate(cx, JSVAL_TO_OBJECT(v));
     } else {
         fun = JS_ValueToFunction(cx, v);
         if (!fun)
             return NULL;
         script = FUN_SCRIPT(fun);
     }
+
+    if (!script) {
+        JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
+                             JSSMSG_SCRIPTS_ONLY);
+    }
+
     return script;
 }
 
 static JSBool
 GetTrapArgs(JSContext *cx, uintN argc, jsval *argv, JSScript **scriptp,
             int32 *ip)
 {
     jsval v;
@@ -1363,81 +1369,80 @@ Disassemble(JSContext *cx, JSObject *obj
         !strcmp(JS_GetStringBytes(JSVAL_TO_STRING(argv[0])), "-l")) {
         lines = JS_TRUE;
         argv++, argc--;
     } else {
         lines = JS_FALSE;
     }
     for (i = 0; i < argc; i++) {
         script = ValueToScript(cx, argv[i]);
-        if (!script) {
-            JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
-                                 JSSMSG_SCRIPTS_ONLY);
+        if (!script)
             return JS_FALSE;
-        }
         if (VALUE_IS_FUNCTION(cx, argv[i])) {
             JSFunction *fun = JS_ValueToFunction(cx, argv[i]);
             if (fun && (fun->flags & JSFUN_FLAGS_MASK)) {
                 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(INTERPRETED);
-                
+
 #undef SHOW_FLAG
                 putchar('\n');
             }
         }
-        
-        if (!js_Disassemble(cx, script, lines, stdout)) {
-            JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
-                                 JSSMSG_CANT_DISASSEMBLE);
+
+        if (!js_Disassemble(cx, script, lines, stdout))
             return JS_FALSE;
-        }
         SrcNotes(cx, script);
         TryNotes(cx, script);
     }
     return JS_TRUE;
 }
 
 static JSBool
 DisassWithSrc(JSContext *cx, JSObject *obj, uintN argc, jsval *argv,
               jsval *rval)
 {
 #define LINE_BUF_LEN 512
     uintN i, len, line1, line2, bupline;
     JSScript *script;
     FILE *file;
     char linebuf[LINE_BUF_LEN];
     jsbytecode *pc, *end;
+    JSBool ok;
     static char sep[] = ";-------------------------";
 
-    for (i = 0; i < argc; i++) {
+    ok = JS_TRUE;
+    for (i = 0; ok && i < argc; i++) {
         script = ValueToScript(cx, argv[i]);
-        if (!script || !script->filename) {
+        if (!script)
+           return JS_FALSE;
+
+        if (!script->filename) {
             JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
                                  JSSMSG_FILE_SCRIPTS_ONLY);
             return JS_FALSE;
         }
 
         file = fopen(script->filename, "r");
         if (!file) {
             JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
-                            JSSMSG_CANT_OPEN,
-                            script->filename, strerror(errno));
+                                 JSSMSG_CANT_OPEN, script->filename,
+                                 strerror(errno));
             return JS_FALSE;
         }
 
         pc = script->code;
         end = pc + script->length;
 
         /* burn the leading lines */
         line2 = JS_PCToLineNumber(cx, script, pc);
@@ -1457,38 +1462,38 @@ DisassWithSrc(JSContext *cx, JSObject *o
                 if (bupline && line1 == line2)
                     fprintf(gOutFile, "%s %3u: RESTORE\n", sep, line2);
                 bupline = 0;
                 while (line1 < line2) {
                     if (!fgets(linebuf, LINE_BUF_LEN, file)) {
                         JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
                                              JSSMSG_UNEXPECTED_EOF,
                                              script->filename);
+                        ok = JS_FALSE;
                         goto bail;
                     }
                     line1++;
                     fprintf(gOutFile, "%s %3u: %s", sep, line1, linebuf);
                 }
             }
 
             len = js_Disassemble1(cx, script, pc,
                                   PTRDIFF(pc, script->code, jsbytecode),
                                   JS_TRUE, stdout);
             if (!len) {
-                JS_ReportErrorNumber(cx, my_GetErrorMessage, NULL,
-                                     JSSMSG_CANT_DISASSEMBLE);
-                return JS_FALSE;
+                ok = JS_FALSE;
+                goto bail;
             }
             pc += len;
         }
 
       bail:
         fclose(file);
     }
-    return JS_TRUE;
+    return ok;
 #undef LINE_BUF_LEN
 }
 
 static JSBool
 Tracing(JSContext *cx, JSObject *obj, uintN argc, jsval *argv, jsval *rval)
 {
     JSBool bval;
     JSString *str;
--- a/js/src/jsshell.msg
+++ b/js/src/jsshell.msg
@@ -43,10 +43,9 @@
 
 MSG_DEF(JSSMSG_NOT_AN_ERROR,             0, 0, JSEXN_NONE, "<Error #0 is reserved>")
 MSG_DEF(JSSMSG_CANT_OPEN,                1, 2, JSEXN_NONE, "can't open {0}: {1}") 
 MSG_DEF(JSSMSG_TRAP_USAGE,               2, 0, JSEXN_NONE, "usage: trap [fun] [pc] expr") 
 MSG_DEF(JSSMSG_LINE2PC_USAGE,            3, 0, JSEXN_NONE, "usage: line2pc [fun] line") 
 MSG_DEF(JSSMSG_FILE_SCRIPTS_ONLY,        4, 0, JSEXN_NONE, "only works on JS scripts read from files") 
 MSG_DEF(JSSMSG_UNEXPECTED_EOF,           5, 1, JSEXN_NONE, "unexpected EOF in {0}") 
 MSG_DEF(JSSMSG_DOEXP_USAGE,              6, 0, JSEXN_NONE, "usage: doexp obj id") 
-MSG_DEF(JSSMSG_SCRIPTS_ONLY,             7, 0, JSEXN_NONE, "only works on JS scripts") 
-MSG_DEF(JSSMSG_CANT_DISASSEMBLE,         8, 0, JSEXN_NONE, "Could not disassemble script")
+MSG_DEF(JSSMSG_SCRIPTS_ONLY,             7, 0, JSEXN_NONE, "only works on scripts")