From 7ee11aae4e571e9101562d20a4ad4a6a119b5fba Mon Sep 17 00:00:00 2001
From: Leonardo Balter <leonardo.balter@gmail.com>
Date: Mon, 29 Jun 2015 17:59:54 -0400
Subject: [PATCH] Map.prototype.set

---
 .../append-new-values-normalizes-zero-key.js  | 25 ++++++++++
 .../set/append-new-values-return-map.js       | 30 ++++++++++++
 .../Map/prototype/set/append-new-values.js    | 47 +++++++++++++++++++
 ...does-not-have-mapdata-internal-slot-set.js | 24 ++++++++++
 ...-not-have-mapdata-internal-slot-weakmap.js | 24 ++++++++++
 .../does-not-have-mapdata-internal-slot.js    | 32 +++++++++++++
 test/built-ins/Map/prototype/set/length.js    | 22 +++++++++
 test/built-ins/Map/prototype/set/name.js      | 22 +++++++++
 .../replaces-a-value-normalizes-zero-key.js   | 26 ++++++++++
 .../set/replaces-a-value-returns-map.js       | 30 ++++++++++++
 .../Map/prototype/set/replaces-a-value.js     | 23 +++++++++
 test/built-ins/Map/prototype/set/set.js       | 22 +++++++++
 .../prototype/set/this-not-object-throw.js    | 43 +++++++++++++++++
 13 files changed, 370 insertions(+)
 create mode 100644 test/built-ins/Map/prototype/set/append-new-values-normalizes-zero-key.js
 create mode 100644 test/built-ins/Map/prototype/set/append-new-values-return-map.js
 create mode 100644 test/built-ins/Map/prototype/set/append-new-values.js
 create mode 100644 test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-set.js
 create mode 100644 test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-weakmap.js
 create mode 100644 test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot.js
 create mode 100644 test/built-ins/Map/prototype/set/length.js
 create mode 100644 test/built-ins/Map/prototype/set/name.js
 create mode 100644 test/built-ins/Map/prototype/set/replaces-a-value-normalizes-zero-key.js
 create mode 100644 test/built-ins/Map/prototype/set/replaces-a-value-returns-map.js
 create mode 100644 test/built-ins/Map/prototype/set/replaces-a-value.js
 create mode 100644 test/built-ins/Map/prototype/set/set.js
 create mode 100644 test/built-ins/Map/prototype/set/this-not-object-throw.js

