Skip to content

Commit b549058

Browse files
joyeecheungaddaleax
authored andcommitted
console: split console into global.js and constructor.js
Since we do not actually use the Console constructor to instantiate the global console, move the two piece of code into two different JS files for clarity, and make console.js a mere re-export of the global console. The hope is to make the global console, a namespace, more web-compatible while keeping the Console constructor available for backwards compatibility. PR-URL: #24709 Reviewed-By: Gus Caplan <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent b48865f commit b549058

File tree

5 files changed

+80
-63
lines changed

5 files changed

+80
-63
lines changed

lib/console.js

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright Joyent, Inc. and other Node contributors.
2+
//
3+
// Permission is hereby granted, free of charge, to any person obtaining a
4+
// copy of this software and associated documentation files (the
5+
// "Software"), to deal in the Software without restriction, including
6+
// without limitation the rights to use, copy, modify, merge, publish,
7+
// distribute, sublicense, and/or sell copies of the Software, and to permit
8+
// persons to whom the Software is furnished to do so, subject to the
9+
// following conditions:
10+
//
11+
// The above copyright notice and this permission notice shall be included
12+
// in all copies or substantial portions of the Software.
13+
//
14+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15+
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16+
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17+
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18+
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20+
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21+
22+
'use strict';
23+
24+
module.exports = require('internal/console/global');

lib/internal/console/constructor.js

+8-62
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,8 @@
1-
// Copyright Joyent, Inc. and other Node contributors.
2-
//
3-
// Permission is hereby granted, free of charge, to any person obtaining a
4-
// copy of this software and associated documentation files (the
5-
// "Software"), to deal in the Software without restriction, including
6-
// without limitation the rights to use, copy, modify, merge, publish,
7-
// distribute, sublicense, and/or sell copies of the Software, and to permit
8-
// persons to whom the Software is furnished to do so, subject to the
9-
// following conditions:
10-
//
11-
// The above copyright notice and this permission notice shall be included
12-
// in all copies or substantial portions of the Software.
13-
//
14-
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15-
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16-
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
17-
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18-
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
19-
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20-
// USE OR OTHER DEALINGS IN THE SOFTWARE.
21-
221
'use strict';
232

