Skip to content

Commit 6c3647c

Browse files
committed
console: allow Object.prototype fields as labels
Console.prototype.timeEnd() returns NaN if the timer label corresponds to a property on Object.prototype. This commit uses Object.create(null) to construct the _times object. Fixes: nodejs/node-v0.x-archive#9069 PR-URL: nodejs/node-v0.x-archive#9116 Reviewed-By: Trevor Norris <[email protected]>
1 parent b8604fa commit 6c3647c

File tree

2 files changed

+26
-10
lines changed

2 files changed

+26
-10
lines changed

lib/console.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ function Console(stdout, stderr) {
4040
Object.defineProperty(this, '_stdout', prop);
4141
prop.value = stderr;
4242
Object.defineProperty(this, '_stderr', prop);
43-
prop.value = {};
43+
prop.value = Object.create(null);
4444
Object.defineProperty(this, '_times', prop);
4545

4646
// bind the prototype functions to this Console instance

test/simple/test-console.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ assert.ok(process.stderr.writable);
3131
assert.equal('number', typeof process.stdout.fd);
3232
assert.equal('number', typeof process.stderr.fd);
3333

34+
assert.throws(function () {
35+
console.timeEnd('no such label');
36+
});
37+
38+
assert.doesNotThrow(function () {
39+
console.time('label');
40+
console.timeEnd('label');
41+
});
42+
3443
// an Object with a custom .inspect() function
3544
var custom_inspect = { foo: 'bar', inspect: function () { return 'inspect'; } };
3645

@@ -57,6 +66,17 @@ console.dir({ foo : { bar : { baz : true } } }, { depth: 1 });
5766
// test console.trace()
5867
console.trace('This is a %j %d', { formatted: 'trace' }, 10, 'foo');
5968

69+
// test console.time() and console.timeEnd() output
70+
console.time('label');
71+
console.timeEnd('label');
72+
73+
// verify that Object.prototype properties can be used as labels
74+
console.time('__proto__');
75+
console.timeEnd('__proto__');
76+
console.time('constructor');
77+
console.timeEnd('constructor');
78+
console.time('hasOwnProperty');
79+
console.timeEnd('hasOwnProperty');
6080

6181
global.process.stdout.write = stdout_write;
6282

@@ -71,12 +91,8 @@ assert.notEqual(-1, strings.shift().indexOf('foo: [Object]'));
7191
assert.equal(-1, strings.shift().indexOf('baz'));
7292
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
7393
strings.shift().split('\n').shift());
74-
75-
assert.throws(function () {
76-
console.timeEnd('no such label');
77-
});
78-
79-
assert.doesNotThrow(function () {
80-
console.time('label');
81-
console.timeEnd('label');
82-
});
94+
assert.ok(/^label: \d+ms$/.test(strings.shift().trim()));
95+
assert.ok(/^__proto__: \d+ms$/.test(strings.shift().trim()));
96+
assert.ok(/^constructor: \d+ms$/.test(strings.shift().trim()));
97+
assert.ok(/^hasOwnProperty: \d+ms$/.test(strings.shift().trim()));
98+
assert.equal(strings.length, 0);

0 commit comments

Comments
 (0)