iter-map-fn-this-arg.js (1582B)
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 description: > 6 `this` value of mapping function with custom `this` argument (traversed via iterator) 7 info: | 8 [...] 9 2. If mapfn is undefined, let mapping be false. 10 3. else 11 a. If IsCallable(mapfn) is false, throw a TypeError exception. 12 b. If thisArg was supplied, let T be thisArg; else let T be undefined. 13 c. Let mapping be true 14 [...] 15 6. If usingIterator is not undefined, then 16 [...] 17 g. Repeat 18 [...] 19 vii. If mapping is true, then 20 1. Let mappedValue be Call(mapfn, T, «nextValue, k»). 21 features: [Symbol.iterator] 22 ---*/ 23 24 var thisVals = []; 25 var nextResult = { 26 done: false, 27 value: {} 28 }; 29 var nextNextResult = { 30 done: false, 31 value: {} 32 }; 33 var mapFn = function() { 34 thisVals.push(this); 35 }; 36 var items = {}; 37 var thisVal = {}; 38 39 items[Symbol.iterator] = function() { 40 return { 41 next: function() { 42 var result = nextResult; 43 nextResult = nextNextResult; 44 nextNextResult = { 45 done: true 46 }; 47 48 return result; 49 } 50 }; 51 }; 52 53 Array.from(items, mapFn, thisVal); 54 55 assert.sameValue(thisVals.length, 2, 'The value of thisVals.length is expected to be 2'); 56 assert.sameValue(thisVals[0], thisVal, 'The value of thisVals[0] is expected to equal the value of thisVal'); 57 assert.sameValue(thisVals[1], thisVal, 'The value of thisVals[1] is expected to equal the value of thisVal'); 58 59 reportCompare(0, 0);