Skip to content

Commit 9cefae1

Browse files
committed
refactor: evalin command
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: passed - 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 ---
1 parent 3410341 commit 9cefae1

File tree

5 files changed

+113
-174
lines changed

5 files changed

+113
-174
lines changed

lib/node_modules/@stdlib/repl/base/ctor/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,25 @@ var list = repl.varsWorkspace( repl.currentWorkspace );
261261
repl.close();
262262
```
263263

264+
#### REPL.prototype.evalin( workspace, expression )
265+
266+
Evaluates an expression in a specified workspace.
267+
268+
```javascript
269+
// Create a new REPL:
270+
var repl = new REPL();
271+
272+
// ...
273+
274+
// Evaluate in base workspace:
275+
repl.evalin( 'base', 'var x = 5;' );
276+
277+
// ...
278+
279+
// Close the REPL:
280+
repl.close();
281+
```
282+
264283
#### REPL.prototype.renameWorkspace( oldName, newName )
265284

266285
Renames a workspace.

lib/node_modules/@stdlib/repl/base/ctor/lib/main.js

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
// MODULES //
2424

25+
var vm = require( 'vm' );
2526
var EventEmitter = require( 'events' ).EventEmitter;
2627
var logger = require( 'debug' );
2728
var inherit = require( '@stdlib/utils/inherit' );
@@ -59,6 +60,8 @@ var tokenizerFactory = require( './tokenizer.js' );
5960
var processCommand = require( './process_command.js' );
6061
var compileCommand = require( './compile_command.js' );
6162
var processInput = require( './process_input.js' );
63+
var updateRegExpCache = require( './update_regexp_cache.js' );
64+
var restoreRegExpMatches = require( './restore_regexp_matches.js' );
6265
var isMultilineInput = require( './is_multiline_input.js' );
6366
var contains = require( './contains.js' );
6467
var filter = require( './filter.js' );
@@ -70,6 +73,7 @@ var ALIAS_OVERRIDES = require( './alias_overrides.js' );
7073
// VARIABLES //
7174

7275
var debug = logger( 'repl' );
76+
var RE_WHITESPACE = /^\s*$/;
7377

7478

7579
// MAIN //
@@ -992,6 +996,92 @@ setNonEnumerableReadOnly( REPL.prototype, 'varsWorkspace', function varsWorkspac
992996
return out;
993997
});
994998

999+
/**
1000+
* Evaluates an expression in a specified workspace.
1001+
*
1002+
* @name evalin
1003+
* @memberof REPL.prototype
1004+
* @type {Function}
1005+
* @param {string} workspace - workspace name
1006+
* @param {string} expression - expression to evaluate
1007+
* @returns {void}
1008+
*
1009+
* @example
1010+
* // Create a new REPL:
1011+
* var repl = new REPL();
1012+
*
1013+
* // ...
1014+
*
1015+
* // Evaluate in base workspace:
1016+
* repl.evalin( 'base', 'var x = 5;' );
1017+
*
1018+
* // ...
1019+
*
1020+
* // Close the REPL:
1021+
* repl.close();
1022+
*/
1023+
setNonEnumerableReadOnly( REPL.prototype, 'evalin', function evalin( workspace, expression ) {
1024+
var script;
1025+
var opts;
1026+
var FLG;
1027+
var ws;
1028+
if ( !isString( workspace ) ) {
1029+
throw new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', workspace ) );
1030+
}
1031+
if ( !isString( expression ) ) {
1032+
throw new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', workspace ) );
1033+
}
1034+
if ( RE_WHITESPACE.test( expression ) ) {
1035+
debug( 'Expression only consists of whitespace. Nothing to evaluate.' );
1036+
return;
1037+
}
1038+
// Cache the name of the current workspace:
1039+
ws = this._currentWorkspace;
1040+
1041+
// Temporarily silence logging:
1042+
FLG = this._quiet;
1043+
this._quiet = true;
1044+
1045+
try {
1046+
// Switch to the target workspace:
1047+
this.switchWorkspace( workspace );
1048+
} catch ( error ) {
1049+
this._quiet = FLG;
1050+
throw error;
1051+
}
1052+
// Try evaluating the expression...
1053+
opts = {
1054+
'filename': '<repl>',
1055+
'lineOffset': 0
1056+
};
1057+
try {
1058+
// FIXME: this needs to follow the same logic as `process_line`, such as code wrapping, asynchronous execution, and handling top-level `await`!!!
1059+
script = new vm.Script( expression, opts );
1060+
} catch ( error ) {
1061+
this.switchWorkspace( ws );
1062+
this._quiet = FLG;
1063+
throw error;
1064+
}
1065+
// Set the (non-standard) properties on the `RegExp` expression object to the cached matches:
1066+
restoreRegExpMatches( this._regexp );
1067+
1068+
opts = {
1069+
'timeout': this._timeout,
1070+
'displayErrors': false,
1071+
'breakOnSigint': true // Note: only applies for Node.js versions >=6.3.0
1072+
};
1073+
1074+
// FIXME: we need to follow similar logic as `drain.js`, such as SIGINT handling!!!
1075+
script.runInContext( this._context, opts );
1076+
updateRegExpCache( this._regexp );
1077+
1078+
// Return to the previous workspace:
1079+
this.switchWorkspace( ws );
1080+
1081+
// Re-enable logging (if enabled):
1082+
this._quiet = FLG;
1083+
});
1084+
9951085
/**
9961086
* Renames a workspace.
9971087
*

lib/node_modules/@stdlib/repl/lib/commands/evalin.js

Lines changed: 4 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,12 @@
2222

2323
// MODULES //
2424

25-
var vm = require( 'vm' );
2625
var logger = require( 'debug' );
27-
var isString = require( '@stdlib/assert/is-string' ).isPrimitive;
28-
var format = require( '@stdlib/string/format' );
29-
var updateRegExpCache = require( './../update_regexp_cache.js' );
30-
var restoreRegExpMatches = require( './../restore_regexp_matches.js' );
3126

3227

3328
// VARIABLES //
3429

3530
var debug = logger( 'repl:command:evalin' );
36-
var RE_WHITESPACE = /^\s*$/;
3731

3832

3933
// MAIN //
@@ -57,82 +51,13 @@ function command( repl ) {
5751
* @returns {void}
5852
*/
5953
function onCommand( workspace, expression ) {
60-
var script;
61-
var opts;
62-
var err;
63-
var FLG;
64-
var ws;
65-
if ( !isString( workspace ) ) {
66-
err = new TypeError( format( 'invalid argument. First argument must be a string. Value: `%s`.', workspace ) );
67-
debug( 'Error: %s', err.message );
68-
repl._ostream.write( 'Error: '+err.message+'\n' );
69-
return;
70-
}
71-
if ( !isString( expression ) ) {
72-
err = new TypeError( format( 'invalid argument. Second argument must be a string. Value: `%s`.', workspace ) );
73-
debug( 'Error: %s', err.message );
74-
repl._ostream.write( 'Error: '+err.message+'\n' );
75-
return;
76-
}
77-
if ( RE_WHITESPACE.test( expression ) ) {
78-
debug( 'Expression only consists of whitespace. Nothing to evaluate.' );
79-
return;
80-
}
81-
// Cache the name of the current workspace:
82-
ws = repl.currentWorkspace;
83-
84-
// Temporarily silence logging:
85-
FLG = repl._quiet;
86-
repl._quiet = true;
87-
8854
try {
89-
// Switch to the target workspace:
90-
repl.switchWorkspace( workspace );
91-
} catch ( error ) {
92-
debug( 'Error: %s', error.message );
93-
repl._ostream.write( 'Error: '+error.message+'\n' );
94-
repl._quiet = FLG;
95-
return;
96-
}
97-
// Try evaluating the expression...
98-
opts = {
99-
'filename': '<repl>',
100-
'lineOffset': 0
101-
};
102-
try {
103-
// FIXME: this needs to follow the same logic as `process_line`, such as code wrapping, asynchronous execution, and handling top-level `await`!!!
104-
script = new vm.Script( expression, opts );
105-
} catch ( error ) {
106-
debug( 'Error: %s', error.message );
107-
repl._ostream.write( 'Error: '+error.message+'\n' );
108-
repl.switchWorkspace( ws );
109-
repl._quiet = FLG;
110-
return;
111-
}
112-
// Set the (non-standard) properties on the `RegExp` expression object to the cached matches:
113-
restoreRegExpMatches( repl._regexp );
114-
115-
opts = {
116-
'timeout': repl._timeout,
117-
'displayErrors': false,
118-
'breakOnSigint': true // Note: only applies for Node.js versions >=6.3.0
119-
};
120-
121-
// FIXME: we need to follow similar logic as `drain.js`, such as SIGINT handling!!!
122-
try {
123-
script.runInContext( repl._context, opts );
55+
repl.evalin( workspace, expression );
12456
repl._ostream.write( 'Successfully evaluated expression.\n' );
125-
} catch ( error ) {
126-
debug( 'Error: %s', error.message );
127-
repl._ostream.write( 'Error: '+error.message+'\n' );
57+
} catch ( err ) {
58+
debug( 'Error: %s', err.message );
59+
repl._ostream.write( 'Error: '+err.message+'\n' );
12860
}
129-
updateRegExpCache( repl._regexp );
130-
131-
// Return to the previous workspace:
132-
repl.switchWorkspace( ws );
133-
134-
// Re-enable logging (if enabled):
135-
repl._quiet = FLG;
13661
}
13762
}
13863

lib/node_modules/@stdlib/repl/lib/restore_regexp_matches.js

Lines changed: 0 additions & 48 deletions
This file was deleted.

lib/node_modules/@stdlib/repl/lib/update_regexp_cache.js

Lines changed: 0 additions & 47 deletions
This file was deleted.

0 commit comments

Comments
 (0)