Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 | 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 44x 22x 44x 44x 22x 22x 22x 22x 44x 22x 22x 22x 22x 22x 22x 44x 1x 1x 1x 1x 1x 1x 1x 1x 1x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 44x 11x 11x 11x 11x 11x | /** * @license Apache-2.0 * * Copyright (c) 2019 The Stdlib Authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ /* eslint-disable no-underscore-dangle */ 'use strict'; // MODULES // var inspect = require( 'util' ).inspect; var logger = require( 'debug' ); var isndarrayLike = require( '@stdlib/assert/is-ndarray-like' ); var replace = require( '@stdlib/string/replace' ); var noop = require( '@stdlib/utils/noop' ); var nextTick = require( '@stdlib/utils/next-tick' ); var displayPrompt = require( './display_prompt.js' ); var updateRegExpCache = require( './update_regexp_cache.js' ); var restoreRegExpMatches = require( './restore_regexp_matches.js' ); // VARIABLES // var debug = logger( 'repl:drain' ); // MAIN // /** * Drains a REPL's command queue. * * @private * @param {REPL} repl - REPL instance * @returns {void} */ function drain( repl ) { var DONE; var SYNC; var opts; var code; var res; var pre; var tmp; if ( repl._busy ) { debug( 'Waiting on a command to finish...' ); return; } if ( repl._queue.length === 0 ) { debug( 'Command queue is empty.' ); if ( !repl._closed ) { displayPrompt( repl, false ); } repl.emit( 'drain' ); return; } debug( 'Evaluating next command...' ); // Set a flag indicating that the REPL is busy: repl._busy = true; // Remove a command from the queue: code = repl._queue.pop(); debug( 'Command: %s', code.raw ); // Set an internal flag for determining whether a command executes synchronously: SYNC = true; // Set the REPL command callback: repl._done = ( code.async ) ? done : noop; // Set the (non-standard) properties on the `RegExp` expression object to the cached matches: restoreRegExpMatches( repl._regexp ); // Evaluate command... opts = { 'timeout': repl._timeout, 'displayErrors': false, 'breakOnSigint': true // Note: only applies for Node.js versions >=6.3.0 }; try { if ( repl._sandbox ) { res = code.compiled.runInContext( repl._context, opts ); } else { res = code.compiled.runInThisContext( opts ); } } catch ( error ) { debug( 'Error: %s', error.message ); repl._ostream.write( 'Error: '+error.message+'\n' ); repl.emit( 'command', code.raw, false ); // command failed updateRegExpCache( repl._regexp ); repl._busy = false; displayPrompt( repl, false ); // Clear the command queue, as an error may cause downstream "dependencies" to not be fulfilled, etc: repl._queue.clear(); return; } // If an "asynchronous" command ran synchronously, then initial value of `SYNC` in the `done` callback below will be `true`; here, we reset the flag to ensure that, during the next turn of the event loop, the value is `false`: SYNC = false; // If we are running "asynchronous" code, wait until the `__done__` callback is invoked to continue processing... if ( code.async ) { return; } // Update the cache of saved regular expression substring matches: updateRegExpCache( repl._regexp ); // Cache the result: repl._ans = res; if ( code.silent === false && res !== void 0 ) { pre = replace( repl._outputPrompt, '%d', (repl._count+1).toString() ); // TODO: pretty printing (can defer to `util.inspect` for now, but will invariably want more control over this later, possibly including default configuration, etc, either at startup, during runtime, or according to an external configuration file) if ( isndarrayLike( res ) ) { tmp = res.toString(); // FIXME: this is a hack in order to avoid printing private ndarray properties in the REPL, as done by the built-in `util.inspect`. Ideally, we'd roll our own inspector which specifically accommodates stdlib's ndarray and other specialized classes. } else { tmp = repl._ans; } repl._ostream.write( pre+inspect( tmp )+'\n' ); } // Finish processing: return beforeNextTick(); /** * Callback invoked when a command finishes. * * @private * @param {(Error|null)} [error] - error * @param {*} [results] - results * @returns {void} */ function done( error, results ) { var args; var i; if ( DONE ) { debug( 'Command already finished. Command callback called more than once.' ); if ( error ) { debug( 'Error: %s', error.message ); } if ( arguments.length > 1 ) { debug( 'Results: %s', JSON.stringify( results ) ); } return; } // Update the internal flag indicating whether a command has finished executing (which is important within the context of `SIGINT` event handling): DONE = true; // If the command ran synchronously, defer resolution of the results until the next tick of the event loop... if ( SYNC ) { debug( 'Asynchronous command executed synchronously.' ); args = []; for ( i = 0; i < arguments.length; i++ ) { args.push( arguments[ i ] ); } nextTick( defer ); return; } // Update the cache of saved regular expression substring matches: updateRegExpCache( repl._regexp ); if ( error ) { debug( 'Error: %s', error.message ); repl._ostream.write( 'Error: '+error.message+'\n' ); repl.emit( 'command', code.raw, false ); // command failed displayPrompt( repl, false ); // Indicate that command execution has completed: repl._busy = false; // Clear the command queue, as an error may cause downstream "dependencies" to not be fulfilled, etc: repl._queue.clear(); return; } // Cache the result: repl._ans = [ res, results ]; if ( code.silent === false && ( res !== void 0 || results !== void 0 ) ) { // eslint-disable-line max-len pre = replace( repl._outputPrompt, '%d', (repl._count+1).toString() ); // TODO: pretty printing (can defer to `util.inspect` for now, but will invariably want more control over this later, possibly including default configuration, etc, either at startup, during runtime, or according to an external configuration file) repl._ostream.write( pre+inspect( repl._ans )+'\n' ); } // Finish processing: beforeNextTick(); /** * Callback invoked upon the next tick of the event loop. * * @private */ function defer() { done.apply( null, args ); } } /** * Initiates common processing steps before the next tick of the event loop. * * @private */ function beforeNextTick() { // Announce that the command succeeded: debug( 'Finished evaluating command.' ); repl.emit( 'command', code.raw, true ); nextTick( onTick ); } /** * Callback invoked upon the next tick of the event loop. * * @private */ function onTick() { // Indicate that command execution has completed: repl._busy = false; drain( repl ); } } // EXPORTS // module.exports = drain; |