Skip to content
This repository was archived by the owner on Jul 23, 2021. It is now read-only.

Hash symbols and valueOf results #17

Merged
merged 1 commit into from
Aug 27, 2020
Merged

Hash symbols and valueOf results #17

merged 1 commit into from
Aug 27, 2020

Conversation

conartist6
Copy link
Member

@conartist6 conartist6 commented Aug 23, 2020

Fixes an obvious bug: TypeError: Cannot convert a Symbol value to a string, and also tries to avoid the cost of hash collisions between symbols.

This is an alternative to immutable-js#1753, based primarily on this comment.

@conartist6 conartist6 force-pushed the hash-symbols branch 2 times, most recently from 38a01e3 to 29d7cc6 Compare August 23, 2020 18:32
@conartist6 conartist6 changed the title Hash symbols as objects Hash symbols and valueOf results Aug 23, 2020

hashed = nextHash();

symbolMap[sym] = hashed;
Copy link
Member Author

Choose a reason for hiding this comment

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

Note that this is technically a memory leak. I'm just hoping there's no reason that programmers would create an unbounded number of symbols.

Comment on lines +13 to +26
if (o == null) {
return hashNullish(o);
}

if (typeof o.hashCode === 'function') {
// Drop any high bits from accidentally long hash codes.
return smi(o.hashCode(o));
}

const v = valueOf(o);

if (v == null) {
return hashNullish(v);
}
Copy link

@Methuselah96 Methuselah96 Aug 27, 2020

Choose a reason for hiding this comment

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

Can you explain why we are moving this code of out of the 'object' || 'function' case to the top of the method?

Copy link
Member Author

@conartist6 conartist6 Aug 27, 2020

Choose a reason for hiding this comment

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

Quite simply: require('moment')().valueOf(), which returns a number (the timestamp). The current code assumes incorrectly that the result of object.valueOf() is always an object.

Choose a reason for hiding this comment

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

Oh, I see now you're tackling immutable-js#1643 as well in this PR. I was trying to figure out what this code had to do with symbols, but now it makes sense.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants