Skip to content

Commit

Permalink
fix: add stricter timeout and clean ups
Browse files Browse the repository at this point in the history
Signed-off-by: Snehil Shah <[email protected]>

---
type: pre_commit_static_analysis_report
description: Results of running static analysis checks when committing changes.
report:
  - task: lint_filenames
    status: passed
  - task: lint_editorconfig
    status: passed
  - task: lint_markdown
    status: na
  - task: lint_package_json
    status: na
  - task: lint_repl_help
    status: na
  - task: lint_javascript_src
    status: passed
  - task: lint_javascript_cli
    status: na
  - task: lint_javascript_examples
    status: na
  - task: lint_javascript_tests
    status: na
  - task: lint_javascript_benchmarks
    status: na
  - task: lint_python
    status: na
  - task: lint_r
    status: na
  - task: lint_c_src
    status: na
  - task: lint_c_examples
    status: na
  - task: lint_c_benchmarks
    status: na
  - task: lint_c_tests_fixtures
    status: na
  - task: lint_shell
    status: na
  - task: lint_typescript_declarations
    status: na
  - task: lint_typescript_tests
    status: na
  - task: lint_license_headers
    status: passed
---

---
type: pre_push_report
description: Results of running various checks prior to pushing changes.
report:
  - task: run_javascript_examples
    status: na
  - task: run_c_examples
    status: na
  - task: run_cpp_examples
    status: na
  - task: run_javascript_readme_examples
    status: na
  - task: run_c_benchmarks
    status: na
  - task: run_cpp_benchmarks
    status: na
  - task: run_fortran_benchmarks
    status: na
  - task: run_javascript_benchmarks
    status: na
  - task: run_julia_benchmarks
    status: na
  - task: run_python_benchmarks
    status: na
  - task: run_r_benchmarks
    status: na
  - task: run_javascript_tests
    status: na
---
  • Loading branch information
Snehil-Shah committed Jan 30, 2025
1 parent bebc0fe commit cf2eaa7
Showing 1 changed file with 28 additions and 28 deletions.
56 changes: 28 additions & 28 deletions lib/node_modules/@stdlib/repl/lib/eager_evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ var debug = logger( 'repl:eager-evaluator' );
var AOPTS = {
'ecmaVersion': 'latest'
};
var ROPTS = {
'timeout': 400, // (in milliseconds) shouldn't be much noticeable when naturally typing
'displayErrors': false,
'breakOnSigint': true // Node.js >=6.3.0
};
var tempDB = {
'base_sin': {
'isPure': true
Expand All @@ -57,14 +62,14 @@ var ANSI_RESET = ANSI_COLORS[ 'reset' ];
* @private
* @param {REPL} repl - repl instance
* @param {Object} rli - readline instance
* @param {Boolean} enabled - boolean indicating whether the syntax-highlighter should be initially enabled
* @returns {EagerEvaluator} eager-evaluator 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...' );
debug( 'Creating a new eager evaluator...' );

// Cache a reference to the provided REPL instance:
this._repl = repl;
Expand All @@ -75,11 +80,11 @@ function EagerEvaluator( repl, rli, enabled ) {
// Cache a reference to the command array:
this._cmd = repl._cmd;

// Flag indicate weather eagerevaluation is enable or not
// Initialize a flag indicating whether the eager evaluator is enabled:
this._enabled = enabled;

// Flag indicate weather preview is on terminal or not
this._isPreview = false;
// Initialize a flag indicating whether we are currently previewing eagerly-evaluated output:
this._isPreviewing = false;

return this;
}
Expand All @@ -92,7 +97,7 @@ function EagerEvaluator( repl, rli, enabled ) {
* @memberof EagerEvaluator.prototype
* @type {Function}
* @param {string} code - input code
* @returns {boolean} - boolean indicating whether the code is side-effect-free or not
* @returns {boolean} - boolean indicating whether the code is side-effect-free
*
*/
setNonEnumerableReadOnly( EagerEvaluator.prototype, '_isSideEffectFree', function isSideEffectFree( code ) {
Expand All @@ -102,8 +107,8 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, '_isSideEffectFree', functio
try {
ast = parse( code, AOPTS );
}
catch (err) {
debug( 'Encountered an error when generating AST: %s', err );
catch ( err ) {
debug( 'Encountered an error when generating AST: %s', err.message );
return false;
}
// Iterate from each node in the body:
Expand All @@ -119,7 +124,7 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, '_isSideEffectFree', functio
*
* @private
* @param {Object} node - ast node
* @returns {Boolean} - boolean indicating whether the node is side-effect=free or not
* @returns {Boolean} - boolean indicating whether the node is side-effect-free
*/
function traverse( node ) {
var fname;
Expand Down Expand Up @@ -194,7 +199,7 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'clear', function clear() {
readline.clearLine( this._repl._ostream, 0 );
readline.moveCursor( this._repl._ostream, 0, -1 );
readline.cursorTo( this._repl._ostream, cursorPosition + this._repl.promptLength() );
this._isPreview = false;
this._isPreviewing = false;
} );

/**
Expand All @@ -210,10 +215,10 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'clear', function clear() {
*
*/
setNonEnumerableReadOnly( EagerEvaluator.prototype, 'beforeKeypress', function beforeKeypress() {
if (!this._isPreview ) {
if (!this._isPreviewing ) {
return;
}
if ( this._isPreview ) {
if ( this._isPreviewing ) {
this.clear();
}
});
Expand All @@ -235,7 +240,6 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'onKeypress', function onKey
var executable;
var nlInd;
var code;
var opts;
var pre;
var res;
var tmp;
Expand All @@ -245,35 +249,31 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'onKeypress', function onKey
}

code = this._cmd.join( '\n' ) + this._rli.line;
opts = {
'timeout': this._repl._timeout,
'displayErrors': false,
'breakOnSigint': true // Node.js >=6.3.0
};
debug( 'Eagerly evaluating: %s', code );
if ( !this._isSideEffectFree( code ) ) {
debug( 'code have side effect' );
debug( 'Code is not side-effect free, exiting eager-evaluation.' );
return;
}
debug( 'try to process command' );
tmp = processCommand( code );
if ( tmp instanceof Error ) {
debug( 'getting error %s', tmp.message );
debug( 'Error encountered when processing command: %s', tmp.message );
return;
}
debug( 'try to compile command' );
debug( 'Trying to compile command' );
executable = compileCommand( tmp );
if ( executable instanceof Error ) {
debug( 'getting error %s', executable.message );
debug( 'Error encountered when compiling command: %s', executable.message );
return;
}
try {
if ( this._repl._sandbox ) {
res = executable.compiled.runInContext( this._repl._context, opts );
res = executable.compiled.runInContext( this._repl._context, ROPTS );
} else {
res = executable.compiled.runInThisContext( opts );
res = executable.compiled.runInThisContext( ROPTS );
}
} catch (err) {
debug( 'getting error when executing the command %s', err.message );
} catch ( err ) {
debug( 'Encountered an error when executing the command: %s', err.message );
return;
}

Expand All @@ -287,7 +287,7 @@ setNonEnumerableReadOnly( EagerEvaluator.prototype, 'onKeypress', function onKey
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._isPreview = true;
this._isPreviewing = true;
debug( 'sucess' );
});

Expand Down

0 comments on commit cf2eaa7

Please sign in to comment.