diff --git a/lib/console.js b/lib/console.js new file mode 100644 index 00000000000..85a89ecc990 --- /dev/null +++ b/lib/console.js @@ -0,0 +1,24 @@ +// 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'; + +module.exports = require('internal/console/global'); diff --git a/lib/internal/console/constructor.js b/lib/internal/console/constructor.js index f0b9a74cfc7..9588cd5ebe2 100644 --- a/lib/internal/console/constructor.js +++ b/lib/internal/console/constructor.js @@ -1,26 +1,8 @@ -// 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'; +// The Console constructor is not actually used to construct the global +// console. It's exported for backwards compatibility. + const { trace } = internalBinding('trace_events'); const { isStackOverflowError, @@ -72,8 +54,6 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy'); const kUseStdout = Symbol('kUseStdout'); const kUseStderr = Symbol('kUseStderr'); -// This constructor is not used to construct the global console. -// It's exported for backwards compatibility. function Console(options /* or: stdout, stderr, ignoreErrors = true */) { // We have to test new.target here to see if this function is called // with new, because we need to define a custom instanceof to accommodate @@ -533,42 +513,8 @@ Console.prototype.table = function(tabularData, properties) { function noop() {} -// See https://console.spec.whatwg.org/#console-namespace -// > For historical web-compatibility reasons, the namespace object -// > for console must have as its [[Prototype]] an empty object, -// > created as if by ObjectCreate(%ObjectPrototype%), -// > instead of %ObjectPrototype%. - -// Since in Node.js, the Console constructor has been exposed through -// require('console'), we need to keep the Console constructor but -// we cannot actually use `new Console` to construct the global console. -// Therefore, the console.Console.prototype is not -// in the global console prototype chain anymore. - -// TODO(joyeecheung): -// - Move the Console constructor into internal/console.js -// - Move the global console creation code along with the inspector console -// wrapping code in internal/bootstrap/node.js into a separate file. -// - Make this file a simple re-export of those two files. -// This is only here for v11.x conflict resolution. -const globalConsole = Object.create(Console.prototype); - -// Since Console is not on the prototype chain of the global console, -// the symbol properties on Console.prototype have to be looked up from -// the global console itself. In addition, we need to make the global -// console a namespace by binding the console methods directly onto -// the global console with the receiver fixed. -for (const prop of Reflect.ownKeys(Console.prototype)) { - if (prop === 'constructor') { continue; } - const desc = Reflect.getOwnPropertyDescriptor(Console.prototype, prop); - if (typeof desc.value === 'function') { // fix the receiver - desc.value = desc.value.bind(globalConsole); - } - Reflect.defineProperty(globalConsole, prop, desc); -} - -globalConsole[kBindStreamsLazy](process); -globalConsole[kBindProperties](true, 'auto'); - -module.exports = globalConsole; -module.exports.Console = Console; +module.exports = { + Console, + kBindStreamsLazy, + kBindProperties +}; diff --git a/lib/internal/console/global.js b/lib/internal/console/global.js new file mode 100644 index 00000000000..0b480b165bd --- /dev/null +++ b/lib/internal/console/global.js @@ -0,0 +1,45 @@ +'use strict'; + +// See https://console.spec.whatwg.org/#console-namespace +// > For historical web-compatibility reasons, the namespace object +// > for console must have as its [[Prototype]] an empty object, +// > created as if by ObjectCreate(%ObjectPrototype%), +// > instead of %ObjectPrototype%. + +// Since in Node.js, the Console constructor has been exposed through +// require('console'), we need to keep the Console constructor but +// we cannot actually use `new Console` to construct the global console. +// Therefore, the console.Console.prototype is not +// in the global console prototype chain anymore. + +const { + Console, + kBindStreamsLazy, + kBindProperties +} = require('internal/console/constructor'); + +// This is only here for v11.x conflict resolution. +const globalConsole = Object.create(Console.prototype); + +// Since Console is not on the prototype chain of the global console, +// the symbol properties on Console.prototype have to be looked up from +// the global console itself. In addition, we need to make the global +// console a namespace by binding the console methods directly onto +// the global console with the receiver fixed. +for (const prop of Reflect.ownKeys(Console.prototype)) { + if (prop === 'constructor') { continue; } + const desc = Reflect.getOwnPropertyDescriptor(Console.prototype, prop); + if (typeof desc.value === 'function') { // fix the receiver + desc.value = desc.value.bind(globalConsole); + } + Reflect.defineProperty(globalConsole, prop, desc); +} + +globalConsole[kBindStreamsLazy](process); +globalConsole[kBindProperties](true, 'auto'); + +// This is a legacy feature - the Console constructor is exposed on +// the global console instance. +globalConsole.Console = Console; + +module.exports = globalConsole; diff --git a/node.gyp b/node.gyp index 450fad26c50..4a1a003dac3 100644 --- a/node.gyp +++ b/node.gyp @@ -96,6 +96,8 @@ 'lib/internal/cluster/shared_handle.js', 'lib/internal/cluster/utils.js', 'lib/internal/cluster/worker.js', + 'lib/internal/console/constructor.js', + 'lib/internal/console/global.js', 'lib/internal/crypto/certificate.js', 'lib/internal/crypto/cipher.js', 'lib/internal/crypto/diffiehellman.js', diff --git a/test/parallel/test-bootstrap-modules.js b/test/parallel/test-bootstrap-modules.js index ebc31ba9c28..eb5afea961b 100644 --- a/test/parallel/test-bootstrap-modules.js +++ b/test/parallel/test-bootstrap-modules.js @@ -9,7 +9,7 @@ const common = require('../common'); const assert = require('assert'); const isMainThread = common.isMainThread; -const kMaxModuleCount = isMainThread ? 57 : 78; +const kMaxModuleCount = isMainThread ? 59 : 80; assert(list.length <= kMaxModuleCount, `Total length: ${list.length}\n` + list.join('\n')