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

Side by Side Diff: test/mjsunit/array-unshift.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 | « test/mjsunit/array-splice.js ('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
1 // Copyright 2010 the V8 project authors. All rights reserved. 1 // Copyright 2010 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 19 matching lines...) Expand all
30 (function() { 30 (function() {
31 var array = new Array(10); 31 var array = new Array(10);
32 assertEquals(13, array.unshift('1st', '2ns', '3rd')); 32 assertEquals(13, array.unshift('1st', '2ns', '3rd'));
33 assertTrue(0 in array); 33 assertTrue(0 in array);
34 assertTrue(1 in array); 34 assertTrue(1 in array);
35 assertTrue(2 in array); 35 assertTrue(2 in array);
36 assertFalse(3 in array); 36 assertFalse(3 in array);
37 })(); 37 })();
38 38
39 39
40 // Check that unshif with no args has a side-effect of 40 // Check that unshift with no args has a side-effect of
41 // feeling the holes with elements from the prototype 41 // filling the holes with elements from the prototype
42 // (if present, of course) 42 // (if present, of course)
43 (function() { 43 (function() {
44 var len = 3; 44 var len = 3;
45 var array = new Array(len); 45 var array = new Array(len);
46 46
47 var at0 = '@0'; 47 var at0 = '@0';
48 var at2 = '@2'; 48 var at2 = '@2';
49 49
50 Array.prototype[0] = at0; 50 Array.prototype[0] = at0;
51 Array.prototype[2] = at2; 51 Array.prototype[2] = at2;
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 assertFalse(array.hasOwnProperty(3)); 108 assertFalse(array.hasOwnProperty(3));
109 109
110 assertEquals(array[7], Array.prototype[7]); 110 assertEquals(array[7], Array.prototype[7]);
111 assertFalse(array.hasOwnProperty(7)); 111 assertFalse(array.hasOwnProperty(7));
112 112
113 assertTrue(delete Array.prototype[3]); 113 assertTrue(delete Array.prototype[3]);
114 assertTrue(delete Array.prototype[5]); 114 assertTrue(delete Array.prototype[5]);
115 assertTrue(delete Array.prototype[7]); 115 assertTrue(delete Array.prototype[7]);
116 })(); 116 })();
117 117
118 // Check that unshift with no args has a side-effect of
119 // filling the holes with elements from the prototype
120 // (if present, of course)
121 (function() {
122 var len = 3;
123 var array = new Array(len);
124
125 var at0 = '@0';
126 var at2 = '@2';
127
128 var array_proto = [];
129 array_proto[0] = at0;
130 array_proto[2] = at2;
131 array.__proto__ = array_proto;
132
133 // array owns nothing...
134 assertFalse(array.hasOwnProperty(0));
135 assertFalse(array.hasOwnProperty(1));
136 assertFalse(array.hasOwnProperty(2));
137
138 // ... but sees values from array_proto.
139 assertEquals(array[0], at0);
140 assertEquals(array[1], undefined);
141 assertEquals(array[2], at2);
142
143 assertEquals(len, array.unshift());
144
145 // unshift makes array own 0 and 2...
146 assertTrue(array.hasOwnProperty(0));
147 assertFalse(array.hasOwnProperty(1));
148 assertTrue(array.hasOwnProperty(2));
149
150 // ... so they are not affected be delete.
151 assertEquals(array[0], at0);
152 assertEquals(array[1], undefined);
153 assertEquals(array[2], at2);
154 })();
155
156
157 // Now check the case with array of holes and some elements on prototype.
158 (function() {
159 var len = 9;
160 var array = new Array(len);
161 var array_proto = []
162 array_proto[3] = "@3";
163 array_proto[7] = "@7";
164 array.__proto__ = array_proto;
165
166 assertEquals(len, array.length);
167 for (var i = 0; i < array.length; i++) {
168 assertEquals(array[i], array_proto[i]);
169 }
170
171 assertEquals(len + 1, array.unshift('head'));
172
173 assertEquals(len + 1, array.length);
174 // Note that unshift copies values from prototype into the array.
175 assertEquals(array[4], array_proto[3]);
176 assertTrue(array.hasOwnProperty(4));
177
178 assertEquals(array[8], array_proto[7]);
179 assertTrue(array.hasOwnProperty(8));
180
181 // ... but keeps the rest as holes:
182 array_proto[5] = "@5";
183 assertEquals(array[5], array_proto[5]);
184 assertFalse(array.hasOwnProperty(5));
185
186 assertEquals(array[3], array_proto[3]);
187 assertFalse(array.hasOwnProperty(3));
188
189 assertEquals(array[7], array_proto[7]);
190 assertFalse(array.hasOwnProperty(7));
191 })();
192
118 // Check the behaviour when approaching maximal values for length. 193 // Check the behaviour when approaching maximal values for length.
119 (function() { 194 (function() {
120 for (var i = 0; i < 7; i++) { 195 for (var i = 0; i < 7; i++) {
121 try { 196 try {
122 new Array((1 << 32) - 3).unshift(1, 2, 3, 4, 5); 197 new Array((1 << 32) - 3).unshift(1, 2, 3, 4, 5);
123 throw 'Should have thrown RangeError'; 198 throw 'Should have thrown RangeError';
124 } catch (e) { 199 } catch (e) {
125 assertTrue(e instanceof RangeError); 200 assertTrue(e instanceof RangeError);
126 } 201 }
127 202
128 // Check smi boundary 203 // Check smi boundary
129 var bigNum = (1 << 30) - 3; 204 var bigNum = (1 << 30) - 3;
130 assertEquals(bigNum + 7, new Array(bigNum).unshift(1, 2, 3, 4, 5, 6, 7)); 205 assertEquals(bigNum + 7, new Array(bigNum).unshift(1, 2, 3, 4, 5, 6, 7));
131 } 206 }
132 })(); 207 })();
133 208
134 (function() { 209 (function() {
135 for (var i = 0; i < 7; i++) { 210 for (var i = 0; i < 7; i++) {
136 var a = [6, 7, 8, 9]; 211 var a = [6, 7, 8, 9];
137 a.unshift(1, 2, 3, 4, 5); 212 a.unshift(1, 2, 3, 4, 5);
138 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a); 213 assertEquals([1, 2, 3, 4, 5, 6, 7, 8, 9], a);
139 } 214 }
140 })(); 215 })();
OLDNEW
« no previous file with comments | « test/mjsunit/array-splice.js ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698