| 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 in non-strict mode (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 |
flags: [noStrict] |
| 24 |
---*/ |
| 25 |
|
| 26 |
var thisVals = []; |
| 27 |
var nextResult = { |
| 28 |
done: false, |
| 29 |
value: {} |
| 30 |
}; |
| 31 |
var nextNextResult = { |
| 32 |
done: false, |
| 33 |
value: {} |
| 34 |
}; |
| 35 |
var mapFn = function() { |
| 36 |
thisVals.push(this); |
| 37 |
}; |
| 38 |
var items = {}; |
| 39 |
var global = function() { |
| 40 |
return this; |
| 41 |
}(); |
| 42 |
|
| 43 |
items[Symbol.iterator] = function() { |
| 44 |
return { |
| 45 |
next: function() { |
| 46 |
var result = nextResult; |
| 47 |
nextResult = nextNextResult; |
| 48 |
nextNextResult = { |
| 49 |
done: true |
| 50 |
}; |
| 51 |
|
| 52 |
return result; |
| 53 |
} |
| 54 |
}; |
| 55 |
}; |
| 56 |
|
| 57 |
Array.from(items, mapFn); |
| 58 |
|
| 59 |
assert.sameValue(thisVals.length, 2); |
| 60 |
assert.sameValue(thisVals[0], global, 'First iteration `this` value'); |
| 61 |
assert.sameValue(thisVals[1], global, 'Second iteration `this` value'); |