2018-11-29 06:17:57 +08:00
|
|
|
'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.
|
|
|
|
|
2019-11-22 18:04:46 +01:00
|
|
|
const {
|
2020-10-21 11:34:41 +02:00
|
|
|
FunctionPrototypeBind,
|
2019-11-22 18:04:46 +01:00
|
|
|
ReflectDefineProperty,
|
|
|
|
ReflectGetOwnPropertyDescriptor,
|
|
|
|
ReflectOwnKeys,
|
|
|
|
} = primordials;
|
2019-03-31 13:30:12 +02:00
|
|
|
|
2018-11-29 06:17:57 +08:00
|
|
|
const {
|
2023-02-28 12:14:11 +01:00
|
|
|
Console,
|
2024-02-20 19:25:31 +01:00
|
|
|
kBindProperties,
|
2018-11-29 06:17:57 +08:00
|
|
|
} = require('internal/console/constructor');
|
|
|
|
|
2023-01-09 21:38:36 -08:00
|
|
|
const globalConsole = { __proto__: {} };
|
2018-11-29 06:17:57 +08:00
|
|
|
|
|
|
|
// 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.
|
2019-11-22 18:04:46 +01:00
|
|
|
for (const prop of ReflectOwnKeys(Console.prototype)) {
|
2018-11-29 06:17:57 +08:00
|
|
|
if (prop === 'constructor') { continue; }
|
2019-11-22 18:04:46 +01:00
|
|
|
const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
|
2018-11-29 06:17:57 +08:00
|
|
|
if (typeof desc.value === 'function') { // fix the receiver
|
2020-05-22 14:02:04 +02:00
|
|
|
const name = desc.value.name;
|
2020-10-21 11:34:41 +02:00
|
|
|
desc.value = FunctionPrototypeBind(desc.value, globalConsole);
|
2022-06-03 10:23:58 +02:00
|
|
|
ReflectDefineProperty(desc.value, 'name', { __proto__: null, value: name });
|
2018-11-29 06:17:57 +08:00
|
|
|
}
|
2019-11-22 18:04:46 +01:00
|
|
|
ReflectDefineProperty(globalConsole, prop, desc);
|
2018-11-29 06:17:57 +08:00
|
|
|
}
|
|
|
|
|
2024-02-20 19:25:31 +01:00
|
|
|
globalConsole[kBindProperties](true, 'auto');
|
|
|
|
|
2018-11-29 06:17:57 +08:00
|
|
|
// This is a legacy feature - the Console constructor is exposed on
|
|
|
|
// the global console instance.
|
|
|
|
globalConsole.Console = Console;
|
|
|
|
|
|
|
|
module.exports = globalConsole;
|