Skip to content

Fix: Map/Set polyfill should store -0 in keys as +0 #2905

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/js/es6/map.js
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,8 @@ $jscomp.polyfill('Map',

/** @override */
PolyfillMap.prototype.set = function(key, value) {
// normalize -0/+0 to +0
key = key === 0 ? 0 : key;
var r = maybeGetEntry(this, key);
if (!r.list) {
r.list = (this.data_[r.id] = []);
Expand Down
2 changes: 2 additions & 0 deletions src/com/google/javascript/jscomp/js/es6/set.js
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ $jscomp.polyfill('Set',

/** @override */
PolyfillSet.prototype.add = function(value) {
// normalize -0/+0 to +0
value = value === 0 ? 0 : value;
this.map_.set(value, value);
this.size = this.map_.size;
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,18 @@ testSuite({
checkSetGet(map, keys[i], {});
}
assertEquals(37, map.size);
},

// Note: +0 and -0 are the same key
testKeysNormalizeZero() {
const map = new Map().set(-0, 'foo');
assertTrue(map.has(-0));
assertEquals(map.get(0), map.get(-0));
assertEquals('foo', map.get(0));
assertEquals('foo', map.get(-0));
// stored value should be +0
assertEquals(Infinity, 1 / map.keys().next().value);
assertTrue(map.delete(-0));
assertFalse(map.has(0));
assertFalse(map.has(-0));
},

testNanKeys() {
Expand Down Expand Up @@ -159,6 +167,13 @@ testSuite({
assertEquals(3, map.get('a'));
},

testConstructor_normalizeZero() {
const map = new Map([[-0, 'foo']]);
assertEquals(Infinity, 1 / map.keys().next().value);
assertEquals('foo', map.get(0));
assertEquals('foo', map.get(-0));
},

testForEach() {
const map = new Map();
map.set('a', 'b');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,18 @@ testSuite({
checkAddHas(set, keys[i]);
}
assertEquals(37, set.size);
},

testAdd_normalizeZero() {
// Note: +0 and -0 are the same
const set = new Set().add(-0);
assertTrue(set.has(0));
assertTrue(set.has(-0));
// stored value should be +0
assertEquals(Infinity, 1 / set.keys().next().value);
assertTrue(set.delete(-0));
assertFalse(set.has(0));
assertFalse(set.has(-0));
},

testAdd_nans() {
Expand Down Expand Up @@ -151,6 +158,13 @@ testSuite({
assertTrue(set.has('b'));
},

testConstructor_normalizeZero() {
const set = new Set([-0]);
assertEquals(Infinity, 1 / set.keys().next().value);
assertTrue(set.has(0));
assertTrue(set.has(-0));
},

testForEach() {
const set = new Set();
set.add('a');
Expand Down