Bug 630735 - NodeVector is not GC-safe (r=jorendorff)
authorDave Herman <dherman@mozilla.com>
Wed, 02 Feb 2011 21:11:55 -0800
changeset 62067 53403b41c153c5d68f042737076f9c77e99f9107
parent 62066 08bccc769e48b37862407e6014e589fd6276f056
child 62068 c56d2246df658acddcee5ef1b0b588f2cf3782d5
push idunknown
push userunknown
push dateunknown
reviewersjorendorff
bugs630735
milestone2.0b11pre
Bug 630735 - NodeVector is not GC-safe (r=jorendorff)
js/src/jsreflect.cpp
js/src/shell/js.cpp
--- a/js/src/jsreflect.cpp
+++ b/js/src/jsreflect.cpp
@@ -130,17 +130,17 @@ char const *nodeTypeNames[] = {
 
 char const *callbackNames[] = {
 #define ASTDEF(ast, str, method) method,
 #include "jsast.tbl"
 #undef ASTDEF
     NULL
 };
 
-typedef Vector<Value, 8> NodeVector;
+typedef AutoValueVector NodeVector;
 
 /*
  * JSParseNode is a somewhat intricate data structure, and its invariants have
  * evolved, making it more likely that there could be a disconnect between the
  * parser and the AST serializer. We use these macros to check invariants on a
  * parse node and raise a dynamic error on failure.
  */
 #define LOCAL_ASSERT(expr)                                                             \
@@ -910,22 +910,27 @@ NodeBuilder::tryStatement(Value body, No
     Value handler;
 
     Value cb = callbacks[AST_TRY_STMT];
     if (!cb.isNull()) {
         return newArray(catches, &handler) &&
                callback(cb, body, handler, opt(finally), pos, dst);
     }
 
-    if (catches.empty())
+    switch (catches.length()) {
+      case 0:
         handler.setNull();
-    else if (catches.length() == 1)
+        break;
+      case 1:
         handler = catches[0];
-    else if (!newArray(catches, &handler))
-        return false;
+        break;
+      default:
+        if (!newArray(catches, &handler))
+            return false;
+    }
 
     return newNode(AST_TRY_STMT, pos,
                    "block", body,
                    "handler", handler,
                    "finalizer", finally,
                    dst);
 }
 
--- a/js/src/shell/js.cpp
+++ b/js/src/shell/js.cpp
@@ -443,24 +443,21 @@ Process(JSContext *cx, JSObject *obj, ch
         }
         ungetc(ch, file);
 
         int64 t1 = PRMJ_Now();
         oldopts = JS_GetOptions(cx);
         JS_SetOptions(cx, oldopts | JSOPTION_COMPILE_N_GO | JSOPTION_NO_SCRIPT_RVAL);
         script = JS_CompileFileHandle(cx, obj, filename, file);
         JS_SetOptions(cx, oldopts);
-        if (script) {
-            if (!compileOnly) {
-                (void)JS_ExecuteScript(cx, obj, script, NULL);
-                int64 t2 = PRMJ_Now() - t1;
-                if (printTiming)
-                    printf("runtime = %.3f ms\n", double(t2) / PRMJ_USEC_PER_MSEC);
-            }
-            JS_DestroyScript(cx, script);
+        if (script && !compileOnly) {
+            (void)JS_ExecuteScript(cx, obj, script, NULL);
+            int64 t2 = PRMJ_Now() - t1;
+            if (printTiming)
+                printf("runtime = %.3f ms\n", double(t2) / PRMJ_USEC_PER_MSEC);
         }
 
         goto cleanup;
     }
 
     /* It's an interactive filehandle; drop into read-eval-print loop. */
     lineno = 1;
     hitEOF = JS_FALSE;
@@ -538,31 +535,28 @@ Process(JSContext *cx, JSObject *obj, ch
         oldopts = JS_GetOptions(cx);
         if (!compileOnly)
             JS_SetOptions(cx, oldopts | JSOPTION_COMPILE_N_GO);
         script = JS_CompileScript(cx, obj, buffer, len, "typein",
                                   startline);
         if (!compileOnly)
             JS_SetOptions(cx, oldopts);
 
-        if (script) {
-            if (!compileOnly) {
-                ok = JS_ExecuteScript(cx, obj, script, &result);
-                if (ok && !JSVAL_IS_VOID(result)) {
-                    str = JS_ValueToSource(cx, result);
-                    ok = !!str;
-                    if (ok) {
-                        JSAutoByteString bytes(cx, str);
-                        ok = !!bytes;
-                        if (ok)
-                            fprintf(gOutFile, "%s\n", bytes.ptr());
-                    }
+        if (script && !compileOnly) {
+            ok = JS_ExecuteScript(cx, obj, script, &result);
+            if (ok && !JSVAL_IS_VOID(result)) {
+                str = JS_ValueToSource(cx, result);
+                ok = !!str;
+                if (ok) {
+                    JSAutoByteString bytes(cx, str);
+                    ok = !!bytes;
+                    if (ok)
+                        fprintf(gOutFile, "%s\n", bytes.ptr());
                 }
             }
-            JS_DestroyScript(cx, script);
         }
         *buffer = '\0';
     } while (!hitEOF && !gQuitting);
 
     free(buffer);
     fprintf(gOutFile, "\n");
 cleanup:
     if (file != stdin)
@@ -1014,17 +1008,16 @@ Options(JSContext *cx, uintN argc, jsval
 }
 
 static JSBool
 Load(JSContext *cx, uintN argc, jsval *vp)
 {
     uintN i;
     JSString *str;
     JSScript *script;
-    JSBool ok;
     uint32 oldopts;
 
     JSObject *thisobj = JS_THIS_OBJECT(cx, vp);
     if (!thisobj)
         return JS_FALSE;
 
     jsval *argv = JS_ARGV(cx, vp);
     for (i = 0; i < argc; i++) {
@@ -1034,26 +1027,20 @@ Load(JSContext *cx, uintN argc, jsval *v
         argv[i] = STRING_TO_JSVAL(str);
         JSAutoByteString filename(cx, str);
         if (!filename)
             return JS_FALSE;
         errno = 0;
         oldopts = JS_GetOptions(cx);
         JS_SetOptions(cx, oldopts | JSOPTION_COMPILE_N_GO | JSOPTION_NO_SCRIPT_RVAL);
         script = JS_CompileFile(cx, thisobj, filename.ptr());
+        if (!script)
+            return JS_FALSE;
         JS_SetOptions(cx, oldopts);
-        if (!script) {
-            ok = JS_FALSE;
-        } else {
-            ok = !compileOnly
-                 ? JS_ExecuteScript(cx, thisobj, script, NULL)
-                 : JS_TRUE;
-            JS_DestroyScript(cx, script);
-        }
-        if (!ok)
+        if (!compileOnly && !JS_ExecuteScript(cx, thisobj, script, NULL))
             return JS_FALSE;
     }
 
     return JS_TRUE;
 }
 
 static JSBool
 Evaluate(JSContext *cx, uintN argc, jsval *vp)
@@ -4194,17 +4181,16 @@ Compile(JSContext *cx, uintN argc, jsval
     }
 
     JSString *scriptContents = JSVAL_TO_STRING(arg0);
     JSScript *result = JS_CompileUCScript(cx, NULL, JS_GetStringCharsZ(cx, scriptContents),
                                           JS_GetStringLength(scriptContents), "<string>", 0);
     if (!result)
         return JS_FALSE;
 
-    JS_DestroyScript(cx, result);
     JS_SET_RVAL(cx, vp, JSVAL_VOID);
     return JS_TRUE;
 }
 
 static JSBool
 Parse(JSContext *cx, uintN argc, jsval *vp)
 {
     if (argc < 1) {