array-natives-elements.js 10.1 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 34 35 36 37 38 39 40 41 42
// 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));
}

43
var __sequence = 0;
44
function array_natives_test() {
45 46

  // Ensure small array literals start in specific element kind mode.
47 48 49 50 51
  assertTrue(%HasSmiElements([]));
  assertTrue(%HasSmiElements([1]));
  assertTrue(%HasSmiElements([1,2]));
  assertTrue(%HasDoubleElements([1.1]));
  assertTrue(%HasDoubleElements([1.1,2]));
52

53 54
  // Push
  var a0 = make_array("[1, 2, 3]");
55
  assertTrue(%HasSmiElements(a0));
56
  assertEquals(4, a0.push(4));
57
  assertTrue(%HasSmiElements(a0));
58
  assertEquals(5, a0.push(1.3));
59
  assertTrue(%HasDoubleElements(a0));
60
  assertEquals(6, a0.push(1.5));
61
  assertTrue(%HasDoubleElements(a0));
62
  assertEquals(7, a0.push({}));
63
  assertTrue(%HasObjectElements(a0));
64
  assertEquals(8, a0.push({}));
65
  assertTrue(%HasObjectElements(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(%HasSmiElements(a1));
72 73
  assertEquals([1,2,3], a1);
  a1 = [1,2,3].concat([4,5,6]);
74
  assertTrue(%HasSmiElements(a1));
75 76
  assertEquals([1,2,3,4,5,6], a1);
  a1 = [1,2,3].concat([4,5,6], [7,8,9]);
77
  assertTrue(%HasSmiElements(a1));
78 79
  assertEquals([1,2,3,4,5,6,7,8,9], a1);
  a1 = [1.1,2,3].concat([]);
80
  assertTrue(%HasDoubleElements(a1));
81 82
  assertEquals([1.1,2,3], a1);
  a1 = [1,2,3].concat([1.1, 2]);
83
  assertTrue(%HasDoubleElements(a1));
84 85
  assertEquals([1,2,3,1.1,2], a1);
  a1 = [1.1,2,3].concat([1, 2]);
86
  assertTrue(%HasDoubleElements(a1));
87 88
  assertEquals([1.1,2,3,1,2], a1);
  a1 = [1.1,2,3].concat([1.2, 2]);
89
  assertTrue(%HasDoubleElements(a1));
90 91 92
  assertEquals([1.1,2,3,1.2,2], a1);

  a1 = [1,2,3].concat([{}]);
93
  assertTrue(%HasObjectElements(a1));
94 95
  assertEquals([1,2,3,{}], a1);
  a1 = [1.1,2,3].concat([{}]);
96
  assertTrue(%HasObjectElements(a1));
97 98
  assertEquals([1.1,2,3,{}], a1);
  a1 = [{}].concat([1,2,3]);
99
  assertTrue(%HasObjectElements(a1));
100 101
  assertEquals([{},1,2,3], a1);
  a1 = [{}].concat([1.1,2,3]);
102
  assertTrue(%HasObjectElements(a1));
103 104 105 106
  assertEquals([{},1.1,2,3], a1);

  // Slice
  var a2 = [1,2,3];
107 108 109
  assertTrue(%HasSmiElements(a2.slice()));
  assertTrue(%HasSmiElements(a2.slice(1)));
  assertTrue(%HasSmiElements(a2.slice(1, 2)));
110 111 112 113
  assertEquals([1,2,3], a2.slice());
  assertEquals([2,3], a2.slice(1));
  assertEquals([2], a2.slice(1,2));
  a2 = [1.1,2,3];
114
  assertTrue(%HasDoubleElements(a2.slice()));
115 116 117 118
  assertTrue(%HasDoubleElements(a2.slice(1)) ||
             %HasSmiElements(a2.slice(1)));
  assertTrue(%HasDoubleElements(a2.slice(1, 2)) ||
            %HasSmiElements(a2.slice(1, 2)));
119 120 121 122
  assertEquals([1.1,2,3], a2.slice());
  assertEquals([2,3], a2.slice(1));
  assertEquals([2], a2.slice(1,2));
  a2 = [{},2,3];
123
  assertTrue(%HasObjectElements(a2.slice()));
124 125 126 127
  assertTrue(%HasObjectElements(a2.slice(1)) ||
             %HasSmiElements(a2.slice(1)));
  assertTrue(%HasObjectElements(a2.slice(1, 2)) ||
             %HasSmiElements(a2.slice(1, 2)));
128 129 130 131 132 133 134 135
  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);
136 137
  assertTrue(%HasSmiElements(a3r));
  assertTrue(%HasSmiElements(a3));
138 139 140 141
  assertEquals([], a3r);
  assertEquals([1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1);
142 143
  assertTrue(%HasSmiElements(a3r));
  assertTrue(%HasSmiElements(a3));
144 145 146 147
  assertEquals([1], a3r);
  assertEquals([2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, 2);
148 149
  assertTrue(%HasSmiElements(a3r));
  assertTrue(%HasSmiElements(a3));
150 151 152 153
  assertEquals([], a3r);
  assertEquals([2, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, 2);
154 155
  assertTrue(%HasSmiElements(a3r));
  assertTrue(%HasSmiElements(a3));
156 157 158 159
  assertEquals([1], a3r);
  assertEquals([2, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0);
160 161
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
162 163
  assertEquals([], a3r);
  assertEquals([1.1, 2, 3], a3);
164
  a3 = [1.1, 2, 3];
165
  a3r = a3.splice(0, 1);
166 167
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
168 169
  assertEquals([1.1], a3r);
  assertEquals([2, 3], a3);
170
  a3 = [1.1, 2, 3];
171
  a3r = a3.splice(0, 0, 2);
172 173
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
174 175
  assertEquals([], a3r);
  assertEquals([2, 1.1, 2, 3], a3);
176
  a3 = [1.1, 2, 3];
177
  assertTrue(%HasDoubleElements(a3));
178
  a3r = a3.splice(0, 1, 2);
179 180
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
181 182 183 184
  assertEquals([1.1], a3r);
  assertEquals([2, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0, 2.1);
185 186
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
187 188 189 190
  assertEquals([], a3r);
  assertEquals([2.1, 1.1, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 1, 2.2);
191 192
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
193 194 195 196
  assertEquals([1.1], a3r);
  assertEquals([2.2, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, 2.1);
197 198
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
199 200 201 202
  assertEquals([], a3r);
  assertEquals([2.1, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, 2.2);
203 204
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
205 206 207 208
  assertEquals([1], a3r);
  assertEquals([2.2, 2, 3], a3);
  a3 = [{},2,3];
  a3r = a3.splice(0, 0);
209 210
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
211 212 213 214
  assertEquals([], a3r);
  assertEquals([{}, 2, 3], a3);
  a3 = [1,2,{}];
  a3r = a3.splice(0, 1);
215 216
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
217 218 219 220
  assertEquals([1], a3r);
  assertEquals([2, {}], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 0, {});
221 222
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
223 224 225 226
  assertEquals([], a3r);
  assertEquals([{}, 1, 2, 3], a3);
  a3 = [1,2,3];
  a3r = a3.splice(0, 1, {});
227 228
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
229 230 231 232
  assertEquals([1], a3r);
  assertEquals([{}, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 0, {});
233 234
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
235 236 237 238
  assertEquals([], a3r);
  assertEquals([{}, 1.1, 2, 3], a3);
  a3 = [1.1,2,3];
  a3r = a3.splice(0, 1, {});
239 240
  assertTrue(%HasObjectElements(a3r));
  assertTrue(%HasObjectElements(a3));
241 242
  assertEquals([1.1], a3r);
  assertEquals([{}, 2, 3], a3);
243 244
  a3 = [1.1, 2.2, 3.3];
  a3r = a3.splice(2, 1);
245 246
  assertTrue(%HasDoubleElements(a3r));
  assertTrue(%HasDoubleElements(a3));
247
  assertEquals([3.3], a3r);
248
  //assertTrue(%HasDoubleElements(a3r));
249
  assertEquals([1.1, 2.2], a3);
250
  //assertTrue(%HasDoubleElements(a3r));
251
  a3r = a3.splice(1, 1, 4.4, 5.5);
252 253
  //assertTrue(%HasDoubleElements(a3r));
  //assertTrue(%HasDoubleElements(a3));
254 255
  assertEquals([2.2], a3r);
  assertEquals([1.1, 4.4, 5.5], a3);
256 257 258 259

  // Pop
  var a4 = [1,2,3];
  assertEquals(3, a4.pop());
260
  assertEquals([1,2], a4);
261
  //assertTrue(%HasSmiElements(a4));
262 263
  a4 = [1.1,2,3];
  assertEquals(3, a4.pop());
264
  assertEquals([1.1,2], a4);
265
  //assertTrue(%HasDoubleElements(a4));
266 267
  a4 = [{},2,3];
  assertEquals(3, a4.pop());
268
  assertEquals([{},2], a4);
269
  //assertTrue(%HasObjectElements(a4));
270 271 272 273

  // Shift
  var a4 = [1,2,3];
  assertEquals(1, a4.shift());
274
  assertEquals([2,3], a4);
275
  //assertTrue(%HasSmiElements(a4));
276 277
  a4 = [1.1,2,3];
  assertEquals(1.1, a4.shift());
278
  assertEquals([2,3], a4);
279
  //assertTrue(%HasDoubleElements(a4));
280 281
  a4 = [{},2,3];
  assertEquals({}, a4.shift());
282
  assertEquals([2,3], a4);
283
  //assertTrue(%HasObjectElements(a4));
284 285 286

  // Unshift
  var a4 = [1,2,3];
287
  assertEquals(4, a4.unshift(1));
288
  assertTrue(%HasSmiElements(a4));
289 290
  assertEquals([1,1,2,3], a4);
  a4 = [1,2,3];
291
  assertEquals(4, a4.unshift(1.1));
292
  assertTrue(%HasDoubleElements(a4));
293 294
  assertEquals([1.1,1,2,3], a4);
  a4 = [1.1,2,3];
295
  assertEquals(4, a4.unshift(1));
296
  assertTrue(%HasDoubleElements(a4));
297 298
  assertEquals([1,1.1,2,3], a4);
  a4 = [{},2,3];
299
  assertEquals(4, a4.unshift(1));
300
  assertTrue(%HasObjectElements(a4));
301 302
  assertEquals([1,{},2,3], a4);
  a4 = [{},2,3];
303
  assertEquals(4, a4.unshift(1.1));
304
  assertTrue(%HasObjectElements(a4));
305 306 307
  assertEquals([1.1,{},2,3], a4);
}

308
%PrepareFunctionForOptimization(array_natives_test);
309
for (var i = 0; i < 3; i++) {
310
  array_natives_test();
311
}
312
%OptimizeFunctionOnNextCall(array_natives_test);
313
array_natives_test();