3+
// The Console constructor is not actually used to construct the global
4+
// console. It's exported for backwards compatibility.
5+
246
const { trace } = internalBinding('trace_events');
257
const {
268
isStackOverflowError,
@@ -72,8 +54,6 @@ const kBindStreamsLazy = Symbol('kBindStreamsLazy');
7254
const kUseStdout = Symbol('kUseStdout');
7355
const kUseStderr = Symbol('kUseStderr');
7456

75-
// This constructor is not used to construct the global console.
76-
// It's exported for backwards compatibility.
7757
function Console(options /* or: stdout, stderr, ignoreErrors = true */) {
7858
// We have to test new.target here to see if this function is called
7959
// with new, because we need to define a custom instanceof to accommodate
@@ -533,42 +513,8 @@ Console.prototype.table = function(tabularData, properties) {
533513

534514
function noop() {}
535515

536-
// See https://console.spec.whatwg.org/#console-namespace
537-
// > For historical web-compatibility reasons, the namespace object
538-
// > for console must have as its [[Prototype]] an empty object,
539-
// > created as if by ObjectCreate(%ObjectPrototype%),
540-
// > instead of %ObjectPrototype%.
541-
542-
// Since in Node.js, the Console constructor has been exposed through
543-
// require('console'), we need to keep the Console constructor but
544-
// we cannot actually use `new Console` to construct the global console.
545-
// Therefore, the console.Console.prototype is not
546-
// in the global console prototype chain anymore.
547-
548-
// TODO(joyeecheung):
549-
// - Move the Console constructor into internal/console.js
550-
// - Move the global console creation code along with the inspector console
551-
// wrapping code in internal/bootstrap/node.js into a separate file.
552-
// - Make this file a simple re-export of those two files.
553-
// This is only here for v11.x conflict resolution.
554-
const globalConsole = Object.create(Console.prototype);
555-
556-
// Since Console is not on the prototype chain of the global console,
557-
// the symbol properties on Console.prototype have to be looked up from
558-
// the global console itself. In addition, we need to make the global
559-
// console a namespace by binding the console methods directly onto
560-
// the global console with the receiver fixed.
561-
for (const prop of Reflect.ownKeys(Console.prototype)) {
562-
if (prop === 'constructor') { continue; }
563-
const desc = Reflect.getOwnPropertyDescriptor(Console.prototype, prop);
564-
if (typeof desc.value === 'function') { // fix the receiver
565-
desc.value = desc.value.bind(globalConsole);
566-
}
567-
Reflect.defineProperty(globalConsole, prop, desc);
568-
}
569-
570-
globalConsole[kBindStreamsLazy](process);
571-
globalConsole[kBindProperties](true, 'auto');
572-
573-
module.exports = globalConsole;
574-
module.exports.Console = Console;
516+
module.exports = {
517+
Console,
518+
kBindStreamsLazy,
519+
kBindProperties
520+
};

lib/internal/console/global.js

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
'use strict';
2+
3+
// See https://console.spec.whatwg.org/#console-namespace
4+
// > For historical web-compatibility reasons, the namespace object
5+
// > for console must have as its [[Prototype]] an empty object,
6+
// > created as if by ObjectCreate(%ObjectPrototype%),
7+
// > instead of %ObjectPrototype%.
8+
9+
// Since in Node.js, the Console constructor has been exposed through
10+
// require('console'), we need to keep the Console constructor but
11+
// we cannot actually use `new Console` to construct the global console.
12+
// Therefore, the console.Console.prototype is not
13+
// in the global console prototype chain anymore.
14+
15+
const {
16+
Console,
17+
kBindStreamsLazy,
18+
kBindProperties
19+
} = require('internal/console/constructor');
20+
21+
// This is only here for v11.x conflict resolution.
22+
const globalConsole = Object.create(Console.prototype);
23+
24+
// Since Console is not on the prototype chain of the global console,
25+
// the symbol properties on Console.prototype have to be looked up from
26+
// the global console itself. In addition, we need to make the global
27+
// console a namespace by binding the console methods directly onto
28+
// the global console with the receiver fixed.
29+
for (const prop of Reflect.ownKeys(Console.prototype)) {
30+
if (prop === 'constructor') { continue; }
31+
const desc = Reflect.getOwnPropertyDescriptor(Console.prototype, prop);
32+
if (typeof desc.value === 'function') { // fix the receiver
33+
desc.value = desc.value.bind(globalConsole);
34+
}
35+
Reflect.defineProperty(globalConsole, prop, desc);
36+
}
37+
38+
globalConsole[kBindStreamsLazy](process);
39+
globalConsole[kBindProperties](true, 'auto');
40+
41+
// This is a legacy feature - the Console constructor is exposed on
42+
// the global console instance.
43+
globalConsole.Console = Console;
44+
45+
module.exports = globalConsole;

node.gyp

+2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@
9696
'lib/internal/cluster/shared_handle.js',
9797
'lib/internal/cluster/utils.js',
9898
'lib/internal/cluster/worker.js',
99+
'lib/internal/console/constructor.js',
100+
'lib/internal/console/global.js',
99101
'lib/internal/crypto/certificate.js',
100102
'lib/internal/crypto/cipher.js',
101103
'lib/internal/crypto/diffiehellman.js',

test/parallel/test-bootstrap-modules.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const common = require('../common');
99
const assert = require('assert');
1010

1111
const isMainThread = common.isMainThread;
12-
const kMaxModuleCount = isMainThread ? 57 : 78;
12+
const kMaxModuleCount = isMainThread ? 59 : 80;
1313

1414
assert(list.length <= kMaxModuleCount,
1515
`Total length: ${list.length}\n` + list.join('\n')

0 commit comments

Comments
 (0)