summaryrefslogtreecommitdiffstats
path: root/js/src/jit-test/tests/cacheir/getter-setter-guards1.js
blob: 9ca64dd7da07b6133c26f8600f2308e063db88e1 (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
// Objects with same shape but different getter/setter.
function testOwnProp() {
  var count = 0;
  var objects = [{get x() { count += 1; }, set x(v) { count += 2; }},
                 {get x() { count += 3; }, set x(v) { count += 4; }},
                 {get x() { count += 5; }, set x(v) { count += 6; }}];
  assertEq(shapeOf(objects[0]), shapeOf(objects[1]));
  assertEq(shapeOf(objects[0]), shapeOf(objects[2]));
  for (var i = 0; i < 150; i++) {
    var o = objects[i % objects.length];
    o.x++;
  }
  assertEq(count, 1050);
}
testOwnProp();

// Ensure optimized TypedArray length properly deoptimizes when the getter is
// redefined.
function testTypedArrayLength() {
  var ta = new Int32Array(10);
  var lengthHolder = Object.getPrototypeOf(Int32Array.prototype);
  for (var i = 0; i < 150; i++) {
    assertEq(ta.length, i <= 100 ? 10 : (i <= 130 ? "foo" : "bar"));
    assertEq(ta.byteLength, 40);
    assertEq(ta.byteOffset, 0);
    if (i === 100) {
      var desc = Object.getOwnPropertyDescriptor(lengthHolder, "length");
      desc.get = () => "foo";
      Object.defineProperty(lengthHolder, "length", desc);
    }
    if (i === 130) {
      var desc = Object.getOwnPropertyDescriptor(lengthHolder, "length");
      desc.get = () => "bar";
      Object.defineProperty(lengthHolder, "length", desc);
    }
  }
}
testTypedArrayLength();

// Native accessors on the global object. Redefine a few times and ensure the
// right function is called. Use |useWindowProxy: false| to allow optimizing all
// native getters.
function testGlobalProp() {
  var g = newGlobal({useWindowProxy: false});
  g.evaluate("" + function test() {
    var arr = [Math, Object];
    var expected = 0;
    for (var i = 0; i < 150; i++) {
      assertEq(timesAccessed, i <= 100 ? i + 1 : (i > 130 ? Infinity : NaN));
      if (i === 100) {
        var desc = Object.getOwnPropertyDescriptor(this, "timesAccessed");
        desc.get = Math.abs;
        Object.defineProperty(this, "timesAccessed", desc);
      }
      if (i === 130) {
        var desc = Object.getOwnPropertyDescriptor(this, "timesAccessed");
        desc.get = Math.min;
        Object.defineProperty(this, "timesAccessed", desc);
      }
    }
  });
  g.evaluate("test()");
}
testGlobalProp();