Skip to content

Commit 6384ea3

Browse files
committed
Add fix and test for immutable-js#1175
Closes immutable-js#1175
1 parent b783d7e commit 6384ea3

File tree

9 files changed

+39
-9
lines changed

9 files changed

+39
-9
lines changed

__tests__/issues.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,23 @@
22
///<reference path='../resources/jest.d.ts'/>
33

44
declare var Symbol: any;
5-
import { List, OrderedMap, Seq } from '../';
5+
import { List, OrderedMap, Seq, Set } from '../';
6+
7+
describe('Issue #1175', () => {
8+
it('invalid hashCode() response should not infinitly recurse', () => {
9+
class BadHash {
10+
equals() {
11+
return false;
12+
}
13+
hashCode() {
14+
return 2 ** 32;
15+
}
16+
}
17+
18+
const set = Set([new BadHash()]);
19+
expect(set.size).toEqual(1);
20+
});
21+
});
622

723
describe('Issue #1220 : Seq.rest() throws an exception when invoked on a single item sequence ', () => {
824
it('should be iterable', () => {

dist/immutable-nonambient.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@
349349
* assert.equal(set.has(b), true);
350350
* ```
351351
*
352+
* Note: hashCode() MUST return a Uint32 number. The easiest way to
353+
* guarantee this is to return `myHash | 0` from a custom implementation.
354+
*
352355
* If two values have the same `hashCode`, they are [not guaranteed
353356
* to be equal][Hash Collision]. If two values have different `hashCode`s,
354357
* they must not be equal.

dist/immutable.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ declare module Immutable {
349349
* assert.equal(set.has(b), true);
350350
* ```
351351
*
352+
* Note: hashCode() MUST return a Uint32 number. The easiest way to
353+
* guarantee this is to return `myHash | 0` from a custom implementation.
354+
*
352355
* If two values have the same `hashCode`, they are [not guaranteed
353356
* to be equal][Hash Collision]. If two values have different `hashCode`s,
354357
* they must not be equal.

dist/immutable.es.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -859,7 +859,8 @@ function hash(o) {
859859
: hashString(o);
860860
}
861861
if (typeof o.hashCode === 'function') {
862-
return o.hashCode();
862+
// Drop any high bits from accidentally long hash codes.
863+
return smi(o.hashCode());
863864
}
864865
if (type === 'object') {
865866
return hashJSObj(o);

dist/immutable.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -865,7 +865,8 @@ function hash(o) {
865865
: hashString(o);
866866
}
867867
if (typeof o.hashCode === 'function') {
868-
return o.hashCode();
868+
// Drop any high bits from accidentally long hash codes.
869+
return smi(o.hashCode());
869870
}
870871
if (type === 'object') {
871872
return hashJSObj(o);

dist/immutable.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pages/lib/genTypeDefData.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -384,10 +384,12 @@ function DocVisitor(source) {
384384
members: [
385385
{
386386
index: true,
387-
params: [{
388-
name: 'key',
389-
type: { k: TypeKind.String }
390-
}],
387+
params: [
388+
{
389+
name: 'key',
390+
type: { k: TypeKind.String }
391+
}
392+
],
391393
type: parseType(node.type)
392394
}
393395
]

src/Hash.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ export function hash(o) {
4343
: hashString(o);
4444
}
4545
if (typeof o.hashCode === 'function') {
46-
return o.hashCode();
46+
// Drop any high bits from accidentally long hash codes.
47+
return smi(o.hashCode());
4748
}
4849
if (type === 'object') {
4950
return hashJSObj(o);

type-definitions/Immutable.d.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -349,6 +349,9 @@ declare module Immutable {
349349
* assert.equal(set.has(b), true);
350350
* ```
351351
*
352+
* Note: hashCode() MUST return a Uint32 number. The easiest way to
353+
* guarantee this is to return `myHash | 0` from a custom implementation.
354+
*
352355
* If two values have the same `hashCode`, they are [not guaranteed
353356
* to be equal][Hash Collision]. If two values have different `hashCode`s,
354357
* they must not be equal.

0 commit comments

Comments
 (0)