Merge
authorsundar
Mon, 23 Sep 2013 14:20:45 +0530
changeset 20221 dfd9fa85fe56
parent 20217 1d7360f5c7c2 (current diff)
parent 20220 6786e56c6931 (diff)
child 20227 1141c4b6e435
Merge
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Fri Sep 20 19:17:04 2013 -0700
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeDate.java	Mon Sep 23 14:20:45 2013 +0530
@@ -75,11 +75,11 @@
     private static final int FORMAT_LOCAL_TIME      = 5;
 
     // Constants defined in ECMA 15.9.1.10
-    private static final double hoursPerDay      = 24;
-    private static final double minutesPerHour   = 60;
-    private static final double secondsPerMinute = 60;
-    private static final double msPerSecond   = 1_000;
-    private static final double msPerMinute  = 60_000;
+    private static final int    hoursPerDay      = 24;
+    private static final int    minutesPerHour   = 60;
+    private static final int    secondsPerMinute = 60;
+    private static final int    msPerSecond   = 1_000;
+    private static final int    msPerMinute  = 60_000;
     private static final double msPerHour = 3_600_000;
     private static final double msPerDay = 86_400_000;
 
@@ -926,13 +926,13 @@
                 case FORMAT_DATE :
                 case FORMAT_LOCAL_DATE_TIME:
                     // EEE MMM dd yyyy
-                    sb.append(weekDays[(int) weekDay(t)])
+                    sb.append(weekDays[weekDay(t)])
                             .append(' ')
-                            .append(months[(int) monthFromTime(t)])
+                            .append(months[monthFromTime(t)])
                             .append(' ');
-                    zeroPad(sb, (int) dayFromTime(t), 2);
+                    zeroPad(sb, dayFromTime(t), 2);
                     sb.append(' ');
-                    zeroPad(sb, (int) yearFromTime(t), 4);
+                    zeroPad(sb, yearFromTime(t), 4);
                     if (format == FORMAT_DATE) {
                         break;
                     }
@@ -948,11 +948,11 @@
                     offset = (offset / 60) * 100 + offset % 60;
 
                     // HH:mm:ss GMT+HHmm
-                    zeroPad(sb, (int) hourFromTime(t), 2);
+                    zeroPad(sb, hourFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) minFromTime(t), 2);
+                    zeroPad(sb, minFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) secFromTime(t), 2);
+                    zeroPad(sb, secFromTime(t), 2);
                     sb.append(" GMT")
                             .append(offset < 0 ? '-' : '+');
                     zeroPad(sb, Math.abs(offset), 4);
@@ -963,20 +963,20 @@
 
                 case FORMAT_LOCAL_DATE:
                     // yyyy-MM-dd
-                    zeroPad(sb, (int) yearFromTime(t), 4);
+                    zeroPad(sb, yearFromTime(t), 4);
                     sb.append('-');
-                    zeroPad(sb, (int) monthFromTime(t) + 1, 2);
+                    zeroPad(sb, monthFromTime(t) + 1, 2);
                     sb.append('-');
-                    zeroPad(sb, (int) dayFromTime(t), 2);
+                    zeroPad(sb, dayFromTime(t), 2);
                     break;
 
                 case FORMAT_LOCAL_TIME:
                     // HH:mm:ss
-                    zeroPad(sb, (int) hourFromTime(t), 2);
+                    zeroPad(sb, hourFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) minFromTime(t), 2);
+                    zeroPad(sb, minFromTime(t), 2);
                     sb.append(':');
-                    zeroPad(sb, (int) secFromTime(t), 2);
+                    zeroPad(sb, secFromTime(t), 2);
                     break;
 
                 default:
@@ -996,19 +996,19 @@
             final StringBuilder sb = new StringBuilder(29);
             final double t = nd.getTime();
             // EEE, dd MMM yyyy HH:mm:ss z
-            sb.append(weekDays[(int) weekDay(t)])
+            sb.append(weekDays[weekDay(t)])
                     .append(", ");
-            zeroPad(sb, (int) dayFromTime(t), 2);
+            zeroPad(sb, dayFromTime(t), 2);
             sb.append(' ')
-                    .append(months[(int) monthFromTime(t)])
+                    .append(months[monthFromTime(t)])
                     .append(' ');
