Skip to content

Commit

Permalink
repl: make console, module and require non-enumerable
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
BridgeAR authored and MylesBorins committed May 22, 2018
1 parent 5ffce3e commit ada41b0
Show file tree
Hide file tree
Showing 8 changed files with 36 additions and 75 deletions.
15 changes: 11 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -793,7 +793,7 @@ REPLServer.prototype.createContext = function() {
const _console = new Console(this.outputStream);
Object.defineProperty(context, 'console', {
configurable: true,
enumerable: true,
writable: true,
value: _console
});

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

var require = makeRequireFunction(module);
context.module = module;
context.require = require;
Object.defineProperty(context, 'module', {
configurable: true,
writable: true,
value: module
});
Object.defineProperty(context, 'require', {
configurable: true,
writable: true,
value: makeRequireFunction(module)
});

addBuiltinLibsToObject(context);

Expand Down
44 changes: 0 additions & 44 deletions test/parallel/test-repl-console.js

This file was deleted.

39 changes: 23 additions & 16 deletions test/parallel/test-repl-context.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,32 +4,39 @@ const assert = require('assert');
const repl = require('repl');
const vm = require('vm');

// Create a dummy stream that does nothing
// Create a dummy stream that does nothing.
const stream = new common.ArrayStream();

// Test when useGlobal is false
testContext(repl.start({
input: stream,
output: stream,
useGlobal: false
}));
// Test context when useGlobal is false.
{
const r = repl.start({
input: stream,
output: stream,
useGlobal: false
});

function testContext(repl) {
const context = repl.createContext();
// ensure that the repl context gets its own "console" instance
// Ensure that the repl context gets its own "console" instance.
assert(r.context.console);

// Ensure that the repl console instance is not the global one.
assert.notStrictEqual(r.context.console, console);

const context = r.createContext();
// Ensure that the repl context gets its own "console" instance.
assert(context.console instanceof require('console').Console);

// ensure that the repl's global property is the context
// Ensure that the repl's global property is the context.
assert.strictEqual(context.global, context);

// ensure that the repl console instance does not have a setter
assert.throws(() => context.console = 'foo', TypeError);
repl.close();
// Ensure that the repl console instance is writable.
context.console = 'foo';
r.close();
}

testContextSideEffects(repl.start({ input: stream, output: stream }));
// Test for context side effects.
{
const server = repl.start({ input: stream, output: stream });

function testContextSideEffects(server) {
assert.ok(!server.underscoreAssigned);
assert.strictEqual(server.lines.length, 0);

Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-repl-function-definition-edge-case.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Reference: https://github.com/nodejs/node/pull/7624
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const repl = require('repl');
const stream = require('stream');

common.globalCheck = false;

const r = initRepl();

r.input.emit('data', 'function a() { return 42; } (1)\n');
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-repl-let-process.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@
const common = require('../common');
const repl = require('repl');

common.globalCheck = false;

// Regression test for https://github.com/nodejs/node/issues/6802
const input = new common.ArrayStream();
repl.start({ input, output: process.stdout, useGlobal: true });
Expand Down
4 changes: 1 addition & 3 deletions test/parallel/test-repl-mode.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
'use strict';
const common = require('../common');
require('../common');
const assert = require('assert');
const Stream = require('stream');
const repl = require('repl');

common.globalCheck = false;

const tests = [
testSloppyMode,
testStrictMode,
Expand Down
2 changes: 0 additions & 2 deletions test/parallel/test-repl-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@ const common = require('../common');
const assert = require('assert');
const repl = require('repl');

common.globalCheck = false;

// Create a dummy stream that does nothing
const stream = new common.ArrayStream();

Expand Down
1 change: 0 additions & 1 deletion test/parallel/test-repl-reset-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@

'use strict';
const common = require('../common');
common.globalCheck = false;

const assert = require('assert');
const repl = require('repl');
Expand Down

0 comments on commit ada41b0

Please sign in to comment.