Browse Source

Fix number of args emitted by EventEmitter during "fast case" (lte 3 args)

v0.7.4-release
Jeremy Martin 14 years ago
committed by Ryan Dahl
parent
commit
144b2a5338
  1. 15
      lib/events.js
  2. 27
      test/simple/test-event-emitter-num-args.js

15
lib/events.js

@ -22,11 +22,19 @@ EventEmitter.prototype.emit = function (type) {
if (!handler) return false;
if (typeof handler == 'function') {
if (arguments.length <= 3) {
// fast case
switch (arguments.length) {
// fast cases
case 1:
handler.call(this);
break;
case 2:
handler.call(this, arguments[1]);
break;
case 3:
handler.call(this, arguments[1], arguments[2]);
} else {
break;
// slower
default:
var args = Array.prototype.slice.call(arguments, 1);
handler.apply(this, args);
}
@ -35,7 +43,6 @@ EventEmitter.prototype.emit = function (type) {
} else if (isArray(handler)) {
var args = Array.prototype.slice.call(arguments, 1);
var listeners = handler.slice();
for (var i = 0, l = listeners.length; i < l; i++) {
listeners[i].apply(this, args);

27
test/simple/test-event-emitter-num-args.js

@ -0,0 +1,27 @@
common = require("../common");
assert = common.assert
var events = require('events');
var e = new events.EventEmitter(),
num_args_emited = [];
e.on("numArgs", function() {
var numArgs = arguments.length;
console.log("numArgs: " + numArgs);
num_args_emited.push(numArgs);
});
console.log("start");
e.emit("numArgs");
e.emit("numArgs", null);
e.emit("numArgs", null, null);
e.emit("numArgs", null, null, null);
e.emit("numArgs", null, null, null, null);
e.emit("numArgs", null, null, null, null, null);
process.addListener("exit", function () {
assert.deepEqual([0, 1, 2, 3, 4, 5], num_args_emited);
});
Loading…
Cancel
Save