summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Object/fromEntries/evaluation-order.js
blob: 30677d6897df5e6fd4988f208d890ae552e28704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Copyright (C) 2018 Kevin Gibbons. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-object.fromentries
description: Evaluation order is iterator.next(), get '0', get '1', toPropertyKey, repeat.
info: |
  Object.fromEntries ( iterable )

  ...
  4. Let stepsDefine be the algorithm steps defined in CreateDataPropertyOnObject Functions.
  5. Let adder be CreateBuiltinFunction(stepsDefine, « »).
  6. Return ? AddEntriesFromIterable(obj, iterable, adder).

includes: [compareArray.js]
features: [Symbol.iterator, Object.fromEntries]
---*/

var effects = [];

function makeEntry(label) {
  return {
    get '0'() {
      effects.push('access property "0" of ' + label + ' entry');
      return {
        toString: function() {
          effects.push('toString of ' + label + ' key');
          return label + ' key';
        },
      };
    },
    get '1'() {
      effects.push('access property "1" of ' + label + ' entry');
      return label + ' value';
    },
  };
}

var iterable = {
  [Symbol.iterator]: function() {
    effects.push('get Symbol.iterator');
    var count = 0;
    return {
      next: function() {
        effects.push('next ' + count);
        if (count === 0) {
          ++count;
          return {
            done: false,
            value: makeEntry('first', 'first key', 'first value'),
          };
        } else if (count === 1) {
          ++count;
          return {
            done: false,
            value: makeEntry('second', 'second key', 'second value'),
          };
        } else {
          return {
            done: true,
          };
        }
      },
    };
  },
};

var result = Object.fromEntries(iterable);
assert.compareArray(effects, [
  'get Symbol.iterator',
  'next 0',
  'access property "0" of first entry',
  'access property "1" of first entry',
  'toString of first key',
  'next 1',
  'access property "0" of second entry',
  'access property "1" of second entry',
  'toString of second key',
  'next 2',
], 'Object.fromEntries evaluation order');
assert.sameValue(result['first key'], 'first value');
assert.sameValue(result['second key'], 'second value');

reportCompare(0, 0);