8075207: Nashorn parser API returns StatementTree objects in out of order

Reviewed-by: lagergren, forax, hannesw
This commit is contained in:
Athijegannathan Sundararajan 2015-03-16 17:01:47 +05:30
parent 4c6eb2b310
commit cf37646b3f
8 changed files with 971 additions and 875 deletions

View File

@ -52,7 +52,6 @@ var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
var parser = Parser.create("-scripting", "--const-as-var");
var protoFound = false;
function checkFile(file) {
// print("checking " + file);
@ -92,7 +91,3 @@ if (file.isDirectory()) {
} else {
checkFile(file);
}
if (protoFound) {
print("__proto__ is non-standard. Use Object.get/setPrototypeOf instead");
}

View File

@ -44,6 +44,7 @@ public interface DoWhileLoopTree extends ConditionalLoopTree {
*
* @return the condition expression
*/
@Override
ExpressionTree getCondition();
/**
@ -51,5 +52,6 @@ public interface DoWhileLoopTree extends ConditionalLoopTree {
*
* @return the statement
*/
@Override
StatementTree getStatement();
}

View File

@ -57,6 +57,7 @@ public interface ForInLoopTree extends LoopTree {
*
* @return the statement
*/
@Override
StatementTree getStatement();
/**

View File

@ -25,6 +25,7 @@
package jdk.nashorn.api.tree;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import jdk.nashorn.internal.ir.AccessNode;
import jdk.nashorn.internal.ir.BinaryNode;
@ -92,7 +93,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
final Block body = node.getBody();
return new CompilationUnitTreeImpl(node,
translateStats(body != null? body.getStatements() : null));
translateStats(body != null? getOrderedStatements(body.getStatements()) : null));
}
@Override
@ -103,25 +104,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
@Override
public boolean enterBlock(final Block block) {
// FIXME: revisit this!
if (block.isSynthetic()) {
final int statCount = block.getStatementCount();
switch (statCount) {
case 0: {
final EmptyNode emptyNode = new EmptyNode(-1, block.getToken(), block.getFinish());
curStat = new EmptyStatementTreeImpl(emptyNode);
return false;
}
case 1: {
curStat = translateStat(block.getStatements().get(0));
return false;
}
}
}
curStat = new BlockTreeImpl(block,
translateStats(block.getStatements()));
return false;
return handleBlock(block, false);
}
@Override
@ -245,7 +228,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
final List<? extends ExpressionTree> paramTrees
= translateExprs(functionNode.getParameters());
final BlockTree blockTree = (BlockTree) translateBlock(functionNode.getBody());
final BlockTree blockTree = (BlockTree) translateBlock(functionNode.getBody(), true);
curExpr = new FunctionExpressionTreeImpl(functionNode, paramTrees, blockTree);
return false;
@ -420,7 +403,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
final List<? extends ExpressionTree> paramTrees
= translateExprs(funcNode.getParameters());
final BlockTree blockTree = (BlockTree) translateBlock(funcNode.getBody());
final BlockTree blockTree = (BlockTree) translateBlock(funcNode.getBody(), true);
curStat = new FunctionDeclarationTreeImpl(varNode, paramTrees, blockTree);
} else {
curStat = new VariableTreeImpl(varNode, translateExpr(initNode));
@ -453,14 +436,51 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
}
private StatementTree translateBlock(final Block blockNode) {
return translateBlock(blockNode, false);
}
private StatementTree translateBlock(final Block blockNode, final boolean sortStats) {
if (blockNode == null) {
return null;
}
curStat = null;
blockNode.accept(this);
handleBlock(blockNode, sortStats);
return curStat;
}
private boolean handleBlock(final Block block, final boolean sortStats) {
// FIXME: revisit this!
if (block.isSynthetic()) {
final int statCount = block.getStatementCount();
switch (statCount) {
case 0: {
final EmptyNode emptyNode = new EmptyNode(-1, block.getToken(), block.getFinish());
curStat = new EmptyStatementTreeImpl(emptyNode);
return false;
}
case 1: {
curStat = translateStat(block.getStatements().get(0));
return false;
}
default: {
// fall through
break;
}
}
}
final List<? extends Statement> stats = block.getStatements();
curStat = new BlockTreeImpl(block,
translateStats(sortStats? getOrderedStatements(stats) : stats));
return false;
}
private List<? extends Statement> getOrderedStatements(final List<? extends Statement> stats) {
final List<? extends Statement> statList = new ArrayList<>(stats);
statList.sort(Comparator.comparingInt(Node::getStart));
return statList;
}
private List<? extends StatementTree> translateStats(final List<? extends Statement> stats) {
if (stats == null) {
return null;
@ -511,7 +531,7 @@ final class IRTranslator extends NodeVisitor<LexicalContext> {
return curStat;
}
private IdentifierTree translateIdent(final IdentNode ident) {
private static IdentifierTree translateIdent(final IdentNode ident) {
return new IdentifierTreeImpl(ident);
}
}

View File

@ -139,7 +139,7 @@ final class ParserImpl implements Parser {
}
}
private CompilationUnitTree translate(final FunctionNode node) {
private static CompilationUnitTree translate(final FunctionNode node) {
return new IRTranslator().translate(node);
}
}

View File

@ -43,6 +43,7 @@ public interface WhileLoopTree extends ConditionalLoopTree {
*
* @return the condition expression
*/
@Override
ExpressionTree getCondition();
/**
@ -50,5 +51,6 @@ public interface WhileLoopTree extends ConditionalLoopTree {
*
* @return the statement contained
*/
@Override
StatementTree getStatement();
}

View File

@ -0,0 +1,76 @@
/*
* Copyright (c) 2015, 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-8075207: Nashorn parser API returns StatementTree objects in out of order
*
* @test
* @option -scripting
* @run
*/
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
var ExpressionStatementTree = Java.type("jdk.nashorn.api.tree.ExpressionStatementTree");
var FunctionDeclarationTree = Java.type("jdk.nashorn.api.tree.FunctionDeclarationTree");
var VariableTree = Java.type("jdk.nashorn.api.tree.VariableTree");
var parser = Parser.create();
var ast = parser.parse("hello.js", <<CODE
var hello = 'hello';
function print_hello() {
var x = 2;
print(hello);
function inner_func() {}
var y = function() {
var PI = Math.PI;
function inner2() {}
var E = Math.E;
}
}
var hello = "hello 2";
CODE, print);
var stats = ast.sourceElements;
Assert.assertTrue(stats.get(0) instanceof VariableTree);
Assert.assertTrue(stats.get(1) instanceof FunctionDeclarationTree);
Assert.assertTrue(stats.get(2) instanceof VariableTree);
var print_hello = stats.get(1);
Assert.assertEquals(print_hello.name, "print_hello");
var print_hello_stats = print_hello.body.statements;
Assert.assertTrue(print_hello_stats.get(0) instanceof VariableTree);
Assert.assertTrue(print_hello_stats.get(1) instanceof ExpressionStatementTree);
Assert.assertTrue(print_hello_stats.get(2) instanceof FunctionDeclarationTree);
Assert.assertTrue(print_hello_stats.get(3) instanceof VariableTree);
var anonFunc = print_hello_stats.get(3).initializer;
var anonFunc_stats = anonFunc.body.statements;
Assert.assertTrue(anonFunc_stats.get(0) instanceof VariableTree);
Assert.assertTrue(anonFunc_stats.get(1) instanceof FunctionDeclarationTree);
Assert.assertTrue(anonFunc_stats.get(2) instanceof VariableTree);

File diff suppressed because it is too large Load Diff