array-constructor.js 5.76 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
// 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

// Test Array call with known Boolean.
(() => {
  function foo(x) { return Array(!!x); }

11
  %PrepareFunctionForOptimization(foo);
12 13 14 15 16 17 18 19 20 21 22
  assertEquals([true], foo(true));
  assertEquals([false], foo(false));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([true], foo(true));
  assertEquals([false], foo(false));
})();

// Test Array construct with known Boolean.
(() => {
  function foo(x) { return new Array(!!x); }

23
  %PrepareFunctionForOptimization(foo);
24 25 26 27 28 29 30 31 32 33 34
  assertEquals([true], foo(true));
  assertEquals([false], foo(false));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([true], foo(true));
  assertEquals([false], foo(false));
})();

// Test Array call with known String.
(() => {
  function foo(x) { return Array("" + x); }

35
  %PrepareFunctionForOptimization(foo);
36 37 38 39 40 41 42 43 44 45 46
  assertEquals(["a"], foo("a"));
  assertEquals(["b"], foo("b"));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(["a"], foo("a"));
  assertEquals(["b"], foo("b"));
})();

// Test Array construct with known String.
(() => {
  function foo(x) { return new Array("" + x); }

47
  %PrepareFunctionForOptimization(foo);
48 49 50 51 52 53 54 55 56 57 58
  assertEquals(["a"], foo("a"));
  assertEquals(["b"], foo("b"));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(["a"], foo("a"));
  assertEquals(["b"], foo("b"));
})();

// Test Array call with known fixed small integer.
(() => {
  function foo() { return Array(2); }

59
  %PrepareFunctionForOptimization(foo);
60 61 62 63 64 65 66 67 68 69
  assertEquals(2, foo().length);
  assertEquals(2, foo().length);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo().length);
})();

// Test Array construct with known fixed small integer.
(() => {
  function foo() { return new Array(2); }

70
  %PrepareFunctionForOptimization(foo);
71 72 73 74 75 76 77 78 79 80
  assertEquals(2, foo().length);
  assertEquals(2, foo().length);
  %OptimizeFunctionOnNextCall(foo);
  assertEquals(2, foo().length);
})();

// Test Array call with multiple parameters.
(() => {
  function foo(x, y, z) { return Array(x, y, z); }

81
  %PrepareFunctionForOptimization(foo);
82 83 84 85 86 87 88 89 90 91
  assertEquals([1, 2, 3], foo(1, 2, 3));
  assertEquals([1, 2, 3], foo(1, 2, 3));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([1, 2, 3], foo(1, 2, 3));
})();

// Test Array construct with multiple parameters.
(() => {
  function foo(x, y, z) { return new Array(x, y, z); }

92
  %PrepareFunctionForOptimization(foo);
93 94 95 96 97
  assertEquals([1, 2, 3], foo(1, 2, 3));
  assertEquals([1, 2, 3], foo(1, 2, 3));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([1, 2, 3], foo(1, 2, 3));
})();
98 99 100 101 102

// Test Array construct inside try-catch block.
(() => {
  function foo(x) { try { return new Array(x) } catch (e) { return e } }

103
  %PrepareFunctionForOptimization(foo);
104 105 106 107 108 109
  assertEquals([], foo(0));
  assertEquals([], foo(0));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([], foo(0));
  assertInstanceof(foo(-1), RangeError);
})();
110

111
// Packed
112 113
// Test non-extensible Array call with multiple parameters.
(() => {
114
  function foo(x, y, z, t) { return Object.preventExtensions(new Array(x, y, z, t)); }
115 116

  %PrepareFunctionForOptimization(foo);
117 118 119
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertFalse(Object.isExtensible(foo(1,2,3, 'a')));
120
  %OptimizeFunctionOnNextCall(foo);
121 122
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertFalse(Object.isExtensible(foo(1,2,3, 'a')));
123 124 125 126
})();

// Test sealed Array call with multiple parameters.
(() => {
127
  function foo(x, y, z, t) { return Object.seal(new Array(x, y, z, t)); }
128 129

  %PrepareFunctionForOptimization(foo);
130 131 132
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isSealed(foo(1,2,3, 'a')));
133
  %OptimizeFunctionOnNextCall(foo);
134 135
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isSealed(foo(1,2,3, 'a')));
136 137 138 139
})();

// Test frozen Array call with multiple parameters.
(() => {
140
  function foo(x, y, z, t) { return Object.freeze(new Array(x, y, z, t)); }
141 142

  %PrepareFunctionForOptimization(foo);
143 144 145
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isFrozen(foo(1,2,3, 'a')));
146
  %OptimizeFunctionOnNextCall(foo);
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
  assertEquals([1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isFrozen(foo(1,2,3, 'a')));
})();

// Holey
// Test non-extensible Array call with multiple parameters.
(() => {
  function foo(x, y, z, t) { return Object.preventExtensions([, x, y, z, t]); }

  %PrepareFunctionForOptimization(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertFalse(Object.isExtensible(foo(1,2,3, 'a')));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertFalse(Object.isExtensible(foo(1,2,3, 'a')));
})();

// Test sealed Array call with multiple parameters.
(() => {
  function foo(x, y, z, t) { return Object.seal([, x, y, z, t]); }

  %PrepareFunctionForOptimization(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isSealed(foo(1,2,3, 'a')));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isSealed(foo(1,2,3, 'a')));
})();

// Test frozen Array call with multiple parameters.
(() => {
  function foo(x, y, z, t) { return Object.freeze([, x, y, z, t]); }

  %PrepareFunctionForOptimization(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isFrozen(foo(1,2,3, 'a')));
  %OptimizeFunctionOnNextCall(foo);
  assertEquals([, 1, 2, 3, 'a'], foo(1, 2, 3, 'a'));
  assertTrue(Object.isFrozen(foo(1,2,3, 'a')));
189
})();