1// Copyright 2011 the V8 project authors. All rights reserved.
2// Redistribution and use in source and binary forms, with or without
3// modification, are permitted provided that the following conditions are
4// met:
5//
6//     * Redistributions of source code must retain the above copyright
7//       notice, this list of conditions and the following disclaimer.
8//     * Redistributions in binary form must reproduce the above
9//       copyright notice, this list of conditions and the following
10//       disclaimer in the documentation and/or other materials provided
11//       with the distribution.
12//     * Neither the name of Google Inc. nor the names of its
13//       contributors may be used to endorse or promote products derived
14//       from this software without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28// Flags: --allow-natives-syntax
29function init_array(a) {
30  for (var i = 0; i < 10; ++i ){
31    a[i] = i;
32  }
33}
34
35function init_sparse_array(a) {
36  for (var i = 0; i < 10; ++i ){
37    a[i] = i;
38  }
39  a[5000000] = 256;
40  return %NormalizeElements(a);
41}
42
43function testPolymorphicLoads() {
44  function make_polymorphic_load_function() {
45    function load(a, i) {
46      return a[i];
47    }
48
49    var object_array = new Object;
50    var sparse_object_array = new Object;
51    var js_array = new Array(10);
52    var sparse_js_array = %NormalizeElements([]);
53
54    init_array(object_array);
55    init_array(js_array);
56    init_sparse_array(sparse_object_array);
57    init_sparse_array(sparse_js_array);
58
59    assertEquals(1, load(object_array, 1));
60    assertEquals(1, load(js_array, 1));
61    assertEquals(1, load(sparse_object_array, 1));
62    assertEquals(1, load(sparse_js_array, 1));
63
64    return load;
65  }
66
67  var object_array = new Object;
68  var sparse_object_array = new Object;
69  var js_array = new Array(10);
70  var sparse_js_array = %NormalizeElements([]);
71
72  init_array(object_array);
73  init_array(js_array);
74  init_sparse_array(sparse_object_array);
75  init_sparse_array(sparse_js_array);
76
77  load = make_polymorphic_load_function();
78  assertEquals(undefined, load(js_array, new Object()));
79  load = make_polymorphic_load_function();
80  assertEquals(undefined, load(object_array, new Object()));
81  load = make_polymorphic_load_function();
82  assertEquals(undefined, load(sparse_js_array, new Object()));
83  load = make_polymorphic_load_function();
84  assertEquals(undefined, load(sparse_object_array, new Object()));
85
86  // Try with crankshaft.
87  load = make_polymorphic_load_function();
88  %OptimizeFunctionOnNextCall(load);
89  assertEquals(1, load(object_array, 1));
90  assertEquals(1, load(js_array, 1));
91  assertEquals(1, load(sparse_object_array, 1));
92  assertEquals(1, load(sparse_js_array, 1));
93
94  load = make_polymorphic_load_function();
95  %OptimizeFunctionOnNextCall(load);
96  assertEquals(undefined, load(js_array, new Object()));
97  load = make_polymorphic_load_function();
98  %OptimizeFunctionOnNextCall(load);
99  assertEquals(undefined, load(object_array, new Object()));
100  load = make_polymorphic_load_function();
101  %OptimizeFunctionOnNextCall(load);
102  assertEquals(undefined, load(sparse_js_array, new Object()));
103  load = make_polymorphic_load_function();
104  %OptimizeFunctionOnNextCall(load);
105  assertEquals(undefined, load(sparse_object_array, new Object()));
106}
107
108function testPolymorphicStores() {
109  function make_polymorphic_store_function() {
110    function store(a, i, val) {
111      a[i] = val;
112    }
113
114    var object_array = new Object;
115    var sparse_object_array = new Object;
116    var js_array = new Array(10);
117    var sparse_js_array = [];
118    sparse_js_array.length = 5000001;
119
120    init_array(object_array);
121    init_array(js_array);
122    init_sparse_array(sparse_object_array);
123    init_sparse_array(sparse_js_array);
124
125    store(object_array, 1, 256);
126    store(js_array, 1, 256);
127    store(sparse_object_array, 1, 256);
128    store(sparse_js_array, 1, 256);
129
130    return store;
131  }
132
133  var object_array = new Object;
134  var sparse_object_array = new Object;
135  var js_array = new Array(10);
136  var sparse_js_array = %NormalizeElements([]);
137  sparse_js_array.length = 5000001;
138
139  init_array(object_array);
140  init_array(js_array);
141  init_sparse_array(sparse_object_array);
142  init_sparse_array(sparse_js_array);
143
144  store = make_polymorphic_store_function();
145  store(object_array, 2, 257);
146  store = make_polymorphic_store_function();
147  store(js_array, 2, 257);
148  store = make_polymorphic_store_function();
149  store(sparse_object_array, 2, 257);
150  store = make_polymorphic_store_function();
151  store(sparse_js_array, 2, 257);
152
153  assertEquals(257, object_array[2]);
154  assertEquals(257, js_array[2]);
155  assertEquals(257, sparse_js_array[2]);
156  assertEquals(257, sparse_object_array[2]);
157
158  // Now try Crankshaft optimized polymorphic stores
159  store = make_polymorphic_store_function();
160  %OptimizeFunctionOnNextCall(store);
161  store(object_array, 3, 258);
162  store = make_polymorphic_store_function();
163  %OptimizeFunctionOnNextCall(store);
164  store(js_array, 3, 258);
165  store = make_polymorphic_store_function();
166  %OptimizeFunctionOnNextCall(store);
167  store(sparse_object_array, 3, 258);
168  store = make_polymorphic_store_function();
169  %OptimizeFunctionOnNextCall(store);
170  store(sparse_js_array, 3, 258);
171
172  assertEquals(258, object_array[3]);
173  assertEquals(258, js_array[3]);
174  assertEquals(258, sparse_js_array[3]);
175  assertEquals(258, sparse_object_array[3]);
176}
177
178testPolymorphicLoads();
179testPolymorphicStores();
180