src/sample/nashorn/console.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:
30702
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     1
/*
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     3
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     4
 * Redistribution and use in source and binary forms, with or without
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     5
 * modification, are permitted provided that the following conditions
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     6
 * are met:
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     7
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     8
 *   - Redistributions of source code must retain the above copyright
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
     9
 *     notice, this list of conditions and the following disclaimer.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    10
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    11
 *   - Redistributions in binary form must reproduce the above copyright
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    12
 *     notice, this list of conditions and the following disclaimer in the
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    13
 *     documentation and/or other materials provided with the distribution.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    14
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    15
 *   - Neither the name of Oracle nor the names of its
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    16
 *     contributors may be used to endorse or promote products derived
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    17
 *     from this software without specific prior written permission.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    18
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    19
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    20
 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    21
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    22
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    23
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    24
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    25
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    26
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    27
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    28
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    29
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    30
 */
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    31
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    32
/**
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    33
 * Simple Web Console-like support for Nashorn. In addition to
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    34
 * Web console object methods, this console add methods of
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    35
 * java.io.Console as well. Note:not all web console methods are 
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    36
 * implemented but useful subset is implemented.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    37
 *
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    38
 * See also: https://developer.mozilla.org/en/docs/Web/API/console
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    39
 */
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    40
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    41
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    42
if (typeof console == 'undefined') {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    43
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    44
(function() {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    45
    var LocalDateTime = Java.type("java.time.LocalDateTime");
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    46
    var System = Java.type("java.lang.System");
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    47
    var jconsole = System.console();
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    48
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    49
    // add a new global variable called "console"
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    50
    this.console = {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    51
    };
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    52
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    53
    function addConsoleMethods() {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    54
        // expose methods of java.io.Console as an extension
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    55
        var placeholder = "-*-";
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    56
        // put a placeholder for each name from java.lang.Object
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    57
        var objMethods = Object.bindProperties({}, new java.lang.Object());
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    58
        for (var m in objMethods) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    59
            console[m] = placeholder;
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    60
        }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    61
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    62
        // bind only the methods of java.io.Console
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    63
        // This bind will skip java.lang.Object methods as console
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    64
        // has properties of same name.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    65
        Object.bindProperties(console, jconsole);
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    66
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    67
        // Now, delete java.lang.Object methods
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    68
        for (var m in console) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    69
            if (console[m] == placeholder) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    70
                delete console[m];
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    71
            }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    72
        }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    73
    }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    74
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    75
    addConsoleMethods();
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    76
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    77
    function consoleLog(type, msg) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    78
        // print type of message, then time.
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    79
        jconsole.format("%s [%s] ", type, LocalDateTime.now().toString());
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    80
        if (typeof msg == 'string') {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    81
            jconsole.format(msg + "\n", Array.prototype.slice.call(arguments, 2));
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    82
        } else {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    83
            // simple space separated values and newline at the end
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    84
            var arr = Array.prototype.slice.call(arguments, 1);
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    85
            jconsole.format("%s\n", arr.join(" "));
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    86
        }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    87
    }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    88
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    89
    console.toString = function() "[object Console]";
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    90
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    91
    // web console functions
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    92
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    93
    console.assert = function(expr) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    94
        if (! expr) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    95
            arguments[0] = "Assertion Failed:"; 
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    96
            consoleLog.apply(console, arguments);
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    97
            // now, stack trace at the end
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    98
            jconsole.format("%s\n", new Error().stack);
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
    99
        }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   100
    };
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   101
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   102
    // dummy clear to avoid error!
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   103
    console.clear = function() {};
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   104
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   105
    var counter = {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   106
        get: function(label) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   107
            if (! this[label]) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   108
                return this[label] = 1;
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   109
            } else {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   110
                return ++this[label];
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   111
            }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   112
        }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   113
    };
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   114
   
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   115
    // counter 
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   116
    console.count = function(label) {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   117
        label = label? String(label) : "<no label>";
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   118
        jconsole.format("%s: %d\n",label, counter.get(label).intValue());
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   119
    }
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   120
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   121
    // logging
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   122
    console.error = consoleLog.bind(jconsole, "ERROR");
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   123
    console.info = consoleLog.bind(jconsole, "INFO");
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   124
    console.log = console.info;
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   125
    console.debug = console.log;
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   126
    console.warn = consoleLog.bind(jconsole, "WARNING");
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   127
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   128
    // print stack trace
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   129
    console.trace = function() {
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   130
        jconsole.format("%s\n", new Error().stack);
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   131
    };
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   132
})();
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   133
ce6a8edae925 8080848: delete of bound Java method property results in crash
sundar
parents:
diff changeset
   134
}