array-natives-elements.js 10.2 KB
Newer Older
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
// Copyright 2012 the V8 project authors. All rights reserved.
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above
//       copyright notice, this list of conditions and the following
//       disclaimer in the documentation and/or other materials provided
//       with the distribution.
//     * Neither the name of Google Inc. nor the names of its
//       contributors may be used to endorse or promote products derived
//       from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

28
// Flags: --allow-natives-syntax
29 30 31 32

// IC and Crankshaft support for smi-only elements in dynamic array literals.
function get(foo) { return foo; }  // Used to generate dynamic values.

33
var __sequence = 0;
34
function array_natives_test() {
35 36

  // Ensure small array literals start in specific element kind mode.
37
  assertTrue(%HasFastSmiElements([]));
38 39 40 41 42
  assertTrue(%HasFastSmiElements([1]));
  assertTrue(%HasFastSmiElements([1,2]));
  assertTrue(%HasFastDoubleElements([1.1]));
  assertTrue(%HasFastDoubleElements([1.1,2]));

43 44 45 46 47 48 49 50
  // This code exists to eliminate the learning influence of AllocationSites
  // on the following tests.
  function make_array_string(literal) {
    this.__sequence = this.__sequence + 1;
    return "/* " + this.__sequence + " */  " + literal;
  }
  function make_array(literal) {
    return eval(make_array_string(literal));
51
  }
52 53 54 55

  // Push
  var a0 = make_array("[1, 2, 3]");
  assertTrue(%HasFastSmiElements(a0));
56
  assertEquals(4, a0.push(4));
57
  assertTrue(%HasFastSmiElements(a0));
58
  assertEquals(5, a0.push(1.3));
59
  assertTrue(%HasFastDoubleElements(a0));
60
  assertEquals(6, a0.push(1.5));
61
  assertTrue(%HasFastDoubleElements(a0));
62
  assertEquals(7, a0.push({}));
63
  assertTrue(%HasFastObjectElements(a0));
64
  assertEquals(8, a0.push({}));
65
  assertTrue(%HasFastObjectElements(a0));
66 67 68 69 70
  assertEquals([1,2,3,4,1.3,1.5,{},{}], a0);

  // Concat
  var a1;
  a1 = [1,2,3].concat([]);
71
  //assertTrue(%HasFastSmiElements(a1));
72 73 74 75 76 77 78 79
  assertEquals([1,2,3], a1);
  a1 = [1,2,3].concat([4,5,6]);
  assertTrue(%HasFastSmiElements(a1));
  assertEquals([1,2,3,4,5,6], a1);
  a1 = [1,2,3].concat([4,5,6], [7,8,9]);
  assertTrue(%HasFastSmiElements(a1));
  assertEquals([1,2,3,4,5,6,7,8,9], a1);
  a1 = [1.1,2,3].concat([]);
80
  assertTrue(%HasFastDoubleElements(a1));
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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  assertEquals([1.1,2,3], a1);
  a1 = [1,2,3].concat([1.1, 2]);
  assertTrue(%HasFastDoubleElements(a1));
  assertEquals([1,2,3,1.1,2], a1);
  a1 = [1.1,2,3].concat([1, 2]);
  assertTrue(%HasFastDoubleElements(a1));
  assertEquals([1.1,2,3,1,2], a1);
  a1 = [1.1,2,3].concat([1.2, 2]);
  assertTrue(%HasFastDoubleElements(a1));
  assertEquals([1.1,2,3,1.2,2], a1);

  a1 = [1,2,3].concat([{}]);
  assertTrue(%HasFastObjectElements(a1));
  assertEquals([1,2,3,{}], a1);
  a1 = [1.1,2,3].concat([{}]);
  assertTrue(%HasFastObjectElements(a1));
  assertEquals([1.1,2,3,{}], a1);
  a1 = [{}].concat([1,2,3]);
  assertTrue(%HasFastObjectElements(a1));
  assertEquals([{},1,2,3], a1);
  a1 = [{}].concat([1.1,2,3]);
  assertTrue(%HasFastObjectElements(a1));
  assertEquals([{},1.1,2,3], a1);

  // Slice
  var a2 = [1,2,3];
  assertTrue(%HasFastSmiElements(a2.slice()));
  assertTrue(%HasFastSmiElements(a2.slice(1)));
  assertTrue(%HasFastSmiElements(a2.slice(1, 2)));
  assertEquals([1,2,3], a2.slice());
  assertEquals([2,3], a2.slice(1));
  assertEquals([2], a2.slice(1,2));
  a2 = [1.1,2,3];
  assertTrue(%HasFastDoubleElements(a2.slice()));
  assertTrue(%HasFastDoubleElements(a2.slice(1)));
  assertTrue(%HasFastDoubleElements(a2.slice(1, 2)));
  assertEquals([1.1,2,3], a2.slice());
  assertEquals([2,3], a2.slice(1));
  assertEquals([2], a2.slice(1,2));
  a2 = [{},2,3];
  assertTrue(%HasFastObjectElements(a2.slice()));
  assertTrue(%HasFastObjectElements(a2.slice(1)));
  assertTrue(%HasFastObjectElements(a2.slice(1, 2)));
  assertEquals([{},2,3], a2.slice());
  assertEquals([2,3], a2.slice(1));
  assertEquals([2], a2.slice(1,2));

  // Splice
  var a3 = [1,2,3];
  var a3r;
  a3r = a3.splice(0, 0);
  assertTrue(%HasFastSmiElements(a3r));
  assertTrue(%HasFastSmiElements(a3));
  assertEquals([], a3r);
  assertEquals([1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1);
  assertTrue(%HasFastSmiElements(a3r));
  assertTrue(%HasFastSmiElements(a3));
  assertEquals([1], a3r);
  assertEquals([2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, 2);
  assertTrue(%HasFastSmiElements(a3r));
  assertTrue(%HasFastSmiElements(a3));
  assertEquals([], a3r);
  assertEquals([2, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, 2);
  assertTrue(%HasFastSmiElements(a3r));
  assertTrue(%HasFastSmiElements(a3));
  assertEquals([1], a3r);
  assertEquals([2, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([], a3r);
  assertEquals([1.1, 2, 3], a3);
160
  a3 = [1.1, 2, 3];
161 162 163 164 165
  a3r = a3.splice(0, 1);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([1.1], a3r);
  assertEquals([2, 3], a3);
166
  a3 = [1.1, 2, 3];
167
  a3r = a3.splice(0, 0, 2);
168
  assertTrue(%HasFastDoubleElements(a3r));
169 170 171
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([], a3r);
  assertEquals([2, 1.1, 2, 3], a3);
172 173
  a3 = [1.1, 2, 3];
  assertTrue(%HasFastDoubleElements(a3));
174 175 176 177 178 179 180
  a3r = a3.splice(0, 1, 2);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([1.1], a3r);
  assertEquals([2, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0, 2.1);
181
  assertTrue(%HasFastDoubleElements(a3r));
182 183 184 185 186 187 188 189 190 191 192
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([], a3r);
  assertEquals([2.1, 1.1, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 1, 2.2);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([1.1], a3r);
  assertEquals([2.2, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, 2.1);
193
  assertTrue(%HasFastDoubleElements(a3r));
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([], a3r);
  assertEquals([2.1, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, 2.2);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([1], a3r);
  assertEquals([2.2, 2, 3], a3);
  a3 = [{},2,3];
  a3r = a3.splice(0, 0);
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([], a3r);
  assertEquals([{}, 2, 3], a3);
  a3 = [1,2,{}];
  a3r = a3.splice(0, 1);
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([1], a3r);
  assertEquals([2, {}], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, {});
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([], a3r);
  assertEquals([{}, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, {});
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([1], a3r);
  assertEquals([{}, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0, {});
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([], a3r);
  assertEquals([{}, 1.1, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 1, {});
  assertTrue(%HasFastObjectElements(a3r));
  assertTrue(%HasFastObjectElements(a3));
  assertEquals([1.1], a3r);
  assertEquals([{}, 2, 3], a3);
239 240 241 242 243 244 245 246 247 248 249 250 251
  a3 = [1.1, 2.2, 3.3];
  a3r = a3.splice(2, 1);
  assertTrue(%HasFastDoubleElements(a3r));
  assertTrue(%HasFastDoubleElements(a3));
  assertEquals([3.3], a3r);
  //assertTrue(%HasFastDoubleElements(a3r));
  assertEquals([1.1, 2.2], a3);
  //assertTrue(%HasFastDoubleElements(a3r));
  a3r = a3.splice(1, 1, 4.4, 5.5);
  //assertTrue(%HasFastDoubleElements(a3r));
  //assertTrue(%HasFastDoubleElements(a3));
  assertEquals([2.2], a3r);
  assertEquals([1.1, 4.4, 5.5], a3);
252 253 254 255

  // Pop
  var a4 = [1,2,3];
  assertEquals(3, a4.pop());
256 257
  assertEquals([1,2], a4);
  //assertTrue(%HasFastSmiElements(a4));
258 259
  a4 = [1.1,2,3];
  assertEquals(3, a4.pop());
260 261
  assertEquals([1.1,2], a4);
  //assertTrue(%HasFastDoubleElements(a4));
262 263
  a4 = [{},2,3];
  assertEquals(3, a4.pop());
264 265
  assertEquals([{},2], a4);
  //assertTrue(%HasFastObjectElements(a4));
266 267 268 269

  // Shift
  var a4 = [1,2,3];
  assertEquals(1, a4.shift());
270 271
  assertEquals([2,3], a4);
  //assertTrue(%HasFastSmiElements(a4));
272 273
  a4 = [1.1,2,3];
  assertEquals(1.1, a4.shift());
274 275
  assertEquals([2,3], a4);
  //assertTrue(%HasFastDoubleElements(a4));
276 277
  a4 = [{},2,3];
  assertEquals({}, a4.shift());
278 279
  assertEquals([2,3], a4);
  //assertTrue(%HasFastObjectElements(a4));
280 281 282

  // Unshift
  var a4 = [1,2,3];
283
  assertEquals(4, a4.unshift(1));
284 285 286
  assertTrue(%HasFastSmiElements(a4));
  assertEquals([1,1,2,3], a4);
  a4 = [1,2,3];
287
  assertEquals(4, a4.unshift(1.1));
288
  assertTrue(%HasFastDoubleElements(a4));
289 290
  assertEquals([1.1,1,2,3], a4);
  a4 = [1.1,2,3];
291
  assertEquals(4, a4.unshift(1));
292
  assertTrue(%HasFastDoubleElements(a4));
293 294
  assertEquals([1,1.1,2,3], a4);
  a4 = [{},2,3];
295
  assertEquals(4, a4.unshift(1));
296 297 298
  assertTrue(%HasFastObjectElements(a4));
  assertEquals([1,{},2,3], a4);
  a4 = [{},2,3];
299
  assertEquals(4, a4.unshift(1.1));
300 301 302 303
  assertTrue(%HasFastObjectElements(a4));
  assertEquals([1.1,{},2,3], a4);
}

304
for (var i = 0; i < 3; i++) {
305
  array_natives_test();
306
}
307
%OptimizeFunctionOnNextCall(array_natives_test);
308
array_natives_test();