Skip to content

events: make eventNames() return only enumerable properties#5817

Closed
lpinca wants to merge 1 commit intonodejs:masterfrom
lpinca:enumerable-symbol-properties
Closed

events: make eventNames() return only enumerable properties#5817
lpinca wants to merge 1 commit intonodejs:masterfrom
lpinca:enumerable-symbol-properties

Conversation

@lpinca
Copy link
Copy Markdown
Member

@lpinca lpinca commented Mar 21, 2016

Pull Request check-list

Please make sure to review and check all of these items:

  • Does make -j8 test (UNIX) or vcbuild test nosign (Windows) pass with
    this change (including linting)?
  • Is the commit message formatted according to CONTRIBUTING.md?
  • If this change fixes a bug (or a performance problem), is a regression
    test (or a benchmark) included?
  • Is a documentation update included (if this change modifies
    existing APIs, or introduces new ones)?

Affected core subsystem(s)

events

Description of change

Right now eventNames() returns all the symbol properties (enumerable or not) directly found in _events. This patch prevents non-enumerable symbol properties from being included in the returned array.

Right now `eventNames()` returns all the symbol properties (enumerable
or not) directly found in `_events`. This patch prevents non-enumerable
symbol properties from being included in the returned array.
@thefourtheye thefourtheye added the events Issues and PRs related to the events subsystem / EventEmitter. label Mar 21, 2016
@vkurchatkin
Copy link
Copy Markdown
Contributor

What's the point? There is no public way to add non enumerable property anyway

@lpinca
Copy link
Copy Markdown
Member Author

lpinca commented Mar 21, 2016

Yes, this is indeed very edge case but we are using Object.keys() for regular properties not Object.getOwnPropertyNames() so one reason is consistency.

@benjamingr
Copy link
Copy Markdown
Member

This makes it a tiny bit slower for no additional benefit. There is no public way to add a non enumerable property to _events and we don't do it internally.

If anything, it could be nice to use Object.getOwnPropertyNames instead of Object.keys since that guarantees the return order (although to be fair, keys is well ordered in practice too).

@lpinca
Copy link
Copy Markdown
Member Author

lpinca commented Mar 21, 2016

What about some madness like this:

'use strict';

const EE = require('events');
const s = Symbol();

Object.defineProperty(Object.prototype, s, {
  get: function () {
    Object.defineProperty(this, s, { value: 'foo' });
  }
});

const ee = new EE();

ee.listeners(s);

ee._events.hasOwnProperty(s); // => `true`
ee._events.propertyIsEnumerable(s); // => `false`

@lpinca
Copy link
Copy Markdown
Member Author

lpinca commented Mar 21, 2016

Ofc if you do that you are crazy but that's an example of how a non-enumerable property can go into _events using public APIs.

@benjamingr
Copy link
Copy Markdown
Member

If someone definePropertys on Object.prototype a lot of code in Node breaks anyway since we have plenty of places where we use objects as maps. This would be the least of our worries most likely.

@lpinca
Copy link
Copy Markdown
Member Author

lpinca commented Mar 21, 2016

Ok well these were the only reasons behind this change, I close this.

@lpinca lpinca closed this Mar 21, 2016
@lpinca lpinca deleted the enumerable-symbol-properties branch March 21, 2016 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

events Issues and PRs related to the events subsystem / EventEmitter.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants