From 93c8cbec9c0d600ea74c4ad3adf6e0d5edc85b53 Mon Sep 17 00:00:00 2001 From: Athijegannathan Sundararajan Date: Tue, 24 Sep 2013 20:43:42 +0530 Subject: [PATCH 1/5] 8025312: parseInt should convert 'radix' argument to ToInt32 even if empty string is parsed Reviewed-by: jlaskey, hannesw --- .../internal/runtime/GlobalFunctions.java | 2 +- nashorn/test/script/basic/JDK-8025312.js | 35 +++++++++++++++++++ .../test/script/basic/JDK-8025312.js.EXPECTED | 1 + 3 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 nashorn/test/script/basic/JDK-8025312.js create mode 100644 nashorn/test/script/basic/JDK-8025312.js.EXPECTED diff --git a/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java b/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java index c504276f41c..9ddc7090b2b 100644 --- a/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java +++ b/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java @@ -90,6 +90,7 @@ public final class GlobalFunctions { public static double parseInt(final Object self, final Object string, final Object rad) { final String str = JSType.trimLeft(JSType.toString(string)); final int length = str.length(); + int radix = JSType.toInt32(rad); // empty string is not valid if (length == 0) { @@ -113,7 +114,6 @@ public final class GlobalFunctions { } boolean stripPrefix = true; - int radix = JSType.toInt32(rad); if (radix != 0) { if (radix < 2 || radix > 36) { diff --git a/nashorn/test/script/basic/JDK-8025312.js b/nashorn/test/script/basic/JDK-8025312.js new file mode 100644 index 00000000000..6f2b42f38af --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025312.js @@ -0,0 +1,35 @@ +/* + * 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-8025312: parseInt should convert 'radix' argument to ToInt32 even if empty string is parsed + * + * @test + * @run + */ + +parseInt("", { + valueOf: function() { + print("inside valueOf of 'radix'"); + } +}); diff --git a/nashorn/test/script/basic/JDK-8025312.js.EXPECTED b/nashorn/test/script/basic/JDK-8025312.js.EXPECTED new file mode 100644 index 00000000000..31ffef83890 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025312.js.EXPECTED @@ -0,0 +1 @@ +inside valueOf of 'radix' From f8d5db06b9ca8423b4b5982c9c9cb8b923950d24 Mon Sep 17 00:00:00 2001 From: Athijegannathan Sundararajan Date: Wed, 25 Sep 2013 08:17:37 +0530 Subject: [PATCH 2/5] 8025325: parseFloat does not handle '.' in exponent part Reviewed-by: hannesw --- .../internal/runtime/GlobalFunctions.java | 2 +- nashorn/test/script/basic/JDK-8025325.js | 35 +++++++++++++++++++ .../test/script/basic/JDK-8025325.js.EXPECTED | 5 +++ 3 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 nashorn/test/script/basic/JDK-8025325.js create mode 100644 nashorn/test/script/basic/JDK-8025325.js.EXPECTED diff --git a/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java b/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java index 9ddc7090b2b..c750d80e913 100644 --- a/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java +++ b/nashorn/src/jdk/nashorn/internal/runtime/GlobalFunctions.java @@ -211,7 +211,7 @@ loop: switch (ch) { case '.': // dot allowed only once - if (dotSeen) { + if (exponentOffset != -1 || dotSeen) { break loop; } dotSeen = true; diff --git a/nashorn/test/script/basic/JDK-8025325.js b/nashorn/test/script/basic/JDK-8025325.js new file mode 100644 index 00000000000..63dfef9d96e --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025325.js @@ -0,0 +1,35 @@ +/* + * 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-8025325: parseFloat does not handle '.' in exponent part + * + * @test + * @run + */ + +print(parseFloat("2e2.")); +print(parseFloat("2e2.3")); +print(parseFloat("2e2.fdgdf")); +print(parseFloat("2e2. gdfgdf")); +print(parseFloat("2e2. ")); diff --git a/nashorn/test/script/basic/JDK-8025325.js.EXPECTED b/nashorn/test/script/basic/JDK-8025325.js.EXPECTED new file mode 100644 index 00000000000..4ddf679a54b --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025325.js.EXPECTED @@ -0,0 +1,5 @@ +200 +200 +200 +200 +200 From 0414dcb01d17910a5a7946b4eab779bc75790657 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Walln=C3=B6fer?= Date: Wed, 25 Sep 2013 16:37:56 +0200 Subject: [PATCH 3/5] 8025434: RegExp lastIndex can exceed int range Reviewed-by: lagergren, sundar --- .../internal/objects/NativeRegExp.java | 2 +- nashorn/test/script/basic/JDK-8025434.js | 66 +++++++++++++++++++ 2 files changed, 67 insertions(+), 1 deletion(-) create mode 100644 nashorn/test/script/basic/JDK-8025434.js diff --git a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java index 5a2196356fe..2c4d3324e60 100644 --- a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java +++ b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java @@ -883,7 +883,7 @@ public final class NativeRegExp extends ScriptObject { * @return last index property as int */ public int getLastIndex() { - return JSType.toInt32(lastIndex); + return JSType.toInteger(lastIndex); } /** diff --git a/nashorn/test/script/basic/JDK-8025434.js b/nashorn/test/script/basic/JDK-8025434.js new file mode 100644 index 00000000000..1d42959416d --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025434.js @@ -0,0 +1,66 @@ +/* + * 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-8025434: RegExp lastIndex can exceed int range + * + * @test + * @run + */ + +var r = /a/g; + +r.lastIndex = 0x100000000; +if (r.test("a")) { + throw new Error("Expected no match"); +} + +r.lastIndex = 0x100000000000000; +if (r.test("a")) { + throw new Error("Expected no match"); +} + +r.lastIndex = -0x100000000; +if (r.test("a")) { + throw new Error("Expected match"); +} + +r.lastIndex = -0x100000000000000; +if (r.test("a")) { + throw new Error("Expected no match"); +} + +r.lastIndex = 1; +if (r.test("a")) { + throw new Error("Expected no match"); +} + +r.lastIndex = -1; +if (r.test("a")) { + throw new Error("Expected no match"); +} + +r.lastIndex = 0; +if (!r.test("a")) { + throw new Error("Expected match"); +} From abc7352b43e5fbd043c49933af0e93eb6a584b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Walln=C3=B6fer?= Date: Thu, 26 Sep 2013 10:14:24 +0200 Subject: [PATCH 4/5] 8025197: String replace method fails with regexp /$/gi Reviewed-by: sundar --- .../internal/objects/NativeRegExp.java | 7 +++- nashorn/test/script/basic/JDK-8025197.js | 37 +++++++++++++++++++ .../test/script/basic/JDK-8025197.js.EXPECTED | 7 ++++ 3 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 nashorn/test/script/basic/JDK-8025197.js create mode 100644 nashorn/test/script/basic/JDK-8025197.js.EXPECTED diff --git a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java index 2c4d3324e60..be4a93d0fdc 100644 --- a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java +++ b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java @@ -697,8 +697,13 @@ public final class NativeRegExp extends ScriptObject { appendReplacement(matcher, string, replacement, sb); } - // ECMA 15.5.4.10 String.prototype.match(regexp) thisIndex = matcher.end(); + if (thisIndex == string.length() && matcher.start() == matcher.end()) { + // Avoid getting empty match at end of string twice + break; + } + + // ECMA 15.5.4.10 String.prototype.match(regexp) if (thisIndex == previousLastIndex) { setLastIndex(thisIndex + 1); previousLastIndex = thisIndex + 1; diff --git a/nashorn/test/script/basic/JDK-8025197.js b/nashorn/test/script/basic/JDK-8025197.js new file mode 100644 index 00000000000..5099b673691 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025197.js @@ -0,0 +1,37 @@ +/* + * 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-8025197: String replace method fails with regexp /$/gi + * + * @test + * @run + */ + +print('dog'.replace(/$/gi, 's')); +print('dog'.replace(/(?:g)$/gi, 's')); +print('dog'.replace(/(?:a)$/gi, 's')); +print('dog'.replace(/(?!g)$/gi, 's')); +print('dog'.replace(/(?!a)$/gi, 's')); +print('dog'.replace(/g?$/gi, 's')); +print('dog'.replace(/.?$/gi, 's')); diff --git a/nashorn/test/script/basic/JDK-8025197.js.EXPECTED b/nashorn/test/script/basic/JDK-8025197.js.EXPECTED new file mode 100644 index 00000000000..9f2a08f67e5 --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025197.js.EXPECTED @@ -0,0 +1,7 @@ +dogs +dos +dog +dogs +dogs +doss +doss From 2e8063226ddac217a1c62fb99b68ede844c9281f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannes=20Walln=C3=B6fer?= Date: Thu, 26 Sep 2013 11:47:24 +0200 Subject: [PATCH 5/5] 8025486: RegExp constructor arguments are not evaluated in right order Reviewed-by: sundar --- .../internal/objects/NativeRegExp.java | 16 +++--- nashorn/test/script/basic/JDK-8025486.js | 55 +++++++++++++++++++ .../test/script/basic/JDK-8025486.js.EXPECTED | 3 + 3 files changed, 65 insertions(+), 9 deletions(-) create mode 100644 nashorn/test/script/basic/JDK-8025486.js create mode 100644 nashorn/test/script/basic/JDK-8025486.js.EXPECTED diff --git a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java index be4a93d0fdc..318ca2a0edd 100644 --- a/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java +++ b/nashorn/src/jdk/nashorn/internal/objects/NativeRegExp.java @@ -191,23 +191,21 @@ public final class NativeRegExp extends ScriptObject { public static NativeRegExp newRegExp(final Object regexp, final Object flags) { String patternString = ""; String flagString = ""; - boolean flagsDefined = false; - - if (flags != UNDEFINED) { - flagsDefined = true; - flagString = JSType.toString(flags); - } if (regexp != UNDEFINED) { if (regexp instanceof NativeRegExp) { - if (!flagsDefined) { - return (NativeRegExp)regexp; // 15.10.3.1 - undefined flags and regexp as + if (flags != UNDEFINED) { + throw typeError("regex.cant.supply.flags"); } - throw typeError("regex.cant.supply.flags"); + return (NativeRegExp)regexp; // 15.10.3.1 - undefined flags and regexp as } patternString = JSType.toString(regexp); } + if (flags != UNDEFINED) { + flagString = JSType.toString(flags); + } + return new NativeRegExp(patternString, flagString); } diff --git a/nashorn/test/script/basic/JDK-8025486.js b/nashorn/test/script/basic/JDK-8025486.js new file mode 100644 index 00000000000..8012f72383c --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025486.js @@ -0,0 +1,55 @@ +/* + * 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-8025486: RegExp constructor arguments are not evaluated in right order + * + * @test + * @run + */ + +new RegExp({ + toString: function() { + print("source"); + return "a"; + } +}, { + toString: function() { + print("flags"); + return "g"; + } +}); + +try { + new RegExp(/asdf/, { + toString: function() { + fail("toString should not be called"); + } + }); + fail("expected TypeError"); +} catch (e) { + if (!(e instanceof TypeError)) { + fail("expected TypeError"); + } + print(e); +} diff --git a/nashorn/test/script/basic/JDK-8025486.js.EXPECTED b/nashorn/test/script/basic/JDK-8025486.js.EXPECTED new file mode 100644 index 00000000000..cf46bd7a29d --- /dev/null +++ b/nashorn/test/script/basic/JDK-8025486.js.EXPECTED @@ -0,0 +1,3 @@ +source +flags +TypeError: Cannot supply flags when constructing one RegExp from another