Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(356)

Side by Side Diff: test/mjsunit/es7/array-includes-receiver.js

Issue 2146293003: [builtins] implement Array.prototype.includes in TurboFan (Closed) Base URL: https://chromium.googlesource.com/v8/v8.git@master
Patch Set: fix some things, rebase, rename some variables in TF, etc Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« src/elements.cc ('K') | « src/runtime/runtime-array.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright 2016 the V8 project authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 // Flags: --allow-natives-syntax
6
7 // Ensure `Array.prototype.includes` functions correctly for numerous elements
8 // kinds, and various exotic receiver types,
9
10 // TODO(caitp): update kIterCount to a high enough number to trigger inlining,
11 // once inlining this builtin is supported
12 var kIterCount = 1;
13 var kTests = {
14 Array: {
15 FAST_ELEMENTS() {
16 var r = /foo/;
17 var s = new String("bar");
18 var p = new Proxy({}, {});
19 var o = {};
20
21 var array = [r, s, p];
22 assertTrue(%HasFastObjectElements(array));
23 assertFalse(%HasFastHoleyElements(array));
24
25 for (var i = 0; i < kIterCount; ++i) {
26 assertTrue(array.includes(p));
27 assertFalse(array.includes(o));
28 }
29 },
30
31 FAST_HOLEY_ELEMENTS() {
32 var r = /foo/;
33 var p = new Proxy({}, {});
34 var o = {};
35
36 var array = [r, , p];
37 assertTrue(%HasFastObjectElements(array));
38 assertTrue(%HasFastHoleyElements(array));
39
40 for (var i = 0; i < kIterCount; ++i) {
41 assertTrue(array.includes(p));
42 assertFalse(array.includes(o));
43 }
44 },
45
46 FAST_SMI_ELEMENTS() {
47 var array = [0, 88, 9999, 1, -5, 7];
48 assertTrue(%HasFastSmiElements(array));
49 assertFalse(%HasFastHoleyElements(array));
50
51 for (var i = 0; i < kIterCount; ++i) {
52 assertTrue(array.includes(9999));
53 assertTrue(array.includes(-5));
54 assertFalse(array.includes(-5.00001));
55 assertFalse(array.includes(undefined));
56 assertFalse(array.includes(NaN));
57 }
58 },
59
60 FAST_HOLEY_SMI_ELEMENTS() {
61 var array = [49, , , 72, , , 67, -48];
62 assertTrue(%HasFastSmiElements(array));
63 assertTrue(%HasFastHoleyElements(array));
64
65 for (var i = 0; i < kIterCount; ++i) {
66 assertTrue(array.includes(72));
67 assertTrue(array.includes(-48));
68 assertFalse(array.includes(72, 4));
69 assertTrue(array.includes(undefined));
70 assertFalse(array.includes(undefined, -2));
71 assertFalse(array.includes(NaN));
72 }
73 },
74
75 FAST_DOUBLE_ELEMENTS() {
76 var array = [7.00000001, -13000.89412, 73451.4124,
77 5824.48, 6.0000495, 48.3488, 44.0, 76.35, NaN, 78.4];
78 assertTrue(%HasFastDoubleElements(array));
79 assertFalse(%HasFastHoleyElements(array));
80
81 for (var i = 0; i < kIterCount; ++i) {
82 assertTrue(array.includes(7.00000001));
83 assertFalse(array.includes(7.00000001, 2));
84 assertTrue(array.includes(NaN));
85 assertFalse(array.includes(NaN, -1));
86 assertTrue(array.includes(-13000.89412));
87 assertFalse(array.includes(-13000.89412, -2));
88 assertFalse(array.includes(undefined));
89 }
90 },
91
92 FAST_HOLEY_DOUBLE_ELEMENTS() {
93 var array = [7.00000001, -13000.89412, ,
94 5824.48, , 48.3488, , NaN, , 78.4];
95 assertTrue(%HasFastDoubleElements(array));
96 assertTrue(%HasFastHoleyElements(array));
97
98 for (var i = 0; i < kIterCount; ++i) {
99 assertTrue(array.includes(7.00000001));
100 assertFalse(array.includes(7.00000001, 2));
101 assertTrue(array.includes(NaN));
102 assertFalse(array.includes(NaN, -2));
103 assertTrue(array.includes(-13000.89412));
104 assertFalse(array.includes(-13000.89412, -2));
105 assertTrue(array.includes(undefined, -2));
106 assertFalse(array.includes(undefined, -1));
107 }
108 },
109
110 DICTIONARY_ELEMENTS() {
111 var array = [];
112 Object.defineProperty(array, 4, { get() { return NaN; } });
113 Object.defineProperty(array, 7, { value: Function });
114
115 assertTrue(%HasDictionaryElements(array));
116
117 for (var i = 0; i < kIterCount; ++i) {
118 assertTrue(array.includes(NaN));
119 assertFalse(array.includes(NaN, -3));
120 assertTrue(array.includes(Function));
121 assertTrue(array.includes(undefined));
122 assertFalse(array.includes(undefined, 7));
123 }
124 },
125 },
126
127 Object: {
128 FAST_ELEMENTS() {
129 var r = /foo/;
130 var s = new String("bar");
131 var p = new Proxy({}, {});
132 var o = {};
133
134 var object = { 0: r, 1: s, 2: p, length: 3 };
135 assertTrue(%HasFastObjectElements(object));
136 // TODO(caitp): JSObjects always seem to start with FAST_HOLEY_ELEMENTS
137 // assertFalse(%HasFastHoleyElements(object));
138
139 for (var i = 0; i < kIterCount; ++i) {
140 assertTrue(Array.prototype.includes.call(object, p));
141 assertFalse(Array.prototype.includes.call(object, o));
142 }
143 },
144
145 FAST_HOLEY_ELEMENTS() {
146 var r = /foo/;
147 var p = new Proxy({}, {});
148 var o = {};
149
150 var object = { 0: r, 2: p, length: 3 };
151 assertTrue(%HasFastObjectElements(object));
152 assertTrue(%HasFastHoleyElements(object));
153
154 for (var i = 0; i < kIterCount; ++i) {
155 assertTrue(Array.prototype.includes.call(object, p));
156 assertFalse(Array.prototype.includes.call(object, o));
157 }
158 },
159
160 FAST_SMI_ELEMENTS() {
161 var object = { 0: 0, 1: 88, 2: 9999, 3: 1, 4: -5, 5: 7, length: 6 };
162 // TODO(caitp): JSObjects always seem to start with FAST_HOLEY_ELEMENTS
163 // assertTrue(%HasFastSmiElements(object));
164 // assertFalse(%HasFastHoleyElements(object));
165
166 for (var i = 0; i < kIterCount; ++i) {
167 assertTrue(Array.prototype.includes.call(object, 9999));
168 assertTrue(Array.prototype.includes.call(object, -5));
169 assertFalse(Array.prototype.includes.call(object, -5.00001));
170 assertFalse(Array.prototype.includes.call(object, undefined));
171 assertFalse(Array.prototype.includes.call(object, NaN));
172 }
173 },
174
175 FAST_HOLEY_SMI_ELEMENTS() {
176 var object = { 0: 49, 3: 72, 6: 67, 7: -48, length: 8 };
177 // TODO(caitp): JSObjects always seem to start with FAST_HOLEY_ELEMENTS
178 // assertTrue(%HasFastSmiElements(object));
179 // assertTrue(%HasFastHoleyElements(object));
180
181 for (var i = 0; i < kIterCount; ++i) {
182 assertTrue(Array.prototype.includes.call(object, 72));
183 assertTrue(Array.prototype.includes.call(object, -48));
184 assertFalse(Array.prototype.includes.call(object, 72, 4));
185 assertTrue(Array.prototype.includes.call(object, undefined));
186 assertFalse(Array.prototype.includes.call(object, undefined, -2));
187 assertFalse(Array.prototype.includes.call(object, NaN));
188 }
189 },
190
191 FAST_DOUBLE_ELEMENTS() {
192 var object = { 0: 7.00000001, 1: -13000.89412, 2: 73451.4124,
193 3: 5824.48, 4: 6.0000495, 5: 48.3488, 6: 44.0, 7: 76.35,
194 8: NaN, 9: 78.4, length: 10 };
195 // TODO(caitp): JSObjects always seem to start with FAST_HOLEY_ELEMENTS
196 // assertTrue(%HasFastDoubleElements(object));
197 // assertFalse(%HasFastHoleyElements(object));
198
199 for (var i = 0; i < kIterCount; ++i) {
200 assertTrue(Array.prototype.includes.call(object, 7.00000001));
201 assertFalse(Array.prototype.includes.call(object, 7.00000001, 2));
202 assertTrue(Array.prototype.includes.call(object, NaN));
203 assertFalse(Array.prototype.includes.call(object, NaN, -1));
204 assertTrue(Array.prototype.includes.call(object, -13000.89412));
205 assertFalse(Array.prototype.includes.call(object, -13000.89412, -2));
206 assertFalse(Array.prototype.includes.call(object, undefined));
207 }
208 },
209
210 FAST_HOLEY_DOUBLE_ELEMENTS() {
211 var object = { 0: 7.00000001, 1: -13000.89412, 3: 5824.48, 5: 48.3488,
212 7: NaN, 9: 78.4, length: 10 };
213 // TODO(caitp): JSObjects always seem to start with FAST_HOLEY_ELEMENTS
214 // assertTrue(%HasFastDoubleElements(object));
215 // assertTrue(%HasFastHoleyElements(object));
216
217 for (var i = 0; i < kIterCount; ++i) {
218 assertTrue(Array.prototype.includes.call(object, 7.00000001));
219 assertFalse(Array.prototype.includes.call(object, 7.00000001, 2));
220 assertTrue(Array.prototype.includes.call(object, NaN));
221 assertFalse(Array.prototype.includes.call(object, NaN, -2));
222 assertTrue(Array.prototype.includes.call(object, -13000.89412));
223 assertFalse(Array.prototype.includes.call(object, -13000.89412, -2));
224 assertTrue(Array.prototype.includes.call(object, undefined, -2));
225 assertFalse(Array.prototype.includes.call(object, undefined, -1));
226 }
227 },
228
229 DICTIONARY_ELEMENTS() {
230 var object = { length: 8 };
231 Object.defineProperty(object, 4, { get() { return NaN; } });
232 Object.defineProperty(object, 7, { value: Function });
233
234 assertTrue(%HasDictionaryElements(object));
235
236 for (var i = 0; i < kIterCount; ++i) {
237 assertTrue(Array.prototype.includes.call(object, NaN));
238 assertFalse(Array.prototype.includes.call(object, NaN, -3));
239 assertTrue(Array.prototype.includes.call(object, Function));
240 assertTrue(Array.prototype.includes.call(object, undefined));
241 assertFalse(Array.prototype.includes.call(object, undefined, 7));
242 }
243
244 (function prototypeModifiedDuringAccessor() {
245 function O() {
246 return {
247 __proto__: {},
248 get 0() {
249 this.__proto__.__proto__ = {
250 get 1() {
251 this[2] = "c";
252 return "b";
253 }
254 };
255 return "a";
256 },
257 length: 3
258 };
259 }
260
261 // Switch to slow path when first accessor modifies the prototype
262 assertTrue(Array.prototype.includes.call(O(), "a"));
263 assertTrue(Array.prototype.includes.call(O(), "b"));
264 assertTrue(Array.prototype.includes.call(O(), "c"));
265
266 // Avoid switching to slow path due to avoiding the accessor
267 assertFalse(Array.prototype.includes.call(O(), "c", 2));
268 assertFalse(Array.prototype.includes.call(O(), "b", 1));
269 assertTrue(Array.prototype.includes.call(O(), undefined, 1));
270 });
271 },
272 },
273
274 String: {
275 FAST_STRING_ELEMENTS() {
276 for (var i = 0; i < kIterCount; ++i) {
277 assertTrue(Array.prototype.includes.call("froyo", "y"));
278 assertFalse(Array.prototype.includes.call("froyo", "y", -1));
279 assertTrue(Array.prototype.includes.call("froyo", "y", -2));
280 assertFalse(Array.prototype.includes.call("froyo", NaN));
281 assertFalse(Array.prototype.includes.call("froyo", undefined));
282 }
283 },
284
285 SLOW_STRING_ELEMENTS() {
286 var string = new String("froyo");
287
288 // Never accessible from A.p.includes as 'length' is not configurable
289 Object.defineProperty(string, 34, { value: NaN });
290 Object.defineProperty(string, 12, { get() { return "nope" } });
291
292 for (var i = 0; i < kIterCount; ++i) {
293 assertTrue(Array.prototype.includes.call("froyo", "y"));
294 assertFalse(Array.prototype.includes.call("froyo", "y", -1));
295 assertTrue(Array.prototype.includes.call("froyo", "y", -2));
296 assertFalse(Array.prototype.includes.call(string, NaN));
297 assertFalse(Array.prototype.includes.call(string, undefined));
298 assertFalse(Array.prototype.includes.call(string, "nope"));
299 }
300 },
301 },
302
303 Arguments: {
304 FAST_SLOPPY_ARGUMENTS_ELEMENTS() {
305 var args = (function(a, b) { return arguments; })("foo", NaN, "bar");
306 assertTrue(%HasSloppyArgumentsElements(args));
307
308 for (var i = 0; i < kIterCount; ++i) {
309 assertFalse(Array.prototype.includes.call(args, undefined));
310 assertTrue(Array.prototype.includes.call(args, NaN));
311 assertFalse(Array.prototype.includes.call(args, NaN, -1));
312 assertTrue(Array.prototype.includes.call(args, "bar", -1));
313 }
314 },
315
316 SLOW_SLOPPY_ARGUMENTS_ELEMENTS() {
317 var args = (function(a, a) { return arguments; })("foo", NaN, "bar");
318 Object.defineProperty(args, 3, { get() { return "silver"; } });
319 Object.defineProperty(args, "length", { value: 4 });
320 assertTrue(%HasSloppyArgumentsElements(args));
321
322 for (var i = 0; i < kIterCount; ++i) {
323 assertFalse(Array.prototype.includes.call(args, undefined));
324 assertTrue(Array.prototype.includes.call(args, NaN));
325 assertFalse(Array.prototype.includes.call(args, NaN, -2));
326 assertTrue(Array.prototype.includes.call(args, "bar", -2));
327 assertTrue(Array.prototype.includes.call(args, "silver", -1));
328 }
329 }
330 }
331 };
332
333 function runSuites(suites) {
334 Object.keys(suites).forEach(suite => runSuite(suites[suite]));
335
336 function runSuite(suite) {
337 Object.keys(suite).forEach(test => suite[test]());
338 }
339 }
340
341 runSuites(kTests);
OLDNEW
« src/elements.cc ('K') | « src/runtime/runtime-array.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698