nashorn/samples/jd.js
author stefank
Tue, 11 Apr 2017 23:45:37 +0200
changeset 46565 8dcbf532ea00
parent 28318 bcfb5663e81a
permissions -rw-r--r--
8178490: Usages of is_object_aligned with pointers are broken Reviewed-by: tschatzl, kbarrett
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
28318
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     1
#// Usage: jjs -cp <asmtools.jar> jd.js -- <classname> [jdis|jdec]
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     2
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     3
/*
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     4
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     5
 *
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     6
 * Redistribution and use in source and binary forms, with or without
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     7
 * modification, are permitted provided that the following conditions
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     8
 * are met:
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
     9
 *
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    10
 *   - Redistributions of source code must retain the above copyright
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    11
 *     notice, this list of conditions and the following disclaimer.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    12
 *
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    13
 *   - Redistributions in binary form must reproduce the above copyright
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    14
 *     notice, this list of conditions and the following disclaimer in the
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    15
 *     documentation and/or other materials provided with the distribution.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    16
 *
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    17
 *   - Neither the name of Oracle nor the names of its
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    18
 *     contributors may be used to endorse or promote products derived
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    19
 *     from this software without specific prior written permission.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    20
 *
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    21
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    22
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    23
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    24
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    25
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    26
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    27
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    28
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    29
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    30
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    31
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    32
 */
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    33
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    34
// javap-like disassembler/decoder tool that disassembles/decodes 
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    35
// java classes but with OpenJDK AsmTools disassembler/decoder syntax.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    36
// You need to build asmtool.jar from OpenJDK codetools project
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    37
// specify it with -cp option.
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    38
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    39
// See also https://wiki.openjdk.java.net/display/CodeTools/AsmTools
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    40
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    41
function usage() {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    42
    print("Usage: jjs -cp <asmtools.jar> jd.js -- <classname> [jdis|jdec]");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    43
    exit(1);
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    44
}
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    45
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    46
if (arguments.length == 0) {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    47
    usage();
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    48
}
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    49
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    50
// argument handling
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    51
// convert to internal class name
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    52
var className = arguments[0].replaceAll('\\.', '/');
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    53
var tool;
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    54
if (arguments.length > 1) {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    55
    tool = arguments[1];
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    56
    switch (tool) {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    57
        case 'jdis':
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    58
        case 'jdec':
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    59
            break;
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    60
        default:
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    61
            usage();
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    62
    }
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    63
} else {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    64
    tool = "jdis"; // default tool
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    65
}
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    66
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    67
// Java classes used
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    68
var AsmTools = Java.type("org.openjdk.asmtools.Main");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    69
var Files = Java.type("java.nio.file.Files");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    70
var StandardCopyOption = Java.type("java.nio.file.StandardCopyOption");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    71
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    72
// retrive input stream for .class bytes
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    73
var cl = AsmTools.class.classLoader;
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    74
var res = cl.getResource(className + ".class");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    75
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    76
if (res) {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    77
    var is = res.openStream();
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    78
    var tmpPath;
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    79
    try {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    80
        // copy the content of the .class to a temp file
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    81
        tmpPath = Files.createTempFile("asmtools-", ".class");
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    82
        // mark as delete-on-exit
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    83
        tmpPath.toFile().deleteOnExit();
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    84
        Files.copy(is, tmpPath, [ StandardCopyOption.REPLACE_EXISTING ]);
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    85
    } finally {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    86
        is.close();
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    87
    } 
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    88
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    89
    // invoke asmtools Main
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    90
    AsmTools.main([ tool, tmpPath.toString() ]);
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    91
} else {
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    92
    print("no such class: " + arguments[0]);
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    93
    exit(1);
bcfb5663e81a 8068524: NashornScriptEngineFactory.getParameter() throws IAE for an unknown key, doesn't conform to the general spec
sundar
parents:
diff changeset
    94
}