Skip to content

Commit ada41b0

Browse files
BridgeARMylesBorins
authored andcommitted
repl: make console, module and require non-enumerable
This aligns these globals with the regular context. PR-URL: #20717 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
1 parent 5ffce3e commit ada41b0

8 files changed

Lines changed: 36 additions & 75 deletions

‎lib/repl.js‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,7 +793,7 @@ REPLServer.prototype.createContext = function() {
793793
const_console=newConsole(this.outputStream);
794794
Object.defineProperty(context,'console',{
795795
configurable: true,
796-
enumerable: true,
796+
writable: true,
797797
value: _console
798798
});
799799

@@ -813,9 +813,16 @@ REPLServer.prototype.createContext = function() {
813813
module.paths=
814814
CJSModule._resolveLookupPaths('<repl>',parentModule,true)||[];
815815

816-
varrequire=makeRequireFunction(module);
817-
context.module=module;
818-
context.require=require;
816+
Object.defineProperty(context,'module',{
817+
configurable: true,
818+
writable: true,
819+
value: module
820+
});
821+
Object.defineProperty(context,'require',{
822+
configurable: true,
823+
writable: true,
824+
value: makeRequireFunction(module)
825+
});
819826

820827
addBuiltinLibsToObject(context);
821828

‎test/parallel/test-repl-console.js‎

Lines changed: 0 additions & 44 deletions
This file was deleted.

‎test/parallel/test-repl-context.js‎

Lines changed: 23 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,32 +4,39 @@ const assert = require('assert');
44
constrepl=require('repl');
55
constvm=require('vm');
66

7-
// Create a dummy stream that does nothing
7+
// Create a dummy stream that does nothing.
88
conststream=newcommon.ArrayStream();
99

10-
// Test when useGlobal is false
11-
testContext(repl.start({
12-
input: stream,
13-
output: stream,
14-
useGlobal: false
15-
}));
10+
// Test context when useGlobal is false.
11+
{
12+
constr=repl.start({
13+
input: stream,
14+
output: stream,
15+
useGlobal: false
16+
});
1617

17-
functiontestContext(repl){
18-
constcontext=repl.createContext();
19-
// ensure that the repl context gets its own "console" instance
18+
// Ensure that the repl context gets its own "console" instance.
19+
assert(r.context.console);
20+
21+
// Ensure that the repl console instance is not the global one.
22+
assert.notStrictEqual(r.context.console,console);
23+
24+
constcontext=r.createContext();
25+
// Ensure that the repl context gets its own "console" instance.
2026
assert(context.consoleinstanceofrequire('console').Console);
2127

22-
// ensure that the repl's global property is the context
28+
// Ensure that the repl's global property is the context.
2329
assert.strictEqual(context.global,context);
2430

25-
// ensure that the repl console instance does not have a setter
26-
assert.throws(()=>context.console='foo',TypeError);
27-
repl.close();
31+
// Ensure that the repl console instance is writable.
32+
context.console='foo';
33+
r.close();
2834
}
2935

30-
testContextSideEffects(repl.start({input: stream,output: stream}));
36+
// Test for context side effects.
37+
{
38+
constserver=repl.start({input: stream,output: stream});
3139

32-
functiontestContextSideEffects(server){
3340
assert.ok(!server.underscoreAssigned);
3441
assert.strictEqual(server.lines.length,0);
3542

‎test/parallel/test-repl-function-definition-edge-case.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
// Reference: https://github.com/nodejs/node/pull/7624
22
'use strict';
3-
constcommon=require('../common');
3+
require('../common');
44
constassert=require('assert');
55
constrepl=require('repl');
66
conststream=require('stream');
77

8-
common.globalCheck=false;
9-
108
constr=initRepl();
119

1210
r.input.emit('data','function a() { return 42; } (1)\n');

‎test/parallel/test-repl-let-process.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
constcommon=require('../common');
33
constrepl=require('repl');
44

5-
common.globalCheck=false;
6-
75
// Regression test for https://github.com/nodejs/node/issues/6802
86
constinput=newcommon.ArrayStream();
97
repl.start({ input,output: process.stdout,useGlobal: true});

‎test/parallel/test-repl-mode.js‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
'use strict';
2-
constcommon=require('../common');
2+
require('../common');
33
constassert=require('assert');
44
constStream=require('stream');
55
constrepl=require('repl');
66

7-
common.globalCheck=false;
8-
97
consttests=[
108
testSloppyMode,
119
testStrictMode,

‎test/parallel/test-repl-options.js‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ const common = require('../common');
2424
constassert=require('assert');
2525
constrepl=require('repl');
2626

27-
common.globalCheck=false;
28-
2927
// Create a dummy stream that does nothing
3028
conststream=newcommon.ArrayStream();
3129

‎test/parallel/test-repl-reset-event.js‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
'use strict';
2323
constcommon=require('../common');
24-
common.globalCheck=false;
2524

2625
constassert=require('assert');
2726
constrepl=require('repl');

0 commit comments

Comments
 (0)