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

Side by Side Diff: test/mjsunit/array-concat.js

Issue 2037008: Properly process arrays with overridden prototype in various Array's functions. (Closed)
Patch Set: Addressing Mads' comments Created 10 years, 7 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
« no previous file with comments | « src/builtins.cc ('k') | test/mjsunit/array-pop.js » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2008 the V8 project authors. All rights reserved. 1 // Copyright 2008 the V8 project authors. All rights reserved.
2 // Redistribution and use in source and binary forms, with or without 2 // Redistribution and use in source and binary forms, with or without
3 // modification, are permitted provided that the following conditions are 3 // modification, are permitted provided that the following conditions are
4 // met: 4 // met:
5 // 5 //
6 // * Redistributions of source code must retain the above copyright 6 // * Redistributions of source code must retain the above copyright
7 // notice, this list of conditions and the following disclaimer. 7 // notice, this list of conditions and the following disclaimer.
8 // * Redistributions in binary form must reproduce the above 8 // * Redistributions in binary form must reproduce the above
9 // copyright notice, this list of conditions and the following 9 // copyright notice, this list of conditions and the following
10 // disclaimer in the documentation and/or other materials provided 10 // disclaimer in the documentation and/or other materials provided
(...skipping 11 matching lines...) Expand all
22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 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. 26 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 27
28 /** 28 /**
29 * @fileoverview Test concat on small and large arrays 29 * @fileoverview Test concat on small and large arrays
30 */ 30 */
31 31
32 var poses = [140, 4000000000]; 32 var poses;
33
34 poses = [140, 4000000000];
35 while (pos = poses.shift()) {
36 var a = new Array(pos);
37 var array_proto = [];
38 a.__proto__ = array_proto;
39 assertEquals(pos, a.length);
40 a.push('foo');
41 assertEquals(pos + 1, a.length);
42 var b = ['bar'];
43 var c = a.concat(b);
44 assertEquals(pos + 2, c.length);
45 assertEquals("undefined", typeof(c[pos - 1]));
46 assertEquals("foo", c[pos]);
47 assertEquals("bar", c[pos + 1]);
48
49 // Can we fool the system by putting a number in a string?
50 var onetwofour = "124";
51 a[onetwofour] = 'doo';
52 assertEquals(a[124], 'doo');
53 c = a.concat(b);
54 assertEquals(c[124], 'doo');
55
56 // If we put a number in the prototype, then the spec says it should be
57 // copied on concat.
58 array_proto["123"] = 'baz';
59 assertEquals(a[123], 'baz');
60
61 c = a.concat(b);
62 assertEquals(pos + 2, c.length);
63 assertEquals("baz", c[123]);
64 assertEquals("undefined", typeof(c[pos - 1]));
65 assertEquals("foo", c[pos]);
66 assertEquals("bar", c[pos + 1]);
67
68 // When we take the number off the prototype it disappears from a, but
69 // the concat put it in c itself.
70 array_proto["123"] = undefined;
71 assertEquals("undefined", typeof(a[123]));
72 assertEquals("baz", c[123]);
73
74 // If the element of prototype is shadowed, the element on the instance
75 // should be copied, but not the one on the prototype.
76 array_proto[123] = 'baz';
77 a[123] = 'xyz';
78 assertEquals('xyz', a[123]);
79 c = a.concat(b);
80 assertEquals('xyz', c[123]);
81
82 // Non-numeric properties on the prototype or the array shouldn't get
83 // copied.
84 array_proto.moe = 'joe';
85 a.ben = 'jerry';
86 assertEquals(a["moe"], 'joe');
87 assertEquals(a["ben"], 'jerry');
88 c = a.concat(b);
89 // ben was not copied
90 assertEquals("undefined", typeof(c.ben));
91
92 // When we take moe off the prototype it disappears from all arrays.
93 array_proto.moe = undefined;
94 assertEquals("undefined", typeof(c.moe));
95
96 // Negative indices don't get concated.
97 a[-1] = 'minus1';
98 assertEquals("minus1", a[-1]);
99 assertEquals("undefined", typeof(a[0xffffffff]));
100 c = a.concat(b);
101 assertEquals("undefined", typeof(c[-1]));
102 assertEquals("undefined", typeof(c[0xffffffff]));
103 assertEquals(c.length, a.length + 1);
104
105 }
106
107 poses = [140, 4000000000];
33 while (pos = poses.shift()) { 108 while (pos = poses.shift()) {
34 var a = new Array(pos); 109 var a = new Array(pos);
35 assertEquals(pos, a.length); 110 assertEquals(pos, a.length);
36 a.push('foo'); 111 a.push('foo');
37 assertEquals(pos + 1, a.length); 112 assertEquals(pos + 1, a.length);
38 var b = ['bar']; 113 var b = ['bar'];
39 var c = a.concat(b); 114 var c = a.concat(b);
40 assertEquals(pos + 2, c.length); 115 assertEquals(pos + 2, c.length);
41 assertEquals("undefined", typeof(c[pos - 1])); 116 assertEquals("undefined", typeof(c[pos - 1]));
42 assertEquals("foo", c[pos]); 117 assertEquals("foo", c[pos]);
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
84 c = a.concat(b); 159 c = a.concat(b);
85 // ben was not copied 160 // ben was not copied
86 assertEquals("undefined", typeof(c.ben)); 161 assertEquals("undefined", typeof(c.ben));
87 // moe was not copied, but we can see it through the prototype 162 // moe was not copied, but we can see it through the prototype
88 assertEquals("joe", c.moe); 163 assertEquals("joe", c.moe);
89 164
90 // When we take moe off the prototype it disappears from all arrays. 165 // When we take moe off the prototype it disappears from all arrays.
91 Array.prototype.moe = undefined; 166 Array.prototype.moe = undefined;
92 assertEquals("undefined", typeof(c.moe)); 167 assertEquals("undefined", typeof(c.moe));
93 168
94 // Negative indeces don't get concated. 169 // Negative indices don't get concated.
95 a[-1] = 'minus1'; 170 a[-1] = 'minus1';
96 assertEquals("minus1", a[-1]); 171 assertEquals("minus1", a[-1]);
97 assertEquals("undefined", typeof(a[0xffffffff])); 172 assertEquals("undefined", typeof(a[0xffffffff]));
98 c = a.concat(b); 173 c = a.concat(b);
99 assertEquals("undefined", typeof(c[-1])); 174 assertEquals("undefined", typeof(c[-1]));
100 assertEquals("undefined", typeof(c[0xffffffff])); 175 assertEquals("undefined", typeof(c[0xffffffff]));
101 assertEquals(c.length, a.length + 1); 176 assertEquals(c.length, a.length + 1);
102 177
103 } 178 }
104 179
105 a = []; 180 a = [];
106 c = a.concat('Hello'); 181 c = a.concat('Hello');
107 assertEquals(1, c.length); 182 assertEquals(1, c.length);
108 assertEquals("Hello", c[0]); 183 assertEquals("Hello", c[0]);
109 assertEquals("Hello", c.toString()); 184 assertEquals("Hello", c.toString());
110 185
111 // Check that concat preserves holes. 186 // Check that concat preserves holes.
112 var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0]) 187 var holey = [void 0,'a',,'c'].concat(['d',,'f',[0,,2],void 0])
113 assertEquals(9, holey.length); // hole in embedded array is ignored 188 assertEquals(9, holey.length); // hole in embedded array is ignored
114 for (var i = 0; i < holey.length; i++) { 189 for (var i = 0; i < holey.length; i++) {
115 if (i == 2 || i == 5) { 190 if (i == 2 || i == 5) {
116 assertFalse(i in holey); 191 assertFalse(i in holey);
117 } else { 192 } else {
118 assertTrue(i in holey); 193 assertTrue(i in holey);
119 } 194 }
120 } 195 }
OLDNEW
« no previous file with comments | « src/builtins.cc ('k') | test/mjsunit/array-pop.js » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698