nashorn/src/jdk/nashorn/api/scripting/resources/engine.js
changeset 16527 5ba350ebcdbb
parent 16205 93fda2507e35
child 17758 2b056941e4dd
equal deleted inserted replaced
16526:261f7f898bfd 16527:5ba350ebcdbb
    44     if (! (writer instanceof java.io.PrintWriter)) {
    44     if (! (writer instanceof java.io.PrintWriter)) {
    45         writer = new java.io.PrintWriter(writer);
    45         writer = new java.io.PrintWriter(writer);
    46     }
    46     }
    47     writer.println(String(str));
    47     writer.println(String(str));
    48 }
    48 }
       
    49 
       
    50 /**
       
    51  * This is C-like printf
       
    52  *
       
    53  * @param format string to format the rest of the print items
       
    54  * @param args variadic argument list
       
    55  */
       
    56 Object.defineProperty(this, "printf", {
       
    57     configurable: true,
       
    58     enumerable: false,
       
    59     writable: true,
       
    60     value: function (format, args/*, more args*/) {
       
    61         print(sprintf.apply(this, arguments));
       
    62     }
       
    63 });
       
    64 
       
    65 /**
       
    66  * This is C-like sprintf
       
    67  *
       
    68  * @param format string to format the rest of the print items
       
    69  * @param args variadic argument list
       
    70  */
       
    71 Object.defineProperty(this, "sprintf", {
       
    72     configurable: true,
       
    73     enumerable: false,
       
    74     writable: true,
       
    75     value: function (format, args/*, more args*/) {
       
    76         var len = arguments.length - 1;
       
    77         var array = [];
       
    78 
       
    79         if (len < 0) {
       
    80             return "";
       
    81         }
       
    82 
       
    83         for (var i = 0; i < len; i++) {
       
    84             if (arguments[i+1] instanceof Date) {
       
    85                 array[i] = arguments[i+1].getTime();
       
    86             } else {
       
    87                 array[i] = arguments[i+1];
       
    88             }
       
    89         }
       
    90 
       
    91         array = Java.toJavaArray(array);
       
    92         return Packages.jdk.nashorn.api.scripting.ScriptUtils.format(format, array);
       
    93     }
       
    94 });