All files / repl/lib create_evaluation_context.js

91.75% Statements 89/97
60% Branches 3/5
100% Functions 1/1
91.75% Lines 89/97

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 9811x 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 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 2204x 2204x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x 116x                 116x 116x 116x 116x 116x 116x 116x 11x 11x 11x 11x 11x  
/**
* @license Apache-2.0
*
* Copyright (c) 2019 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.
*/
 
'use strict';
 
// MODULES //
 
var Module = require( 'module' );
var vm = require( 'vm' );
var Console = require( 'console' ).Console;
var getGlobal = require( '@stdlib/utils/global' );
var objectKeys = require( '@stdlib/utils/keys' );
var setReadOnly = require( '@stdlib/utils/define-read-only-property' );
var setConfigurableReadOnly = require( '@stdlib/utils/define-configurable-read-only-property' );
var GLOBALS = require( './globals.js' );
var createRequire = require( './create_require.js' );
 
 
// VARIABLES //
 
// Cache references to module methods (WARNING: use of *private* methods exposed on the `Module` object; thus, breakage is possible!):
var resolveLookupPaths = Module._resolveLookupPaths; // eslint-disable-line no-underscore-dangle
 
 
// MAIN //
 
/**
* Creates a REPL evaluation context.
*
* @private
* @param {Array} out - strided output array for storing variables added to the evaluation context
* @param {Stream} ostream - output stream
* @param {boolean} sandbox - boolean indicating whether the evaluation context should be "sandboxed"
* @returns {Object} context
*/
function createContext( out, ostream, sandbox ) {
	var context;
	var keys;
	var i;
 
	// Create the REPL context...
	if ( sandbox ) {
		// Create a sandboxed context:
		context = vm.createContext();
 
		// Assign globals from the current global context to the sandboxed context (note: shallow copy!)...
		keys = objectKeys( GLOBALS );
		for ( i = 0; i < keys.length; i++ ) {
			setReadOnly( context, keys[ i ], GLOBALS[ keys[ i ] ] );
		}
		// Create a circular reference as in Node.js:
		setReadOnly( context, 'global', context );
 
		// Create a new `console` interface:
		setReadOnly( context, 'console', new Console( ostream ) );
 
		// Create a new `module` object:
		setReadOnly( context, 'module', new Module( '<repl>' ) );
 
		// Create a new `require` function:
		setReadOnly( context, 'require', createRequire( context.module ) );
	} else {
		context = getGlobal(); // current global variable

		// Create a new `module` object:
		setConfigurableReadOnly( context, 'module', new Module( '<repl>' ) );

		// Create a new `require` function:
		setConfigurableReadOnly( context, 'require', createRequire( context.module ) );
	}
	context.module.paths = resolveLookupPaths( '<repl>', module, true ) || [];
 
	out.push( 'global', context.global );
	out.push( 'require', context.require );
 
	return context;
}
 
 
// EXPORTS //
 
module.exports = createContext;