| 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 |
Arguments of mapping function (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 |
2. If mappedValue is an abrupt completion, return |
| 23 |
IteratorClose(iterator, mappedValue). |
| 24 |
3. Let mappedValue be mappedValue.[[value]]. |
| 25 |
features: [Symbol.iterator] |
| 26 |
---*/ |
| 27 |
|
| 28 |
var args = []; |
| 29 |
var firstResult = { |
| 30 |
done: false, |
| 31 |
value: {} |
| 32 |
}; |
| 33 |
var secondResult = { |
| 34 |
done: false, |
| 35 |
value: {} |
| 36 |
}; |
| 37 |
var mapFn = function(value, idx) { |
| 38 |
args.push(arguments); |
| 39 |
}; |
| 40 |
var items = {}; |
| 41 |
var nextResult = firstResult; |
| 42 |
var nextNextResult = secondResult; |
| 43 |
|
| 44 |
items[Symbol.iterator] = function() { |
| 45 |
return { |
| 46 |
next: function() { |
| 47 |
var result = nextResult; |
| 48 |
nextResult = nextNextResult; |
| 49 |
nextNextResult = { |
| 50 |
done: true |
| 51 |
}; |
| 52 |
|
| 53 |
return result; |
| 54 |
} |
| 55 |
}; |
| 56 |
}; |
| 57 |
|
| 58 |
Array.from(items, mapFn); |
| 59 |
|
| 60 |
assert.sameValue(args.length, 2, 'Iteration count'); |
| 61 |
|
| 62 |
assert.sameValue(args[0].length, 2, 'First iteration: arguments length'); |
| 63 |
assert.sameValue( |
| 64 |
args[0][0], firstResult.value, 'First iteration: first argument' |
| 65 |
); |
| 66 |
assert.sameValue(args[0][1], 0, 'First iteration: second argument'); |
| 67 |
|
| 68 |
assert.sameValue(args[1].length, 2, 'Second iteration: arguments length'); |
| 69 |
assert.sameValue( |
| 70 |
args[1][0], secondResult.value, 'Second iteration: first argument' |
| 71 |
); |
| 72 |
assert.sameValue(args[1][1], 1, 'Second iteration: second argument'); |