8066236: RuntimeNode forces copy creation on visitation
authorattila
Wed, 10 Dec 2014 12:30:48 +0100
changeset 27972 8ec664fdf8da
parent 27971 bff4872beee5
child 27973 ecc16e813691
8066236: RuntimeNode forces copy creation on visitation Reviewed-by: hannesw, lagergren
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java
nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/RuntimeNode.java
nashorn/test/script/basic/JDK-8066236.js
nashorn/test/script/basic/JDK-8066236.js.EXPECTED
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java	Wed Dec 10 11:55:25 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/codegen/LocalVariableTypesCalculator.java	Wed Dec 10 12:30:48 2014 +0100
@@ -93,6 +93,13 @@
  * variable to its widest used type after the join point. That would eliminate some widenings of undefined variables to
  * object, most notably those used only in loops. We need a full liveness analysis for that. Currently, we can establish
  * per-type liveness, which eliminates most of unwanted dead widenings.
+ * NOTE: the way this class is implemented, it actually processes the AST in two passes. The first pass is top-down and
+ * implemented in {@code enterXxx} methods. This pass does not mutate the AST (except for one occurrence, noted below),
+ * as being able to find relevant labels for control flow joins is sensitive to their reference identity, and mutated
+ * label-carrying nodes will create copies of their labels. A second bottom-up pass applying the changes is implemented
+ * in the separate visitor sitting in {@link #leaveFunctionNode(FunctionNode)}. This visitor will also instantiate new
+ * instances of the calculator to be run on nested functions (when not lazy compiling).
+ *
  */
 final class LocalVariableTypesCalculator extends NodeVisitor<LexicalContext>{
 
--- a/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/RuntimeNode.java	Wed Dec 10 11:55:25 2014 +0100
+++ b/nashorn/src/jdk.scripting.nashorn/share/classes/jdk/nashorn/internal/ir/RuntimeNode.java	Wed Dec 10 12:30:48 2014 +0100
@@ -27,7 +27,6 @@
 
 import static jdk.nashorn.internal.runtime.UnwarrantedOptimismException.INVALID_PROGRAM_POINT;
 
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
@@ -468,11 +467,7 @@
     @Override
     public Node accept(final NodeVisitor<? extends LexicalContext> visitor) {
         if (visitor.enterRuntimeNode(this)) {
-            final List<Expression> newArgs = new ArrayList<>();
-            for (final Node arg : args) {
-                newArgs.add((Expression)arg.accept(visitor));
-            }
-            return visitor.leaveRuntimeNode(setArgs(newArgs));
+            return visitor.leaveRuntimeNode(setArgs(Node.accept(visitor, args)));
         }
 
         return this;
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066236.js	Wed Dec 10 12:30:48 2014 +0100
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2014 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-8066236: RuntimeNode forces copy creation on visitation
+ *
+ * @test
+ * @run
+ */
+
+// Note: we're using Function("code") instead of (function(){ code }) so that
+// we don't trigger parser API validation in JDK-8008448 tests. The test code
+// encapsulated in functions below can't be correctly handled by the parser API
+// currently, as it contains parser-generated REFERENCE_ERROR runtime nodes.
+try {
+    Function("L: {this = x;break L}")();
+} catch (e) {
+   print("threw ReferenceError: " + (e instanceof ReferenceError));
+}
+try {
+    Function("L:with(this--)break L;")();
+} catch (e) {
+   print("threw ReferenceError: " + (e instanceof ReferenceError));
+}
+Function("L:with(Object in Object)break L;")();
+print("SUCCESS");
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/nashorn/test/script/basic/JDK-8066236.js.EXPECTED	Wed Dec 10 12:30:48 2014 +0100
@@ -0,0 +1,3 @@
+threw ReferenceError: true
+threw ReferenceError: true
+SUCCESS