Skip to content

Commit ccdfd71

Browse files
committed
refactor: clean up context creation
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 ---
1 parent 9c83422 commit ccdfd71

File tree

5 files changed

+48
-29
lines changed

5 files changed

+48
-29
lines changed

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ var resolveLookupPaths = Module._resolveLookupPaths; // eslint-disable-line no-u
4343
* Creates a REPL evaluation context.
4444
*
4545
* @private
46+
* @param {Object} context - context object
4647
* @param {Array} out - strided output array for storing variables added to the evaluation context
4748
* @param {Stream} ostream - output stream
4849
* @param {Stream} estream - error stream
4950
* @param {boolean} sandbox - boolean indicating whether the evaluation context should be "sandboxed"
5051
* @returns {Object} context
5152
*/
52-
function createContext( out, ostream, estream, sandbox ) {
53-
var context;
53+
function createContext( context, out, ostream, estream, sandbox ) {
5454
var keys;
5555
var i;
5656

5757
// Create the REPL context...
5858
if ( sandbox ) {
5959
// Create a sandboxed context:
60-
context = vm.createContext();
60+
context = vm.createContext( context );
6161

6262
// Assign globals from the current global context to the sandboxed context (note: shallow copy!)...
6363
keys = objectKeys( GLOBALS );

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

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3131
var objectKeys = require( '@stdlib/utils/keys' );
3232
var properties = require( '@stdlib/utils/properties' );
3333
var typeOf = require( '@stdlib/utils/type-of' );
34+
var append = require( '@stdlib/utils/append' );
3435
var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
3536
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
3637
var setReadOnlyAccessor = require( '@stdlib/utils/define-nonenumerable-read-only-accessor' );
@@ -50,6 +51,8 @@ var validate = require( './validate.js' );
5051
var defaults = require( './defaults.js' );
5152
var setAliases = require( './set_aliases.js' );
5253
var setAliasesGlobal = require( './set_aliases_global.js' );
54+
var setCommands = require( './set_commands.js' );
55+
var setCommandsGlobal = require( './set_commands_global.js' );
5356
var createEvalContext = require( './create_evaluation_context.js' );
5457
var completerFactory = require( './completer.js' );
5558
var tokenizerFactory = require( './tokenizer.js' );
@@ -448,25 +451,31 @@ setNonEnumerableReadOnly( REPL.prototype, 'tokenizer', function tokenizer( line
448451
* @name createContext
449452
* @memberof REPL.prototype
450453
* @type {Function}
454+
* @param {Object} context - context object
451455
* @returns {Object} REPL context
452456
*
453457
* @example
454458
* // Create a new REPL:
455459
* var repl = new REPL();
456460
*
457461
* // Return a new REPL context:
458-
* var ctx = repl.createContext();
462+
* var ctx = repl.createContext({});
459463
*
460464
* // Close the REPL:
461465
* repl.close();
462466
*/
463-
setNonEnumerableReadOnly( REPL.prototype, 'createContext', function createContext() {
464-
var context;
465-
467+
setNonEnumerableReadOnly( REPL.prototype, 'createContext', function createContext( context ) {
468+
if ( arguments.length ) {
469+
if ( !isPlainObject( context ) ) {
470+
throw new TypeError( format( 'invalid argument. First argument must be a plain object. Value: `%s`.', context ) );
471+
}
472+
} else {
473+
context = {};
474+
}
466475
debug( 'Creating REPL execution context...' );
467476

468477
// Create an evaluation context:
469-
context = createEvalContext( this._contextVars, this._output, this._error, this._sandbox ); // eslint-disable-line max-len
478+
context = createEvalContext( context, this._contextVars, this._output, this._error, this._sandbox ); // eslint-disable-line max-len
470479

471480
// Add project APIs...
472481
if ( this._sandbox ) {
@@ -475,6 +484,15 @@ setNonEnumerableReadOnly( REPL.prototype, 'createContext', function createContex
475484
setAliasesGlobal( this._globalVars, this._aliases, context, ALIAS_OVERRIDES ); // eslint-disable-line max-len
476485
}
477486

487+
// NOTE: the context should not be augmented **after** this point, except as done by the user when declaring variables and functions!
488+
489+
// Sort the list of global variables:
490+
if ( this._sandbox === false ) {
491+
this._globalVars.sort( propertyComparator );
492+
}
493+
// Capture a snapshot of the current global workspace:
494+
append( this._workspace, properties( context ).sort( propertyComparator ) );
495+
478496
return context;
479497
});
480498

@@ -550,6 +568,23 @@ setNonEnumerableReadOnly( REPL.prototype, 'resetContext', function resetContext(
550568
return this;
551569
});
552570

571+
/**
572+
* Sets commands on a context object.
573+
*
574+
* @name setCommands
575+
* @memberof REPL.prototype
576+
* @type {Function}
577+
* @param {Object} context - context object
578+
* @param {ArrayArray} commands - commands
579+
* @returns {Object} context object
580+
*/
581+
setNonEnumerableReadOnly( REPL.prototype, 'setCommands', function set( context, commands ) {
582+
if ( this._sandbox ) {
583+
return setCommands( context, commands );
584+
}
585+
return setCommandsGlobal( this._globalVars, context, commands );
586+
});
587+
553588
/**
554589
* Creates a new workspace.
555590
*

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

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ var readFileSync = require( '@stdlib/fs/read-file' ).sync;
4141
var RE_EOL = require( '@stdlib/regexp/eol' ).REGEXP;
4242
var setNonEnumerable = require( '@stdlib/utils/define-nonenumerable-property' );
4343
var setNonEnumerableReadOnly = require( '@stdlib/utils/define-nonenumerable-read-only-property' );
44-
var properties = require( '@stdlib/utils/properties' );
45-
var append = require( '@stdlib/utils/append' );
4644
var nextTick = require( '@stdlib/utils/next-tick' );
4745
var Boolean = require( '@stdlib/boolean/ctor' );
4846
var assign = require( '@stdlib/object/assign' );
@@ -51,9 +49,6 @@ var validate = require( './validate.js' );
5149
var parseKey = require( './parse_key.js' );
5250
var defaults = require( './defaults.js' );
5351
var isSettingName = require( './is_setting_name.js' );
54-
var setCommands = require( './set_commands.js' );
55-
var setCommandsGlobal = require( './set_commands_global.js' );
56-
var propertyComparator = require( './property_comparator.js' );
5752
var commands = require( './commands.js' );
5853
var displayPrompt = require( './display_prompt.js' );
5954
var inputPrompt = require( './input_prompt.js' );
@@ -685,28 +680,17 @@ setNonEnumerableReadOnly( REPL.prototype, 'createContext', function createContex
685680
var context;
686681
var cmds;
687682

688-
context = BaseREPL.prototype.createContext.call( this );
683+
// Initialize context object:
684+
context = {};
689685

690686
// Get the list of REPL-specific commands:
691687
cmds = commands( this );
692688

693689
// Add commands requiring privileged access to internal instance variables...
694-
if ( this._sandbox ) {
695-
setCommands( context, cmds );
696-
} else {
697-
setCommandsGlobal( this._globalVars, context, cmds );
698-
}
699-
700-
// NOTE: the context should not be augmented **after** this point, except as done by the user when declaring variables and functions!
690+
context = this.setCommands( context, cmds );
701691

702-
// Sort the list of global variables:
703-
if ( this._sandbox === false ) {
704-
this._globalVars.sort( propertyComparator );
705-
}
706-
// Capture a snapshot of the current global workspace:
707-
append( this._workspace, properties( context ).sort( propertyComparator ) );
708-
709-
return context;
692+
// Call the parent method:
693+
return BaseREPL.prototype.createContext.call( this, context );
710694
});
711695

712696
/**

0 commit comments

Comments
 (0)