Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[v11.x] backport console related changes #25420

Closed

Commits on Jan 9, 2019

  1. lib: make the global console [[Prototype]] an empty object

    From the WHATWG console spec:
    
    > 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.
    
    This patch changes the prototype chain of the global console object,
    so the console.Console.prototype is not in the global console prototype
    chain anymore.
    
    ```
    const proto = Object.getPrototypeOf(global.console);
    // Before this patch
    proto.constructor === global.console.Console
    // After this patch
    proto.constructor === Object
    ```
    
    But, we still maintain that
    
    ```
    global.console instanceof global.console.Console
    ```
    
    through a custom Symbol.hasInstance function of Console that tests
    for a special symbol kIsConsole for backwards compatibility.
    
    This fixes a case in the console Web Platform Test that we commented
    out.
    
    PR-URL: nodejs#23509
    Refs: whatwg/console#3
    Refs: https://console.spec.whatwg.org/#console-namespace
    Reviewed-By: Gus Caplan <me@gus.host>
    Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
    Reviewed-By: Tiancheng "Timothy" Gu <timothygu99@gmail.com>
    Reviewed-By: Denys Otrishko <shishugi@gmail.com>
    Reviewed-By: James M Snell <jasnell@gmail.com>
    Reviewed-By: Rich Trott <rtrott@gmail.com>
    Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
    Reviewed-By: Sakthipriyan Vairamani <thechargingvolcano@gmail.com>
    Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
    joyeecheung committed Jan 9, 2019
    Configuration menu
    Copy the full SHA
    4fc2fbe View commit details
    Browse the repository at this point in the history
  2. console: create the global console from Console constructor

    Specifically for v11.x.
    
    Refs: nodejs#23509
    joyeecheung committed Jan 9, 2019
    Configuration menu
    Copy the full SHA
    c67021a View commit details
    Browse the repository at this point in the history
  3. console: bind methods from the prototype chain in Console

    In 6223236 we made the console.Console function construct an object
    with methods from Console.prototype bound to the instance, instead of
    with methods found on the prototype chain to be bound on the instances,
    thus breaking users overriding these methods when subclassing Console
    because they are not expecting this function to construct a
    namespace whose methods are not looked up from the prototype
    chain.
    
    This patch restores the previous behavior since it does not affect
    the characteristics of the global console anyway. So after this patch,
    the Console function still constructs normal objects with function
    properties looked up from the prototype chain but bound to the
    instances always.
    
    PR-URL: nodejs#24047
    Reviewed-By: John-David Dalton <john.david.dalton@gmail.com>
    Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
    Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
    joyeecheung committed Jan 9, 2019
    Configuration menu
    Copy the full SHA
    132f860 View commit details
    Browse the repository at this point in the history
  4. console: lazy load process.stderr and process.stdout

    This patch:
    
    - Refactors the Console constructor: moves the property binding code
      into and the writable streams binding code into two methods defined
      on the Console.prototype with symbols.
    - Refactors the global console creation: we only need to share the
      property binding code from the Console constructor. To bind the
      streams we can lazy load `process.stdio` and `process.stderr`
      so that we don't create these streams when they are not used.
      This significantly reduces the number of modules loaded during
      bootstrap. Also, by calling the refactored-out method directly
      we can skip the unnecessary typechecks when creating the global
      console and there is no need to create a temporary Console
      anymore.
    - Refactors the error handler creation and the `write` method:
      use a `kUseStdout` symbol to tell the internals which stream
      should be loaded from the console instance. Also put the
      `write` method on the Console prototype so it just loads
      other properties directly off the console instance which simplifies
      the call sites.
    
    Also leaves a few TODOs for further refactoring of the console
    bootstrap.
    
    PR-URL: nodejs#24534
    Reviewed-By: Gus Caplan <me@gus.host>
    joyeecheung committed Jan 9, 2019
    Configuration menu
    Copy the full SHA
    de29e85 View commit details
    Browse the repository at this point in the history