Fri, 31 Oct 2014 20:19:49 +0100
8062583: Throwing object with error prototype causes error proto to be caught
Reviewed-by: sundar, jlaskey
1.1 --- a/src/jdk/nashorn/internal/runtime/ECMAException.java Fri Oct 31 16:29:22 2014 +0100 1.2 +++ b/src/jdk/nashorn/internal/runtime/ECMAException.java Fri Oct 31 20:19:49 2014 +0100 1.3 @@ -96,15 +96,17 @@ 1.4 // If thrown object is an Error or sub-object like TypeError, then 1.5 // an ECMAException object has been already initialized at constructor. 1.6 if (thrown instanceof ScriptObject) { 1.7 - final ScriptObject sobj = (ScriptObject)thrown; 1.8 - final Object exception = getException(sobj); 1.9 + final Object exception = getException((ScriptObject)thrown); 1.10 if (exception instanceof ECMAException) { 1.11 - // copy over file name, line number and column number. 1.12 final ECMAException ee = (ECMAException)exception; 1.13 - ee.setFileName(fileName); 1.14 - ee.setLineNumber(line); 1.15 - ee.setColumnNumber(column); 1.16 - return ee; 1.17 + // Make sure exception has correct thrown reference because that's what will end up getting caught. 1.18 + if (ee.getThrown() == thrown) { 1.19 + // copy over file name, line number and column number. 1.20 + ee.setFileName(fileName); 1.21 + ee.setLineNumber(line); 1.22 + ee.setColumnNumber(column); 1.23 + return ee; 1.24 + } 1.25 } 1.26 } 1.27 1.28 @@ -154,7 +156,11 @@ 1.29 * @return a {@link ECMAException} 1.30 */ 1.31 public static Object getException(final ScriptObject errObj) { 1.32 - return errObj.get(ECMAException.EXCEPTION_PROPERTY); 1.33 + // Exclude inherited properties that may belong to errors in the prototype chain. 1.34 + if (errObj.hasOwnProperty(ECMAException.EXCEPTION_PROPERTY)) { 1.35 + return errObj.get(ECMAException.EXCEPTION_PROPERTY); 1.36 + } 1.37 + return null; 1.38 } 1.39 1.40 /**
2.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 2.2 +++ b/test/script/basic/JDK-8062583.js Fri Oct 31 20:19:49 2014 +0100 2.3 @@ -0,0 +1,51 @@ 2.4 +/* 2.5 + * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved. 2.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 2.7 + * 2.8 + * This code is free software; you can redistribute it and/or modify it 2.9 + * under the terms of the GNU General Public License version 2 only, as 2.10 + * published by the Free Software Foundation. 2.11 + * 2.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 2.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 2.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 2.15 + * version 2 for more details (a copy is included in the LICENSE file that 2.16 + * accompanied this code). 2.17 + * 2.18 + * You should have received a copy of the GNU General Public License version 2.19 + * 2 along with this work; if not, write to the Free Software Foundation, 2.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2.21 + * 2.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2.23 + * or visit www.oracle.com if you need additional information or have any 2.24 + * questions. 2.25 + */ 2.26 + 2.27 +/** 2.28 + * JDK-8062583: Throwing object with error prototype causes error proto to be caught 2.29 + * 2.30 + * @test 2.31 + * @run 2.32 + */ 2.33 + 2.34 +function CustomError() { 2.35 + this.name = "CustomError"; 2.36 +} 2.37 + 2.38 +CustomError.prototype = new Error(); 2.39 + 2.40 +var c1 = new CustomError(); 2.41 + 2.42 +try { 2.43 + throw c1; 2.44 +} catch (e) { 2.45 + print(e === c1); 2.46 + print(e === CustomError.prototype); 2.47 + print(e.stack.replace(/\\/g, '/')); 2.48 + print(e.nashornException.toString().replace(/\\/g, '/')); 2.49 +} 2.50 + 2.51 +var c2 = new CustomError(); 2.52 +Error.captureStackTrace(c2); 2.53 +print(c2.stack.replace(/\\/g, '/')); 2.54 +print(c2.nashornException.toString().replace(/\\/g, '/'));
3.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 3.2 +++ b/test/script/basic/JDK-8062583.js.EXPECTED Fri Oct 31 20:19:49 2014 +0100 3.3 @@ -0,0 +1,8 @@ 3.4 +true 3.5 +false 3.6 +CustomError 3.7 + at <program> (test/script/basic/JDK-8062583.js:40) 3.8 +test/script/basic/JDK-8062583.js:40:4 CustomError 3.9 +CustomError 3.10 + at <program> (test/script/basic/JDK-8062583.js:49) 3.11 +test/script/basic/JDK-8062583.js:49 CustomError