src/sample/nashorn/nashornastviewer.js
author mbaesken
Thu, 28 Nov 2019 13:02:39 +0100
changeset 59323 ae2eb76c486d
parent 47216 71c04702a3d5
permissions -rw-r--r--
8234821: remove unused functions from libjli Reviewed-by: clanger, alanb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
29409
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     1
#// Usage: jjs -scripting -fx nashornastviewer.js -- <scriptfile>
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     2
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     3
/*
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     4
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     5
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     8
 * are met:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     9
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    12
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    16
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    20
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    32
 */
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    33
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    34
# NOTE: This script requires JDK 9 build to run
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    35
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    36
if (!$OPTIONS._fx) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    37
    print("Usage: jjs -scripting -fx nashornastviewer.js -- <.js file>");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    38
    exit(1);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    39
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    40
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    41
// Using JavaFX from Nashorn. See also:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    42
// http://docs.oracle.com/javase/8/docs/technotes/guides/scripting/nashorn/javafx.html
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    43
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    44
// This example shows AST of a script file as a JavaFX
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    45
// tree view in a window. If no file is specified, AST of
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    46
// this script file is shown. This script demonstrates
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    47
// Nashorn Parser API too - http://openjdk.java.net/jeps/236
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    48
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    49
// JavaFX classes used
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    50
var StackPane = Java.type("javafx.scene.layout.StackPane");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    51
var Scene     = Java.type("javafx.scene.Scene");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    52
var TreeItem  = Java.type("javafx.scene.control.TreeItem");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    53
var TreeView  = Java.type("javafx.scene.control.TreeView");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    54
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    55
// Java classes used
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    56
var Enum = Java.type("java.lang.Enum");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    57
var File = Java.type("java.io.File");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    58
var List = Java.type("java.util.List");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    59
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    60
var Tree = Java.type("jdk.nashorn.api.tree.Tree");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    61
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    62
// Create a javafx TreeItem to view a AST node
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    63
function treeItemForASTNode(ast, name) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    64
    var item = new TreeItem(name);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    65
    // make an iteratable script object from a Tree
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    66
    ast = Object.bindProperties({}, ast);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    67
    for (var prop in ast) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    68
       var node = ast[prop];
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    69
       var type = typeof node;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    70
 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    71
       if (node == null || type == "function") {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    72
           // skip nulls and Java methods
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    73
           continue;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    74
       }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    75
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    76
       var subitem = null;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    77
       if (node instanceof Tree) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    78
           subitem = treeItemForASTNode(node, prop);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    79
       } else if (node instanceof List) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    80
           var len = node.size();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    81
           subitem = new TreeItem(prop);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    82
           for (var i = 0; i < len; i++) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    83
               var li = treeItemForASTNode(node.get(i), String(i));
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    84
               subitem.children.add(li); 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    85
           }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    86
       } else if (node instanceof Enum || type != 'object') {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    87
           subitem = new TreeItem(prop + ": " + node);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    88
       }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    89
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    90
       if (subitem) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    91
           item.children.add(subitem);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    92
       }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    93
    }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    94
    return item;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    95
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    96
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    97
// do we have a script file passed? if not, use current script
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    98
var sourceName = arguments.length == 0? __FILE__ : arguments[0];
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    99
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   100
var parser = Parser.create("-scripting");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   101
// parse script to get CompilationUnitTree of it
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   102
var ast = parser.parse(new File(sourceName), null);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   103
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   104
// JavaFX start method
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   105
function start(stage) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   106
    stage.title = "AST Viewer";
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   107
    var rootItem = treeItemForASTNode(ast, sourceName);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   108
    var tree = new TreeView(rootItem);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   109
    var root = new StackPane();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   110
    root.children.add(tree);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   111
    stage.scene = new Scene(root, 300, 450);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   112
    stage.show();
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   113
}