summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/fields/private-field-destructuring.js
blob: 82aa032a8ce05f7adf8e45abfd0e194c18884a11 (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
function assertThrows(fun, errorType) {
  try {
    fun();
    throw 'Expected error, but none was thrown';
  } catch (e) {
    if (!(e instanceof errorType)) {
      throw 'Wrong error type thrown';
    }
  }
}

class A {
  #a;
  #b;
  #c;
  #d;
  #e;
  static destructure(o, x) {
    [o.#a, o.#b, o.#c, o.#d, ...o.#e] = x;
  }

  static get(o) {
    return {a: o.#a, b: o.#b, c: o.#c, d: o.#d, e: o.#e};
  }
};

for (var i = 0; i < 1000; i++) {
  var a = new A();
  A.destructure(a, [1, 2, 3, 4, 5]);
  var res = A.get(a);
  assertEq(res.a, 1);
  assertEq(res.b, 2);
  assertEq(res.c, 3);
  assertEq(res.d, 4);
  assertEq(res.e.length, 1);
  assertEq(res.e[0], 5);

  var obj = {};
  assertThrows(() => A.destructure(obj, [1, 2, 3, 4, 5]), TypeError);
  assertThrows(() => A.get(obj), TypeError);
}