Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

events: Support prototype property name as an event type #4366

Closed
wants to merge 1 commit into
from
Jump to file or symbol
Failed to load files and symbols.
+41 −4
Split
View
@@ -44,7 +44,7 @@ exports.EventEmitter = EventEmitter;
// that to be increased. Set to zero for unlimited.
var defaultMaxListeners = 10;
EventEmitter.prototype.setMaxListeners = function(n) {
- if (!this._events) this._events = {};
+ if (!this._events) this._events = Object.create(null);
this._maxListeners = n;
};
@@ -140,7 +140,7 @@ EventEmitter.prototype.addListener = function(type, listener) {
throw new Error('addListener only takes instances of Function');
}
- if (!this._events) this._events = {};
+ if (!this._events) this._events = Object.create(null);
// To avoid recursion in the case that type == "newListeners"! Before
// adding it to the listeners, first emit "newListeners".
@@ -234,7 +234,7 @@ EventEmitter.prototype.removeListener = function(type, listener) {
EventEmitter.prototype.removeAllListeners = function(type) {
if (arguments.length === 0) {
- this._events = {};
+ this._events = Object.create(null);
return this;
}
@@ -244,7 +244,7 @@ EventEmitter.prototype.removeAllListeners = function(type) {
};
EventEmitter.prototype.listeners = function(type) {
- if (!this._events) this._events = {};
+ if (!this._events) this._events = Object.create(null);
if (!this._events[type]) this._events[type] = [];
if (!isArray(this._events[type])) {
this._events[type] = [this._events[type]];
@@ -0,0 +1,37 @@
+// 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);
+});