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