Bug 686002 - Refactor Number class initialization. r=bhackett
authorJeff Walden <jwalden@mit.edu>
Mon, 02 May 2011 17:03:47 -0400
changeset 77601 71f42bb834152c08b8f6f783b6ba7ede0890f222
parent 77600 d90f49766941fdf6bbccc9d109afe7cd3969c257
child 77602 621b2adf85114828897c22748bb013b1e8d3db67
push id3
push userfelipc@gmail.com
push dateFri, 30 Sep 2011 20:09:13 +0000
reviewersbhackett
bugs686002
milestone9.0a1
Bug 686002 - Refactor Number class initialization. r=bhackett
js/src/jsnum.cpp
--- a/js/src/jsnum.cpp
+++ b/js/src/jsnum.cpp
@@ -70,16 +70,18 @@
 #include "jsopcode.h"
 #include "jsprf.h"
 #include "jsscope.h"
 #include "jsstr.h"
 #include "jstracer.h"
 #include "jsvector.h"
 #include "jslibmath.h"
 
+#include "vm/GlobalObject.h"
+
 #include "jsatominlines.h"
 #include "jsinferinlines.h"
 #include "jsinterpinlines.h"
 #include "jsnuminlines.h"
 #include "jsobjinlines.h"
 #include "jsstrinlines.h"
 
 #include "vm/String-inl.h"
@@ -1094,49 +1096,62 @@ FinishRuntimeNumberState(JSRuntime *rt)
     Foreground::free_(storage);
 }
 
 } /* namespace js */
 
 JSObject *
 js_InitNumberClass(JSContext *cx, JSObject *obj)
 {
-    JSObject *proto, *ctor;
-    JSRuntime *rt;
+    JS_ASSERT(obj->isNative());
 
     /* XXX must do at least once per new thread, so do it per JSContext... */
     FIX_FPU();
 
-    proto = js_InitClass(cx, obj, NULL, &NumberClass, Number, 1,
-                         NULL, number_methods, NULL, NULL);
-    if (!proto || !(ctor = JS_GetConstructor(cx, proto)))
+    GlobalObject *global = obj->asGlobal();
+
+    JSObject *numberProto = global->createBlankPrototype(cx, &NumberClass);
+    if (!numberProto)
         return NULL;
-    proto->setPrimitiveThis(Int32Value(0));
+    numberProto->setPrimitiveThis(Int32Value(0));
 
-    if (!JS_DefineFunctions(cx, obj, number_functions))
+    JSFunction *ctor = global->createConstructor(cx, Number, &NumberClass,
+                                                 CLASS_ATOM(cx, Number), 1);
+    if (!ctor)
         return NULL;
 
+    if (!LinkConstructorAndPrototype(cx, ctor, numberProto))
+        return NULL;
+
+    /* Add numeric constants (MAX_VALUE, NaN, &c.) to the Number constructor. */
     if (!JS_DefineConstDoubles(cx, ctor, number_constants))
         return NULL;
 
-    /* ECMA 15.1.1.1 */
-    rt = cx->runtime;
-    if (!JS_DefineProperty(cx, obj, js_NaN_str, rt->NaNValue,
-                           JS_PropertyStub, JS_StrictPropertyStub,
-                           JSPROP_PERMANENT | JSPROP_READONLY)) {
+    if (!DefinePropertiesAndBrand(cx, numberProto, NULL, number_methods))
+        return NULL;
+
+    if (!JS_DefineFunctions(cx, global, number_functions))
+        return NULL;
+
+    /* ES5 15.1.1.1, 15.1.1.2 */
+    if (!DefineNativeProperty(cx, global, ATOM_TO_JSID(cx->runtime->atomState.NaNAtom),
+                              cx->runtime->NaNValue, JS_PropertyStub, JS_StrictPropertyStub,
+                              JSPROP_PERMANENT | JSPROP_READONLY, 0, 0) ||
+        !DefineNativeProperty(cx, global, ATOM_TO_JSID(cx->runtime->atomState.InfinityAtom),
+                              cx->runtime->positiveInfinityValue,
+                              JS_PropertyStub, JS_StrictPropertyStub,
+                              JSPROP_PERMANENT | JSPROP_READONLY, 0, 0))
+    {
         return NULL;
     }
 
-    /* ECMA 15.1.1.2 */
-    if (!JS_DefineProperty(cx, obj, js_Infinity_str, rt->positiveInfinityValue,
-                           JS_PropertyStub, JS_StrictPropertyStub,
-                           JSPROP_PERMANENT | JSPROP_READONLY)) {
+    if (!DefineConstructorAndPrototype(cx, global, JSProto_Number, ctor, numberProto))
         return NULL;
-    }
-    return proto;
+
+    return numberProto;
 }
 
 namespace v8 {
 namespace internal {
 extern char* DoubleToCString(double v, char* buffer, int buflen);
 }
 }