Title: [191393] trunk/Source/_javascript_Core
Revision
191393
Author
[email protected]
Date
2015-10-21 11:42:06 -0700 (Wed, 21 Oct 2015)

Log Message

Date creation should share a little code
https://bugs.webkit.org/show_bug.cgi?id=150399

Reviewed by Filip Pizlo.

I want to fix a bug in this code, but I don't want to fix it in two
different places. (See https://bugs.webkit.org/show_bug.cgi?id=150386.)

* runtime/DateConstructor.cpp:
(JSC::DateConstructor::getOwnPropertySlot):
(JSC::milliseconds): Factored out a shared helper function. If you look
closely, you'll see that one copy of this code previously checked isfinite
while the other checked isnan. isnan returning nan was obviously a no-op,
so I removed it. isfinite, it turns out, is also a no-op -- but less
obviously so, so I kept it for now.

(JSC::constructDate):
(JSC::dateUTC): Use the helper function.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (191392 => 191393)


--- trunk/Source/_javascript_Core/ChangeLog	2015-10-21 18:29:39 UTC (rev 191392)
+++ trunk/Source/_javascript_Core/ChangeLog	2015-10-21 18:42:06 UTC (rev 191393)
@@ -1,3 +1,24 @@
+2015-10-21  Geoffrey Garen  <[email protected]>
+
+        Date creation should share a little code
+        https://bugs.webkit.org/show_bug.cgi?id=150399
+
+        Reviewed by Filip Pizlo.
+
+        I want to fix a bug in this code, but I don't want to fix it in two
+        different places. (See https://bugs.webkit.org/show_bug.cgi?id=150386.)
+
+        * runtime/DateConstructor.cpp:
+        (JSC::DateConstructor::getOwnPropertySlot):
+        (JSC::milliseconds): Factored out a shared helper function. If you look
+        closely, you'll see that one copy of this code previously checked isfinite
+        while the other checked isnan. isnan returning nan was obviously a no-op,
+        so I removed it. isfinite, it turns out, is also a no-op -- but less
+        obviously so, so I kept it for now.
+
+        (JSC::constructDate):
+        (JSC::dateUTC): Use the helper function.
+
 2015-10-21  Guillaume Emont  <[email protected]>
 
         llint: align stack pointer on mips too

Modified: trunk/Source/_javascript_Core/runtime/DateConstructor.cpp (191392 => 191393)


--- trunk/Source/_javascript_Core/runtime/DateConstructor.cpp	2015-10-21 18:29:39 UTC (rev 191392)
+++ trunk/Source/_javascript_Core/runtime/DateConstructor.cpp	2015-10-21 18:42:06 UTC (rev 191393)
@@ -112,6 +112,42 @@
     return getStaticFunctionSlot<InternalFunction>(exec, dateConstructorTable, jsCast<DateConstructor*>(object), propertyName, slot);
 }
 
+static double millisecondsFromComponents(ExecState* exec, const ArgList& args, WTF::TimeType timeType)
+{
+    double doubleArguments[] = {
+        args.at(0).toNumber(exec), 
+        args.at(1).toNumber(exec), 
+        args.at(2).toNumber(exec), 
+        args.at(3).toNumber(exec), 
+        args.at(4).toNumber(exec), 
+        args.at(5).toNumber(exec), 
+        args.at(6).toNumber(exec)
+    };
+
+    int numArgs = args.size();
+
+    if ((!std::isfinite(doubleArguments[0]) || (doubleArguments[0] > INT_MAX) || (doubleArguments[0] < INT_MIN))
+        || (!std::isfinite(doubleArguments[1]) || (doubleArguments[1] > INT_MAX) || (doubleArguments[1] < INT_MIN))
+        || (numArgs >= 3 && (!std::isfinite(doubleArguments[2]) || (doubleArguments[2] > INT_MAX) || (doubleArguments[2] < INT_MIN)))
+        || (numArgs >= 4 && (!std::isfinite(doubleArguments[3]) || (doubleArguments[3] > INT_MAX) || (doubleArguments[3] < INT_MIN)))
+        || (numArgs >= 5 && (!std::isfinite(doubleArguments[4]) || (doubleArguments[4] > INT_MAX) || (doubleArguments[4] < INT_MIN)))
+        || (numArgs >= 6 && (!std::isfinite(doubleArguments[5]) || (doubleArguments[5] > INT_MAX) || (doubleArguments[5] < INT_MIN)))
+        || (numArgs >= 7 && (!std::isfinite(doubleArguments[6]) || (doubleArguments[6] > INT_MAX) || (doubleArguments[6] < INT_MIN))))
+        return PNaN;
+
+    GregorianDateTime t;
+    int year = JSC::toInt32(doubleArguments[0]);
+    t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
+    t.setMonth(JSC::toInt32(doubleArguments[1]));
+    t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
+    t.setHour(JSC::toInt32(doubleArguments[3]));
+    t.setMinute(JSC::toInt32(doubleArguments[4]));
+    t.setSecond(JSC::toInt32(doubleArguments[5]));
+    t.setIsDST(-1);
+    double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
+    return gregorianDateTimeToMS(exec->vm(), t, ms, timeType);
+}
+
 // ECMA 15.9.3
 JSObject* constructDate(ExecState* exec, JSGlobalObject* globalObject, const ArgList& args)
 {
@@ -132,38 +168,8 @@
             else
                 value = primitive.toNumber(exec);
         }
-    } else {
-        double doubleArguments[7] = {
-            args.at(0).toNumber(exec), 
-            args.at(1).toNumber(exec), 
-            args.at(2).toNumber(exec), 
-            args.at(3).toNumber(exec), 
-            args.at(4).toNumber(exec), 
-            args.at(5).toNumber(exec), 
-            args.at(6).toNumber(exec)
-        };
-        if ((!std::isfinite(doubleArguments[0]) || (doubleArguments[0] > INT_MAX) || (doubleArguments[0] < INT_MIN))
-            || (!std::isfinite(doubleArguments[1]) || (doubleArguments[1] > INT_MAX) || (doubleArguments[1] < INT_MIN))
-            || (numArgs >= 3 && (!std::isfinite(doubleArguments[2]) || (doubleArguments[2] > INT_MAX) || (doubleArguments[2] < INT_MIN)))
-            || (numArgs >= 4 && (!std::isfinite(doubleArguments[3]) || (doubleArguments[3] > INT_MAX) || (doubleArguments[3] < INT_MIN)))
-            || (numArgs >= 5 && (!std::isfinite(doubleArguments[4]) || (doubleArguments[4] > INT_MAX) || (doubleArguments[4] < INT_MIN)))
-            || (numArgs >= 6 && (!std::isfinite(doubleArguments[5]) || (doubleArguments[5] > INT_MAX) || (doubleArguments[5] < INT_MIN)))
-            || (numArgs >= 7 && (!std::isfinite(doubleArguments[6]) || (doubleArguments[6] > INT_MAX) || (doubleArguments[6] < INT_MIN))))
-            value = PNaN;
-        else {
-            GregorianDateTime t;
-            int year = JSC::toInt32(doubleArguments[0]);
-            t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
-            t.setMonth(JSC::toInt32(doubleArguments[1]));
-            t.setMonthDay((numArgs >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
-            t.setHour(JSC::toInt32(doubleArguments[3]));
-            t.setMinute(JSC::toInt32(doubleArguments[4]));
-            t.setSecond(JSC::toInt32(doubleArguments[5]));
-            t.setIsDST(-1);
-            double ms = (numArgs >= 7) ? doubleArguments[6] : 0;
-            value = gregorianDateTimeToMS(vm, t, ms, WTF::LocalTime);
-        }
-    }
+    } else
+        value = millisecondsFromComponents(exec, args, WTF::LocalTime);
 
     return DateInstance::create(vm, globalObject->dateStructure(), value);
 }
