Bug 679019 - Move initializing the *Error classes into a helper function. r=jorendorff
authorJeff Walden <jwalden@mit.edu>
Mon, 09 May 2011 13:06:52 -0700
changeset 75419 9c338d07d7f836a9e1e60cc90f30ada4b150f232
parent 75418 8e85ef78e47f901e48a4efc9099b3dfab7feaad2
child 75420 9c2be071d1d6577bdd9bac101bea06bf873b25c3
push id1275
push userjwalden@mit.edu
push dateWed, 17 Aug 2011 17:19:24 +0000
treeherdermozilla-inbound@9c2be071d1d6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjorendorff
bugs679019
milestone9.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 679019 - Move initializing the *Error classes into a helper function. r=jorendorff
js/src/jsexn.cpp
--- a/js/src/jsexn.cpp
+++ b/js/src/jsexn.cpp
@@ -1050,62 +1050,67 @@ InitErrorClass(JSContext *cx, GlobalObje
 
     if (!DefinePropertiesAndBrand(cx, errorProto, NULL, exception_methods))
         return NULL;
 
     /* Install the fully-constructed Error and Error.prototype. */
     if (!DefineConstructorAndPrototype(cx, global, JSProto_Error, ctor, errorProto))
         return NULL;
 
+    JS_ASSERT(!errorProto->getPrivate());
+
     return errorProto;
 }
 
+static bool
+InitSpecialErrorClass(JSContext *cx, GlobalObject *global, intN type, JSObject &errorProto)
+{
+    JSProtoKey key = GetExceptionProtoKey(type);
+    JSAtom *atom = cx->runtime->atomState.classAtoms[key];
+    JSObject *ctor;
+    JSObject *proto =
+        DefineConstructorAndPrototype(cx, global, key, atom, &errorProto, &js_ErrorClass,
+                                      Exception, 1, NULL, NULL, NULL, NULL, &ctor);
+    if (!proto)
+        return false;
+    JS_ASSERT(proto->getPrivate() == NULL);
+    ctor->setReservedSlot(JSSLOT_ERROR_EXNTYPE, Int32Value(int32(type)));
+
+    /* Add properties to the prototype. */
+    Value empty = StringValue(cx->runtime->emptyString);
+    jsid nameId = ATOM_TO_JSID(cx->runtime->atomState.nameAtom);
+    jsid messageId = ATOM_TO_JSID(cx->runtime->atomState.messageAtom);
+    jsid fileNameId = ATOM_TO_JSID(cx->runtime->atomState.fileNameAtom);
+    jsid lineNumberId = ATOM_TO_JSID(cx->runtime->atomState.lineNumberAtom);
+    return DefineNativeProperty(cx, proto, nameId, StringValue(atom),
+                                PropertyStub, StrictPropertyStub, 0, 0, 0) &&
+           DefineNativeProperty(cx, proto, messageId, empty,
+                                PropertyStub, StrictPropertyStub, 0, 0, 0) &&
+           DefineNativeProperty(cx, proto, fileNameId, empty,
+                                PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE, 0, 0) &&
+           DefineNativeProperty(cx, proto, lineNumberId, Int32Value(0),
+                                PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE, 0, 0);
+}
+
 JSObject *
 js_InitExceptionClasses(JSContext *cx, JSObject *obj)
 {
     JS_ASSERT(obj->isGlobal());
     JS_ASSERT(obj->isNative());
 
     GlobalObject *global = obj->asGlobal();
 
     JSObject *errorProto = InitErrorClass(cx, global);
     if (!errorProto)
         return NULL;
 
-    /* Define all error constructors. */
-    Value empty = StringValue(cx->runtime->emptyString);
-    jsid nameId = ATOM_TO_JSID(cx->runtime->atomState.nameAtom);
-    jsid messageId = ATOM_TO_JSID(cx->runtime->atomState.messageAtom);
-    jsid fileNameId = ATOM_TO_JSID(cx->runtime->atomState.fileNameAtom);
-    jsid lineNumberId = ATOM_TO_JSID(cx->runtime->atomState.lineNumberAtom);
+    /* Define all remaining *Error constructors. */
     for (intN i = JSEXN_ERR + 1; i < JSEXN_LIMIT; i++) {
-        JSProtoKey protoKey = GetExceptionProtoKey(i);
-        JSAtom *atom = cx->runtime->atomState.classAtoms[protoKey];
-        JSObject *ctor;
-        JSObject *proto =
-            DefineConstructorAndPrototype(cx, global, protoKey, atom, errorProto, &js_ErrorClass,
-                                          Exception, 1, NULL, NULL, NULL, NULL, &ctor);
-        if (!proto)
+        if (!InitSpecialErrorClass(cx, global, i, *errorProto))
             return NULL;
-        JS_ASSERT(proto->getPrivate() == NULL);
-
-        ctor->setReservedSlot(JSSLOT_ERROR_EXNTYPE, Int32Value(int32(i)));
-
-        /* Add properties to the prototype. */
-        JSAutoResolveFlags rf(cx, JSRESOLVE_QUALIFIED | JSRESOLVE_DECLARING);
-        if (!DefineNativeProperty(cx, proto, nameId, StringValue(atom),
-                                  PropertyStub, StrictPropertyStub, 0, 0, 0) ||
-            !DefineNativeProperty(cx, proto, messageId, empty,
-                                  PropertyStub, StrictPropertyStub, 0, 0, 0) ||
-            !DefineNativeProperty(cx, proto, fileNameId, empty,
-                                  PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE, 0, 0) ||
-            !DefineNativeProperty(cx, proto, lineNumberId, Valueify(JSVAL_ZERO),
-                                  PropertyStub, StrictPropertyStub, JSPROP_ENUMERATE, 0, 0)) {
-            return NULL;
-        }
     }
 
     return errorProto;
 }
 
 const JSErrorFormatString*
 js_GetLocalizedErrorMessage(JSContext* cx, void *userRef, const char *locale,
                             const uintN errorNumber)