8177691: Labeled break in catch and finally works wrongly, when invoked through nashorn
authorsdama
Fri, 01 Sep 2017 06:01:08 +0530
changeset 47038 f57fa7f112a0
parent 47037 07210623c057
child 47039 c7bdfebc55a9
8177691: Labeled break in catch and finally works wrongly, when invoked through nashorn Summary: Added support to check if the block contains goto statements before flagging it as terminal Reviewed-by: hannesw, jlaskey Contributed-by: srinivas.dama@oracle.com
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Block.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java
nashorn/test/script/basic/JDK-8177691.js
nashorn/test/script/basic/JDK-8177691.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Block.java	Wed Aug 30 18:47:37 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/Block.java	Fri Sep 01 06:01:08 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2017, 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
@@ -100,6 +100,12 @@
     public static final int IS_SWITCH_BLOCK    = 1 << 7;
 
     /**
+     * Is this block tagged as breakable based on its contents
+     * (block having labelled break statement)
+     */
+    public static final int IS_BREAKABLE       = 1 << 8;
+
+    /**
      * Constructor
      *
      * @param token      The first token of the block
@@ -315,7 +321,7 @@
      */
     @Override
     public boolean isTerminal() {
-        return getFlag(IS_TERMINAL);
+        return getFlag(IS_TERMINAL) && !getFlag(IS_BREAKABLE);
     }
 
     /**
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java	Wed Aug 30 18:47:37 2017 +0200
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/parser/Parser.java	Fri Sep 01 06:01:08 2017 +0530
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2017, 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
@@ -2274,6 +2274,11 @@
         //targetNode is what we are breaking out from.
         final String labelName = labelNode == null ? null : labelNode.getLabelName();
         final ParserContextBreakableNode targetNode = lc.getBreakable(labelName);
+
+        if( targetNode instanceof ParserContextBlockNode) {
+            targetNode.setFlag(Block.IS_BREAKABLE);
+        }
+
         if (targetNode == null) {
             throw error(AbstractParser.message("illegal.break.stmt"), breakToken);
         }
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8177691.js	Fri Sep 01 06:01:08 2017 +0530
@@ -0,0 +1,71 @@
+/*
+ * Copyright (c) 2017, 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-8177691: Labeled break in catch and finally works wrongly, when invoked through nashorn
+ *
+ * @test
+ * @run
+ */
+
+function box1() {
+    label: {
+        try {
+            throw 1;
+        }
+        catch (e) {
+            break label;
+        }
+        throw 2;
+    }
+    return 'OK';
+};
+
+function box2() {
+    label: {
+        try {
+            throw 1;
+        }
+        finally {
+            break label;
+        }
+        throw 2;
+    }
+    return 'OK';
+};
+
+function box3() {
+    label: {
+        try {
+            throw 1;
+        }
+        finally {
+            break label;
+        }
+    }
+    return 'OK';
+};
+
+print(box1());
+print(box2());
+print(box3());
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8177691.js.EXPECTED	Fri Sep 01 06:01:08 2017 +0530
@@ -0,0 +1,3 @@
+OK
+OK
+OK