-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <me@gus.host> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
b48865f
commit b549058
Showing
5 changed files
with
80 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters