| 1 |
// Copyright (C) 2018 Kevin Gibbons. All rights reserved. |
| 2 |
// This code is governed by the BSD license found in the LICENSE file. |
| 3 |
|
| 4 |
/*--- |
| 5 |
esid: sec-object.fromentries |
| 6 |
description: Evaluation order is iterator.next(), get '0', get '1', toPropertyKey, repeat. |
| 7 |
info: | |
| 8 |
Object.fromEntries ( iterable ) |
| 9 |
|
| 10 |
... |
| 11 |
4. Let stepsDefine be the algorithm steps defined in CreateDataPropertyOnObject Functions. |
| 12 |
5. Let adder be CreateBuiltinFunction(stepsDefine, « »). |
| 13 |
6. Return ? AddEntriesFromIterable(obj, iterable, adder). |
| 14 |
|
| 15 |
includes: [compareArray.js] |
| 16 |
features: [Symbol.iterator, Object.fromEntries] |
| 17 |
---*/ |
| 18 |
|
| 19 |
var effects = []; |
| 20 |
|
| 21 |
function makeEntry(label) { |
| 22 |
return { |
| 23 |
get '0'() { |
| 24 |
effects.push('access property "0" of ' + label + ' entry'); |
| 25 |
return { |
| 26 |
toString: function() { |
| 27 |
effects.push('toString of ' + label + ' key'); |
| 28 |
return label + ' key'; |
| 29 |
}, |
| 30 |
}; |
| 31 |
}, |
| 32 |
get '1'() { |
| 33 |
effects.push('access property "1" of ' + label + ' entry'); |
| 34 |
return label + ' value'; |
| 35 |
}, |
| 36 |
}; |
| 37 |
} |
| 38 |
|
| 39 |
var iterable = { |
| 40 |
[Symbol.iterator]: function() { |
| 41 |
effects.push('get Symbol.iterator'); |
| 42 |
var count = 0; |
| 43 |
return { |
| 44 |
next: function() { |
| 45 |
effects.push('next ' + count); |
| 46 |
if (count === 0) { |
| 47 |
++count; |
| 48 |
return { |
| 49 |
done: false, |
| 50 |
value: makeEntry('first', 'first key', 'first value'), |
| 51 |
}; |
| 52 |
} else if (count === 1) { |
| 53 |
++count; |
| 54 |
return { |
| 55 |
done: false, |
| 56 |
value: makeEntry('second', 'second key', 'second value'), |
| 57 |
}; |
| 58 |
} else { |
| 59 |
return { |
| 60 |
done: true, |
| 61 |
}; |
| 62 |
} |
| 63 |
}, |
| 64 |
}; |
| 65 |
}, |
| 66 |
}; |
| 67 |
|
| 68 |
var result = Object.fromEntries(iterable); |
| 69 |
assert.compareArray(effects, [ |
| 70 |
'get Symbol.iterator', |
| 71 |
'next 0', |
| 72 |
'access property "0" of first entry', |
| 73 |
'access property "1" of first entry', |
| 74 |
'toString of first key', |
| 75 |
'next 1', |
| 76 |
'access property "0" of second entry', |
| 77 |
'access property "1" of second entry', |
| 78 |
'toString of second key', |
| 79 |
'next 2', |
| 80 |
], 'Object.fromEntries evaluation order'); |
| 81 |
assert.sameValue(result['first key'], 'first value'); |
| 82 |
assert.sameValue(result['second key'], 'second value'); |
| 83 |
|
| 84 |
reportCompare(0, 0); |