constant-fold-cow-array.js 3.02 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Flags: --allow-natives-syntax --no-always-opt --opt

// Check that we properly deoptimize TurboFan'ed code when we constant-fold
// elements from a COW array and we change the length of the array.
(function() {
  const a = [1, 2, 3];
  const foo = () => a[0];
12
  %PrepareFunctionForOptimization(foo);
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a.length = 1;
  assertEquals(1, foo());
  assertUnoptimized(foo);
})();

// Check that we properly deoptimize TurboFan'ed code when we constant-fold
// elements from a COW array and we change the element of the array.
(function() {
  const a = [1, 2, 3];
  const foo = () => a[0];
28
  %PrepareFunctionForOptimization(foo);
29 30 31 32 33 34 35 36 37
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(42, foo());
  assertUnoptimized(foo);
})();
38

39
// Packed
40 41
// Non-extensible
(function() {
42
  const a = Object.preventExtensions([1, 2, '3']);
43 44 45 46 47 48 49 50 51 52 53 54 55
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(42, foo());
})();

// Sealed
(function() {
56
  const a = Object.seal([1, 2, '3']);
57 58 59 60 61 62 63 64 65 66 67 68 69
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(42, foo());
})();

// Frozen
(function() {
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  const a = Object.freeze([1, 2, '3']);
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(1, foo());
})();

// Holey
// Non-extensible
(function() {
  const a = Object.preventExtensions([1, 2, , '3']);
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(42, foo());
})();

// Sealed
(function() {
  const a = Object.seal([1, 2, , '3']);
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(42, foo());
})();

// Frozen
(function() {
  const a = Object.freeze([1, 2, , '3']);
114 115 116 117 118 119 120 121 122 123
  const foo = () => a[0];
  %PrepareFunctionForOptimization(foo);
  assertEquals(1, foo());
  assertEquals(1, foo());
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(1, foo());
  assertOptimized(foo);
  a[0] = 42;
  assertEquals(1, foo());
})();