11cb0ef41Sopenharmony_ci'use strict';
21cb0ef41Sopenharmony_ci
31cb0ef41Sopenharmony_ci// See https://console.spec.whatwg.org/#console-namespace
41cb0ef41Sopenharmony_ci// > For historical web-compatibility reasons, the namespace object
51cb0ef41Sopenharmony_ci// > for console must have as its [[Prototype]] an empty object,
61cb0ef41Sopenharmony_ci// > created as if by ObjectCreate(%ObjectPrototype%),
71cb0ef41Sopenharmony_ci// > instead of %ObjectPrototype%.
81cb0ef41Sopenharmony_ci
91cb0ef41Sopenharmony_ci// Since in Node.js, the Console constructor has been exposed through
101cb0ef41Sopenharmony_ci// require('console'), we need to keep the Console constructor but
111cb0ef41Sopenharmony_ci// we cannot actually use `new Console` to construct the global console.
121cb0ef41Sopenharmony_ci// Therefore, the console.Console.prototype is not
131cb0ef41Sopenharmony_ci// in the global console prototype chain anymore.
141cb0ef41Sopenharmony_ci
151cb0ef41Sopenharmony_ciconst {
161cb0ef41Sopenharmony_ci  FunctionPrototypeBind,
171cb0ef41Sopenharmony_ci  ObjectCreate,
181cb0ef41Sopenharmony_ci  ReflectDefineProperty,
191cb0ef41Sopenharmony_ci  ReflectGetOwnPropertyDescriptor,
201cb0ef41Sopenharmony_ci  ReflectOwnKeys,
211cb0ef41Sopenharmony_ci} = primordials;
221cb0ef41Sopenharmony_ci
231cb0ef41Sopenharmony_ciconst {
241cb0ef41Sopenharmony_ci  Console,
251cb0ef41Sopenharmony_ci} = require('internal/console/constructor');
261cb0ef41Sopenharmony_ci
271cb0ef41Sopenharmony_ciconst globalConsole = ObjectCreate({});
281cb0ef41Sopenharmony_ci
291cb0ef41Sopenharmony_ci// Since Console is not on the prototype chain of the global console,
301cb0ef41Sopenharmony_ci// the symbol properties on Console.prototype have to be looked up from
311cb0ef41Sopenharmony_ci// the global console itself. In addition, we need to make the global
321cb0ef41Sopenharmony_ci// console a namespace by binding the console methods directly onto
331cb0ef41Sopenharmony_ci// the global console with the receiver fixed.
341cb0ef41Sopenharmony_cifor (const prop of ReflectOwnKeys(Console.prototype)) {
351cb0ef41Sopenharmony_ci  if (prop === 'constructor') { continue; }
361cb0ef41Sopenharmony_ci  const desc = ReflectGetOwnPropertyDescriptor(Console.prototype, prop);
371cb0ef41Sopenharmony_ci  if (typeof desc.value === 'function') { // fix the receiver
381cb0ef41Sopenharmony_ci    const name = desc.value.name;
391cb0ef41Sopenharmony_ci    desc.value = FunctionPrototypeBind(desc.value, globalConsole);
401cb0ef41Sopenharmony_ci    ReflectDefineProperty(desc.value, 'name', { __proto__: null, value: name });
411cb0ef41Sopenharmony_ci  }
421cb0ef41Sopenharmony_ci  ReflectDefineProperty(globalConsole, prop, desc);
431cb0ef41Sopenharmony_ci}
441cb0ef41Sopenharmony_ci
451cb0ef41Sopenharmony_ci// This is a legacy feature - the Console constructor is exposed on
461cb0ef41Sopenharmony_ci// the global console instance.
471cb0ef41Sopenharmony_ciglobalConsole.Console = Console;
481cb0ef41Sopenharmony_ci
491cb0ef41Sopenharmony_cimodule.exports = globalConsole;
50