1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/samples/console.js Thu May 21 18:44:51 2015 +0530 1.3 @@ -0,0 +1,134 @@ 1.4 +/* 1.5 + * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved. 1.6 + * 1.7 + * Redistribution and use in source and binary forms, with or without 1.8 + * modification, are permitted provided that the following conditions 1.9 + * are met: 1.10 + * 1.11 + * - Redistributions of source code must retain the above copyright 1.12 + * notice, this list of conditions and the following disclaimer. 1.13 + * 1.14 + * - Redistributions in binary form must reproduce the above copyright 1.15 + * notice, this list of conditions and the following disclaimer in the 1.16 + * documentation and/or other materials provided with the distribution. 1.17 + * 1.18 + * - Neither the name of Oracle nor the names of its 1.19 + * contributors may be used to endorse or promote products derived 1.20 + * from this software without specific prior written permission. 1.21 + * 1.22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS 1.23 + * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 1.24 + * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.25 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 1.26 + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.27 + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.28 + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.29 + * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 1.30 + * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 1.31 + * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 1.32 + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.33 + */ 1.34 + 1.35 +/** 1.36 + * Simple Web Console-like support for Nashorn. In addition to 1.37 + * Web console object methods, this console add methods of 1.38 + * java.io.Console as well. Note:not all web console methods are 1.39 + * implemented but useful subset is implemented. 1.40 + * 1.41 + * See also: https://developer.mozilla.org/en/docs/Web/API/console 1.42 + */ 1.43 + 1.44 + 1.45 +if (typeof console == 'undefined') { 1.46 + 1.47 +(function() { 1.48 + var LocalDateTime = Java.type("java.time.LocalDateTime"); 1.49 + var System = Java.type("java.lang.System"); 1.50 + var jconsole = System.console(); 1.51 + 1.52 + // add a new global variable called "console" 1.53 + this.console = { 1.54 + }; 1.55 + 1.56 + function addConsoleMethods() { 1.57 + // expose methods of java.io.Console as an extension 1.58 + var placeholder = "-*-"; 1.59 + // put a placeholder for each name from java.lang.Object 1.60 + var objMethods = Object.bindProperties({}, new java.lang.Object()); 1.61 + for (var m in objMethods) { 1.62 + console[m] = placeholder; 1.63 + } 1.64 + 1.65 + // bind only the methods of java.io.Console 1.66 + // This bind will skip java.lang.Object methods as console 1.67 + // has properties of same name. 1.68 + Object.bindProperties(console, jconsole); 1.69 + 1.70 + // Now, delete java.lang.Object methods 1.71 + for (var m in console) { 1.72 + if (console[m] == placeholder) { 1.73 + delete console[m]; 1.74 + } 1.75 + } 1.76 + } 1.77 + 1.78 + addConsoleMethods(); 1.79 + 1.80 + function consoleLog(type, msg) { 1.81 + // print type of message, then time. 1.82 + jconsole.format("%s [%s] ", type, LocalDateTime.now().toString()); 1.83 + if (typeof msg == 'string') { 1.84 + jconsole.format(msg + "\n", Array.prototype.slice.call(arguments, 2)); 1.85 + } else { 1.86 + // simple space separated values and newline at the end 1.87 + var arr = Array.prototype.slice.call(arguments, 1); 1.88 + jconsole.format("%s\n", arr.join(" ")); 1.89 + } 1.90 + } 1.91 + 1.92 + console.toString = function() "[object Console]"; 1.93 + 1.94 + // web console functions 1.95 + 1.96 + console.assert = function(expr) { 1.97 + if (! expr) { 1.98 + arguments[0] = "Assertion Failed:"; 1.99 + consoleLog.apply(console, arguments); 1.100 + // now, stack trace at the end 1.101 + jconsole.format("%s\n", new Error().stack); 1.102 + } 1.103 + }; 1.104 + 1.105 + // dummy clear to avoid error! 1.106 + console.clear = function() {}; 1.107 + 1.108 + var counter = { 1.109 + get: function(label) { 1.110 + if (! this[label]) { 1.111 + return this[label] = 1; 1.112 + } else { 1.113 + return ++this[label]; 1.114 + } 1.115 + } 1.116 + }; 1.117 + 1.118 + // counter 1.119 + console.count = function(label) { 1.120 + label = label? String(label) : "<no label>"; 1.121 + jconsole.format("%s: %d\n",label, counter.get(label).intValue()); 1.122 + } 1.123 + 1.124 + // logging 1.125 + console.error = consoleLog.bind(jconsole, "ERROR"); 1.126 + console.info = consoleLog.bind(jconsole, "INFO"); 1.127 + console.log = console.info; 1.128 + console.debug = console.log; 1.129 + console.warn = consoleLog.bind(jconsole, "WARNING"); 1.130 + 1.131 + // print stack trace 1.132 + console.trace = function() { 1.133 + jconsole.format("%s\n", new Error().stack); 1.134 + }; 1.135 +})(); 1.136 + 1.137 +}