Bug 1160356 - Reorganize the code for the Date function/constructor into three separate methods, to be more consistent with ES6's definition of it. Don't change the actual algorithm yet -- this is just code motion. r=evilpie
authorJeff Walden <jwalden@mit.edu>
Thu, 30 Apr 2015 09:58:58 -0700
changeset 273485 a411bd3307dac2f43d9feadd9653fc0a4917e94a
parent 273484 9804500b0ab7f9bcaa9037fff7458bb80cd6402a
child 273486 f35d190d8e2fd0de55a351a804739c2ae6fb8808
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersevilpie
bugs1160356
milestone40.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 1160356 - Reorganize the code for the Date function/constructor into three separate methods, to be more consistent with ES6's definition of it. Don't change the actual algorithm yet -- this is just code motion. r=evilpie
js/src/jsdate.cpp
--- a/js/src/jsdate.cpp
+++ b/js/src/jsdate.cpp
@@ -3007,75 +3007,111 @@ static const JSFunctionSpec date_methods
 #if JS_HAS_TOSOURCE
     JS_FN(js_toSource_str,       date_toSource,           0,0),
 #endif
     JS_FN(js_toString_str,       date_toString,           0,0),
     JS_FN(js_valueOf_str,        date_valueOf,            0,0),
     JS_FS_END
 };
 
-bool
-js::DateConstructor(JSContext* cx, unsigned argc, Value* vp)
+static bool
+NewDateObject(JSContext* cx, const CallArgs& args, double d)
+{
+    JSObject* obj = NewDateObjectMsec(cx, d);
+    if (!obj)
+        return false;
+
+    args.rval().setObject(*obj);
+    return true;
+}
+
+static bool
+ToDateString(JSContext* cx, const CallArgs& args, double d)
 {
-    CallArgs args = CallArgsFromVp(argc, vp);
-
-    /* Date called as function. */
-    if (!args.isConstructing())
-        return date_format(cx, NowAsMillis(), FORMATSPEC_FULL, args.rval());
-
-    /* Date called as constructor. */
-    double d;
-    if (args.length() == 0) {
-        /* ES5 15.9.3.3. */
-        d = NowAsMillis();
-    } else if (args.length() == 1) {
-        /* ES5 15.9.3.2. */
-
-        /* Step 1. */
+    return date_format(cx, d, FORMATSPEC_FULL, args.rval());
+}
+
+static bool
+DateNoArguments(JSContext* cx, const CallArgs& args)
+{
+    MOZ_ASSERT(args.length() == 0);
+
+    double now = NowAsMillis();
+
+    if (args.isConstructing())
+        return NewDateObject(cx, args, now);
+
+    return ToDateString(cx, args, now);
+}
+
+static bool
+DateOneArgument(JSContext* cx, const CallArgs& args)
+{
+    MOZ_ASSERT(args.length() == 1);
+
+    if (args.isConstructing()) {
+        double d;
+
         if (!ToPrimitive(cx, args[0]))
             return false;
 
         if (args[0].isString()) {
-            /* Step 2. */
-            JSString* str = args[0].toString();
-            if (!str)
-                return false;
-
-            JSLinearString* linearStr = str->ensureLinear(cx);
+            JSLinearString* linearStr = args[0].toString()->ensureLinear(cx);
             if (!linearStr)
                 return false;
 
             if (!ParseDate(linearStr, &d, &cx->runtime()->dateTimeInfo))
                 d = GenericNaN();
             else
                 d = TimeClip(d);
         } else {
-            /* Step 3. */
             if (!ToNumber(cx, args[0], &d))
                 return false;
             d = TimeClip(d);
         }
-    } else {
-        double msec_time;
-        if (!date_msecFromArgs(cx, args, &msec_time))
+
+        return NewDateObject(cx, args, d);
+    }
+
+    return ToDateString(cx, args, NowAsMillis());
+}
+
+static bool
+DateMultipleArguments(JSContext* cx, const CallArgs& args)
+{
+    MOZ_ASSERT(args.length() >= 2);
+
+    if (args.isConstructing()) {
+        double millis;
+        if (!date_msecFromArgs(cx, args, &millis))
             return false;
 
-        if (IsFinite(msec_time)) {
-            msec_time = UTC(msec_time, &cx->runtime()->dateTimeInfo);
-            msec_time = TimeClip(msec_time);
+        if (IsFinite(millis)) {
+            millis = UTC(millis, &cx->runtime()->dateTimeInfo);
+            millis = TimeClip(millis);
         }
-        d = msec_time;
+
+        return NewDateObject(cx, args, millis);
     }
 
-    JSObject* obj = NewDateObjectMsec(cx, d);
-    if (!obj)
-        return false;
-
-    args.rval().setObject(*obj);
-    return true;
+    return ToDateString(cx, args, NowAsMillis());
+}
+
+bool
+js::DateConstructor(JSContext* cx, unsigned argc, Value* vp)
+{
+    CallArgs args = CallArgsFromVp(argc, vp);
+
+    if (args.length() == 0)
+        return DateNoArguments(cx, args);
+
+    if (args.length() == 1)
+        return DateOneArgument(cx, args);
+
+    return DateMultipleArguments(cx, args);
 }
 
 static bool
 FinishDateClassInit(JSContext* cx, HandleObject ctor, HandleObject proto)
 {
     proto->as<DateObject>().setUTCTime(GenericNaN());
 
     /*