@@ -58,6 +58,7 @@ const Module = require('module');
5858const domain = require ( 'domain' ) ;
5959const debug = util . debuglog ( 'repl' ) ;
6060const errors = require ( 'internal/errors' ) ;
61+ const { sendInspectorCommand } = require ( 'internal/util/inspector' ) ;
6162
6263const parentModule = module ;
6364const replMap = new WeakMap ( ) ;
@@ -75,6 +76,7 @@ for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
7576 GLOBAL_OBJECT_PROPERTIES [ n ] ;
7677}
7778const kBufferedCommandSymbol = Symbol ( 'bufferedCommand' ) ;
79+ const kContextId = Symbol ( 'contextId' ) ;
7880
7981try {
8082 // hack for require.resolve("./relative") to work properly.
@@ -155,6 +157,8 @@ function REPLServer(prompt,
155157 self . last = undefined ;
156158 self . breakEvalOnSigint = ! ! breakEvalOnSigint ;
157159 self . editorMode = false ;
160+ // Context id for use with the inspector protocol.
161+ self [ kContextId ] = undefined ;
158162
159163 // just for backwards compat, see github.com/joyent/node/pull/7127
160164 self . rli = this ;
@@ -644,7 +648,16 @@ REPLServer.prototype.createContext = function() {
644648 if ( this . useGlobal ) {
645649 context = global ;
646650 } else {
647- context = vm . createContext ( ) ;
651+ sendInspectorCommand ( ( session ) => {
652+ session . post ( 'Runtime.enable' ) ;
653+ session . on ( 'Runtime.executionContextCreated' , ( { params } ) => {
654+ this [ kContextId ] = params . context . id ;
655+ } ) ;
656+ context = vm . createContext ( ) ;
657+ session . post ( 'Runtime.disable' ) ;
658+ } , ( ) => {
659+ context = vm . createContext ( ) ;
660+ } ) ;
648661 context . global = context ;
649662 const _console = new Console ( this . outputStream ) ;
650663 Object . defineProperty ( context , 'console' , {
@@ -779,6 +792,18 @@ function filteredOwnPropertyNames(obj) {
779792 return Object . getOwnPropertyNames ( obj ) . filter ( intFilter ) ;
780793}
781794
795+ function getGlobalLexicalScopeNames ( contextId ) {
796+ return sendInspectorCommand ( ( session ) => {
797+ let names = [ ] ;
798+ session . post ( 'Runtime.globalLexicalScopeNames' , {
799+ executionContextId : contextId
800+ } , ( error , result ) => {
801+ if ( ! error ) names = result . names ;
802+ } ) ;
803+ return names ;
804+ } , ( ) => [ ] ) ;
805+ }
806+
782807REPLServer . prototype . complete = function ( ) {
783808 this . completer . apply ( this , arguments ) ;
784809} ;
@@ -942,6 +967,7 @@ function complete(line, callback) {
942967 // If context is instance of vm.ScriptContext
943968 // Get global vars synchronously
944969 if ( this . useGlobal || vm . isContext ( this . context ) ) {
970+ completionGroups . push ( getGlobalLexicalScopeNames ( this [ kContextId ] ) ) ;
945971 var contextProto = this . context ;
946972 while ( contextProto = Object . getPrototypeOf ( contextProto ) ) {
947973 completionGroups . push (
0 commit comments