nashorn/samples/findproto.js
author duke
Wed, 05 Jul 2017 20:34:59 +0200
changeset 30705 9104b426a3e8
parent 29409 613987e37f79
permissions -rw-r--r--
Merge
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
/*
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     2
 * 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
     3
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     4
 * 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
     5
 * modification, are permitted provided that the following conditions
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     6
 * are met:
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     7
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    10
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    12
 *     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
    13
 *     documentation and/or other materials provided with the distribution.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    14
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    15
 *   - 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
    16
 *     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
    17
 *     from this software without specific prior written permission.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    18
 *
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    19
 * 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
    20
 * 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
    21
 * 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
    22
 * 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
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    25
 * 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
    26
 * 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
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    28
 * 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
    29
 * 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
    30
 */
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    31
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    32
// __proto__ magic property is non-standard. Use Object.getPrototypeOf
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    33
// and Object.setPrototypeOf instead. This script finds __proto__
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    34
// usage in scripts a given directory (recursively). If no directory 
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    35
// is specified the current working directory is scanned for scripts.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    36
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    37
if (arguments.length == 0) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    38
    arguments[0] = ".";
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
var File = Java.type("java.io.File");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    42
var file = new File(arguments[0]);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    43
if (!file.exists()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    44
    print(arguments[0] + " is neither a file nor a directory");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    45
    exit(1);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    46
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    47
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    48
var Files = Java.type("java.nio.file.Files");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    49
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    50
var SimpleTreeVisitor = Java.type("jdk.nashorn.api.tree.SimpleTreeVisitorES5_1");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    51
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    52
var parser = Parser.create("-scripting", "--const-as-var");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    53
var protoFound = false;
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
function checkFile(file) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    56
    // print("checking " + file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    57
    var ast = parser.parse(file, print);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    58
    if (!ast) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    59
        return;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    60
    }
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
    // locate __proto__ usage and warn
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    63
    ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    64
        lineMap: null,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    65
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    66
        printWarning: function(node) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    67
            var pos = node.startPosition;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    68
            var line = this.lineMap.getLineNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    69
            var column = this.lineMap.getColumnNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    70
            print("WARNING: __proto__ usage in " + file + " @ " + line + ":" + column);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    71
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    72
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    73
        checkProto: function(node, name) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    74
            if (name == "__proto__") {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    75
                protoFound = true;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    76
                this.printWarning(node);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    77
            }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    78
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    79
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    80
        visitCompilationUnit: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    81
            this.lineMap = node.lineMap;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    82
            Java.super(visitor).visitCompilationUnit(node, extra);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    83
        },
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    84
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    85
        visitIdentifier: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    86
            this.checkProto(node, node.name);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    87
            Java.super(visitor).visitIdentifier(node, extra);
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
        visitMemberSelect: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    91
            this.checkProto(node, node.identifier);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    92
            Java.super(visitor).visitMemberSelect(node, extra);
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
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    95
        visitProperty: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    96
            this.checkProto(node, node.key);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    97
            Java.super(visitor).visitProperty(node, extra);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    98
        }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    99
    }, null);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   100
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   101
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   102
if (file.isDirectory()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   103
    Files.walk(file.toPath())
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   104
        .filter(function(p) Files.isRegularFile(p))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   105
        .filter(function(p) p.toFile().name.endsWith('.js'))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   106
        .forEach(checkFile);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   107
} else {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   108
    checkFile(file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   109
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   110
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   111
if (protoFound) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   112
    print("__proto__ is non-standard. Use Object.get/setPrototypeOf instead");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
   113
}