@@ -46,16 +46,17 @@ var path = require('path');
46
46
var fs = require ( 'fs' ) ;
47
47
var rl = require ( 'readline' ) ;
48
48
49
+ global . module = module ;
50
+ global . exports = exports ;
51
+ global . require = require ;
52
+
49
53
// If obj.hasOwnProperty has been overridden, then calling
50
54
// obj.hasOwnProperty(prop) will break.
51
55
// See: https://github.com/joyent/node/issues/1707
52
56
function hasOwnProperty ( obj , prop ) {
53
57
return Object . prototype . hasOwnProperty . call ( obj , prop ) ;
54
58
}
55
59
56
-
57
- var context ;
58
-
59
60
exports . disableColors = process . env . NODE_DISABLE_COLORS ? true : false ;
60
61
61
62
// hack for require.resolve("./relative") to work properly.
@@ -71,16 +72,27 @@ exports.writer = util.inspect;
71
72
function REPLServer ( prompt , stream , eval ) {
72
73
var self = this ;
73
74
74
- self . eval = eval || function ( code , context , file , cb ) {
75
+ var contextWarning ;
76
+ Object . defineProperty ( this , 'context' , {
77
+ get : function ( ) {
78
+ if ( ! contextWarning ) {
79
+ contextWarning = 'repl.context is deprecated.' ;
80
+ console . error ( contextWarning ) ;
81
+ }
82
+ return global ;
83
+ }
84
+ } ) ;
85
+
86
+
87
+ self . eval = eval || function ( code , file , cb ) {
75
88
try {
76
- var err , result = vm . runInContext ( code , context , file ) ;
89
+ var err , result = vm . runInThisContext ( code , file ) ;
77
90
} catch ( e ) {
78
91
err = e ;
79
92
}
80
93
cb ( err , result ) ;
81
94
} ;
82
95
83
- self . resetContext ( ) ;
84
96
self . bufferedCommand = '' ;
85
97
86
98
if ( stream ) {
@@ -173,14 +185,13 @@ function REPLServer(prompt, stream, eval) {
173
185
// First we attempt to eval as expression with parens.
174
186
// This catches '{a : 1}' properly.
175
187
self . eval ( '(' + evalCmd + ')' ,
176
- self . context ,
177
188
'repl' ,
178
189
function ( e , ret ) {
179
190
if ( e && ! isSyntaxError ( e ) ) return finish ( e ) ;
180
191
181
192
if ( typeof ret === 'function' || e ) {
182
193
// Now as statement without parens.
183
- self . eval ( evalCmd , self . context , 'repl' , finish ) ;
194
+ self . eval ( evalCmd , 'repl' , finish ) ;
184
195
} else {
185
196
finish ( null , ret ) ;
186
197
}
@@ -218,7 +229,7 @@ function REPLServer(prompt, stream, eval) {
218
229
219
230
// If we got any output - print it (if no error)
220
231
if ( ! e && ret !== undefined ) {
221
- self . context . _ = ret ;
232
+ global . _ = ret ;
222
233
self . outputStream . write ( exports . writer ( ret ) + '\n' ) ;
223
234
}
224
235
@@ -245,25 +256,12 @@ exports.start = function(prompt, source, eval) {
245
256
} ;
246
257
247
258
248
- REPLServer . prototype . createContext = function ( ) {
249
- var context = vm . createContext ( ) ;
250
-
251
- for ( var i in global ) context [ i ] = global [ i ] ;
252
- context . module = module ;
253
- context . require = require ;
254
- context . global = context ;
255
- context . global . global = context ;
256
-
257
- return context ;
258
- } ;
259
-
259
+ var resetWarning ;
260
260
REPLServer . prototype . resetContext = function ( force ) {
261
- if ( ! context || force ) {
262
- context = this . createContext ( ) ;
263
- for ( var i in require . cache ) delete require . cache [ i ] ;
261
+ if ( ! resetWarning ) {
262
+ resetWarning = 'REPLServer.resetContext is deprecated.' ;
263
+ console . error ( resetWarning ) ;
264
264
}
265
-
266
- this . context = context ;
267
265
} ;
268
266
269
267
REPLServer . prototype . displayPrompt = function ( ) {
@@ -413,26 +411,9 @@ REPLServer.prototype.complete = function(line, callback) {
413
411
if ( ! expr ) {
414
412
// If context is instance of vm.ScriptContext
415
413
// Get global vars synchronously
416
- if ( this . context . constructor . name === 'Context' ) {
417
- completionGroups . push ( Object . getOwnPropertyNames ( this . context ) ) ;
418
- addStandardGlobals ( ) ;
419
- completionGroupsLoaded ( ) ;
420
- } else {
421
- this . eval ( '.scope' , this . context , 'repl' , function ( err , globals ) {
422
- if ( err || ! globals ) {
423
- addStandardGlobals ( ) ;
424
- } else if ( Array . isArray ( globals [ 0 ] ) ) {
425
- // Add grouped globals
426
- globals . forEach ( function ( group ) {
427
- completionGroups . push ( group ) ;
428
- } ) ;
429
- } else {
430
- completionGroups . push ( globals ) ;
431
- addStandardGlobals ( ) ;
432
- }
433
- completionGroupsLoaded ( ) ;
434
- } ) ;
435
- }
414
+ completionGroups . push ( Object . getOwnPropertyNames ( global ) ) ;
415
+ addStandardGlobals ( ) ;
416
+ completionGroupsLoaded ( ) ;
436
417
437
418
function addStandardGlobals ( ) {
438
419
// Global object properties
@@ -457,7 +438,7 @@ REPLServer.prototype.complete = function(line, callback) {
457
438
}
458
439
459
440
} else {
460
- this . eval ( expr , this . context , 'repl' , function ( e , obj ) {
441
+ this . eval ( expr , 'repl' , function ( e , obj ) {
461
442
// if (e) console.log(e);
462
443
463
444
if ( obj != null ) {
@@ -584,16 +565,6 @@ function defineDefaultCommands(repl) {
584
565
}
585
566
} ) ;
586
567
587
- repl . defineCommand ( 'clear' , {
588
- help : 'Break, and also clear the local context' ,
589
- action : function ( ) {
590
- this . outputStream . write ( 'Clearing context...\n' ) ;
591
- this . bufferedCommand = '' ;
592
- this . resetContext ( true ) ;
593
- this . displayPrompt ( ) ;
594
- }
595
- } ) ;
596
-
597
568
repl . defineCommand ( 'exit' , {
598
569
help : 'Exit the repl' ,
599
570
action : function ( ) {
@@ -628,32 +599,3 @@ function trimWhitespace(cmd) {
628
599
function regexpEscape ( s ) {
629
600
return s . replace ( / [ - [ \] { } ( ) * + ? . , \\ ^ $ | # \s ] / g, '\\$&' ) ;
630
601
}
631
-
632
-
633
- /**
634
- * Converts commands that use var and function <name>() to use the
635
- * local exports.context when evaled. This provides a local context
636
- * on the REPL.
637
- *
638
- * @param {String } cmd The cmd to convert.
639
- * @return {String } The converted command.
640
- */
641
- REPLServer . prototype . convertToContext = function ( cmd ) {
642
- var self = this , matches ,
643
- scopeVar = / ^ \s * v a r \s * ( [ _ \w \$ ] + ) ( .* ) $ / m,
644
- scopeFunc = / ^ \s * f u n c t i o n \s * ( [ _ \w \$ ] + ) / ;
645
-
646
- // Replaces: var foo = "bar"; with: self.context.foo = bar;
647
- matches = scopeVar . exec ( cmd ) ;
648
- if ( matches && matches . length === 3 ) {
649
- return 'self.context.' + matches [ 1 ] + matches [ 2 ] ;
650
- }
651
-
652
- // Replaces: function foo() {}; with: foo = function foo() {};
653
- matches = scopeFunc . exec ( self . bufferedCommand ) ;
654
- if ( matches && matches . length === 2 ) {
655
- return matches [ 1 ] + ' = ' + self . bufferedCommand ;
656
- }
657
-
658
- return cmd ;
659
- } ;
0 commit comments