generated-transition-stub.js 5.08 KB
Newer Older
1
// Copyright 2013 the V8 project authors. All rights reserved.
2 3
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
4

5
// Flags: --allow-natives-syntax
6

7 8
%NeverOptimizeFunction(test);
function test() {
9

10
  const iteration_count = 1;
11

12 13 14
  function transition1(a, i, v) {
    a[i] = v;
  }
15

16 17 18 19
  //
  // Test PACKED SMI -> PACKED DOUBLE
  //

20
  const a1 = [0, 1, 2, 3, 4];
21
  transition1(a1, 0, 2.5);
22
  const a2 = [0, 1, 2, 3, 4];
23
  transition1(a2, 0, 2.5);
24
  assertFalse(%HasHoleyElements(a2));
25 26
  %OptimizeFunctionOnNextCall(transition1);

27
  const a3 = [0, 1, 2, 3, 4];
28
  assertTrue(%HasSmiElements(a3));
29
  transition1(a3, 0, 2.5);
30
  assertFalse(%HasHoleyElements(a3));
31 32 33 34
  assertEquals(4, a3[4]);
  assertEquals(2.5, a3[0]);

  // Test handling of hole.
35
  const a4 = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
36
  a4.length = 7;
37
  assertTrue(%HasSmiElements(a4));
38
  transition1(a4, 0, 2.5);
39
  assertFalse(%HasHoleyElements(a4));
40 41 42 43 44
  assertEquals(2.5, a4[0]);
  assertEquals(undefined, a4[8]);

  // Large array should deopt to runtimea
  for (j = 0; j < iteration_count; ++j) {
45
    const a5 = new Array();
46 47 48
    for (i = 0; i < 0x40000; ++i) {
      a5[i] = 0;
    }
49
    assertTrue(%HasSmiElements(a5) || %HasDoubleElements(a5));
50 51
    transition1(a5, 0, 2.5);
    assertEquals(2.5, a5[0]);
52 53
  }

54 55 56
  //
  // Test HOLEY SMI -> HOLEY DOUBLE
  //
57

58 59 60
  function transition2(a, i, v) {
    a[i] = v;
  }
61

62
  const b1 = [0, 1, 2, , 4];
63
  transition2(b1, 0, 2.5);
64
  const b2 = [0, 1, 2, , 4];
65
  transition2(b2, 0, 2.5);
66
  assertTrue(%HasHoleyElements(b2));
67 68
  %OptimizeFunctionOnNextCall(transition2);

69
  const b3 = [0, 1, 2, , 4];
70 71
  assertTrue(%HasSmiElements(b3));
  assertTrue(%HasHoleyElements(b3));
72
  transition2(b3, 0, 2.5);
73
  assertTrue(%HasHoleyElements(b3));
74 75 76 77 78
  assertEquals(4, b3[4]);
  assertEquals(2.5, b3[0]);

  // Large array should deopt to runtime
  for (j = 0; j < iteration_count; ++j) {
79
    const b4 = [0, ,0];
80 81 82
    for (i = 3; i < 0x40000; ++i) {
      b4[i] = 0;
    }
83
    assertTrue(%HasSmiElements(b4));
84 85
    transition2(b4, 0, 2.5);
    assertEquals(2.5, b4[0]);
86 87
  }

88 89 90
  //
  // Test PACKED DOUBLE -> PACKED OBJECT
  //
91

92 93 94
  function transition3(a, i, v) {
    a[i] = v;
  }
95

96
  const c1 = [0, 1, 2, 3.5, 4];
97
  transition3(c1, 0, new Object());
98
  const c2 = [0, 1, 2, 3.5, 4];
99
  transition3(c2, 0, new Object());
100 101
  assertTrue(%HasObjectElements(c2));
  assertTrue(!%HasHoleyElements(c2));
102 103
  %OptimizeFunctionOnNextCall(transition3);

104
  const c3 = [0, 1, 2, 3.5, 4];
105 106
  assertTrue(%HasDoubleElements(c3));
  assertTrue(!%HasHoleyElements(c3));
107
  transition3(c3, 0, new Array());
108 109
  assertTrue(!%HasHoleyElements(c3));
  assertTrue(%HasObjectElements(c3));
110 111 112 113 114 115
  assertEquals(4, c3[4]);
  assertEquals(0, c3[0].length);

  // Large array under the deopt threshold should be able to trigger GC without
  // causing crashes.
  for (j = 0; j < iteration_count; ++j) {
116
    const c4 = [0, 2.5, 0];
117 118 119
    for (i = 3; i < 0xa000; ++i) {
      c4[i] = 0;
    }
120 121
    assertTrue(%HasDoubleElements(c4));
    assertTrue(!%HasHoleyElements(c4));
122
    transition3(c4, 0, new Array(5));
123 124
    assertTrue(!%HasHoleyElements(c4));
    assertTrue(%HasObjectElements(c4));
125
    assertEquals(5, c4[0].length);
126 127
  }

128 129
  // Large array should deopt to runtime
  for (j = 0; j < iteration_count; ++j) {
130
    const c5 = [0, 2.5, 0];
131 132 133
    for (i = 3; i < 0x40000; ++i) {
      c5[i] = 0;
    }
134 135
    assertTrue(%HasDoubleElements(c5));
    assertTrue(!%HasHoleyElements(c5));
136
    transition3(c5, 0, new Array(5));
137 138
    assertTrue(!%HasHoleyElements(c5));
    assertTrue(%HasObjectElements(c5));
139
    assertEquals(5, c5[0].length);
140 141
  }

142 143 144
  //
  // Test HOLEY DOUBLE -> HOLEY OBJECT
  //
145

146 147 148
  function transition4(a, i, v) {
      a[i] = v;
  }
149

150
  const d1 = [0, 1, , 3.5, 4];
151
  transition4(d1, 0, new Object());
152
  const d2 = [0, 1, , 3.5, 4];
153
  transition4(d2, 0, new Object());
154 155
  assertTrue(%HasObjectElements(d2));
  assertTrue(%HasHoleyElements(d2));
156 157
  %OptimizeFunctionOnNextCall(transition4);

158
  const d3 = [0, 1, , 3.5, 4];
159 160
  assertTrue(%HasDoubleElements(d3));
  assertTrue(%HasHoleyElements(d3));
161
  transition4(d3, 0, new Array());
162 163
  assertTrue(%HasHoleyElements(d3));
  assertTrue(%HasObjectElements(d3));
164 165 166 167 168 169
  assertEquals(4, d3[4]);
  assertEquals(0, d3[0].length);

  // Large array under the deopt threshold should be able to trigger GC without
  // causing crashes.
  for (j = 0; j < iteration_count; ++j) {
170
    const d4 = [, 2.5, ,];
171 172 173
    for (i = 3; i < 0xa000; ++i) {
      d4[i] = 0;
    }
174 175
    assertTrue(%HasDoubleElements(d4));
    assertTrue(%HasHoleyElements(d4));
176
    transition4(d4, 0, new Array(5));
177 178
    assertTrue(%HasHoleyElements(d4));
    assertTrue(%HasObjectElements(d4));
179 180
    assertEquals(5, d4[0].length);
    assertEquals(undefined, d4[2]);
181 182
  }

183 184
  // Large array should deopt to runtime
  for (j = 0; j < iteration_count; ++j) {
185
    const d5 = [, 2.5, ,];
186 187 188
    for (i = 3; i < 0x40000; ++i) {
      d5[i] = 0;
    }
189 190
    assertTrue(%HasDoubleElements(d5));
    assertTrue(%HasHoleyElements(d5));
191
    transition4(d5, 0, new Array(5));
192 193
    assertTrue(%HasHoleyElements(d5));
    assertTrue(%HasObjectElements(d5));
194 195
    assertEquals(5, d5[0].length);
    assertEquals(undefined, d5[2]);
196
  }
197

198
}
199
test();