Skip to content

Commit

Permalink
console: take care of console set undefined case
Browse files Browse the repository at this point in the history
If global.console write undefined would come earlier than global.console read
then lazy read in get would happen, despite write was done.
This commit fix the problem.

Fixes: nodejs#11805
  • Loading branch information
Wandalen authored and Trott committed Aug 15, 2017
1 parent 6e86bca commit 79dc629
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
9 changes: 8 additions & 1 deletion lib/internal/bootstrap_node.js
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@
configurable: true,
enumerable: false,
get: function() {
NativeModule.require('console').log( 'console get' );
if (!console) {
console = (originalConsole === undefined) ?
NativeModule.require('console') :
Expand All @@ -286,7 +287,13 @@
return console;
},
set: function(customConsole) {
console = customConsole;
NativeModule.require('console').log( 'console set' );
Object.defineProperty(global, 'console', {
configurable: true,
writable: true,
enumerable: false,
value: console
});
}
});
setupInspectorCommandLineAPI();
Expand Down
21 changes: 21 additions & 0 deletions test/parallel/test-console-assing-undefined.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict';

// Should be above require, because code in require read console
// what we are trying to avoid
// set should be earlier than get

global.console = undefined;

// Initially, the `console` variable is `undefined`, since console will be
// lazily loaded in the getter.

const common = require('../common');
const assert = require('assert');

// global.console's getter is called
// Since the `console` cache variable is `undefined` and therefore false-y,
// the getter still calls NativeModule.require() and returns the object
// obtained from it, instead of returning `undefined` as expected.

assert.strictEqual(global.console, undefined, 'first read');
assert.strictEqual(global.console, undefined, 'second read');

0 comments on commit 79dc629

Please sign in to comment.