mirror of
https://github.com/nodejs/node.git
synced 2026-11-25 23:01:06 -05:00
repl: make console, module and require non-enumerable
This aligns these globals with the regular context. PR-URL: https://github.com/nodejs/node/pull/20717 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
This commit is contained in:
1 parent
5ffce3ef06
commit
ada41b02c5
8 files changed
+36
-75
No files matched your search
+11
-4
@@ -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
|
||||
});
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// Copyright Joyent, Inc. and other Node contributors.
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a
|
||||
// copy of this software and associated documentation files (the
|
||||
// "Software"), to deal in the Software without restriction, including
|
||||
// without limitation the rights to use, copy, modify, merge, publish,
|
||||
// distribute, sublicense, and/or sell copies of the Software, and to permit
|
||||
// persons to whom the Software is furnished to do so, subject to the
|
||||
// following conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be included
|
||||
// in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
|
||||
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
||||
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
|
||||
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
||||
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
||||
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
|
||||
// USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
const assert = require('assert');
|
||||
const repl = require('repl');
|
||||
|
||||
// Create a dummy stream that does nothing
|
||||
const stream = new common.ArrayStream();
|
||||
|
||||
const r = repl.start({
|
||||
input: stream,
|
||||
output: stream,
|
||||
useGlobal: false
|
||||
});
|
||||
|
||||
|
||||
// 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);
|
||||
|
||||
// ensure that the repl console instance does not have a setter
|
||||
assert.throws(() => r.context.console = 'foo', TypeError);
|
||||
@@ -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);
|
||||
|
||||
|
||||
@@ -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');
|
||||
|
||||
@@ -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 });
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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();
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@
|
||||
|
||||
'use strict';
|
||||
const common = require('../common');
|
||||
common.globalCheck = false;
|
||||
|
||||
const assert = require('assert');
|
||||
const repl = require('repl');
|
||||
|
||||
Reference in new issue
Block a user