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 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 | 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 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 567x 567x 567x 567x 567x 567x 144x 144x 567x 5x 5x 5x 567x 9x 9x 9x 418x 164x 141x 141x 409x 21x 21x 245x 2x 2x 224x 266x 567x 11x 11x 11x 11x 11x 11x 11x 11x 23x 23x 23x 2x 2x 21x 21x 21x 23x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 3756x 3756x 3756x 3756x 3756x 3756x 3372x 3372x 3372x 384x 384x 243x 243x 384x 141x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 53x 53x 53x 53x 53x 53x 53x 53x 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 5011x 53x 53x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 11x 5232x 5232x 5232x 5232x 5232x 5232x 5232x 5232x 5232x 5232x 1476x 1476x 3756x 3756x 3756x 3756x 3756x 3756x 3756x 4132x 3615x 3615x 3615x 141x 141x 4132x 141x 141x 4132x 6x 6x 6x 135x 135x 135x 4132x 5232x 77x 77x 77x 58x 58x 58x 4132x 58x 58x 58x 58x 58x 58x 58x 11x 11x 11x 11x 11x 11x | /** * @license Apache-2.0 * * Copyright (c) 2025 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, no-restricted-syntax, no-invalid-this, max-len */ 'use strict'; // MODULES // var readline = require( 'readline' ); var inspect = require( 'util' ).inspect; var logger = require( 'debug' ); var parse = require( 'acorn' ).parse; var replace = require( '@stdlib/string/replace' ); var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' ); var copy = require( '@stdlib/array/base/copy' ); var max = require( '@stdlib/math/base/special/max' ); var processCommand = require( './process_command.js' ); var compileCommand = require( './compile_command.js' ); var ANSI_COLORS = require( './ansi_colors.js' ); // VARIABLES // var debug = logger( 'repl:eager-evaluator' ); var AOPTS = { 'ecmaVersion': 'latest' }; var ROPTS = { 'timeout': 100, // (in milliseconds) this controls how long eagerly evaluated commands have to execute; we need to avoid setting this too high in order to avoid eager evaluation interfering with the UX when naturally typing 'displayErrors': false, 'breakOnSigint': true // Node.js >=6.3.0 }; var tempDB = { 'base_sin': { 'isPure': true } }; var ANSI_GRAY = ANSI_COLORS[ 'brightBlack' ]; var ANSI_RESET = ANSI_COLORS[ 'reset' ]; // FUNCTIONS // /** * Recursively traverses the node to determine whether the node is side-effect-free. * * @private * @param {Object} node - ast node * @returns {boolean} boolean indicating whether the node is side-effect-free */ function traverse( node ) { var properties; var fname; var i; if ( !node ) { return false; } if ( node.type === 'Literal' || node.type === 'Identifier' || ( node.type === 'UnaryExpression' && node.operator !== 'delete' ) ) { return true; } if ( node.type === 'BinaryExpression' ) { if ( traverse( node.left ) && traverse( node.right ) ) { return true; } } else if ( node.type === 'MemberExpression' ) { if ( traverse( node.object ) ) { return true; } } else if ( node.type === 'ExpressionStatement' ) { if ( traverse( node.expression ) ) { return true; } } else if ( node.type === 'CallExpression' ) { fname = getFunctionName( node.callee ); if ( tempDB[fname] && tempDB[fname].isPure ) { // Examine each function argument for potential side-effects... for ( i = 0; i < node.arguments.length; i++ ) { if ( !traverse( node.arguments[ i ] ) ) { return false; } } return true; } } else if ( node.type === 'ArrayExpression' ) { for ( i = 0; i < node.elements.length; i++ ) { if ( !traverse( node.elements[ i ] ) ) { return false; } } return true; } else if ( node.type === 'ObjectExpression' ) { for ( i = 0; i < node.properties.length; i++ ) { properties = node.properties; if ( !traverse( properties[ i ].key ) || !traverse( properties[ i ].value ) ) { return false; } } return true; } return false; } /** * Resolves the function name associated with a provided AST node. * * @private * @param {Object} node - ast node * @returns {string} function name representing the node */ function getFunctionName( node ) { if ( !node ) { return ''; } if ( node.type === 'MemberExpression' ) { return getFunctionName( node.object ) + '_' + node.property.name; } if ( node.type === 'Identifier' ) { return node.name; } return ''; } // MAIN // /** * Constructor for creating an eager evaluator. * * @private * @param {REPL} repl - repl instance * @param {Object} rli - readline instance * @param {boolean} enabled - boolean indicating whether the eager evaluator should be initially enabled * @returns {EagerEvaluator} eager evaluator instance */ function EagerEvaluator( repl, rli, enabled ) { if ( !(this instanceof EagerEvaluator) ) { return new EagerEvaluator( repl, rli, enabled ); } debug( 'Creating a new eager evaluator...' ); // Cache a reference to the provided REPL instance: this._repl = repl; // Cache a reference to the readline interface: this._rli = rli; // Cache a reference to the command array: this._cmd = repl._cmd; // Initialize a flag indicating whether the eager evaluator is enabled: this._enabled = enabled; // Initialize a flag indicating whether we are currently previewing eagerly-evaluated output: this._isPreviewing = false; return this; } /** * Checks whether provided code is free of side-effects. * * @private * @name _isSideEffectFree * @memberof EagerEvaluator.prototype * @type {Function} * @param {string} code - input code * @returns {boolean} boolean indicating whether provided code is free of side-effects */ setNonEnumerableReadOnly( EagerEvaluator.prototype, '_isSideEffectFree', function isSideEffectFree( code ) { var ast; var i; try { ast = parse( code, AOPTS ); } catch ( err ) { debug( 'Encountered an error when generating AST: %s', err.message ); return false; } for ( i = 0; i < ast.body.length; i++ ) { if ( !traverse( ast.body[ i ] ) ) { return false; } } return true; }); /** * Clears eagerly-evaluated output. * * @private * @name clear * @memberof EagerEvaluator.prototype * @type {Function} * @returns {void} */ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'clear', function clear() { var cursorPosition; cursorPosition = this._rli.cursor; readline.moveCursor( this._repl._ostream, 0, 1 ); readline.clearLine( this._repl._ostream, 0 ); readline.moveCursor( this._repl._ostream, 0, -1 ); readline.cursorTo( this._repl._ostream, cursorPosition + this._repl.promptLength() ); this._isPreviewing = false; }); /** * Disables the eager evaluator. * * @private * @name disable * @memberof EagerEvaluator.prototype * @type {Function} * @returns {EagerEvaluator} eager evaluator instance */ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'disable', function disable() { this._enabled = false; return this; }); /** * Enables the eager evaluator. * * @private * @name enable * @memberof EagerEvaluator.prototype * @type {Function} * @returns {EagerEvaluator} eager evaluator instance */ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'enable', function enable() { this._enabled = true; return this; }); /** * Callback which should be invoked **before** a "keypress" event. * * @private * @name beforeKeypress * @memberof EagerEvaluator.prototype * @type {Function} * @param {string} data - input data * @param {(Object|void)} key - key object * @returns {void} */ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'beforeKeypress', function beforeKeypress() { if ( this._isPreviewing ) { this.clear(); } }); /** * Callback for handling a "keypress" event. * * @private * @name onKeypress * @memberof EagerEvaluator.prototype * @type {Function} * @param {string} data - input data * @param {(Object|void)} key - key object * @returns {void} */ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'onKeypress', function onKeypress() { var cursorPosition; var executable; var index; var code; var cmd; var pre; var res; var tmp; if ( !this._enabled || this._rli.line === '' ) { return; } // Build the final command: cmd = copy( this._cmd ); cmd[ max( cmd.length - 1, 0 ) ] = this._rli.line; // eager-evaluation should only work when on the last line, hence updating the last index code = cmd.join( '\n' ); debug( 'Eagerly evaluating: %s', code ); if ( !this._isSideEffectFree( code ) ) { debug( 'Unable to perform eager-evaluation due to potential side-effects. Skipping...' ); return; } debug( 'Processing command...' ); tmp = processCommand( code ); if ( tmp instanceof Error ) { debug( 'Error encountered when processing command: %s', tmp.message ); return; } debug( 'Compiling command...' ); executable = compileCommand( tmp ); if ( executable instanceof Error ) { debug( 'Error encountered when compiling command: %s', executable.message ); return; } try { if ( this._repl._sandbox ) { res = executable.compiled.runInContext( this._repl._context, ROPTS ); } else { res = executable.compiled.runInThisContext( ROPTS ); } } catch ( err ) { debug( 'Encountered an error when executing the command: %s', err.message ); return; } res = inspect( res ); index = res.indexOf( '\n' ); if ( index !== -1 ) { res = res.slice( 0, index ) + '...'; } cursorPosition = this._rli.cursor; pre = replace( this._repl._outputPrompt, '%d', ( this._repl._count+1 ).toString() ); this._repl._ostream.write( '\n' + ANSI_GRAY + pre + res + ANSI_RESET ); readline.moveCursor( this._repl._ostream, 0, -1 ); readline.cursorTo( this._repl._ostream, cursorPosition + this._repl.promptLength() ); this._isPreviewing = true; debug( 'Successfully evaluated command.' ); }); // EXPORTS // module.exports = EagerEvaluator; |