1 /* 2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24 /** 25 * JDK-8075207: Nashorn parser API returns StatementTree objects in out of order 26 * 27 * @test 28 * @option -scripting 29 * @run 30 */ 31 32 var Parser = Java.type("jdk.nashorn.api.tree.Parser"); 33 var ExpressionStatementTree = Java.type("jdk.nashorn.api.tree.ExpressionStatementTree"); 34 var FunctionDeclarationTree = Java.type("jdk.nashorn.api.tree.FunctionDeclarationTree"); 35 var VariableTree = Java.type("jdk.nashorn.api.tree.VariableTree"); 36 37 var parser = Parser.create(); 38 39 var ast = parser.parse("hello.js", <<CODE 40 41 var hello = 'hello'; 42 43 function print_hello() { 44 var x = 2; 45 print(hello); 46 function inner_func() {} 47 var y = function() { 48 var PI = Math.PI; 49 function inner2() {} 50 var E = Math.E; 51 } 52 } 53 54 var hello = "hello 2"; 55 56 CODE, print); 57 58 var stats = ast.sourceElements; 59 Assert.assertTrue(stats.get(0) instanceof VariableTree); 60 Assert.assertTrue(stats.get(1) instanceof FunctionDeclarationTree); 61 Assert.assertTrue(stats.get(2) instanceof VariableTree); 62 63 var print_hello = stats.get(1); 64 Assert.assertEquals(print_hello.name.name, "print_hello"); 65 var print_hello_stats = print_hello.body.statements; 66 Assert.assertTrue(print_hello_stats.get(0) instanceof VariableTree); 67 Assert.assertTrue(print_hello_stats.get(1) instanceof ExpressionStatementTree); 68 Assert.assertTrue(print_hello_stats.get(2) instanceof FunctionDeclarationTree); 69 Assert.assertTrue(print_hello_stats.get(3) instanceof VariableTree); 70 71 var anonFunc = print_hello_stats.get(3).initializer; 72 var anonFunc_stats = anonFunc.body.statements; 73 Assert.assertTrue(anonFunc_stats.get(0) instanceof VariableTree); 74 Assert.assertTrue(anonFunc_stats.get(1) instanceof FunctionDeclarationTree); 75 Assert.assertTrue(anonFunc_stats.get(2) instanceof VariableTree); 76