JDK-8075207.js revision 1217:6490bba01455
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
32var Parser = Java.type("jdk.nashorn.api.tree.Parser");
33var ExpressionStatementTree = Java.type("jdk.nashorn.api.tree.ExpressionStatementTree");
34var FunctionDeclarationTree = Java.type("jdk.nashorn.api.tree.FunctionDeclarationTree");
35var VariableTree = Java.type("jdk.nashorn.api.tree.VariableTree");
36
37var parser = Parser.create();
38
39var ast = parser.parse("hello.js", <<CODE
40
41var hello = 'hello';
42
43function 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
54var hello = "hello 2";
55
56CODE, print);
57
58var stats = ast.sourceElements;
59Assert.assertTrue(stats.get(0) instanceof VariableTree);
60Assert.assertTrue(stats.get(1) instanceof FunctionDeclarationTree);
61Assert.assertTrue(stats.get(2) instanceof VariableTree);
62
63var print_hello = stats.get(1);
64Assert.assertEquals(print_hello.name, "print_hello");
65var print_hello_stats = print_hello.body.statements;
66Assert.assertTrue(print_hello_stats.get(0) instanceof VariableTree);
67Assert.assertTrue(print_hello_stats.get(1) instanceof ExpressionStatementTree);
68Assert.assertTrue(print_hello_stats.get(2) instanceof FunctionDeclarationTree);
69Assert.assertTrue(print_hello_stats.get(3) instanceof VariableTree);
70
71var anonFunc = print_hello_stats.get(3).initializer;
72var anonFunc_stats = anonFunc.body.statements;
73Assert.assertTrue(anonFunc_stats.get(0) instanceof VariableTree);
74Assert.assertTrue(anonFunc_stats.get(1) instanceof FunctionDeclarationTree);
75Assert.assertTrue(anonFunc_stats.get(2) instanceof VariableTree);
76
77