This repository was archived by the owner on Jul 23, 2021. It is now read-only.
forked from immutable-js/immutable-js
-
-
Notifications
You must be signed in to change notification settings - Fork 6
Hash symbols and valueOf results #17
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,41 +10,50 @@ import { smi } from './Math'; | |
const defaultValueOf = Object.prototype.valueOf; | ||
|
||
export function hash(o) { | ||
switch (typeof o) { | ||
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); | ||
} | ||
|
||
switch (typeof v) { | ||
case 'boolean': | ||
// The hash values for built-in constants are a 1 value for each 5-byte | ||
// shift region expect for the first, which encodes the value. This | ||
// reduces the odds of a hash collision for these common values. | ||
return o ? 0x42108421 : 0x42108420; | ||
return v ? 0x42108421 : 0x42108420; | ||
case 'number': | ||
return hashNumber(o); | ||
return hashNumber(v); | ||
case 'string': | ||
return o.length > STRING_HASH_CACHE_MIN_STRLEN | ||
? cachedHashString(o) | ||
: hashString(o); | ||
return v.length > STRING_HASH_CACHE_MIN_STRLEN | ||
? cachedHashString(v) | ||
: hashString(v); | ||
case 'object': | ||
case 'function': | ||
if (o === null) { | ||
return 0x42108422; | ||
} | ||
if (typeof o.hashCode === 'function') { | ||
// Drop any high bits from accidentally long hash codes. | ||
return smi(o.hashCode(o)); | ||
} | ||
if (o.valueOf !== defaultValueOf && typeof o.valueOf === 'function') { | ||
o = o.valueOf(o); | ||
} | ||
return hashJSObj(o); | ||
case 'undefined': | ||
return 0x42108423; | ||
return hashJSObj(v); | ||
case 'symbol': | ||
return hashSymbol(v); | ||
default: | ||
if (typeof o.toString === 'function') { | ||
return hashString(o.toString()); | ||
if (typeof v.toString === 'function') { | ||
return hashString(v.toString()); | ||
} | ||
throw new Error('Value type ' + typeof o + ' cannot be hashed.'); | ||
throw new Error('Value type ' + typeof v + ' cannot be hashed.'); | ||
} | ||
} | ||
|
||
function hashNullish(nullish) { | ||
return nullish === null ? 0x42108422 : /* undefined */ 0x42108423; | ||
} | ||
|
||
// Compress arbitrarily large numbers into smi hashes. | ||
function hashNumber(n) { | ||
if (n !== n || n === Infinity) { | ||
|
@@ -90,6 +99,19 @@ function hashString(string) { | |
return smi(hashed); | ||
} | ||
|
||
function hashSymbol(sym) { | ||
let hashed = symbolMap[sym]; | ||
if (hashed !== undefined) { | ||
return hashed; | ||
} | ||
|
||
hashed = nextHash(); | ||
|
||
symbolMap[sym] = hashed; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
|
||
return hashed; | ||
} | ||
|
||
function hashJSObj(obj) { | ||
let hashed; | ||
if (usingWeakMap) { | ||
|
@@ -116,10 +138,7 @@ function hashJSObj(obj) { | |
} | ||
} | ||
|
||
hashed = ++objHashUID; | ||
if (objHashUID & 0x40000000) { | ||
objHashUID = 0; | ||
} | ||
hashed = nextHash(); | ||
|
||
if (usingWeakMap) { | ||
weakMap.set(obj, hashed); | ||
|
@@ -186,14 +205,30 @@ function getIENodeHash(node) { | |
} | ||
} | ||
|
||
function valueOf(obj) { | ||
return obj.valueOf !== defaultValueOf && typeof obj.valueOf === 'function' | ||
? obj.valueOf(obj) | ||
: obj; | ||
} | ||
|
||
function nextHash() { | ||
const nextHash = ++_objHashUID; | ||
if (_objHashUID & 0x40000000) { | ||
_objHashUID = 0; | ||
} | ||
return nextHash; | ||
} | ||
|
||
// If possible, use a WeakMap. | ||
const usingWeakMap = typeof WeakMap === 'function'; | ||
let weakMap; | ||
if (usingWeakMap) { | ||
weakMap = new WeakMap(); | ||
} | ||
|
||
let objHashUID = 0; | ||
const symbolMap = Object.create(null); | ||
|
||
let _objHashUID = 0; | ||
|
||
let UID_HASH_KEY = '__immutablehash__'; | ||
if (typeof Symbol === 'function') { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
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 ofobject.valueOf()
is always an object.There was a problem hiding this comment.
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.