Before this commit the EventEmitter methods were anonymous functions.
V8 tries to infer names for anonymous functions based on the execution
context but it frequently gets it wrong and when that happens, the
stack trace is usually confusing and unhelpful. This commit names all
methods so V8 can fall back to the method.name property.
The above gotcha applies to all anonymous functions but is exacerbated
for EventEmitter methods because those are invoked with a plenitude of
different receivers.
Signed-off-by: Trevor Norris <trev.norris@gmail.com>
// Obviously not all Emitters should be limited to 10. This function allows
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
-EventEmitter.prototype.setMaxListeners = function(n) {
+EventEmitter.prototype.setMaxListeners = function setMaxListeners(n) {
if (typeof n !== 'number' || n < 0 || isNaN(n))
throw TypeError('n must be a positive number');
this._maxListeners = n;
};
-EventEmitter.prototype.emit = function(type) {
+EventEmitter.prototype.emit = function emit(type) {
var er, handler, len, args, i, listeners;
if (!this._events)
return true;
};
-EventEmitter.prototype.addListener = function(type, listener) {
+EventEmitter.prototype.addListener = function addListener(type, listener) {
var m;
if (typeof listener !== 'function')
EventEmitter.prototype.on = EventEmitter.prototype.addListener;
-EventEmitter.prototype.once = function(type, listener) {
+EventEmitter.prototype.once = function once(type, listener) {
if (typeof listener !== 'function')
throw TypeError('listener must be a function');
};
// emits a 'removeListener' event iff the listener was removed
-EventEmitter.prototype.removeListener = function(type, listener) {
+EventEmitter.prototype.removeListener =
+ function removeListener(type, listener) {
var list, position, length, i;
if (typeof listener !== 'function')
return this;
};
-EventEmitter.prototype.removeAllListeners = function(type) {
+EventEmitter.prototype.removeAllListeners =
+ function removeAllListeners(type) {
var key, listeners;
if (!this._events)
return this;
};
-EventEmitter.prototype.listeners = function(type) {
+EventEmitter.prototype.listeners = function listeners(type) {
var ret;
if (!this._events || !this._events[type])
ret = [];
at Module._compile (module.js:*:*)
at evalScript (node.js:*:*)
at Socket.<anonymous> (node.js:*:*)
- at Socket.EventEmitter.emit (events.js:*:*)
+ at Socket.emit (events.js:*:*)
at _stream_readable.js:*:*
at process._tickCallback (node.js:*:*)
42
at Module._compile (module.js:*:*)
at evalScript (node.js:*:*)
at Socket.<anonymous> (node.js:*:*)
- at Socket.EventEmitter.emit (events.js:*:*)
+ at Socket.emit (events.js:*:*)
at _stream_readable.js:*:*
at process._tickCallback (node.js:*:*)
at Module._compile (module.js:*:*)
at evalScript (node.js:*:*)
at Socket.<anonymous> (node.js:*:*)
- at Socket.EventEmitter.emit (events.js:*:*)
+ at Socket.emit (events.js:*:*)
at _stream_readable.js:*:*
at process._tickCallback (node.js:*:*)
100
at Module._compile (module.js:*:*)
at evalScript (node.js:*:*)
at Socket.<anonymous> (node.js:*:*)
- at Socket.EventEmitter.emit (events.js:*:*)
+ at Socket.emit (events.js:*:*)
at _stream_readable.js:*:*
at process._tickCallback (node.js:*:*)
--- /dev/null
+// Copyright Joyent, Inc. and other Node contributors.
+//
+// Permission is hereby granted, free of charge, to any person obtaining a
+// copy of this software and associated documentation files (the
+// "Software"), to deal in the Software without restriction, including
+// without limitation the rights to use, copy, modify, merge, publish,
+// distribute, sublicense, and/or sell copies of the Software, and to permit
+// persons to whom the Software is furnished to do so, subject to the
+// following conditions:
+//
+// The above copyright notice and this permission notice shall be included
+// in all copies or substantial portions of the Software.
+//
+// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
+// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
+// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
+// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
+// USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+var common = require('../common');
+var assert = require('assert');
+var events = require('events');
+
+var E = events.EventEmitter.prototype;
+assert.equal(E.constructor.name, 'EventEmitter');
+assert.equal(E.on, E.addListener); // Same method.
+Object.getOwnPropertyNames(E).forEach(function(name) {
+ if (name === 'constructor' || name === 'on') return;
+ assert.equal(E[name].name, name);
+});