aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlagergren <none@none>2013-11-18 16:35:39 +0100
committerlagergren <none@none>2013-11-18 16:35:39 +0100
commit9dedc388106e53f4dc39989369bf496ded63d378 (patch)
treeeecb116ac5180da9c7c426b82b0f9a05df6cc453
parent7bbda0ea1a878c391f767c1e851db8e4c24f020e (diff)
downloadnashorn-9dedc388106e53f4dc39989369bf496ded63d378.tar.gz
8028434: Line number nodes were off for while nodes and do while nodes - the line number of a loop node should be treated as the location of the test expression
Reviewed-by: jlaskey, sundar
-rw-r--r--src/jdk/nashorn/internal/codegen/CodeGenerator.java6
-rw-r--r--src/jdk/nashorn/internal/ir/debug/PrintVisitor.java10
-rw-r--r--src/jdk/nashorn/internal/parser/Parser.java28
-rw-r--r--test/script/basic/JDK-8028434.js58
-rw-r--r--test/script/basic/JDK-8028434.js.EXPECTED1
5 files changed, 87 insertions, 16 deletions
diff --git a/src/jdk/nashorn/internal/codegen/CodeGenerator.java b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
index f5c1fb87..c3b1dbe2 100644
--- a/src/jdk/nashorn/internal/codegen/CodeGenerator.java
+++ b/src/jdk/nashorn/internal/codegen/CodeGenerator.java
@@ -2186,15 +2186,14 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
@Override
public boolean enterWhileNode(final WhileNode whileNode) {
- lineNumber(whileNode);
-
final Expression test = whileNode.getTest();
final Block body = whileNode.getBody();
final Label breakLabel = whileNode.getBreakLabel();
final Label continueLabel = whileNode.getContinueLabel();
+ final boolean isDoWhile = whileNode.isDoWhile();
final Label loopLabel = new Label("loop");
- if (!whileNode.isDoWhile()) {
+ if (!isDoWhile) {
method._goto(continueLabel);
}
@@ -2202,6 +2201,7 @@ final class CodeGenerator extends NodeOperatorVisitor<CodeGeneratorLexicalContex
body.accept(this);
if (!whileNode.isTerminal()) {
method.label(continueLabel);
+ lineNumber(whileNode);
new BranchOptimizer(this, method).execute(test, loopLabel, true);
method.label(breakLabel);
}
diff --git a/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java b/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java
index f939dc67..09af647d 100644
--- a/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java
+++ b/src/jdk/nashorn/internal/ir/debug/PrintVisitor.java
@@ -28,6 +28,7 @@ package jdk.nashorn.internal.ir.debug;
import java.util.List;
import jdk.nashorn.internal.ir.BinaryNode;
import jdk.nashorn.internal.ir.Block;
+import jdk.nashorn.internal.ir.BlockStatement;
import jdk.nashorn.internal.ir.CaseNode;
import jdk.nashorn.internal.ir.CatchNode;
import jdk.nashorn.internal.ir.ExpressionStatement;
@@ -141,7 +142,6 @@ public final class PrintVisitor extends NodeVisitor<LexicalContext> {
@Override
public boolean enterBlock(final Block block) {
sb.append(' ');
- //sb.append(Debug.id(block));
sb.append('{');
indent += TABWIDTH;
@@ -190,12 +190,17 @@ public final class PrintVisitor extends NodeVisitor<LexicalContext> {
sb.append(EOLN);
indent();
sb.append('}');
- // sb.append(Debug.id(block));
return false;
}
@Override
+ public boolean enterBlockStatement(final BlockStatement statement) {
+ statement.getBlock().accept(this);
+ return false;
+ }
+
+ @Override
public boolean enterBinaryNode(final BinaryNode binaryNode) {
binaryNode.lhs().accept(this);
sb.append(' ');
@@ -233,7 +238,6 @@ public final class PrintVisitor extends NodeVisitor<LexicalContext> {
public boolean enterFunctionNode(final FunctionNode functionNode) {
functionNode.toString(sb);
enterBlock(functionNode.getBody());
- //sb.append(EOLN);
return false;
}
diff --git a/src/jdk/nashorn/internal/parser/Parser.java b/src/jdk/nashorn/internal/parser/Parser.java
index 869746cf..8e04fdce 100644
--- a/src/jdk/nashorn/internal/parser/Parser.java
+++ b/src/jdk/nashorn/internal/parser/Parser.java
@@ -1210,21 +1210,24 @@ loop:
*/
private void whileStatement() {
// Capture WHILE token.
- final int whileLine = line;
final long whileToken = token;
// WHILE tested in caller.
next();
// Construct WHILE node.
- WhileNode whileNode = new WhileNode(whileLine, whileToken, Token.descPosition(whileToken), false);
+ WhileNode whileNode = new WhileNode(line, whileToken, Token.descPosition(whileToken), false);
lc.push(whileNode);
try {
expect(LPAREN);
- whileNode = whileNode.setTest(lc, expression());
+ final int whileLine = line;
+ final Expression test = expression();
expect(RPAREN);
- whileNode = whileNode.setBody(lc, getStatement());
- appendStatement(whileNode);
+ final Block body = getStatement();
+ appendStatement(whileNode =
+ new WhileNode(whileLine, whileToken, finish, false).
+ setTest(lc, test).
+ setBody(lc, body));
} finally {
lc.pop(whileNode);
}
@@ -1242,28 +1245,33 @@ loop:
*/
private void doStatement() {
// Capture DO token.
- final int doLine = line;
final long doToken = token;
// DO tested in the caller.
next();
- WhileNode doWhileNode = new WhileNode(doLine, doToken, Token.descPosition(doToken), true);
+ WhileNode doWhileNode = new WhileNode(-1, doToken, Token.descPosition(doToken), true);
lc.push(doWhileNode);
try {
// Get DO body.
- doWhileNode = doWhileNode.setBody(lc, getStatement());
+ final Block body = getStatement();
expect(WHILE);
expect(LPAREN);
- doWhileNode = doWhileNode.setTest(lc, expression());
+ final int doLine = line;
+ final Expression test = expression();
expect(RPAREN);
if (type == SEMICOLON) {
endOfLine();
}
doWhileNode.setFinish(finish);
- appendStatement(doWhileNode);
+
+ //line number is last
+ appendStatement(doWhileNode =
+ new WhileNode(doLine, doToken, finish, true).
+ setBody(lc, body).
+ setTest(lc, test));
} finally {
lc.pop(doWhileNode);
}
diff --git a/test/script/basic/JDK-8028434.js b/test/script/basic/JDK-8028434.js
new file mode 100644
index 00000000..81f9e02a
--- /dev/null
+++ b/test/script/basic/JDK-8028434.js
@@ -0,0 +1,58 @@
+/*
+ * 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-8028434: Check that the line number of the tests in while and do while loops
+ * is correct. It needs to correspond to the line with the test expression.
+ *
+ * @test
+ * @run
+ */
+
+try {
+ while (test.apa < 0) {
+ print("x");
+ }
+} catch (e) {
+ var st = e.getStackTrace();
+ if (st.length != 1) {
+ print("erroneous stacktrace length " + s.length);
+ }
+ if (st[0].lineNumber !== 32) {
+ print("erroneous stacktrace element, lineNumber=" + st[0].lineNumber + " elem=" + st);
+ }
+}
+
+try {
+ do {
+ print("x");
+ } while (test.apa < 0);
+} catch (e) {
+ var st = e.getStackTrace();
+ if (st.length != 1) {
+ print("erroneous stacktrace length " + s.length);
+ }
+ if (st[0].lineNumber !== 48) {
+ print("erroneous stacktrace element, lineNumber= " + st[0].lineNumber + " elem=" + st);
+ }
+}
diff --git a/test/script/basic/JDK-8028434.js.EXPECTED b/test/script/basic/JDK-8028434.js.EXPECTED
new file mode 100644
index 00000000..587be6b4
--- /dev/null
+++ b/test/script/basic/JDK-8028434.js.EXPECTED
@@ -0,0 +1 @@
+x