From a31a62fcc85cedb5b090e3395a2e314d614a5a54 Mon Sep 17 00:00:00 2001 From: Leonardo Balter <leonardo.balter@gmail.com> Date: Mon, 29 Jun 2015 14:45:42 -0400 Subject: [PATCH] Map.property.forEach --- .../prototype/forEach/callback-parameters.js | 44 ++++++++++++++++ .../forEach/callback-result-is-abrupt.js | 28 ++++++++++ .../forEach/callback-this-non-strict.js | 35 +++++++++++++ .../prototype/forEach/callback-this-strict.js | 35 +++++++++++++ .../forEach/deleted-values-during-foreach.js | 41 +++++++++++++++ ...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 ++++++++++++ .../forEach/first-argument-is-not-callable.js | 43 ++++++++++++++++ .../Map/prototype/forEach/forEach.js | 22 ++++++++ .../iterates-in-key-insertion-order.js | 51 +++++++++++++++++++ ...rates-values-added-after-foreach-begins.js | 49 ++++++++++++++++++ .../iterates-values-deleted-then-readded.js | 50 ++++++++++++++++++ .../built-ins/Map/prototype/forEach/length.js | 22 ++++++++ test/built-ins/Map/prototype/forEach/name.js | 22 ++++++++ .../Map/prototype/forEach/return-undefined.js | 27 ++++++++++ .../second-parameter-as-callback-context.js | 37 ++++++++++++++ .../forEach/this-not-object-throw.js | 43 ++++++++++++++++ 18 files changed, 629 insertions(+) create mode 100644 test/built-ins/Map/prototype/forEach/callback-parameters.js create mode 100644 test/built-ins/Map/prototype/forEach/callback-result-is-abrupt.js create mode 100644 test/built-ins/Map/prototype/forEach/callback-this-non-strict.js create mode 100644 test/built-ins/Map/prototype/forEach/callback-this-strict.js create mode 100644 test/built-ins/Map/prototype/forEach/deleted-values-during-foreach.js create mode 100644 test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-set.js create mode 100644 test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-weakmap.js create mode 100644 test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot.js create mode 100644 test/built-ins/Map/prototype/forEach/first-argument-is-not-callable.js create mode 100644 test/built-ins/Map/prototype/forEach/forEach.js create mode 100644 test/built-ins/Map/prototype/forEach/iterates-in-key-insertion-order.js create mode 100644 test/built-ins/Map/prototype/forEach/iterates-values-added-after-foreach-begins.js create mode 100644 test/built-ins/Map/prototype/forEach/iterates-values-deleted-then-readded.js create mode 100644 test/built-ins/Map/prototype/forEach/length.js create mode 100644 test/built-ins/Map/prototype/forEach/name.js create mode 100644 test/built-ins/Map/prototype/forEach/return-undefined.js create mode 100644 test/built-ins/Map/prototype/forEach/second-parameter-as-callback-context.js create mode 100644 test/built-ins/Map/prototype/forEach/this-not-object-throw.js diff --git a/test/built-ins/Map/prototype/forEach/callback-parameters.js b/test/built-ins/Map/prototype/forEach/callback-parameters.js new file mode 100644 index 0000000000..0688762f8a --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/callback-parameters.js @@ -0,0 +1,44 @@ +// 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.5 +description: > + Verify the parameters order on the given callback. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ... +---*/ + +var map = new Map(); +map.set('foo', 42); +map.set('bar', 'baz'); + +var results = []; + +var callback = function(value, key, thisArg) { + results.push({ + value: value, + key: key, + thisArg: thisArg + }); +}; + +map.forEach(callback); + +assert.sameValue(results[0].value, 42); +assert.sameValue(results[0].key, 'foo'); +assert.sameValue(results[0].thisArg, map); + +assert.sameValue(results[1].value, 'baz'); +assert.sameValue(results[1].key, 'bar'); +assert.sameValue(results[1].thisArg, map); + +assert.sameValue(results.length, 2); diff --git a/test/built-ins/Map/prototype/forEach/callback-result-is-abrupt.js b/test/built-ins/Map/prototype/forEach/callback-result-is-abrupt.js new file mode 100644 index 0000000000..5b31a39bc3 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/callback-result-is-abrupt.js @@ -0,0 +1,28 @@ +// 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.5 +description: > + Returns error from callback result is abrupt. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ii. ReturnIfAbrupt(funcResult). + ... +flags: [noStrict] +---*/ + +var map = new Map([[0, 0]]); + +assert.throws(Test262Error, function() { + map.forEach(function() { + throw new Test262Error(); + }); +}); diff --git a/test/built-ins/Map/prototype/forEach/callback-this-non-strict.js b/test/built-ins/Map/prototype/forEach/callback-this-non-strict.js new file mode 100644 index 0000000000..d1190b4197 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/callback-this-non-strict.js @@ -0,0 +1,35 @@ +// 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.5 +description: > + If a thisArg is not provided, undefined will be used as the this value for + each invocation of callbackfn. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ... +flags: [noStrict] +---*/ + +var _this = []; +var map = new Map(); + +map.set(0, 0); +map.set(1, 1); +map.set(2, 2); + +map.forEach(function() { + _this.push(this); +}); + +assert.sameValue(_this[0], this); +assert.sameValue(_this[1], this); +assert.sameValue(_this[2], this); diff --git a/test/built-ins/Map/prototype/forEach/callback-this-strict.js b/test/built-ins/Map/prototype/forEach/callback-this-strict.js new file mode 100644 index 0000000000..a7f557211d --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/callback-this-strict.js @@ -0,0 +1,35 @@ +// 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.5 +description: > + If a thisArg is not provided, undefined will be used as the this value for + each invocation of callbackfn. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ... +flags: [onlyStrict] +---*/ + +var _this = []; +var map = new Map(); + +map.set(0, 0); +map.set(1, 1); +map.set(2, 2); + +map.forEach(function() { + _this.push(this); +}); + +assert.sameValue(_this[0], undefined); +assert.sameValue(_this[1], undefined); +assert.sameValue(_this[2], undefined); diff --git a/test/built-ins/Map/prototype/forEach/deleted-values-during-foreach.js b/test/built-ins/Map/prototype/forEach/deleted-values-during-foreach.js new file mode 100644 index 0000000000..51f70be6f7 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/deleted-values-during-foreach.js @@ -0,0 +1,41 @@ +// 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.5 +description: > + Map state with deleted values during forEach. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ii. ReturnIfAbrupt(funcResult). + ... +---*/ + +var map = new Map(); +map.set('foo', 0); +map.set('bar', 1); + +var count = 0; +var results = []; + +map.forEach(function(value, key) { + if (count === 0) { + map.delete('bar'); + } + results.push({ + value: value, + key: key + }); + count++; +}); + +assert.sameValue(results.length, 1); +assert.sameValue(results[0].key, 'foo'); +assert.sameValue(results[0].value, 0); diff --git a/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-set.js b/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-set.js new file mode 100644 index 0000000000..81c42614a9 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Throws a TypeError if `this` is a Set object. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 3. If M does not have a [[MapData]] internal slot, throw a TypeError + exception. + ... +features: [Set] +---*/ + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(new Set(), function() {}); +}); + +assert.throws(TypeError, function() { + var m = new Map(); + m.forEach.call(new Set(), function() {}); +}); diff --git a/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-weakmap.js b/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot-weakmap.js new file mode 100644 index 0000000000..551c29bc9a --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Throws a TypeError if `this` is a WeakMap object. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 3. If M does not have a [[MapData]] internal slot, throw a TypeError + exception. + ... +features: [WeakMap] +---*/ + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(new WeakMap(), function() {}); +}); + +assert.throws(TypeError, function() { + var m = new Map(); + m.forEach.call(new WeakMap(), function() {}); +}); diff --git a/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot.js b/test/built-ins/Map/prototype/forEach/does-not-have-mapdata-internal-slot.js new file mode 100644 index 0000000000..c5035291b1 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Throws a TypeError if `this` object does not have a [[MapData]] internal slot. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 3. If M does not have a [[MapData]] internal slot, throw a TypeError + exception. + ... +---*/ + +var m = new Map(); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call([], function() {}); +}); + +assert.throws(TypeError, function() { + m.forEach.call([], function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call({}, function() {}); +}); + +assert.throws(TypeError, function() { + m.forEach.call({}, function() {}); +}); diff --git a/test/built-ins/Map/prototype/forEach/first-argument-is-not-callable.js b/test/built-ins/Map/prototype/forEach/first-argument-is-not-callable.js new file mode 100644 index 0000000000..996d67b56b --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/first-argument-is-not-callable.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.5 +description: > + Throws a TypeError if first argument is not callable. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + 4. If IsCallable(callbackfn) is false, throw a TypeError exception. + ... +features: [Symbol] +---*/ + +var map = new Map(); + +assert.throws(TypeError, function() { + map.forEach({}); +}); + +assert.throws(TypeError, function() { + map.forEach([]); +}); + +assert.throws(TypeError, function() { + map.forEach(1); +}); + +assert.throws(TypeError, function() { + map.forEach(''); +}); + +assert.throws(TypeError, function() { + map.forEach(null); +}); + +assert.throws(TypeError, function() { + map.forEach(undefined); +}); + +assert.throws(TypeError, function() { + map.forEach(Symbol()); +}); diff --git a/test/built-ins/Map/prototype/forEach/forEach.js b/test/built-ins/Map/prototype/forEach/forEach.js new file mode 100644 index 0000000000..bf80ca900e --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/forEach.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.5 +description: > + Property type and descriptor. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + typeof Map.prototype.forEach, + 'function', + '`typeof Map.prototype.forEach` is `function`' +); + +verifyNotEnumerable(Map.prototype, 'forEach'); +verifyWritable(Map.prototype, 'forEach'); +verifyConfigurable(Map.prototype, 'forEach'); diff --git a/test/built-ins/Map/prototype/forEach/iterates-in-key-insertion-order.js b/test/built-ins/Map/prototype/forEach/iterates-in-key-insertion-order.js new file mode 100644 index 0000000000..36a27cc55f --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/iterates-in-key-insertion-order.js @@ -0,0 +1,51 @@ +// 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.5 +description: > + Repeats for each non-empty record, in original key insertion order. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ... +---*/ + +var map = new Map([ + ['foo', 'valid foo'], + ['bar', false], + ['baz', 'valid baz'] +]); +map.set(0, false); +map.set(1, false); +map.set(2, 'valid 2'); +map.delete(1); +map.delete('bar'); + +// Not setting a new key, just changing the value +map.set(0, 'valid 0'); + +var results = []; +var callback = function(value) { + results.push(value); +}; + +map.forEach(callback); + +assert.sameValue(results[0], 'valid foo'); +assert.sameValue(results[1], 'valid baz'); +assert.sameValue(results[2], 'valid 0'); +assert.sameValue(results[3], 'valid 2'); +assert.sameValue(results.length, 4); + +map.clear(); +results = []; + +map.forEach(callback); +assert.sameValue(results.length, 0); diff --git a/test/built-ins/Map/prototype/forEach/iterates-values-added-after-foreach-begins.js b/test/built-ins/Map/prototype/forEach/iterates-values-added-after-foreach-begins.js new file mode 100644 index 0000000000..e398400cc0 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/iterates-values-added-after-foreach-begins.js @@ -0,0 +1,49 @@ +// 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.5 +description: > + New keys are visited if created during forEach execution. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ii. ReturnIfAbrupt(funcResult). + ... +---*/ + +var map = new Map(); +map.set('foo', 0); +map.set('bar', 1); + +var count = 0; +var results = []; + +map.forEach(function(value, key) { + if (count === 0) { + map.set('baz', 2); + } + results.push({ + value: value, + key: key + }); + count++; +}); + +assert.sameValue(count, 3); +assert.sameValue(map.size, 3); + +assert.sameValue(results[0].key, 'foo'); +assert.sameValue(results[0].value, 0); + +assert.sameValue(results[1].key, 'bar'); +assert.sameValue(results[1].value, 1); + +assert.sameValue(results[2].key, 'baz'); +assert.sameValue(results[2].value, 2); diff --git a/test/built-ins/Map/prototype/forEach/iterates-values-deleted-then-readded.js b/test/built-ins/Map/prototype/forEach/iterates-values-deleted-then-readded.js new file mode 100644 index 0000000000..0aaaff5094 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/iterates-values-deleted-then-readded.js @@ -0,0 +1,50 @@ +// 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.5 +description: > + New keys are visited if created during forEach execution. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ii. ReturnIfAbrupt(funcResult). + ... +---*/ + +var map = new Map(); +map.set('foo', 0); +map.set('bar', 1); + +var count = 0; +var results = []; + +map.forEach(function(value, key) { + if (count === 0) { + map.delete('foo'); + map.set('foo', 'baz'); + } + results.push({ + value: value, + key: key + }); + count++; +}); + +assert.sameValue(count, 3); +assert.sameValue(map.size, 2); + +assert.sameValue(results[0].key, 'foo'); +assert.sameValue(results[0].value, 0); + +assert.sameValue(results[1].key, 'bar'); +assert.sameValue(results[1].value, 1); + +assert.sameValue(results[2].key, 'foo'); +assert.sameValue(results[2].value, 'baz'); diff --git a/test/built-ins/Map/prototype/forEach/length.js b/test/built-ins/Map/prototype/forEach/length.js new file mode 100644 index 0000000000..3145a46003 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Map.prototype.forEach.length value and descriptor. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Map.prototype.forEach.length, 1, + 'The value of `Map.prototype.forEach.length` is `1`' +); + +verifyNotEnumerable(Map.prototype.forEach, 'length'); +verifyNotWritable(Map.prototype.forEach, 'length'); +verifyConfigurable(Map.prototype.forEach, 'length'); diff --git a/test/built-ins/Map/prototype/forEach/name.js b/test/built-ins/Map/prototype/forEach/name.js new file mode 100644 index 0000000000..8b4efed913 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Map.prototype.forEach.name value and descriptor. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + 17 ECMAScript Standard Built-in Objects + +includes: [propertyHelper.js] +---*/ + +assert.sameValue( + Map.prototype.forEach.name, 'forEach', + 'The value of `Map.prototype.forEach.name` is `"forEach"`' +); + +verifyNotEnumerable(Map.prototype.forEach, 'name'); +verifyNotWritable(Map.prototype.forEach, 'name'); +verifyConfigurable(Map.prototype.forEach, 'name'); diff --git a/test/built-ins/Map/prototype/forEach/return-undefined.js b/test/built-ins/Map/prototype/forEach/return-undefined.js new file mode 100644 index 0000000000..67f443109c --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/return-undefined.js @@ -0,0 +1,27 @@ +// 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.5 +description: > + Returns undefined. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 8. Return undefined. +---*/ + +var map = new Map(); + +var result = map.forEach(function() { + return true; +}); + +assert.sameValue(result, undefined, 'Empty map#forEach returns undefined'); + +map.set(1, 1); +result = map.forEach(function() { + return true; +}); + +assert.sameValue(result, undefined, 'map#forEach returns undefined'); diff --git a/test/built-ins/Map/prototype/forEach/second-parameter-as-callback-context.js b/test/built-ins/Map/prototype/forEach/second-parameter-as-callback-context.js new file mode 100644 index 0000000000..4aec8ce230 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/second-parameter-as-callback-context.js @@ -0,0 +1,37 @@ +// 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.5 +description: > + If a thisArg parameter is provided, it will be used as the this value for each + invocation of callbackfn. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + ... + 5. If thisArg was supplied, let T be thisArg; else let T be undefined. + 6. Let entries be the List that is the value of M’s [[MapData]] internal slot. + 7. Repeat for each Record {[[key]], [[value]]} e that is an element of + entries, in original key insertion order + a. If e.[[key]] is not empty, then + i. Let funcResult be Call(callbackfn, T, «e.[[value]], e.[[key]], M»). + ... +---*/ + +var expectedThis = {}; +var _this = []; + +var map = new Map(); +map.set(0, 0); +map.set(1, 1); +map.set(2, 2); + +var callback = function() { + _this.push(this); +}; + +map.forEach(callback, expectedThis); + +assert.sameValue(_this[0], expectedThis); +assert.sameValue(_this[1], expectedThis); +assert.sameValue(_this[2], expectedThis); diff --git a/test/built-ins/Map/prototype/forEach/this-not-object-throw.js b/test/built-ins/Map/prototype/forEach/this-not-object-throw.js new file mode 100644 index 0000000000..94edf4ce02 --- /dev/null +++ b/test/built-ins/Map/prototype/forEach/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.5 +description: > + Throws a TypeError if `this` is not an Object. +info: > + Map.prototype.forEach ( callbackfn [ , thisArg ] ) + + 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.forEach.call(false, function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(1, function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call('', function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(undefined, function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(null, function() {}); +}); + +assert.throws(TypeError, function() { + Map.prototype.forEach.call(Symbol(), function() {}); +}); + +assert.throws(TypeError, function() { + var map = new Map(); + map.forEach.call(false, function() {}); +}); -- GitLab