Skip to content
New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

events: don't inherit from Object.prototype #6092

Merged
merged 1 commit into from
Apr 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

var domain;

// This constructor is used to store event handlers. Instantiating this is
// faster than explicitly calling `Object.create(null)` to get a "clean" empty
// object (tested with v8 v4.9).
function EventHandlers() {}
EventHandlers.prototype = Object.create(null);

function EventEmitter() {
EventEmitter.init.call(this);
}
Expand Down Expand Up @@ -44,7 +50,7 @@ EventEmitter.init = function() {
}

if (!this._events || this._events === Object.getPrototypeOf(this)._events) {
this._events = {};
this._events = new EventHandlers();
this._eventsCount = 0;
}

Expand Down Expand Up @@ -211,7 +217,7 @@ EventEmitter.prototype.addListener = function addListener(type, listener) {

events = this._events;
if (!events) {
events = this._events = {};
events = this._events = new EventHandlers();
this._eventsCount = 0;
} else {
// To avoid recursion in the case that type === "newListener"! Before
Expand Down Expand Up @@ -296,7 +302,7 @@ EventEmitter.prototype.removeListener =

if (list === listener || (list.listener && list.listener === listener)) {
if (--this._eventsCount === 0)
this._events = {};
this._events = new EventHandlers();
else {
delete events[type];
if (events.removeListener)
Expand All @@ -319,7 +325,7 @@ EventEmitter.prototype.removeListener =
if (list.length === 1) {
list[0] = undefined;
if (--this._eventsCount === 0) {
this._events = {};
this._events = new EventHandlers();
return this;
} else {
delete events[type];
Expand All @@ -346,11 +352,11 @@ EventEmitter.prototype.removeAllListeners =
// not listening for removeListener, no need to emit
if (!events.removeListener) {
if (arguments.length === 0) {
this._events = {};
this._events = new EventHandlers();
this._eventsCount = 0;
} else if (events[type]) {
if (--this._eventsCount === 0)
this._events = {};
this._events = new EventHandlers();
else
delete events[type];
}
Expand All @@ -366,7 +372,7 @@ EventEmitter.prototype.removeAllListeners =
this.removeAllListeners(key);
}
this.removeAllListeners('removeListener');
this._events = {};
this._events = new EventHandlers();
this._eventsCount = 0;
return this;
}
Expand Down
4 changes: 3 additions & 1 deletion src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3228,7 +3228,9 @@ void SetupProcessObject(Environment* env,
env->SetMethod(process, "_setupDomainUse", SetupDomainUse);

// pre-set _events object for faster emit checks
process->Set(env->events_string(), Object::New(env->isolate()));
Local<Object> events_obj = Object::New(env->isolate());
events_obj->SetPrototype(env->context(), Null(env->isolate()));
process->Set(env->events_string(), events_obj);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this process._events?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes.

}


Expand Down
12 changes: 0 additions & 12 deletions test/known_issues/test-events-known-properties.js

This file was deleted.

1 change: 0 additions & 1 deletion test/parallel/test-event-emitter-listeners-side-effects.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ assert(fl.length === 1);
assert(fl[0] === assert.fail);

e.listeners('bar');
assert(!e._events.hasOwnProperty('bar'));

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

won't this still be true?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no _events.hasOwnProperty anymore – you could do something like Object.prototype.hasOwnProperty.call(e._events, 'bar') here, I guess?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh good catch. Guess that makes it semver-major, We should probably use what you suggested to check it though.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, definitely semver major. There are other side effects of this. For instance, there is no toString or valueOf so doing a console.log or util.inspect will yield different results and many of the Object.{method} static methods may croak a bit on it (e.g. Object.keys will still work but I believe some of the others fail).

e.on('foo', assert.ok);
fl = e.listeners('foo');
Expand Down
36 changes: 36 additions & 0 deletions test/parallel/test-event-emitter-special-event-names.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
'use strict';

const common = require('../common');
const EventEmitter = require('events');
const assert = require('assert');

const ee = new EventEmitter();
const handler = () => {};

assert.strictEqual(ee._events.hasOwnProperty, undefined);
assert.strictEqual(ee._events.toString, undefined);

ee.on('__proto__', handler);
ee.on('__defineGetter__', handler);
ee.on('toString', handler);

assert.deepStrictEqual(ee.eventNames(), [
'__proto__',
'__defineGetter__',
'toString'
]);

assert.deepStrictEqual(ee.listeners('__proto__'), [handler]);
assert.deepStrictEqual(ee.listeners('__defineGetter__'), [handler]);
assert.deepStrictEqual(ee.listeners('toString'), [handler]);

ee.on('__proto__', common.mustCall(function(val) {
assert.strictEqual(val, 1);
}));
ee.emit('__proto__', 1);

process.on('__proto__', common.mustCall(function(val) {
assert.strictEqual(val, 1);
}));
process.emit('__proto__', 1);