@@ -211,35 +217,8 @@
 
 EncodedJSValue JSC_HOST_CALL dateUTC(ExecState* exec) 
 {
-    double doubleArguments[7] = {
-        exec->argument(0).toNumber(exec), 
-        exec->argument(1).toNumber(exec), 
-        exec->argument(2).toNumber(exec), 
-        exec->argument(3).toNumber(exec), 
-        exec->argument(4).toNumber(exec), 
-        exec->argument(5).toNumber(exec), 
-        exec->argument(6).toNumber(exec)
-    };
-    int n = exec->argumentCount();
-    if ((std::isnan(doubleArguments[0]) || (doubleArguments[0] > INT_MAX) || (doubleArguments[0] < INT_MIN))
-        || (std::isnan(doubleArguments[1]) || (doubleArguments[1] > INT_MAX) || (doubleArguments[1] < INT_MIN))
-        || (n >= 3 && (std::isnan(doubleArguments[2]) || (doubleArguments[2] > INT_MAX) || (doubleArguments[2] < INT_MIN)))
-        || (n >= 4 && (std::isnan(doubleArguments[3]) || (doubleArguments[3] > INT_MAX) || (doubleArguments[3] < INT_MIN)))
-        || (n >= 5 && (std::isnan(doubleArguments[4]) || (doubleArguments[4] > INT_MAX) || (doubleArguments[4] < INT_MIN)))
-        || (n >= 6 && (std::isnan(doubleArguments[5]) || (doubleArguments[5] > INT_MAX) || (doubleArguments[5] < INT_MIN)))
-        || (n >= 7 && (std::isnan(doubleArguments[6]) || (doubleArguments[6] > INT_MAX) || (doubleArguments[6] < INT_MIN))))
-        return JSValue::encode(jsNaN());
-
-    GregorianDateTime t;
-    int year = JSC::toInt32(doubleArguments[0]);
-    t.setYear((year >= 0 && year <= 99) ? (year + 1900) : year);
-    t.setMonth(JSC::toInt32(doubleArguments[1]));
-    t.setMonthDay((n >= 3) ? JSC::toInt32(doubleArguments[2]) : 1);
-    t.setHour(JSC::toInt32(doubleArguments[3]));
-    t.setMinute(JSC::toInt32(doubleArguments[4]));
-    t.setSecond(JSC::toInt32(doubleArguments[5]));
-    double ms = (n >= 7) ? doubleArguments[6] : 0;
-    return JSValue::encode(jsNumber(timeClip(gregorianDateTimeToMS(exec->vm(), t, ms, WTF::UTCTime))));
+    double ms = millisecondsFromComponents(exec, ArgList(exec), WTF::UTCTime);
+    return JSValue::encode(jsNumber(timeClip(ms)));
 }
 
 } // namespace JSC
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to