• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
15const {
16  FunctionPrototypeBind,
17  ObjectCreate,
18  ReflectDefineProperty,
19  ReflectGetOwnPropertyDescriptor,
20  ReflectOwnKeys,
21} = primordials;
22
23const {
24  Console,
25} = require('internal/console/constructor');
26
27const globalConsole = ObjectCreate({});
28
29// Since Console is not on the prototype chain of the global console,
30// the symbol properties on Console.prototype have to be looked up from
31// the global console itself. In addition, we need to make the global
32// console a namespace by binding the console methods directly onto
33// the global console with the receiver fixed.
34for (const prop of ReflectOwnKeys(Console.prototype)) {
35  if (prop === 'constructor') { continue; }
36  const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
37  if (typeof desc.value === 'function') { // fix the receiver
38    const name = desc.value.name;
39    desc.value = FunctionPrototypeBind(desc.value, globalConsole);
40    ReflectDefineProperty(desc.value, 'name', { __proto__: null, value: name });
41  }
42  ReflectDefineProperty(globalConsole, prop, desc);
43}
44
45// This is a legacy feature - the Console constructor is exposed on
46// the global console instance.
47globalConsole.Console = Console;
48
49module.exports = globalConsole;
50