From 8d189e84b47b99c63177407df14648e80a3c6982 Mon Sep 17 00:00:00 2001 From: "arv@chromium.org" Date: Tue, 19 Aug 2014 15:15:41 +0000 Subject: [PATCH] ES6: Make sure we do not store -0 as the key in Map/Set BUG=v8:3515 LOG=Y R=adamk@chromium.org, dslomov@chromium.org Review URL: https://codereview.chromium.org/478683002 git-svn-id: https://v8.googlecode.com/svn/branches/bleeding_edge@23204 ce2b1a6d-e550-0410-aec6-3dcde31c8c00 --- src/collection.js | 14 ++++++++++++++ test/mjsunit/es6/collections.js | 23 +++++++++++++---------- 2 files changed, 27 insertions(+), 10 deletions(-) diff --git a/src/collection.js b/src/collection.js index 20887dd..0027bd7 100644 --- a/src/collection.js +++ b/src/collection.js @@ -49,6 +49,13 @@ function SetAddJS(key) { throw MakeTypeError('incompatible_method_receiver', ['Set.prototype.add', this]); } + // Normalize -0 to +0 as required by the spec. + // Even though we use SameValueZero as the comparison for the keys we don't + // want to ever store -0 as the key since the key is directly exposed when + // doing iteration. + if (key === 0) { + key = 0; + } return %SetAdd(this, key); } @@ -186,6 +193,13 @@ function MapSetJS(key, value) { throw MakeTypeError('incompatible_method_receiver', ['Map.prototype.set', this]); } + // Normalize -0 to +0 as required by the spec. + // Even though we use SameValueZero as the comparison for the keys we don't + // want to ever store -0 as the key since the key is directly exposed when + // doing iteration. + if (key === 0) { + key = 0; + } return %MapSet(this, key, value); } diff --git a/test/mjsunit/es6/collections.js b/test/mjsunit/es6/collections.js index 911b748..940c0b9 100644 --- a/test/mjsunit/es6/collections.js +++ b/test/mjsunit/es6/collections.js @@ -117,7 +117,8 @@ function TestMapBehavior2(m) { TestMapping(m, i / 10, new Object); TestMapping(m, 'key-' + i, new Object); } - var keys = [ +0, -0, +Infinity, -Infinity, true, false, null, undefined ]; + // -0 is handled in TestMinusZeroMap + var keys = [ 0, +Infinity, -Infinity, true, false, null, undefined ]; for (var i = 0; i < keys.length; i++) { TestMapping(m, keys[i], new Object); } @@ -495,24 +496,26 @@ for (var i = 9; i >= 0; i--) { (function TestMinusZeroSet() { - var m = new Set(); - m.add(0); - m.add(-0); - assertEquals(1, m.size); - assertTrue(m.has(0)); - assertTrue(m.has(-0)); + var s = new Set(); + s.add(-0); + assertSame(0, s.values().next().value); + s.add(0); + assertEquals(1, s.size); + assertTrue(s.has(0)); + assertTrue(s.has(-0)); })(); (function TestMinusZeroMap() { var m = new Map(); - m.set(0, 'plus'); m.set(-0, 'minus'); + assertSame(0, m.keys().next().value); + m.set(0, 'plus'); assertEquals(1, m.size); assertTrue(m.has(0)); assertTrue(m.has(-0)); - assertEquals('minus', m.get(0)); - assertEquals('minus', m.get(-0)); + assertEquals('plus', m.get(0)); + assertEquals('plus', m.get(-0)); })(); -- 2.7.4