-            zeroPad(sb, (int) yearFromTime(t), 4);
+            zeroPad(sb, yearFromTime(t), 4);
             sb.append(' ');
-            zeroPad(sb, (int) hourFromTime(t), 2);
+            zeroPad(sb, hourFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) minFromTime(t), 2);
+            zeroPad(sb, minFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) secFromTime(t), 2);
+            zeroPad(sb, secFromTime(t), 2);
             sb.append(" GMT");
             return sb.toString();
         }
@@ -1023,19 +1023,19 @@
             final StringBuilder sb = new StringBuilder(24);
             final double t = nd.getTime();
             // yyyy-MM-dd'T'HH:mm:ss.SSS'Z'
-            zeroPad(sb, (int) yearFromTime(t), 4);
+            zeroPad(sb, yearFromTime(t), 4);
             sb.append('-');
-            zeroPad(sb, (int) monthFromTime(t) + 1, 2);
+            zeroPad(sb, monthFromTime(t) + 1, 2);
             sb.append('-');
-            zeroPad(sb, (int) dayFromTime(t), 2);
+            zeroPad(sb, dayFromTime(t), 2);
             sb.append('T');
-            zeroPad(sb, (int) hourFromTime(t), 2);
+            zeroPad(sb, hourFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) minFromTime(t), 2);
+            zeroPad(sb, minFromTime(t), 2);
             sb.append(':');
-            zeroPad(sb, (int) secFromTime(t), 2);
+            zeroPad(sb, secFromTime(t), 2);
             sb.append('.');
-            zeroPad(sb, (int) msFromTime(t), 3);
+            zeroPad(sb, msFromTime(t), 3);
             sb.append("Z");
             return sb.toString();
         }
@@ -1072,29 +1072,30 @@
     }
 
     // ECMA 15.9.1.3 Year Number
