Skip to content

Commit 3cbb5cd

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 a Map to construct the _times object. Fixes: nodejs/node-v0.x-archive#9069 PR-URL: #563 Reviewed-By: Vladimir Kurchatkin <[email protected]> Reviewed-By: Chris Dickinson <[email protected]> Reviewed-By: Rod Vagg <[email protected]>
1 parent 87e62bd commit 3cbb5cd

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

lib/console.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ function Console(stdout, stderr) {
2121
Object.defineProperty(this, '_stdout', prop);
2222
prop.value = stderr;
2323
Object.defineProperty(this, '_stderr', prop);
24-
prop.value = {};
24+
prop.value = new Map();
2525
Object.defineProperty(this, '_times', prop);
2626

2727
// bind the prototype functions to this Console instance
@@ -56,12 +56,12 @@ Console.prototype.dir = function(object, options) {
5656

5757

5858
Console.prototype.time = function(label) {
59-
this._times[label] = Date.now();
59+
this._times.set(label, Date.now());
6060
};
6161

6262

6363
Console.prototype.timeEnd = function(label) {
64-
var time = this._times[label];
64+
var time = this._times.get(label);
6565
if (!time) {
6666
throw new Error('No such label: ' + label);
6767
}

test/parallel/test-console.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,15 @@ assert.ok(process.stderr.writable);
77
assert.equal('number', typeof process.stdout.fd);
88
assert.equal('number', typeof process.stderr.fd);
99

10+
assert.throws(function () {
11+
console.timeEnd('no such label');
12+
});
13+
14+
assert.doesNotThrow(function () {
15+
console.time('label');
16+
console.timeEnd('label');
17+
});
18+
1019
// an Object with a custom .inspect() function
1120
var custom_inspect = { foo: 'bar', inspect: function () { return 'inspect'; } };
1221

@@ -33,6 +42,17 @@ console.dir({ foo : { bar : { baz : true } } }, { depth: 1 });
3342
// test console.trace()
3443
console.trace('This is a %j %d', { formatted: 'trace' }, 10, 'foo');
3544

45+
// test console.time() and console.timeEnd() output
46+
console.time('label');
47+
console.timeEnd('label');
48+
49+
// verify that Object.prototype properties can be used as labels
50+
console.time('__proto__');
51+
console.timeEnd('__proto__');
52+
console.time('constructor');
53+
console.timeEnd('constructor');
54+
console.time('hasOwnProperty');
55+
console.timeEnd('hasOwnProperty');
3656

3757
global.process.stdout.write = stdout_write;
3858

@@ -47,12 +67,8 @@ assert.notEqual(-1, strings.shift().indexOf('foo: [Object]'));
4767
assert.equal(-1, strings.shift().indexOf('baz'));
4868
assert.equal('Trace: This is a {"formatted":"trace"} 10 foo',
4969
strings.shift().split('\n').shift());
50-
51-
assert.throws(function () {
52-
console.timeEnd('no such label');
53-
});
54-
55-
assert.doesNotThrow(function () {
56-
console.time('label');
57-
console.timeEnd('label');
58-
});
70+
assert.ok(/^label: \d+ms$/.test(strings.shift().trim()));
71+
assert.ok(/^__proto__: \d+ms$/.test(strings.shift().trim()));
72+
assert.ok(/^constructor: \d+ms$/.test(strings.shift().trim()));
73+
assert.ok(/^hasOwnProperty: \d+ms$/.test(strings.shift().trim()));
74+
assert.equal(strings.length, 0);

0 commit comments

Comments
 (0)