JDK-8185252.js revision 1978:9133969febb5
190075Sobrien/*
2169689Skan * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
3132718Skan * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
490075Sobrien *
590075Sobrien * This code is free software; you can redistribute it and/or modify it
690075Sobrien * under the terms of the GNU General Public License version 2 only, as
790075Sobrien * published by the Free Software Foundation.
890075Sobrien *
990075Sobrien * This code is distributed in the hope that it will be useful, but WITHOUT
1090075Sobrien * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1190075Sobrien * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1290075Sobrien * version 2 for more details (a copy is included in the LICENSE file that
1390075Sobrien * accompanied this code).
1490075Sobrien *
1590075Sobrien * You should have received a copy of the GNU General Public License version
1690075Sobrien * 2 along with this work; if not, write to the Free Software Foundation,
1790075Sobrien * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1890075Sobrien *
19169689Skan * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20169689Skan * or visit www.oracle.com if you need additional information or have any
2190075Sobrien * questions.
22169689Skan */
23169689Skan
24169689Skan/**
2590075Sobrien * Test that Unary minus and plus uses UNARY_MINUS and UNARY_PLUS node Kind
26169689Skan *
27169689Skan * @test
28169689Skan * @bug 8185252
2990075Sobrien * @option -scripting
30169689Skan * @run
31169689Skan */
3290075Sobrien
33132718Skanvar parser = Java.type('jdk.nashorn.api.tree.Parser');
3490075Sobrienvar tree = Java.type('jdk.nashorn.api.tree.Tree');
3590075Sobrienvar list = Java.type('java.util.List');
3690075Sobrienvar visitor = Java.type('jdk.nashorn.api.tree.SimpleTreeVisitorES5_1');
37169689Skanvar cls = Java.type('java.lang.Class')
38169689Skan
3990075Sobrienfunction convert (value) {
4090075Sobrien    if (!value || typeof(value) != 'object') {
4190075Sobrien        return value;
42117395Skan    }
43117395Skan    var  obj = Object.bindProperties({}, value)
44169689Skan    var result = {}
45117395Skan    for (var i in obj) {
46132718Skan        if (i == "lineMap") {
47132718Skan            continue;
48132718Skan        }
49132718Skan
50169689Skan        var val = obj[i]
51132718Skan        // skip these ES6 specific properties to reduce noise
52169689Skan        // in the output - unless there were set to true
53132718Skan        if (typeof(val) == 'boolean' && val == false) {
54132718Skan            switch (i) {
55132718Skan                case "computed":
56169689Skan                case "static":
57132718Skan                case "restParameter":
58169689Skan                case "this":
59117395Skan                case "super":
60117395Skan                case "star":
61169689Skan                case "default":
62169689Skan                case "starDefaultStar":
63117395Skan                case "arrow":
64169689Skan                case "generator":
65117395Skan                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