diff --git a/test/built-ins/Map/prototype/set/append-new-values-normalizes-zero-key.js b/test/built-ins/Map/prototype/set/append-new-values-normalizes-zero-key.js
new file mode 100644
index 0000000000..8e3d46725a
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/append-new-values-normalizes-zero-key.js
@@ -0,0 +1,25 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Appends new value in the map normalizing +0 and -0.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  6. If key is −0, let key be +0.
+  7. Let p be the Record {[[key]]: key, [[value]]: value}.
+  8. Append p as the last element of entries.
+  9. Return M.
+  ...
+---*/
+
+var map = new Map();
+map.set(-0, 42);
+
+assert.sameValue(map.get(0), 42);
+
+map = new Map();
+map.set(+0, 43);
+assert.sameValue(map.get(0), 43);
diff --git a/test/built-ins/Map/prototype/set/append-new-values-return-map.js b/test/built-ins/Map/prototype/set/append-new-values-return-map.js
new file mode 100644
index 0000000000..440858b7c0
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/append-new-values-return-map.js
@@ -0,0 +1,30 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Map.prototype.set returns the given `this` object.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  6. If key is −0, let key be +0.
+  7. Let p be the Record {[[key]]: key, [[value]]: value}.
+  8. Append p as the last element of entries.
+  9. Return M.
+  ...
+---*/
+
+var map = new Map();
+var result = map.set(1, 1);
+
+assert.sameValue(result, map);
+
+result = map.set(1,1).set(2,2).set(3,3);
+
+assert.sameValue(result, map, 'Map.prototype.set is chainable');
+
+var map2 = new Map();
+result = map2.set.call(map, 4, 4);
+
+assert.sameValue(result, map);
diff --git a/test/built-ins/Map/prototype/set/append-new-values.js b/test/built-ins/Map/prototype/set/append-new-values.js
new file mode 100644
index 0000000000..2c15ffed71
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/append-new-values.js
@@ -0,0 +1,47 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Append a new value as the last element of entries.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  6. If key is −0, let key be +0.
+  7. Let p be the Record {[[key]]: key, [[value]]: value}.
+  8. Append p as the last element of entries.
+  9. Return M.
+  ...
+features: [Symbol]
+---*/
+
+var s = Symbol(2);
+var map = new Map([[4, 4], ['foo3', 3], [s, 2]]);
+
+map.set(null, 42);
+map.set(1, 'valid');
+
+assert.sameValue(map.size, 5);
+assert.sameValue(map.get(1), 'valid');
+
+var results = [];
+
+map.forEach(function(value, key) {
+  results.push({
+    value: value,
+    key: key
+  });
+});
+
+var result = results.pop();
+assert.sameValue(result.value, 'valid');
+assert.sameValue(result.key, 1);
+
+result = results.pop();
+assert.sameValue(result.value, 42);
+assert.sameValue(result.key, null);
+
+result = results.pop();
+assert.sameValue(result.value, 2);
+assert.sameValue(result.key, s);
diff --git a/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-set.js b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-set.js
new file mode 100644
index 0000000000..35e1946f90
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-set.js
@@ -0,0 +1,24 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Throws a TypeError if `this` is a Set object.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  3. If M does not have a [[MapData]] internal slot, throw a TypeError
+  exception.
+  ...
+features: [Set]
+---*/
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(new Set(), 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  var m = new Map();
+  m.set.call(new Set(), 1, 1);
+});
diff --git a/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-weakmap.js b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-weakmap.js
new file mode 100644
index 0000000000..8f01f626b2
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot-weakmap.js
@@ -0,0 +1,24 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Throws a TypeError if `this` is a WeakMap object.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  3. If M does not have a [[MapData]] internal slot, throw a TypeError
+  exception.
+  ...
+features: [WeakMap]
+---*/
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(new WeakMap(), 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  var m = new Map();
+  m.set.call(new WeakMap(), 1, 1);
+});
diff --git a/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot.js b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot.js
new file mode 100644
index 0000000000..d5106a7b86
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/does-not-have-mapdata-internal-slot.js
@@ -0,0 +1,32 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Throws a TypeError if `this` object does not have a [[MapData]] internal slot.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  3. If M does not have a [[MapData]] internal slot, throw a TypeError
+  exception.
+  ...
+---*/
+
+var m = new Map();
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call([], 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  m.set.call([], 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call({}, 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  m.set.call({}, 1, 1);
+});
diff --git a/test/built-ins/Map/prototype/set/length.js b/test/built-ins/Map/prototype/set/length.js
new file mode 100644
index 0000000000..c6bdc232bb
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/length.js
@@ -0,0 +1,22 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Map.prototype.set.length value and descriptor.
+info: >
+  Map.prototype.set ( key , value )
+
+  17 ECMAScript Standard Built-in Objects
+
+includes: [propertyHelper.js]
+---*/
+
+assert.sameValue(
+  Map.prototype.set.length, 2,
+  'The value of `Map.prototype.set.length` is `2`'
+);
+
+verifyNotEnumerable(Map.prototype.set, 'length');
+verifyNotWritable(Map.prototype.set, 'length');
+verifyConfigurable(Map.prototype.set, 'length');
diff --git a/test/built-ins/Map/prototype/set/name.js b/test/built-ins/Map/prototype/set/name.js
new file mode 100644
index 0000000000..263b2a2250
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/name.js
@@ -0,0 +1,22 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Map.prototype.set.name value and descriptor.
+info: >
+  Map.prototype.set ( key , value )
+
+  17 ECMAScript Standard Built-in Objects
+
+includes: [propertyHelper.js]
+---*/
+
+assert.sameValue(
+  Map.prototype.set.name, 'set',
+  'The value of `Map.prototype.set.name` is `"set"`'
+);
+
+verifyNotEnumerable(Map.prototype.set, 'name');
+verifyNotWritable(Map.prototype.set, 'name');
+verifyConfigurable(Map.prototype.set, 'name');
diff --git a/test/built-ins/Map/prototype/set/replaces-a-value-normalizes-zero-key.js b/test/built-ins/Map/prototype/set/replaces-a-value-normalizes-zero-key.js
new file mode 100644
index 0000000000..484e891a39
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/replaces-a-value-normalizes-zero-key.js
@@ -0,0 +1,26 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Replaces a value in the map normalizing +0 and -0.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  5. Repeat for each Record {[[key]], [[value]]} p that is an element of
+  entries,
+    a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
+      i. Set p.[[value]] to value.
+      ii. Return M.
+  ...
+---*/
+
+var map = new Map([[+0, 1]]);
+
+map.set(-0, 42);
+assert.sameValue(map.get(+0), 42, 'zero key is normalized in SameValueZero');
+
+map = new Map([[-0, 1]]);
+map.set(+0, 42);
+assert.sameValue(map.get(-0), 42, 'zero key is normalized in SameValueZero');
diff --git a/test/built-ins/Map/prototype/set/replaces-a-value-returns-map.js b/test/built-ins/Map/prototype/set/replaces-a-value-returns-map.js
new file mode 100644
index 0000000000..41f3b06963
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/replaces-a-value-returns-map.js
@@ -0,0 +1,30 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Map.prototype.set returns the given `this` map object.
+info: >
+  Map.prototype.set ( key , value )
+
+  1. Let M be the this value.
+  ...
+  5. Repeat for each Record {[[key]], [[value]]} p that is an element of
+  entries,
+    a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
+      i. Set p.[[value]] to value.
+      ii. Return M.
+  ...
+---*/
+
+var map = new Map([['item', 0]]);
+var map2 = new Map();
+
+var x = map.set('item', 42);
+assert.sameValue(x, map);
+
+x = Map.prototype.set.call(map, 'item', 0);
+assert.sameValue(x, map);
+
+x = map2.set.call(map, 'item', 0);
+assert.sameValue(x, map, 'Map#set returns the map `this` value');
diff --git a/test/built-ins/Map/prototype/set/replaces-a-value.js b/test/built-ins/Map/prototype/set/replaces-a-value.js
new file mode 100644
index 0000000000..f082ceca87
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/replaces-a-value.js
@@ -0,0 +1,23 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Replaces a value in the map.
+info: >
+  Map.prototype.set ( key , value )
+
+  ...
+  5. Repeat for each Record {[[key]], [[value]]} p that is an element of
+  entries,
+    a. If p.[[key]] is not empty and SameValueZero(p.[[key]], key) is true, then
+      i. Set p.[[value]] to value.
+      ii. Return M.
+  ...
+---*/
+
+var m = new Map([['item', 1]]);
+
+m.set('item', 42);
+assert.sameValue(m.get('item'), 42);
+assert.sameValue(m.size, 1);
diff --git a/test/built-ins/Map/prototype/set/set.js b/test/built-ins/Map/prototype/set/set.js
new file mode 100644
index 0000000000..36e0577d38
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/set.js
@@ -0,0 +1,22 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Property type and descriptor.
+info: >
+  Map.prototype.set ( key , value )
+
+  17 ECMAScript Standard Built-in Objects
+includes: [propertyHelper.js]
+---*/
+
+assert.sameValue(
+  typeof Map.prototype.set,
+  'function',
+  '`typeof Map.prototype.set` is `function`'
+);
+
+verifyNotEnumerable(Map.prototype, 'set');
+verifyWritable(Map.prototype, 'set');
+verifyConfigurable(Map.prototype, 'set');
diff --git a/test/built-ins/Map/prototype/set/this-not-object-throw.js b/test/built-ins/Map/prototype/set/this-not-object-throw.js
new file mode 100644
index 0000000000..0e0d4fea68
--- /dev/null
+++ b/test/built-ins/Map/prototype/set/this-not-object-throw.js
@@ -0,0 +1,43 @@
+// Copyright (C) 2015 the V8 project authors. All rights reserved.
+// This code is governed by the BSD license found in the LICENSE file.
+/*---
+es6id: 23.1.3.9
+description: >
+  Throws a TypeError if `this` is not an Object.
+info: >
+  Map.prototype.set ( key , value )
+
+  1. Let M be the this value.
+  2. If Type(M) is not Object, throw a TypeError exception.
+  ...
+features: [Symbol]
+---*/
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(false, 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(1, 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call('', 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(undefined, 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(null, 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  Map.prototype.set.call(Symbol(), 1, 1);
+});
+
+assert.throws(TypeError, function() {
+  var map = new Map();
+  map.set.call(false, 1, 1);
+});
-- 
GitLab