1/*
2 * Copyright (c) 2017, 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 * Test that Unary minus and plus uses UNARY_MINUS and UNARY_PLUS node Kind
26 *
27 * @test
28 * @bug 8185252
29 * @option -scripting
30 * @run
31 */
32
33var parser = Java.type('jdk.nashorn.api.tree.Parser');
34var tree = Java.type('jdk.nashorn.api.tree.Tree');
35var list = Java.type('java.util.List');
36var visitor = Java.type('jdk.nashorn.api.tree.SimpleTreeVisitorES5_1');
37var cls = Java.type('java.lang.Class')
38
39function convert (value) {
40    if (!value || typeof(value) != 'object') {
41        return value;
42    }
43    var  obj = Object.bindProperties({}, value)
44    var result = {}
45    for (var i in obj) {
46        if (i == "lineMap") {
47            continue;
48        }
49
50        var val = obj[i]
51        // skip these ES6 specific properties to reduce noise
52        // in the output - unless there were set to true
53        if (typeof(val) == 'boolean' && val == false) {
54            switch (i) {
55                case "computed":
56                case "static":
57                case "restParameter":
58                case "this":
59                case "super":
60                case "star":
61                case "default":
62                case "starDefaultStar":
63                case "arrow":
64                case "generator":
65                case "let":
66                case "const":
67                    continue;
68             }
69        }
70
71        if (typeof(val) == 'object') {
72            if (val instanceof cls) {
73                continue;
74            }
75            if (val instanceof tree) {
76                result[i] = convert(val)
77            }
78            else if (val instanceof list) {
79                var lst = []
80                for (var j in val) {
81                    lst.push(convert(val[j]))
82                }
83                result[i] = lst
84            }
85            else {
86                result[i] = String(val)
87            }
88        } else if (typeof(val) != 'function') {
89            result[i] = String(val)
90        }
91    }
92    return result
93}
94
95function parse(name, code, args, visitor, listener) {
96    var tree =  parser.create(args).parse(name, code, listener || null)
97    var results = []
98    tree.accept(visitor, results)
99    print(JSON.stringify(results, null, 2))
100}
101
102
103var code = <<EOF
104
105+1;
106-1;
107
108EOF
109
110parse("JDK-8185252.js", code, "-nse", new (Java.extend(visitor, {
111    visitUnary: function (node, obj) {
112        obj.push(convert(node))
113    }
114})))
115