Revert "events: Don't crash on events named __proto__"
Unfortunately, it's just too slow to do this in events.js. Users will just have to live with not having events named __proto__ or toString. This reverts commit b48e303af023dc60b6f6590694ba94d9b8108702.
This commit is contained in:
parent
1528de2373
commit
b7d76a1a7b
@ -53,8 +53,8 @@ var PROCESS;
|
|||||||
EventEmitter.prototype.emit = function(type) {
|
EventEmitter.prototype.emit = function(type) {
|
||||||
// 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 || !this._events.everror ||
|
if (!this._events || !this._events.error ||
|
||||||
(isArray(this._events.everror) && !this._events.everror.length))
|
(isArray(this._events.error) && !this._events.error.length))
|
||||||
{
|
{
|
||||||
if (this.domain) {
|
if (this.domain) {
|
||||||
var er = arguments[1];
|
var er = arguments[1];
|
||||||
@ -75,8 +75,7 @@ EventEmitter.prototype.emit = function(type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!this._events) return false;
|
if (!this._events) return false;
|
||||||
var evtype = 'ev' + type;
|
var handler = this._events[type];
|
||||||
var handler = this._events[evtype];
|
|
||||||
if (!handler) return false;
|
if (!handler) return false;
|
||||||
|
|
||||||
if (typeof handler == 'function') {
|
if (typeof handler == 'function') {
|
||||||
@ -143,37 +142,36 @@ EventEmitter.prototype.addListener = function(type, listener) {
|
|||||||
|
|
||||||
// To avoid recursion in the case that type == "newListener"! Before
|
// To avoid recursion in the case that type == "newListener"! Before
|
||||||
// adding it to the listeners, first emit "newListener".
|
// adding it to the listeners, first emit "newListener".
|
||||||
if (this._events.evnewListener) {
|
if (this._events.newListener) {
|
||||||
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
this.emit('newListener', type, typeof listener.listener === 'function' ?
|
||||||
listener.listener : listener);
|
listener.listener : listener);
|
||||||
}
|
}
|
||||||
|
|
||||||
var evtype = 'ev' + type;
|
if (!this._events[type]) {
|
||||||
if (!this._events[evtype]) {
|
|
||||||
// Optimize the case of one listener. Don't need the extra array object.
|
// Optimize the case of one listener. Don't need the extra array object.
|
||||||
this._events[evtype] = listener;
|
this._events[type] = listener;
|
||||||
} else if (isArray(this._events[evtype])) {
|
} else if (isArray(this._events[type])) {
|
||||||
|
|
||||||
// If we've already got an array, just append.
|
// If we've already got an array, just append.
|
||||||
this._events[evtype].push(listener);
|
this._events[type].push(listener);
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// Adding the second element, need to change to array.
|
// Adding the second element, need to change to array.
|
||||||
this._events[evtype] = [this._events[evtype], listener];
|
this._events[type] = [this._events[type], listener];
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check for listener leak
|
// Check for listener leak
|
||||||
if (isArray(this._events[evtype]) && !this._events[evtype].warned) {
|
if (isArray(this._events[type]) && !this._events[type].warned) {
|
||||||
var m;
|
var m;
|
||||||
m = this._maxListeners;
|
m = this._maxListeners;
|
||||||
|
|
||||||
if (m && m > 0 && this._events[evtype].length > m) {
|
if (m && m > 0 && this._events[type].length > m) {
|
||||||
this._events[evtype].warned = true;
|
this._events[type].warned = true;
|
||||||
console.error('(node) warning: possible EventEmitter memory ' +
|
console.error('(node) warning: possible EventEmitter memory ' +
|
||||||
'leak detected. %d listeners added. ' +
|
'leak detected. %d listeners added. ' +
|
||||||
'Use emitter.setMaxListeners() to increase limit.',
|
'Use emitter.setMaxListeners() to increase limit.',
|
||||||
this._events[evtype].length);
|
this._events[type].length);
|
||||||
console.trace();
|
console.trace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -206,11 +204,10 @@ EventEmitter.prototype.removeListener = function(type, listener) {
|
|||||||
throw new Error('removeListener only takes instances of Function');
|
throw new Error('removeListener only takes instances of Function');
|
||||||
}
|
}
|
||||||
|
|
||||||
var evtype = 'ev' + type;
|
// does not use listeners(), so no side effect of creating _events[type]
|
||||||
// does not use listeners(), so no side effect of creating _events[evtype]
|
if (!this._events || !this._events[type]) return this;
|
||||||
if (!this._events || !this._events[evtype]) return this;
|
|
||||||
|
|
||||||
var list = this._events[evtype];
|
var list = this._events[type];
|
||||||
|
|
||||||
if (isArray(list)) {
|
if (isArray(list)) {
|
||||||
var position = -1;
|
var position = -1;
|
||||||
@ -226,17 +223,17 @@ EventEmitter.prototype.removeListener = function(type, listener) {
|
|||||||
if (position < 0) return this;
|
if (position < 0) return this;
|
||||||
list.splice(position, 1);
|
list.splice(position, 1);
|
||||||
if (list.length == 0)
|
if (list.length == 0)
|
||||||
this._events[evtype] = null;
|
this._events[type] = null;
|
||||||
|
|
||||||
if (this._events.evremoveListener) {
|
if (this._events.removeListener) {
|
||||||
this.emit('removeListener', type, listener);
|
this.emit('removeListener', type, listener);
|
||||||
}
|
}
|
||||||
} else if (list === listener ||
|
} else if (list === listener ||
|
||||||
(list.listener && list.listener === listener))
|
(list.listener && list.listener === listener))
|
||||||
{
|
{
|
||||||
this._events[evtype] = null;
|
this._events[type] = null;
|
||||||
|
|
||||||
if (this._events.evremoveListener) {
|
if (this._events.removeListener) {
|
||||||
this.emit('removeListener', type, listener);
|
this.emit('removeListener', type, listener);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -248,11 +245,11 @@ EventEmitter.prototype.removeAllListeners = function(type) {
|
|||||||
if (!this._events) return this;
|
if (!this._events) return this;
|
||||||
|
|
||||||
// fast path
|
// fast path
|
||||||
if (!this._events.evremoveListener) {
|
if (!this._events.removeListener) {
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
this._events = {};
|
this._events = {};
|
||||||
} else if (type && this._events && this._events['ev' + type]) {
|
} else if (type && this._events && this._events[type]) {
|
||||||
this._events['ev' + type] = null;
|
this._events[type] = null;
|
||||||
}
|
}
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
@ -260,16 +257,15 @@ EventEmitter.prototype.removeAllListeners = function(type) {
|
|||||||
// slow(ish) path, emit 'removeListener' events for all removals
|
// slow(ish) path, emit 'removeListener' events for all removals
|
||||||
if (arguments.length === 0) {
|
if (arguments.length === 0) {
|
||||||
for (var key in this._events) {
|
for (var key in this._events) {
|
||||||
if (key === 'evremoveListener') continue;
|
if (key === 'removeListener') continue;
|
||||||
this.removeAllListeners(key.slice(2));
|
this.removeAllListeners(key);
|
||||||
}
|
}
|
||||||
this.removeAllListeners('removeListener');
|
this.removeAllListeners('removeListener');
|
||||||
this._events = {};
|
this._events = {};
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
var evtype = 'ev' + type;
|
var listeners = this._events[type];
|
||||||
var listeners = this._events[evtype];
|
|
||||||
if (isArray(listeners)) {
|
if (isArray(listeners)) {
|
||||||
while (listeners.length) {
|
while (listeners.length) {
|
||||||
// LIFO order
|
// LIFO order
|
||||||
@ -278,16 +274,15 @@ EventEmitter.prototype.removeAllListeners = function(type) {
|
|||||||
} else if (listeners) {
|
} else if (listeners) {
|
||||||
this.removeListener(type, listeners);
|
this.removeListener(type, listeners);
|
||||||
}
|
}
|
||||||
this._events[evtype] = null;
|
this._events[type] = null;
|
||||||
|
|
||||||
return this;
|
return this;
|
||||||
};
|
};
|
||||||
|
|
||||||
EventEmitter.prototype.listeners = function(type) {
|
EventEmitter.prototype.listeners = function(type) {
|
||||||
var evtype = 'ev' + type;
|
if (!this._events || !this._events[type]) return [];
|
||||||
if (!this._events || !this._events[evtype]) return [];
|
if (!isArray(this._events[type])) {
|
||||||
if (!isArray(this._events[evtype])) {
|
return [this._events[type]];
|
||||||
return [this._events[evtype]];
|
|
||||||
}
|
}
|
||||||
return this._events[evtype].slice(0);
|
return this._events[type].slice(0);
|
||||||
};
|
};
|
||||||
|
@ -29,30 +29,30 @@ var e = new events.EventEmitter();
|
|||||||
for (var i = 0; i < 10; i++) {
|
for (var i = 0; i < 10; i++) {
|
||||||
e.on('default', function() {});
|
e.on('default', function() {});
|
||||||
}
|
}
|
||||||
assert.ok(!e._events.evdefault.hasOwnProperty('warned'));
|
assert.ok(!e._events['default'].hasOwnProperty('warned'));
|
||||||
e.on('default', function() {});
|
e.on('default', function() {});
|
||||||
assert.ok(e._events.evdefault.warned);
|
assert.ok(e._events['default'].warned);
|
||||||
|
|
||||||
// specific
|
// specific
|
||||||
e.setMaxListeners(5);
|
e.setMaxListeners(5);
|
||||||
for (var i = 0; i < 5; i++) {
|
for (var i = 0; i < 5; i++) {
|
||||||
e.on('specific', function() {});
|
e.on('specific', function() {});
|
||||||
}
|
}
|
||||||
assert.ok(!e._events.evspecific.hasOwnProperty('warned'));
|
assert.ok(!e._events['specific'].hasOwnProperty('warned'));
|
||||||
e.on('specific', function() {});
|
e.on('specific', function() {});
|
||||||
assert.ok(e._events.evspecific.warned);
|
assert.ok(e._events['specific'].warned);
|
||||||
|
|
||||||
// only one
|
// only one
|
||||||
e.setMaxListeners(1);
|
e.setMaxListeners(1);
|
||||||
e.on('only one', function() {});
|
e.on('only one', function() {});
|
||||||
assert.ok(!e._events['evonly one'].hasOwnProperty('warned'));
|
assert.ok(!e._events['only one'].hasOwnProperty('warned'));
|
||||||
e.on('only one', function() {});
|
e.on('only one', function() {});
|
||||||
assert.ok(e._events['evonly one'].hasOwnProperty('warned'));
|
assert.ok(e._events['only one'].hasOwnProperty('warned'));
|
||||||
|
|
||||||
// unlimited
|
// unlimited
|
||||||
e.setMaxListeners(0);
|
e.setMaxListeners(0);
|
||||||
for (var i = 0; i < 1000; i++) {
|
for (var i = 0; i < 1000; i++) {
|
||||||
e.on('unlimited', function() {});
|
e.on('unlimited', function() {});
|
||||||
}
|
}
|
||||||
assert.ok(!e._events['evunlimited'].hasOwnProperty('warned'));
|
assert.ok(!e._events['unlimited'].hasOwnProperty('warned'));
|
||||||
|
|
||||||
|
@ -37,21 +37,21 @@ assert.equal(e._events, null);
|
|||||||
|
|
||||||
e.on('foo', assert.fail);
|
e.on('foo', assert.fail);
|
||||||
fl = e.listeners('foo');
|
fl = e.listeners('foo');
|
||||||
assert(e._events.evfoo === assert.fail);
|
assert(e._events.foo === assert.fail);
|
||||||
assert(Array.isArray(fl));
|
assert(Array.isArray(fl));
|
||||||
assert(fl.length === 1);
|
assert(fl.length === 1);
|
||||||
assert(fl[0] === assert.fail);
|
assert(fl[0] === assert.fail);
|
||||||
|
|
||||||
e.listeners('bar');
|
e.listeners('bar');
|
||||||
assert(!e._events.hasOwnProperty('evbar'));
|
assert(!e._events.hasOwnProperty('bar'));
|
||||||
|
|
||||||
e.on('foo', assert.ok);
|
e.on('foo', assert.ok);
|
||||||
fl = e.listeners('foo');
|
fl = e.listeners('foo');
|
||||||
|
|
||||||
assert(Array.isArray(e._events.evfoo));
|
assert(Array.isArray(e._events.foo));
|
||||||
assert(e._events.evfoo.length === 2);
|
assert(e._events.foo.length === 2);
|
||||||
assert(e._events.evfoo[0] === assert.fail);
|
assert(e._events.foo[0] === assert.fail);
|
||||||
assert(e._events.evfoo[1] === assert.ok);
|
assert(e._events.foo[1] === assert.ok);
|
||||||
|
|
||||||
assert(Array.isArray(fl));
|
assert(Array.isArray(fl));
|
||||||
assert(fl.length === 2);
|
assert(fl.length === 2);
|
||||||
|
@ -1,37 +0,0 @@
|
|||||||
// 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 = new events.EventEmitter();
|
|
||||||
|
|
||||||
var emited = false;
|
|
||||||
e.on('__proto__', function() {
|
|
||||||
emited = true;
|
|
||||||
});
|
|
||||||
|
|
||||||
e.emit('__proto__');
|
|
||||||
|
|
||||||
process.on('exit', function() {
|
|
||||||
assert.equal(true, emited);
|
|
||||||
});
|
|
Loading…
x
Reference in New Issue
Block a user