summaryrefslogtreecommitdiffstats
path: root/js/src/tests/test262/built-ins/Array/fromAsync/this-constructor-operations.js
blob: ee968906778fedd6467697d23dd3807deb30e085 (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
// |reftest| async
// Copyright (C) 2023 Igalia, S.L. All rights reserved.
// This code is governed by the BSD license found in the LICENSE file.

/*---
esid: sec-array.fromasync
description: >
  Order of user-observable operations on a custom this-value and its instances
includes: [compareArray.js, asyncHelpers.js]
flags: [async]
features: [Array.fromAsync]
---*/

function formatPropertyName(propertyKey, objectName = "") {
  switch (typeof propertyKey) {
    case "symbol":
      if (Symbol.keyFor(propertyKey) !== undefined) {
        return `${objectName}[Symbol.for('${Symbol.keyFor(propertyKey)}')]`;
      } else if (propertyKey.description.startsWith('Symbol.')) {
        return `${objectName}[${propertyKey.description}]`;
      } else {
        return `${objectName}[Symbol('${propertyKey.description}')]`
      }
    case "string":
      if (propertyKey !== String(Number(propertyKey)))
        return objectName ? `${objectName}.${propertyKey}` : propertyKey;
      // fall through
    default:
      // integer or string integer-index
      return `${objectName}[${propertyKey}]`;
  }
}

asyncTest(async function () {
  const expectedCalls = [
    "construct MyArray",
    "defineProperty A[0]",
    "defineProperty A[1]",
    "set A.length"
  ];
  const actualCalls = [];

  function MyArray(...args) {
    actualCalls.push("construct MyArray");
    return new Proxy(Object.create(null), {
      set(target, key, value) {
        actualCalls.push(`set ${formatPropertyName(key, "A")}`);
        return Reflect.set(target, key, value);
      },
      defineProperty(target, key, descriptor) {
        actualCalls.push(`defineProperty ${formatPropertyName(key, "A")}`);
        return Reflect.defineProperty(target, key, descriptor);
      }
    });
  }

  let result = await Array.fromAsync.call(MyArray, [1, 2]);
  assert.compareArray(expectedCalls, actualCalls, "order of operations for array argument");

  actualCalls.splice(0);  // reset

  const expectedCallsForArrayLike = [
    "construct MyArray",
    "defineProperty A[0]",
    "defineProperty A[1]",
    "set A.length"
  ];
  result = await Array.fromAsync.call(MyArray, {
    length: 2,
    0: 1,
    1: 2
  });
  assert.compareArray(expectedCallsForArrayLike, actualCalls, "order of operations for array-like argument");
});