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

hashing breaks for custom objects with "Invalid value used as weak map key" #106

Closed
Methuselah96 opened this issue Oct 17, 2020 · 1 comment
Labels
bug Something isn't working from-original-repo

Comments

@Methuselah96
Copy link

From @UnsungHero97 on Tue, 06 Nov 2018 20:24:11 GMT

I think the latest changes to Hash.js introduce a weird bug when hashing custom objects, more specifically, a class with valueOf() overridden. The error being thrown is Uncaught TypeError: Invalid value used as weak map key.

It's strange because it doesn't happen until a certain point, a very specific point. It happens when invoking .set() on a Map on the 9th attempt. For the first 8 attempts, all is good. On the 9th attempt, error.

To reproduce, I ran this bit of code in the console at https://facebook.github.io/immutable-js.

class Example {
  constructor(id, key) { this.id = id; this.key = key; }
  valueOf() { return `${this.id}.${this.key}`; } 
  toString() { return `${this.id}.${this.key}`; }
}

const m = Immutable.Map().asMutable();
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 1, ok
...
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 8, ok
m.set(new Example('test', (new Date()).toISOString()), Math.random()); // 9, ERROR

This has worked for me for quite a while. It breaks with release v4.0.0-rc.11.

Here's a screenshot of the console:

screen shot 2018-11-06 at 12 17 27 pm

Copied from original issue: immutable-js#1643

@Methuselah96
Copy link
Author

Fixed in #17.

@Methuselah96 Methuselah96 added the bug Something isn't working label Nov 14, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug Something isn't working from-original-repo
Projects
None yet
Development

No branches or pull requests

1 participant