@@ -31,6 +31,7 @@ var hasOwnProp = require( '@stdlib/assert/has-own-property' );
3131var objectKeys = require ( '@stdlib/utils/keys' ) ;
3232var properties = require ( '@stdlib/utils/properties' ) ;
3333var typeOf = require ( '@stdlib/utils/type-of' ) ;
34+ var append = require ( '@stdlib/utils/append' ) ;
3435var setNonEnumerable = require ( '@stdlib/utils/define-nonenumerable-property' ) ;
3536var setNonEnumerableReadOnly = require ( '@stdlib/utils/define-nonenumerable-read-only-property' ) ;
3637var setReadOnlyAccessor = require ( '@stdlib/utils/define-nonenumerable-read-only-accessor' ) ;
@@ -50,6 +51,8 @@ var validate = require( './validate.js' );
5051var defaults = require ( './defaults.js' ) ;
5152var setAliases = require ( './set_aliases.js' ) ;
5253var setAliasesGlobal = require ( './set_aliases_global.js' ) ;
54+ var setCommands = require ( './set_commands.js' ) ;
55+ var setCommandsGlobal = require ( './set_commands_global.js' ) ;
5356var createEvalContext = require ( './create_evaluation_context.js' ) ;
5457var completerFactory = require ( './completer.js' ) ;
5558var 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*
0 commit comments