1 // Copyright 2008 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 29 30 /** 31 * @fileoverview Test indexing on strings with []. 32 */ 33 34 var foo = "Foo"; 35 assertEquals("Foo", foo); 36 assertEquals("F", foo[0]); 37 assertEquals("o", foo[1]); 38 assertEquals("o", foo[2]); 39 40 // Test string keyed load IC. 41 for (var i = 0; i < 10; i++) { 42 assertEquals("F", foo[0]); 43 assertEquals("o", foo[1]); 44 assertEquals("o", foo[2]); 45 assertEquals("F", (foo[0] + "BarBazQuuxFooBarQuux")[0]); 46 } 47 48 assertEquals("F", foo["0" + ""], "string index"); 49 assertEquals("o", foo["1"], "string index"); 50 assertEquals("o", foo["2"], "string index"); 51 52 assertEquals("undefined", typeof(foo[3]), "out of range"); 53 // SpiderMonkey 1.5 fails this next one. So does FF 2.0.6. 54 assertEquals("undefined", typeof(foo[-1]), "known failure in SpiderMonkey 1.5"); 55 assertEquals("undefined", typeof(foo[-2]), "negative index"); 56 57 foo[0] = "f"; 58 assertEquals("Foo", foo); 59 60 foo[3] = "t"; 61 assertEquals("Foo", foo); 62 assertEquals("undefined", typeof(foo[3]), "out of range"); 63 assertEquals("undefined", typeof(foo[-2]), "negative index"); 64 65 var S = new String("foo"); 66 assertEquals(Object("foo"), S); 67 assertEquals("f", S[0], "string object"); 68 assertEquals("f", S["0"], "string object"); 69 S[0] = 'bente'; 70 assertEquals("f", S[0], "string object"); 71 assertEquals("f", S["0"], "string object"); 72 S[-2] = 'spider'; 73 assertEquals('spider', S[-2]); 74 S[3] = 'monkey'; 75 assertEquals('monkey', S[3]); 76 S['foo'] = 'Fu'; 77 assertEquals("Fu", S.foo); 78 79 // In FF this is ignored I think. In V8 it puts a property on the String object 80 // but you won't ever see it because it is hidden by the 0th character in the 81 // string. The net effect is pretty much the same. 82 S["0"] = 'bente'; 83 assertEquals("f", S[0], "string object"); 84 assertEquals("f", S["0"], "string object"); 85 86 assertEquals(true, 0 in S, "0 in"); 87 assertEquals(false, -1 in S, "-1 in"); 88 assertEquals(true, 2 in S, "2 in"); 89 assertEquals(true, 3 in S, "3 in"); 90 assertEquals(false, 4 in S, "3 in"); 91 92 assertEquals(true, "0" in S, '"0" in'); 93 assertEquals(false, "-1" in S, '"-1" in'); 94 assertEquals(true, "2" in S, '"2" in'); 95 assertEquals(true, "3" in S, '"3" in'); 96 assertEquals(false, "4" in S, '"3" in'); 97 98 assertEquals(true, S.hasOwnProperty(0), "0 hasOwnProperty"); 99 assertEquals(false, S.hasOwnProperty(-1), "-1 hasOwnProperty"); 100 assertEquals(true, S.hasOwnProperty(2), "2 hasOwnProperty"); 101 assertEquals(true, S.hasOwnProperty(3), "3 hasOwnProperty"); 102 assertEquals(false, S.hasOwnProperty(4), "3 hasOwnProperty"); 103 104 assertEquals(true, S.hasOwnProperty("0"), '"0" hasOwnProperty'); 105 assertEquals(false, S.hasOwnProperty("-1"), '"-1" hasOwnProperty'); 106 assertEquals(true, S.hasOwnProperty("2"), '"2" hasOwnProperty'); 107 assertEquals(true, S.hasOwnProperty("3"), '"3" hasOwnProperty'); 108 assertEquals(false, S.hasOwnProperty("4"), '"3" hasOwnProperty'); 109 110 assertEquals(true, "foo".hasOwnProperty(0), "foo 0 hasOwnProperty"); 111 assertEquals(false, "foo".hasOwnProperty(-1), "foo -1 hasOwnProperty"); 112 assertEquals(true, "foo".hasOwnProperty(2), "foo 2 hasOwnProperty"); 113 assertEquals(false, "foo".hasOwnProperty(4), "foo 3 hasOwnProperty"); 114 115 assertEquals(true, "foo".hasOwnProperty("0"), 'foo "0" hasOwnProperty'); 116 assertEquals(false, "foo".hasOwnProperty("-1"), 'foo "-1" hasOwnProperty'); 117 assertEquals(true, "foo".hasOwnProperty("2"), 'foo "2" hasOwnProperty'); 118 assertEquals(false, "foo".hasOwnProperty("4"), 'foo "3" hasOwnProperty'); 119 120 //assertEquals(true, 0 in "foo", "0 in"); 121 //assertEquals(false, -1 in "foo", "-1 in"); 122 //assertEquals(true, 2 in "foo", "2 in"); 123 //assertEquals(false, 3 in "foo", "3 in"); 124 // 125 //assertEquals(true, "0" in "foo", '"0" in'); 126 //assertEquals(false, "-1" in "foo", '"-1" in'); 127 //assertEquals(true, "2" in "foo", '"2" in'); 128 //assertEquals(false, "3" in "foo", '"3" in'); 129 130 delete S[3]; 131 assertEquals("undefined", typeof(S[3])); 132 assertEquals(false, 3 in S); 133 assertEquals(false, "3" in S); 134 135 var N = new Number(43); 136 assertEquals(Object(43), N); 137 N[-2] = "Alpha"; 138 assertEquals("Alpha", N[-2]); 139 N[0] = "Zappa"; 140 assertEquals("Zappa", N[0]); 141 assertEquals("Zappa", N["0"]); 142 143 var A = ["V", "e", "t", "t", "e", "r"]; 144 var A2 = (A[0] = "v"); 145 assertEquals('v', A[0]); 146 assertEquals('v', A2); 147 148 var S = new String("Onkel"); 149 var S2 = (S[0] = 'o'); 150 assertEquals('O', S[0]); 151 assertEquals('o', S2); 152 153 var s = "Tante"; 154 var s2 = (s[0] = 't'); 155 assertEquals('T', s[0]); 156 assertEquals('t', s2); 157 158 var S2 = (S[-2] = 'o'); 159 assertEquals('o', S[-2]); 160 assertEquals('o', S2); 161 162 var s2 = (s[-2] = 't'); 163 assertEquals('undefined', typeof(s[-2])); 164 assertEquals('t', s2); 165 166 // Make sure enough of the one-char string cache is filled. 167 var alpha = ['@']; 168 for (var i = 1; i < 128; i++) { 169 var c = String.fromCharCode(i); 170 alpha[i] = c[0]; 171 } 172 var alphaStr = alpha.join(""); 173 174 // Now test chars. 175 for (var i = 1; i < 128; i++) { 176 assertEquals(alpha[i], alphaStr[i]); 177 assertEquals(String.fromCharCode(i), alphaStr[i]); 178 } 179 180 // Test for keyed ic. 181 var foo = ['a12', ['a', 2, 'c'], 'a31', 42]; 182 var results = [1, 2, 3, NaN]; 183 for (var i = 0; i < 200; ++i) { 184 var index = Math.floor(i / 50); 185 var receiver = foo[index]; 186 var expected = results[index]; 187 var actual = +(receiver[1]); 188 assertEquals(expected, actual); 189 } 190 191 var keys = [0, '1', 2, 3.0, -1, 10]; 192 var str = 'abcd', arr = ['a', 'b', 'c', 'd', undefined, undefined]; 193 for (var i = 0; i < 300; ++i) { 194 var index = Math.floor(i / 50); 195 var key = keys[index]; 196 var expected = arr[index]; 197 var actual = str[key]; 198 assertEquals(expected, actual); 199 } 200 201 // Test heap number case. 202 var keys = [0, Math.floor(2) * 0.5]; 203 var str = 'ab', arr = ['a', 'b']; 204 for (var i = 0; i < 100; ++i) { 205 var index = Math.floor(i / 50); 206 var key = keys[index]; 207 var expected = arr[index]; 208 var actual = str[key]; 209 assertEquals(expected, actual); 210 } 211 212 // Test negative zero case. 213 var keys = [0, -0.0]; 214 var str = 'ab', arr = ['a', 'a']; 215 for (var i = 0; i < 100; ++i) { 216 var index = Math.floor(i / 50); 217 var key = keys[index]; 218 var expected = arr[index]; 219 var actual = str[key]; 220 assertEquals(expected, actual); 221 } 222 223 // Test "not-an-array-index" case. 224 var keys = [0, 0.5]; 225 var str = 'ab', arr = ['a', undefined]; 226 for (var i = 0; i < 100; ++i) { 227 var index = Math.floor(i / 50); 228 var key = keys[index]; 229 var expected = arr[index]; 230 var actual = str[key]; 231 assertEquals(expected, actual); 232 } 233 234 // Test out of range case. 235 var keys = [0, -1]; 236 var str = 'ab', arr = ['a', undefined]; 237 for (var i = 0; i < 100; ++i) { 238 var index = Math.floor(i / 50); 239 var key = keys[index]; 240 var expected = arr[index]; 241 var actual = str[key]; 242 assertEquals(expected, actual); 243 } 244 245 var keys = [0, 10]; 246 var str = 'ab', arr = ['a', undefined]; 247 for (var i = 0; i < 100; ++i) { 248 var index = Math.floor(i / 50); 249 var key = keys[index]; 250 var expected = arr[index]; 251 var actual = str[key]; 252 assertEquals(expected, actual); 253 } 254 255 // Test out of range with a heap number case. 256 var num = Math.floor(4) * 0.5; 257 // TODO(mvstanton): figure out a reliable way to get a heap number every time. 258 // assertFalse(!%_IsSmi(num)); 259 var keys = [0, num]; 260 var str = 'ab', arr = ['a', undefined]; 261 for (var i = 0; i < 100; ++i) { 262 var index = Math.floor(i / 50); 263 var key = keys[index]; 264 var expected = arr[index]; 265 var actual = str[key]; 266 assertEquals(expected, actual); 267 } 268 269 // Test two byte string. 270 var str = '\u0427', arr = ['\u0427']; 271 for (var i = 0; i < 50; ++i) { 272 var expected = arr[0]; 273 var actual = str[0]; 274 assertEquals(expected, actual); 275 } 276