Browse Source

events: Handle emit('error') before ctor

The previous commit did not handle the case when the event type
is 'error', since that is checked before reading the handler.
v0.9.12-release
isaacs 12 years ago
parent
commit
63edde0e01
  1. 6
      lib/events.js
  2. 10
      test/simple/test-event-emitter-subclass.js

6
lib/events.js

@ -53,6 +53,9 @@ EventEmitter.prototype.setMaxListeners = function(n) {
EventEmitter.prototype.emit = function(type) { EventEmitter.prototype.emit = function(type) {
var er, handler, len, args, i, listeners; var er, handler, len, args, i, listeners;
if (!this._events)
this._events = {};
// If there is no 'error' event listener then throw. // If there is no 'error' event listener then throw.
if (type === 'error') { if (type === 'error') {
if (!this._events.error || if (!this._events.error ||
@ -73,9 +76,6 @@ EventEmitter.prototype.emit = function(type) {
} }
} }
if (!this._events)
this._events = {};
handler = this._events[type]; handler = this._events[type];
if (typeof handler === 'undefined') if (typeof handler === 'undefined')

10
test/simple/test-event-emitter-subclass.js

@ -38,6 +38,16 @@ var myee = new MyEE(function() {
called = true; called = true;
}); });
util.inherits(ErrorEE, EventEmitter);
function ErrorEE() {
this.emit('error', new Error('blerg'));
}
assert.throws(function() {
new ErrorEE();
}, /blerg/);
process.on('exit', function() { process.on('exit', function() {
assert(called); assert(called);
console.log('ok'); console.log('ok');

Loading…
Cancel
Save