| 1 |
// Copyright (C) 2015 the V8 project authors. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
/*--- |
| 4 |
esid: sec-array.from |
| 5 |
es6id: 22.1.2.1 |
| 6 |
description: > |
| 7 |
`this` value of mapping function with custom `this` argument (traversed via iterator) |
| 8 |
info: | |
| 9 |
[...] |
| 10 |
2. If mapfn is undefined, let mapping be false. |
| 11 |
3. else |
| 12 |
a. If IsCallable(mapfn) is false, throw a TypeError exception. |
| 13 |
b. If thisArg was supplied, let T be thisArg; else let T be undefined. |
| 14 |
c. Let mapping be true |
| 15 |
[...] |
| 16 |
6. If usingIterator is not undefined, then |
| 17 |
[...] |
| 18 |
g. Repeat |
| 19 |
[...] |
| 20 |
vii. If mapping is true, then |
| 21 |
1. Let mappedValue be Call(mapfn, T, «nextValue, k»). |
| 22 |
features: [Symbol.iterator] |
| 23 |
---*/ |
| 24 |
|
| 25 |
var thisVals = []; |
| 26 |
var nextResult = { |
| 27 |
done: false, |
| 28 |
value: {} |
| 29 |
}; |
| 30 |
var nextNextResult = { |
| 31 |
done: false, |
| 32 |
value: {} |
| 33 |
}; |
| 34 |
var mapFn = function() { |
| 35 |
thisVals.push(this); |
| 36 |
}; |
| 37 |
var items = {}; |
| 38 |
var thisVal = {}; |
| 39 |
|
| 40 |
items[Symbol.iterator] = function() { |
| 41 |
return { |
| 42 |
next: function() { |
| 43 |
var result = nextResult; |
| 44 |
nextResult = nextNextResult; |
| 45 |
nextNextResult = { |
| 46 |
done: true |
| 47 |
}; |
| 48 |
|
| 49 |
return result; |
| 50 |
} |
| 51 |
}; |
| 52 |
}; |
| 53 |
|
| 54 |
Array.from(items, mapFn, thisVal); |
| 55 |
|
| 56 |
assert.sameValue(thisVals.length, 2); |
| 57 |
assert.sameValue(thisVals[0], thisVal, 'First iteration `this` value'); |
| 58 |
assert.sameValue(thisVals[1], thisVal, 'Second iteration `this` value'); |