Wed, 03 Jul 2013 13:03:36 +0200
8019585: Sometimes a var declaration using itself in its init wasn't declared as canBeUndefined, causing erroneous bytecode
Reviewed-by: sundar, attila
1.1 --- a/src/jdk/nashorn/api/scripting/NashornException.java Wed Jul 03 12:39:28 2013 +0200 1.2 +++ b/src/jdk/nashorn/api/scripting/NashornException.java Wed Jul 03 13:03:36 2013 +0200 1.3 @@ -146,7 +146,7 @@ 1.4 * @return array of javascript stack frames 1.5 */ 1.6 public static StackTraceElement[] getScriptFrames(final Throwable exception) { 1.7 - final StackTraceElement[] frames = ((Throwable)exception).getStackTrace(); 1.8 + final StackTraceElement[] frames = exception.getStackTrace(); 1.9 final List<StackTraceElement> filtered = new ArrayList<>(); 1.10 for (final StackTraceElement st : frames) { 1.11 if (ECMAErrors.isScriptFrame(st)) { 1.12 @@ -170,7 +170,7 @@ 1.13 */ 1.14 public static String getScriptStackString(final Throwable exception) { 1.15 final StringBuilder buf = new StringBuilder(); 1.16 - final StackTraceElement[] frames = getScriptFrames((Throwable)exception); 1.17 + final StackTraceElement[] frames = getScriptFrames(exception); 1.18 for (final StackTraceElement st : frames) { 1.19 buf.append("\tat "); 1.20 buf.append(st.getMethodName());
2.1 --- a/src/jdk/nashorn/internal/codegen/Attr.java Wed Jul 03 12:39:28 2013 +0200 2.2 +++ b/src/jdk/nashorn/internal/codegen/Attr.java Wed Jul 03 13:03:36 2013 +0200 2.3 @@ -54,6 +54,7 @@ 2.4 import java.util.Iterator; 2.5 import java.util.List; 2.6 import java.util.Set; 2.7 + 2.8 import jdk.nashorn.internal.codegen.types.Type; 2.9 import jdk.nashorn.internal.ir.AccessNode; 2.10 import jdk.nashorn.internal.ir.BinaryNode; 2.11 @@ -234,10 +235,25 @@ 2.12 @Override 2.13 public boolean enterVarNode(final VarNode varNode) { 2.14 final String name = varNode.getName().getName(); 2.15 - //if this is used the var node symbol needs to be tagged as can be undefined 2.16 + //if this is used before the var node, the var node symbol needs to be tagged as can be undefined 2.17 if (uses.contains(name)) { 2.18 canBeUndefined.add(name); 2.19 } 2.20 + 2.21 + // all uses of the declared varnode inside the var node are potentially undefined 2.22 + // however this is a bit conservative as e.g. var x = 17; var x = 1 + x; does work 2.23 + if (!varNode.isFunctionDeclaration() && varNode.getInit() != null) { 2.24 + varNode.getInit().accept(new NodeVisitor<LexicalContext>(new LexicalContext()) { 2.25 + @Override 2.26 + public boolean enterIdentNode(final IdentNode identNode) { 2.27 + if (name.equals(identNode.getName())) { 2.28 + canBeUndefined.add(name); 2.29 + } 2.30 + return false; 2.31 + } 2.32 + }); 2.33 + } 2.34 + 2.35 return true; 2.36 } 2.37 2.38 @@ -257,6 +273,7 @@ 2.39 } 2.40 return varNode.setName((IdentNode)ident.setSymbol(lc, symbol)); 2.41 } 2.42 + 2.43 return varNode; 2.44 } 2.45 });
3.1 --- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java Wed Jul 03 12:39:28 2013 +0200 3.2 +++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java Wed Jul 03 13:03:36 2013 +0200 3.3 @@ -1847,7 +1847,7 @@ 3.4 // If expression not int see if we can convert, if not use deflt to trigger default. 3.5 if (!type.isInteger()) { 3.6 method.load(deflt); 3.7 - final Class exprClass = type.getTypeClass(); 3.8 + final Class<?> exprClass = type.getTypeClass(); 3.9 method.invoke(staticCallNoLookup(ScriptRuntime.class, "switchTagAsInt", int.class, exprClass.isPrimitive()? exprClass : Object.class, int.class)); 3.10 } 3.11
4.1 --- a/src/jdk/nashorn/internal/objects/ArrayBufferView.java Wed Jul 03 12:39:28 2013 +0200 4.2 +++ b/src/jdk/nashorn/internal/objects/ArrayBufferView.java Wed Jul 03 13:03:36 2013 +0200 4.3 @@ -40,6 +40,7 @@ 4.4 abstract class ArrayBufferView extends ScriptObject { 4.5 4.6 // initialized by nasgen 4.7 + @SuppressWarnings("unused") 4.8 private static PropertyMap $nasgenmap$; 4.9 4.10 ArrayBufferView(final NativeArrayBuffer buffer, final int byteOffset, final int elementLength) {
5.1 --- a/src/jdk/nashorn/internal/objects/Global.java Wed Jul 03 12:39:28 2013 +0200 5.2 +++ b/src/jdk/nashorn/internal/objects/Global.java Wed Jul 03 13:03:36 2013 +0200 5.3 @@ -382,6 +382,7 @@ 5.4 private final Context context; 5.5 5.6 // initialized by nasgen 5.7 + @SuppressWarnings("unused") 5.8 private static PropertyMap $nasgenmap$; 5.9 5.10 /**
6.1 --- a/src/jdk/nashorn/internal/objects/NativeError.java Wed Jul 03 12:39:28 2013 +0200 6.2 +++ b/src/jdk/nashorn/internal/objects/NativeError.java Wed Jul 03 13:03:36 2013 +0200 6.3 @@ -119,6 +119,7 @@ 6.4 * Nashorn extension: Error.captureStackTrace. Capture stack trace at the point of call into the Error object provided. 6.5 * 6.6 * @param self self reference 6.7 + * @param errorObj the error object 6.8 * @return undefined 6.9 */ 6.10 @Function(attributes = Attribute.NOT_ENUMERABLE, where = Where.CONSTRUCTOR) 6.11 @@ -286,9 +287,9 @@ 6.12 final Object exception = ECMAException.getException(sobj); 6.13 if (exception instanceof Throwable) { 6.14 return getScriptStackString(sobj, (Throwable)exception); 6.15 - } else { 6.16 - return ""; 6.17 } 6.18 + 6.19 + return ""; 6.20 } 6.21 6.22 /**
7.1 --- a/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Wed Jul 03 12:39:28 2013 +0200 7.2 +++ b/src/jdk/nashorn/internal/objects/NativeFloat32Array.java Wed Jul 03 13:03:36 2013 +0200 7.3 @@ -48,6 +48,7 @@ 7.4 public static final int BYTES_PER_ELEMENT = 4; 7.5 7.6 // initialized by nasgen 7.7 + @SuppressWarnings("unused") 7.8 private static PropertyMap $nasgenmap$; 7.9 7.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
8.1 --- a/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Wed Jul 03 12:39:28 2013 +0200 8.2 +++ b/src/jdk/nashorn/internal/objects/NativeFloat64Array.java Wed Jul 03 13:03:36 2013 +0200 8.3 @@ -48,6 +48,7 @@ 8.4 public static final int BYTES_PER_ELEMENT = 8; 8.5 8.6 // initialized by nasgen 8.7 + @SuppressWarnings("unused") 8.8 private static PropertyMap $nasgenmap$; 8.9 8.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
9.1 --- a/src/jdk/nashorn/internal/objects/NativeFunction.java Wed Jul 03 12:39:28 2013 +0200 9.2 +++ b/src/jdk/nashorn/internal/objects/NativeFunction.java Wed Jul 03 13:03:36 2013 +0200 9.3 @@ -29,6 +29,7 @@ 9.4 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 9.5 9.6 import java.util.List; 9.7 + 9.8 import jdk.nashorn.api.scripting.ScriptObjectMirror; 9.9 import jdk.nashorn.internal.objects.annotations.Attribute; 9.10 import jdk.nashorn.internal.objects.annotations.Constructor; 9.11 @@ -55,6 +56,7 @@ 9.12 public final class NativeFunction { 9.13 9.14 // initialized by nasgen 9.15 + @SuppressWarnings("unused") 9.16 private static PropertyMap $nasgenmap$; 9.17 9.18 // do *not* create me!
10.1 --- a/src/jdk/nashorn/internal/objects/NativeInt16Array.java Wed Jul 03 12:39:28 2013 +0200 10.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt16Array.java Wed Jul 03 13:03:36 2013 +0200 10.3 @@ -42,6 +42,7 @@ 10.4 public final class NativeInt16Array extends ArrayBufferView { 10.5 10.6 // initialized by nasgen 10.7 + @SuppressWarnings("unused") 10.8 private static PropertyMap $nasgenmap$; 10.9 10.10 /**
11.1 --- a/src/jdk/nashorn/internal/objects/NativeInt32Array.java Wed Jul 03 12:39:28 2013 +0200 11.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt32Array.java Wed Jul 03 13:03:36 2013 +0200 11.3 @@ -47,6 +47,7 @@ 11.4 public static final int BYTES_PER_ELEMENT = 4; 11.5 11.6 // initialized by nasgen 11.7 + @SuppressWarnings("unused") 11.8 private static PropertyMap $nasgenmap$; 11.9 11.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
12.1 --- a/src/jdk/nashorn/internal/objects/NativeInt8Array.java Wed Jul 03 12:39:28 2013 +0200 12.2 +++ b/src/jdk/nashorn/internal/objects/NativeInt8Array.java Wed Jul 03 13:03:36 2013 +0200 12.3 @@ -47,6 +47,7 @@ 12.4 public static final int BYTES_PER_ELEMENT = 1; 12.5 12.6 // initialized by nasgen 12.7 + @SuppressWarnings("unused") 12.8 private static PropertyMap $nasgenmap$; 12.9 12.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
13.1 --- a/src/jdk/nashorn/internal/objects/NativeJava.java Wed Jul 03 12:39:28 2013 +0200 13.2 +++ b/src/jdk/nashorn/internal/objects/NativeJava.java Wed Jul 03 13:03:36 2013 +0200 13.3 @@ -32,6 +32,7 @@ 13.4 import java.util.Collection; 13.5 import java.util.Deque; 13.6 import java.util.List; 13.7 + 13.8 import jdk.internal.dynalink.beans.StaticClass; 13.9 import jdk.internal.dynalink.support.TypeUtilities; 13.10 import jdk.nashorn.internal.objects.annotations.Attribute; 13.11 @@ -54,6 +55,7 @@ 13.12 public final class NativeJava { 13.13 13.14 // initialized by nasgen 13.15 + @SuppressWarnings("unused") 13.16 private static PropertyMap $nasgenmap$; 13.17 13.18 private NativeJava() {
14.1 --- a/src/jdk/nashorn/internal/objects/NativeObject.java Wed Jul 03 12:39:28 2013 +0200 14.2 +++ b/src/jdk/nashorn/internal/objects/NativeObject.java Wed Jul 03 13:03:36 2013 +0200 14.3 @@ -27,7 +27,6 @@ 14.4 14.5 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError; 14.6 import static jdk.nashorn.internal.runtime.ScriptRuntime.UNDEFINED; 14.7 - 14.8 import jdk.nashorn.api.scripting.ScriptObjectMirror; 14.9 import jdk.nashorn.internal.objects.annotations.Attribute; 14.10 import jdk.nashorn.internal.objects.annotations.Constructor; 14.11 @@ -55,6 +54,7 @@ 14.12 private static final InvokeByName TO_STRING = new InvokeByName("toString", ScriptObject.class); 14.13 14.14 // initialized by nasgen 14.15 + @SuppressWarnings("unused") 14.16 private static PropertyMap $nasgenmap$; 14.17 14.18 private NativeObject() {
15.1 --- a/src/jdk/nashorn/internal/objects/NativeRegExp.java Wed Jul 03 12:39:28 2013 +0200 15.2 +++ b/src/jdk/nashorn/internal/objects/NativeRegExp.java Wed Jul 03 13:03:36 2013 +0200 15.3 @@ -68,6 +68,7 @@ 15.4 private Global globalObject; 15.5 15.6 // initialized by nasgen 15.7 + @SuppressWarnings("unused") 15.8 private static PropertyMap $nasgenmap$; 15.9 15.10 NativeRegExp(final String input, final String flagString) {
16.1 --- a/src/jdk/nashorn/internal/objects/NativeUint16Array.java Wed Jul 03 12:39:28 2013 +0200 16.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint16Array.java Wed Jul 03 13:03:36 2013 +0200 16.3 @@ -47,6 +47,7 @@ 16.4 public static final int BYTES_PER_ELEMENT = 2; 16.5 16.6 // initialized by nasgen 16.7 + @SuppressWarnings("unused") 16.8 private static PropertyMap $nasgenmap$; 16.9 16.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
17.1 --- a/src/jdk/nashorn/internal/objects/NativeUint32Array.java Wed Jul 03 12:39:28 2013 +0200 17.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint32Array.java Wed Jul 03 13:03:36 2013 +0200 17.3 @@ -48,6 +48,7 @@ 17.4 public static final int BYTES_PER_ELEMENT = 4; 17.5 17.6 // initialized by nasgen 17.7 + @SuppressWarnings("unused") 17.8 private static PropertyMap $nasgenmap$; 17.9 17.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
18.1 --- a/src/jdk/nashorn/internal/objects/NativeUint8Array.java Wed Jul 03 12:39:28 2013 +0200 18.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint8Array.java Wed Jul 03 13:03:36 2013 +0200 18.3 @@ -47,6 +47,7 @@ 18.4 public static final int BYTES_PER_ELEMENT = 1; 18.5 18.6 // initialized by nasgen 18.7 + @SuppressWarnings("unused") 18.8 private static PropertyMap $nasgenmap$; 18.9 18.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
19.1 --- a/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Wed Jul 03 12:39:28 2013 +0200 19.2 +++ b/src/jdk/nashorn/internal/objects/NativeUint8ClampedArray.java Wed Jul 03 13:03:36 2013 +0200 19.3 @@ -48,6 +48,7 @@ 19.4 public static final int BYTES_PER_ELEMENT = 1; 19.5 19.6 // initialized by nasgen 19.7 + @SuppressWarnings("unused") 19.8 private static PropertyMap $nasgenmap$; 19.9 19.10 private static final Factory FACTORY = new Factory(BYTES_PER_ELEMENT) {
20.1 --- a/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java Wed Jul 03 12:39:28 2013 +0200 20.2 +++ b/src/jdk/nashorn/internal/objects/ScriptFunctionImpl.java Wed Jul 03 13:03:36 2013 +0200 20.3 @@ -149,12 +149,13 @@ 20.4 return typeErrorThrower; 20.5 } 20.6 20.7 - private static PropertyMap createStrictModeMap(PropertyMap map) { 20.8 + private static PropertyMap createStrictModeMap(final PropertyMap map) { 20.9 final int flags = Property.NOT_ENUMERABLE | Property.NOT_CONFIGURABLE; 20.10 + PropertyMap newMap = map; 20.11 // Need to add properties directly to map since slots are assigned speculatively by newUserAccessors. 20.12 - map = map.addProperty(map.newUserAccessors("arguments", flags)); 20.13 - map = map.addProperty(map.newUserAccessors("caller", flags)); 20.14 - return map; 20.15 + newMap = newMap.addProperty(map.newUserAccessors("arguments", flags)); 20.16 + newMap = newMap.addProperty(map.newUserAccessors("caller", flags)); 20.17 + return newMap; 20.18 } 20.19 20.20 // Choose the map based on strict mode!
21.1 --- a/src/jdk/nashorn/internal/runtime/arrays/ObjectArrayData.java Wed Jul 03 12:39:28 2013 +0200 21.2 +++ b/src/jdk/nashorn/internal/runtime/arrays/ObjectArrayData.java Wed Jul 03 13:03:36 2013 +0200 21.3 @@ -146,7 +146,7 @@ 21.4 21.5 @Override 21.6 public ArrayData setEmpty(final long lo, final long hi) { 21.7 - Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi, (long)Integer.MAX_VALUE), ScriptRuntime.EMPTY); 21.8 + Arrays.fill(array, (int)Math.max(lo, 0L), (int)Math.min(hi, Integer.MAX_VALUE), ScriptRuntime.EMPTY); 21.9 return this; 21.10 } 21.11
22.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 22.2 +++ b/test/script/basic/JDK-8019585.js Wed Jul 03 13:03:36 2013 +0200 22.3 @@ -0,0 +1,34 @@ 22.4 +/* 22.5 + * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 22.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 22.7 + * 22.8 + * This code is free software; you can redistribute it and/or modify it 22.9 + * under the terms of the GNU General Public License version 2 only, as 22.10 + * published by the Free Software Foundation. 22.11 + * 22.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 22.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 22.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 22.15 + * version 2 for more details (a copy is included in the LICENSE file that 22.16 + * accompanied this code). 22.17 + * 22.18 + * You should have received a copy of the GNU General Public License version 22.19 + * 2 along with this work; if not, write to the Free Software Foundation, 22.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 22.21 + * 22.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22.23 + * or visit www.oracle.com if you need additional information or have any 22.24 + * questions. 22.25 + */ 22.26 + 22.27 +/** 22.28 + * JDK-8019585 - use before def issues with vars using the declared var 22.29 + * legal - but needs to set "a" as undefined 22.30 + * 22.31 + * @test 22.32 + * @run 22.33 + */ 22.34 + 22.35 +function f() { 22.36 + var a = b == 17 && (a = toto(b)) && toto2(a); 22.37 +}