samples/console.js

Fri, 05 Jun 2015 12:38:53 +0200

author
mhaupt
date
Fri, 05 Jun 2015 12:38:53 +0200
changeset 1398
2f1b9f4daec1
parent 1373
4eabcac368d2
permissions
-rw-r--r--

8080087: Nashorn $ENV.PWD is originally undefined
Summary: On Windows, the PWD environment variable does not exist and cannot be imported in scripting mode, so it is set explicitly.
Reviewed-by: lagergren, sundar

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

mercurial