nashorn/samples/findwith.js
author iveresov
Tue, 30 Jun 2015 14:44:53 -0700
changeset 31521 f57b2ce43484
parent 29536 805894cd3c6b
permissions -rw-r--r--
8079775: Java 9-fastdebug ia32 Error: Unimplemented with "-XX:CompilationPolicyChoice=1 -XX:-TieredCompilation" options 8079062: Java 9-fastdebug crash(hit assertion) with "-XX:CompilationPolicyChoice=1 -XX:-TieredCompilation" options Summary: Revive stack walk compilation policy Reviewed-by: kvn
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
// 'with' statement is not considered a good practive. In strict mode,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    33
// with statements result in early error being thrown.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    34
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    35
// This script finds 'with' usage in scripts a given directory (recursively).
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    36
// If no directory is specified the current working directory is scanned
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    37
// for scripts.
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    38
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    39
if (arguments.length == 0) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    40
    arguments[0] = ".";
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    41
}
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    42
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    43
var File = Java.type("java.io.File");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    44
var file = new File(arguments[0]);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    45
if (!file.exists()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    46
    print(arguments[0] + " is neither a directory nor a file");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    47
    exit(1);
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
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    50
var Files = Java.type("java.nio.file.Files");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    51
var Parser = Java.type("jdk.nashorn.api.tree.Parser");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    52
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
    53
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    54
var parser = Parser.create("-scripting", "--const-as-var");
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    55
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    56
function checkFile(file) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    57
    // print("checking " + file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    58
    var ast = parser.parse(file, print);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    59
    if (!ast) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    60
        return;
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
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    63
    // locate __proto__ usage and warn
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    64
    ast.accept(visitor = new (Java.extend(SimpleTreeVisitor)) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    65
        lineMap: null,
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    66
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    67
        printWarning: function(node) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    68
            var pos = node.startPosition;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    69
            var line = this.lineMap.getLineNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    70
            var column = this.lineMap.getColumnNumber(pos);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    71
            print("WARNING: 'with' usage in " + file + " @ " + line + ":" + column);
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
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    74
        visitCompilationUnit: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    75
            this.lineMap = node.lineMap;
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    76
            Java.super(visitor).visitCompilationUnit(node, extra);
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
        visitWith: function(node, extra) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    80
            this.printWarning(node);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    81
            Java.super(visitor).visitWith(node, extra);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    82
        }
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    83
    }, null);
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
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    86
if (file.isDirectory()) {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    87
    Files.walk(file.toPath())
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    88
        .filter(function(p) Files.isRegularFile(p))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    89
        .filter(function(p) p.toFile().name.endsWith('.js'))
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    90
        .forEach(checkFile);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    91
} else {
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    92
    checkFile(file);
613987e37f79 8074939: Add few sample scripts to demo nashorn parser API
sundar
parents:
diff changeset
    93
}