-    private static double timeFromYear(final double y) {
+    private static double timeFromYear(final int y) {
         return dayFromYear(y) * msPerDay;
     }
 
-    private static double yearFromTime(final double t) {
-        double y = Math.floor(t / (msPerDay * 365.2425)) + 1970;
+    // ECMA 15.9.1.3 Year Number
+    private static int yearFromTime(final double t) {
+        int y = (int) Math.floor(t / (msPerDay * 365.2425)) + 1970;
         final double t2 = timeFromYear(y);
         if (t2 > t) {
             y--;
-        } else if (t2 + msPerDay * daysInYear((int) y) <= t) {
+        } else if (t2 + msPerDay * daysInYear(y) <= t) {
             y++;
         }
         return y;
     }
 
-    private static double dayWithinYear(final double t, final double year) {
-        return day(t) - dayFromYear(year);
+    private static int dayWithinYear(final double t, final int year) {
+        return (int) (day(t) - dayFromYear(year));
     }
 
-    private static double monthFromTime(final double t) {
-        final double year = yearFromTime(t);
-        final double day = dayWithinYear(t, year);
-        final int[] firstDay = firstDayInMonth[isLeapYear((int) year) ? 1 : 0];
+    private static int monthFromTime(final double t) {
+        final int year = yearFromTime(t);
+        final int day = dayWithinYear(t, year);
+        final int[] firstDay = firstDayInMonth[isLeapYear(year) ? 1 : 0];
         int month = 0;
 
         while (month < 11 && firstDay[month + 1] <= day) {
@@ -1103,10 +1104,10 @@
         return month;
     }
 
-    private static double dayFromTime(final double t)  {
-        final double year = yearFromTime(t);
-        final double day = dayWithinYear(t, year);
-        final int[] firstDay = firstDayInMonth[isLeapYear((int) year) ? 1 : 0];
+    private static int dayFromTime(final double t)  {
+        final int year = yearFromTime(t);
+        final int day = dayWithinYear(t, year);
+        final int[] firstDay = firstDayInMonth[isLeapYear(year) ? 1 : 0];
         int month = 0;
 
         while (month < 11 && firstDay[month + 1] <= day) {
@@ -1121,11 +1122,8 @@
         return firstDay[month];
     }
 
-    private static double weekDay(final double time) {
-        if (isNaN(time)) {
-            return NaN;
-        }
-        final double day = (day(time) + 4) % 7;
+    private static int weekDay(final double time) {
+        final int day = (int) (day(time) + 4) % 7;
         return day < 0 ? day + 7 : day;
     }
 
@@ -1140,26 +1138,26 @@
     }
 
     // ECMA 15.9.1.10 Hours, Minutes, Second, and Milliseconds
-    private static double hourFromTime(final double t) {
-        final double h = Math.floor(t / msPerHour) % hoursPerDay;
+    private static int hourFromTime(final double t) {
+        final int h = (int) (Math.floor(t / msPerHour) % hoursPerDay);
         return h < 0 ? h + hoursPerDay: h;
     }
-    private static double minFromTime(final double t) {
-        final double m = Math.floor(t / msPerMinute) % minutesPerHour;
+    private static int minFromTime(final double t) {
+        final int m = (int) (Math.floor(t / msPerMinute) % minutesPerHour);
         return m < 0 ? m + minutesPerHour : m;
     }
 
-    private static double secFromTime(final double t) {
-        final double s = Math.floor(t / msPerSecond) % secondsPerMinute;
+    private static int secFromTime(final double t) {
+        final int s = (int) (Math.floor(t / msPerSecond) % secondsPerMinute);
         return s < 0 ? s + secondsPerMinute : s;
     }
 
-    private static double msFromTime(final double t) {
-        final double m = t % msPerSecond;
+    private static int msFromTime(final double t) {
+        final int m = (int) (t % msPerSecond);
         return m < 0 ? m + msPerSecond : m;
     }
 
-    private static double valueFromTime(final int unit, final double t) {
+    private static int valueFromTime(final int unit, final double t) {
         switch (unit) {
             case YEAR: return yearFromTime(t);
             case MONTH: return monthFromTime(t);
@@ -1180,12 +1178,12 @@
     // ECMA 15.9.1.12 MakeDay (year, month, date)
     private static double makeDay(final double year, final double month, final double date) {
         final double y = year + Math.floor(month / 12);
-        double m = month % 12;
+        int m = (int) (month % 12);
         if (m < 0) {
             m += 12;
         }
-        double d = Math.floor(dayFromYear(y));
-        d += dayFromMonth((int) m, (int) y);
+        double d = dayFromYear(y);
+        d += dayFromMonth(m, (int) y);
 
         return d + date - 1;
     }
@@ -1257,13 +1255,13 @@
                     nullReturn = true;
                 }
 
-                if (! nullReturn) {
+                if (!nullReturn && !isNaN(time)) {
                     d[i - start] = valueFromTime(i, time);
                 }
             }
         }
 
-        return nullReturn? null : d;
+        return nullReturn ? null : d;
     }
 
     // ECMA 15.9.1.14 TimeClip (time)
--- a/nashorn/src/jdk/nashorn/internal/objects/NativeJSON.java	Fri Sep 20 19:17:04 2013 -0700
+++ b/nashorn/src/jdk/nashorn/internal/objects/NativeJSON.java	Mon Sep 23 14:20:45 2013 +0530
@@ -162,22 +162,27 @@
 
         String gap;
 
-        if (space instanceof Number || space instanceof NativeNumber) {
-            int indent;
-            if (space instanceof NativeNumber) {
-                indent = ((NativeNumber)space).intValue();
-            } else {
-                indent = ((Number)space).intValue();
-            }
+        // modifiable 'space' - parameter is final
+        Object modSpace = space;
+        if (modSpace instanceof NativeNumber) {
+            modSpace = JSType.toNumber(JSType.toPrimitive(modSpace, Number.class));
+        } else if (modSpace instanceof NativeString) {
+            modSpace = JSType.toString(JSType.toPrimitive(modSpace, String.class));
+        }
 
-            final StringBuilder sb = new StringBuilder();
-            for (int i = 0; i < Math.min(10, indent); i++) {
-                sb.append(' ');
+        if (modSpace instanceof Number) {
+            int indent = Math.min(10, JSType.toInteger(modSpace));
+            if (indent < 1) {
+                gap = "";
+            } else {
+                final StringBuilder sb = new StringBuilder();
+                for (int i = 0; i < indent; i++) {
+                    sb.append(' ');
+                }
+                gap = sb.toString();
             }
-            gap = sb.toString();
-
-        } else if (space instanceof String || space instanceof ConsString || space instanceof NativeString) {
-            final String str = (space instanceof String) ? (String)space : space.toString();
+        } else if (modSpace instanceof String || modSpace instanceof ConsString) {
+            final String str = modSpace.toString();
             gap = str.substring(0, Math.min(10, str.length()));
         } else {
             gap = "";
--- a/nashorn/src/jdk/nashorn/internal/parser/JSONParser.java	Fri Sep 20 19:17:04 2013 -0700
+++ b/nashorn/src/jdk/nashorn/internal/parser/JSONParser.java	Mon Sep 23 14:20:45 2013 +0530
@@ -349,6 +349,10 @@
 
             case COMMARIGHT:
                 next();
+                // check for trailing comma - not allowed in JSON
+                if (type == RBRACKET) {
+                    throw error(AbstractParser.message("trailing.comma.in.json", type.getNameOrType()));
+                }
                 break;
 
             default:
@@ -388,6 +392,10 @@
 
             case COMMARIGHT:
                 next();
+                // check for trailing comma - not allowed in JSON
+                if (type == RBRACE) {
+                    throw error(AbstractParser.message("trailing.comma.in.json", type.getNameOrType()));
+                }
                 break;
 
             default:
--- a/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Fri Sep 20 19:17:04 2013 -0700
+++ b/nashorn/src/jdk/nashorn/internal/runtime/resources/Messages.properties	Mon Sep 23 14:20:45 2013 +0530
@@ -57,6 +57,7 @@
 parser.error.regex.unsupported.flag=Unsupported RegExp flag: {0}
 parser.error.regex.repeated.flag=Repeated RegExp flag: {0}
 parser.error.regex.syntax={0}
+parser.error.trailing.comma.in.json=Trailing comma is not allowed in JSON
 
 # strict mode error messages
 parser.error.strict.no.with="with" statement cannot be used in strict mode
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025147.js	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,41 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8025147: Trailing comma is not allowed in JSONArray and JSONObject
+ *
+ * @test
+ * @run
+ */
+
+function check(str) {
+    try {
+        JSON.parse(str);
+        fail("should have thrown SyntaxError for " + str);
+    } catch (e) {
+        print(e);
+    }
+}
+
+check("{ \"a\": 333, }");
+check("[ 4343, ]");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025147.js.EXPECTED	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,6 @@
+SyntaxError: Invalid JSON: <json>:1:12 Trailing comma is not allowed in JSON
+{ "a": 333, }
+            ^
+SyntaxError: Invalid JSON: <json>:1:8 Trailing comma is not allowed in JSON
+[ 4343, ]
+        ^
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025149.js	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8025149: JSON.stringify does not handle 'space' argument as per the spec.
+ *
+ * @test
+ * @run
+ */
+
+print(JSON.stringify({ foo : 23, bar: { x : 22} }, undefined ,new Number(Infinity)));
+
+print(JSON.stringify({ foo : 23, bar: { x : 22} }, undefined ,new Number(-Infinity)));
+
+try {
+    JSON.stringify({},[],
+    (n = new Number(0), n.valueOf = function() { throw ("inside n.valueOf") }, n));
+} catch (e) {
+    print(e);
+}
+
+try {
+    JSON.stringify({},[],
+    (s = new String(""), s.toString = function() { throw ("inside s.toString") }, s));
+} catch (e) {
+    print(e);
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025149.js.EXPECTED	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,9 @@
+{
+          "foo": 23,
+          "bar": {
+                    "x": 22
+          }
+}
+{"foo":23,"bar":{"x":22}}
+inside n.valueOf
+inside s.toString
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025163.js	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,39 @@
+/*
+ * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ * 
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ * 
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ * 
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ * 
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/**
+ * JDK-8025163: Date methods should not return -0
+ *
+ * @test
+ * @run
+ */
+
+print(1 / (new Date(0, 0, 1)).getYear());
+print(1 / (new Date(1969, 1, 2)).getDay());
+print(1 / (new Date(1969, 0, 1)).getHours());
+print(1 / (new Date(1969, 0, 1)).getHours());
+print(1 / (new Date(1969, 0, 1)).getMinutes());
+print(1 / (new Date(1969, 0, 1)).getSeconds());
+print(1 / (new Date(1969, 0, 1)).getMilliseconds());
+print(1 / (new Date(1969, 0, 1)).getMilliseconds());
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8025163.js.EXPECTED	Mon Sep 23 14:20:45 2013 +0530
@@ -0,0 +1,8 @@
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity
+Infinity