Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

File objects all hash to the same value #527

Open
wezm opened this issue Feb 25, 2024 · 1 comment
Open

File objects all hash to the same value #527

wezm opened this issue Feb 25, 2024 · 1 comment

Comments

@wezm
Copy link
Contributor

wezm commented Feb 25, 2024

I tried using some Files (obtained from the files attribute of input type="file") as keys in a Dict but despite inserting multiple different files there was only one entry at the end. Turns out they're all considered the same key. typeof file returns "object" which means we go to hashObject. I think it ends up in the else case:

stdlib/src/dict.mjs

Lines 106 to 113 in 6a701d1

} else {
const keys = Object.keys(o);
for (let i = 0; i < keys.length; i++) {
const k = keys[i];
const v = o[k];
h = (h + hashMerge(getHash(v), hashString(k))) | 0;
}
}

The issue is that Object.keys returns [] for all files.

Screenshot from 2024-02-25 11-39-26

It seems that if it went through hashByReference it might work but not sure how to resolve this more generally. I also note that hashObject does a check for a hashCode function. I suppose another option, at least for my project would be to add that to the File prototype or wrap them in a class that does. 🤔

Edit: As a workaround I might be about to use the value returned from URL.createObjectURL(object) as the key instead.

Gleam version: gleam 1.0.0-rc2
Target: javascript
Env: browser

@lpil
Copy link
Member

lpil commented Feb 26, 2024

Gleam uses structural equality and hashing except for a few known mutable JS data structures, which get special cased.

We should document this behaviour for JS objects which are the same structure but may otherwise be considered different.

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

No branches or pull requests

2 participants