summaryrefslogtreecommitdiffstats
path: root/js/src/tests/non262/expressions/short-circuit-compound-assignment-property-key-evaluation.js
blob: 2026aa25d194c27bb53d35865d7384852ce51982 (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
// Test that property keys are only evaluated once.

class PropertyKey {
  constructor(key) {
    this.key = key;
    this.count = 0;
  }

  toString() {
    this.count++;
    return this.key;
  }

  valueOf() {
    throw new Error("unexpected valueOf call");
  }
}

// AndAssignExpr
{
  let obj = {p: true};
  let pk = new PropertyKey("p");

  obj[pk] &&= false;

  assertEq(obj.p, false);
  assertEq(pk.count, 1);

  obj[pk] &&= true;

  assertEq(obj.p, false);
  assertEq(pk.count, 2);
}

// OrAssignExpr
{
  let obj = {p: false};
  let pk = new PropertyKey("p");

  obj[pk] ||= true;

  assertEq(obj.p, true);
  assertEq(pk.count, 1);

  obj[pk] ||= false;

  assertEq(obj.p, true);
  assertEq(pk.count, 2);
}

// CoalesceAssignExpr
{
  let obj = {p: null};
  let pk = new PropertyKey("p");

  obj[pk] ??= true;

  assertEq(obj.p, true);
  assertEq(pk.count, 1);

  obj[pk] ??= false;

  assertEq(obj.p, true);
  assertEq(pk.count, 2);
}

if (typeof reportCompare === "function")
  reportCompare(0, 0);