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