Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
repl: make console, module and require non-enumerable
This aligns these globals with the regular context.
  • Loading branch information
BridgeAR committed May 14, 2018
commit e0ffaf6a6052d6023bb680ec13ab1d1eed181882
15 changes: 11 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,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 @@ -803,9 +803,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