Browse Source

lib: fix TypeError with EventEmitter#on() abuse

Commit 2931348 added EventEmitter#getMaxListeners() but introduced a
regression when people abuse EventEmitter.prototype.on.call() to call
EventEmitter#on() on a non-EE object.  Add a workaround for that.

Fixes: https://github.com/iojs/io.js/issues/523
PR-URL: https://github.com/iojs/io.js/pull/527
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
v1.8.0-commit
Ben Noordhuis 10 years ago
parent
commit
ee9cd004d8
  1. 13
      lib/events.js
  2. 5
      test/parallel/test-event-emitter-get-max-listeners.js

13
lib/events.js

@ -46,11 +46,14 @@ EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
return this;
};
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
if (!util.isUndefined(this._maxListeners))
return this._maxListeners;
else
function $getMaxListeners(that) {
if (util.isUndefined(that._maxListeners))
return EventEmitter.defaultMaxListeners;
return that._maxListeners;
}
EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
return $getMaxListeners(this);
};
EventEmitter.prototype.emit = function emit(type) {
@ -151,7 +154,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {
// Check for listener leak
if (util.isObject(this._events[type]) && !this._events[type].warned) {
var m = this.getMaxListeners();
var m = $getMaxListeners(this);
if (m && m > 0 && this._events[type].length > m) {
this._events[type].warned = true;
console.error('(node) warning: possible EventEmitter memory ' +

5
test/parallel/test-event-emitter-get-max-listeners.js

@ -11,3 +11,8 @@ assert.strictEqual(emitter.getMaxListeners(), 0);
emitter.setMaxListeners(3);
assert.strictEqual(emitter.getMaxListeners(), 3);
// https://github.com/iojs/io.js/issues/523 - second call should not throw.
var recv = {};
EventEmitter.prototype.on.call(recv, 'event', function() {});
EventEmitter.prototype.on.call(recv, 'event', function() {});

Loading…
Cancel
Save