| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 84dd526 commit c39f6c0
4 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -39,6 +39,16 @@ const debug = util.debuglog('repl'); | |||
| 39 | 39 | const parentModule = module; | |
| 40 | 40 | const replMap = new WeakMap(); | |
| 41 | 41 | ||
| 42 | + const GLOBAL_OBJECT_PROPERTIES = ['NaN', 'Infinity', 'undefined', | ||
| 43 | + 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', | ||
| 44 | + 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', | ||
| 45 | + 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', | ||
| 46 | + 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', | ||
| 47 | + 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', | ||
| 48 | + 'Math', 'JSON']; | ||
| 49 | + const GLOBAL_OBJECT_PROPERTY_MAP = {}; | ||
| 50 | + GLOBAL_OBJECT_PROPERTIES.forEach((p) => GLOBAL_OBJECT_PROPERTY_MAP[p] = p); | ||
| 51 | + | ||
| 42 | 52 | try { | |
| 43 | 53 | // hack for require.resolve("./relative") to work properly. | |
| 44 | 54 | module.filename = path.resolve('repl'); | |
@@ -582,10 +592,20 @@ REPLServer.prototype.createContext = function() { | |||
| 582 | 592 | context = global; | |
| 583 | 593 | } else { | |
| 584 | 594 | context = vm.createContext(); | |
| 585 | - for (var i in global) context[i] = global[i]; | ||
| 586 | - context.console = new Console(this.outputStream); | ||
| 587 | 595 | context.global = context; | |
| 588 | - context.global.global = context; | ||
| 596 | + const _console = new Console(this.outputStream); | ||
| 597 | + Object.defineProperty(context, 'console', { | ||
| 598 | + configurable: true, | ||
| 599 | + enumerable: true, | ||
| 600 | + get: () => _console | ||
| 601 | + }); | ||
| 602 | + Object.getOwnPropertyNames(global).filter((name) => { | ||
| 603 | + if (name === 'console' || name === 'global') return false; | ||
| 604 | + return GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined; | ||
| 605 | + }).forEach((name) => { | ||
| 606 | + Object.defineProperty(context, name, | ||
| 607 | + Object.getOwnPropertyDescriptor(global, name)); | ||
| 608 | + }); | ||
| 589 | 609 | } | |
| 590 | 610 | ||
| 591 | 611 | const module = new Module('<repl>'); | |
@@ -1052,13 +1072,7 @@ REPLServer.prototype.memory = function memory(cmd) { | |||
| 1052 | 1072 | function addStandardGlobals(completionGroups, filter) { | |
| 1053 | 1073 | // Global object properties | |
| 1054 | 1074 | // (http://www.ecma-international.org/publications/standards/Ecma-262.htm) | |
| 1055 | - completionGroups.push(['NaN', 'Infinity', 'undefined', | ||
| 1056 | - 'eval', 'parseInt', 'parseFloat', 'isNaN', 'isFinite', 'decodeURI', | ||
| 1057 | - 'decodeURIComponent', 'encodeURI', 'encodeURIComponent', | ||
| 1058 | - 'Object', 'Function', 'Array', 'String', 'Boolean', 'Number', | ||
| 1059 | - 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', | ||
| 1060 | - 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', | ||
| 1061 | - 'Math', 'JSON']); | ||
| 1075 | + completionGroups.push(GLOBAL_OBJECT_PROPERTIES); | ||
| 1062 | 1076 | // Common keywords. Exclude for completion on the empty string, b/c | |
| 1063 | 1077 | // they just get in the way. | |
| 1064 | 1078 | if (filter) { | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -18,3 +18,6 @@ assert(r.context.console); | |||
| 18 | 18 | ||
| 19 | 19 | // ensure that the repl console instance is not the global one | |
| 20 | 20 | assert.notStrictEqual(r.context.console, console); | |
| 21 | + | ||
| 22 | + // ensure that the repl console instance does not have a setter | ||
| 23 | + assert.throws(() => r.context.console = 'foo', TypeError); | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,26 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + const common = require('../common'); | ||
| 3 | + const assert = require('assert'); | ||
| 4 | + const repl = require('repl'); | ||
| 5 | + | ||
| 6 | + // Create a dummy stream that does nothing | ||
| 7 | + const stream = new common.ArrayStream(); | ||
| 8 | + | ||
| 9 | + // Test when useGlobal is false | ||
| 10 | + testContext(repl.start({ | ||
| 11 | + input: stream, | ||
| 12 | + output: stream, | ||
| 13 | + useGlobal: false | ||
| 14 | + })); | ||
| 15 | + | ||
| 16 | + function testContext(repl) { | ||
| 17 | + const context = repl.createContext(); | ||
| 18 | + // ensure that the repl context gets its own "console" instance | ||
| 19 | + assert(context.console instanceof require('console').Console); | ||
| 20 | + | ||
| 21 | + // ensure that the repl's global property is the context | ||
| 22 | + assert(context.global === context); | ||
| 23 | + | ||
| 24 | + // ensure that the repl console instance does not have a setter | ||
| 25 | + assert.throws(() => context.console = 'foo'); | ||
| 26 | + } | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -267,3 +267,19 @@ putIn.run(['.clear']); | |||
| 267 | 267 | testMe.complete('.b', common.mustCall((error, data) => { | |
| 268 | 268 | assert.deepStrictEqual(data, [['break'], 'b']); | |
| 269 | 269 | })); | |
| 270 | + | ||
| 271 | + const testNonGlobal = repl.start({ | ||
| 272 | + input: putIn, | ||
| 273 | + output: putIn, | ||
| 274 | + useGlobal: false | ||
| 275 | + }); | ||
| 276 | + | ||
| 277 | + const builtins = [['Infinity', '', 'Int16Array', 'Int32Array', | ||
| 278 | + 'Int8Array'], 'I']; | ||
| 279 | + | ||
| 280 | + if (typeof Intl === 'object') { | ||
| 281 | + builtins[0].push('Intl'); | ||
| 282 | + } | ||
| 283 | + testNonGlobal.complete('I', common.mustCall((error, data) => { | ||
| 284 | + assert.deepStrictEqual(data, builtins); | ||
| 285 | + })); | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments