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