1// Copyright 2015 the V8 project authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5 // Flags: --allow-natives-syntax --noverify-heap --noenable-slow-asserts
6
7 // --noverify-heap and --noenable-slow-asserts are set because the test is too
8 // slow with it on.
9
10 // Ensure that keyed stores work, and optimized functions learn if the
11 // store required change to dictionary mode. Verify that stores that grow
12 // the array into large object space don't cause a deopt.
13(function() {
14  var a = [];
15
16  function foo(a, i) {
17    a[i] = 5.3;
18  }
19
20  foo(a, 1);
21  foo(a, 2);
22  foo(a, 3);
23  %OptimizeFunctionOnNextCall(foo);
24  a[3] = 0;
25  foo(a, 3);
26  assertEquals(a[3], 5.3);
27  foo(a, 50000);
28  assertUnoptimized(foo);
29  assertTrue(%HasDictionaryElements(a));
30
31  var b = [];
32  foo(b, 1);
33  foo(b, 2);
34  // Put b in dictionary mode.
35  b[10000] = 5;
36  assertTrue(%HasDictionaryElements(b));
37  foo(b, 3);
38  %OptimizeFunctionOnNextCall(foo);
39  foo(b, 50000);
40  assertOptimized(foo);
41  assertTrue(%HasDictionaryElements(b));
42
43  // Clearing feedback for the StoreIC in foo is important for runs with
44  // flag --stress-opt.
45  %ClearFunctionTypeFeedback(foo);
46})();
47
48
49(function() {
50  var a = new Array(10);
51
52  function foo2(a, i) {
53    a[i] = 50;
54  }
55
56  // The KeyedStoreIC will learn GROW_MODE.
57  foo2(a, 10);
58  foo2(a, 12);
59  foo2(a, 31);
60  %OptimizeFunctionOnNextCall(foo2);
61  foo2(a, 40);
62
63  // This test is way too slow without crankshaft.
64  if (4 != %GetOptimizationStatus(foo2)) {
65    assertOptimized(foo2);
66    assertTrue(%HasFastSmiElements(a));
67
68    // Grow a large array into large object space through the keyed store
69    // without deoptimizing. Grow by 10s. If we set elements too sparsely, the
70    // array will convert to dictionary mode.
71    a = new Array(99999);
72    assertTrue(%HasFastSmiElements(a));
73    for (var i = 0; i < 263000; i += 10) {
74      foo2(a, i);
75    }
76
77    // Verify that we are over 1 page in size, and foo2 remains optimized.
78    // This means we've smoothly transitioned to allocating in large object
79    // space.
80    assertTrue(%HasFastSmiElements(a));
81    assertTrue(a.length * 4 > (1024 * 1024));
82    assertOptimized(foo2);
83  }
84
85  %ClearFunctionTypeFeedback(foo2);
86})();
87