OLD | NEW |
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 |
11 // with the distribution. | 11 // with the distribution. |
12 // * Neither the name of Google Inc. nor the names of its | 12 // * Neither the name of Google Inc. nor the names of its |
13 // contributors may be used to endorse or promote products derived | 13 // contributors may be used to endorse or promote products derived |
14 // from this software without specific prior written permission. | 14 // from this software without specific prior written permission. |
15 // | 15 // |
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | 16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | 17 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | 18 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | 19 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | 20 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | 21 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
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 var s = "test"; | 28 var s = "test"; |
29 | 29 |
30 assertEquals("t", s.charAt()); | 30 var slowIndex1 = { valueOf: function() { return 1; } }; |
31 assertEquals("t", s.charAt("string")); | 31 var slowIndex2 = { toString: function() { return "2"; } }; |
32 assertEquals("t", s.charAt(null)); | 32 var slowIndexOutOfRange = { valueOf: function() { return -1; } }; |
33 assertEquals("t", s.charAt(void 0)); | |
34 assertEquals("t", s.charAt(false)); | |
35 assertEquals("e", s.charAt(true)); | |
36 assertEquals("", s.charAt(-1)); | |
37 assertEquals("", s.charAt(4)); | |
38 assertEquals("t", s.charAt(0)); | |
39 assertEquals("t", s.charAt(3)); | |
40 assertEquals("t", s.charAt(NaN)); | |
41 | 33 |
42 assertEquals(116, s.charCodeAt()); | 34 function basicTest() { |
43 assertEquals(116, s.charCodeAt("string")); | 35 assertEquals("t", s.charAt()); |
44 assertEquals(116, s.charCodeAt(null)); | 36 assertEquals("t", s.charAt("string")); |
45 assertEquals(116, s.charCodeAt(void 0)); | 37 assertEquals("t", s.charAt(null)); |
46 assertEquals(116, s.charCodeAt(false)); | 38 assertEquals("t", s.charAt(void 0)); |
47 assertEquals(101, s.charCodeAt(true)); | 39 assertEquals("t", s.charAt(false)); |
48 assertEquals(116, s.charCodeAt(0)); | 40 assertEquals("e", s.charAt(true)); |
49 assertEquals(116, s.charCodeAt(3)); | 41 assertEquals("", s.charAt(-1)); |
50 assertEquals(116, s.charCodeAt(NaN)); | 42 assertEquals("", s.charAt(4)); |
51 assertTrue(isNaN(s.charCodeAt(-1))); | 43 assertEquals("", s.charAt(slowIndexOutOfRange)); |
52 assertTrue(isNaN(s.charCodeAt(4))); | 44 assertEquals("t", s.charAt(0)); |
| 45 assertEquals("t", s.charAt(-0.0)); |
| 46 assertEquals("t", s.charAt(0.4)); |
| 47 assertEquals("e", s.charAt(slowIndex1)); |
| 48 assertEquals("s", s.charAt(slowIndex2)); |
| 49 assertEquals("t", s.charAt(3)); |
| 50 assertEquals("t", s.charAt(3.4)); |
| 51 assertEquals("t", s.charAt(NaN)); |
| 52 |
| 53 assertEquals(116, s.charCodeAt()); |
| 54 assertEquals(116, s.charCodeAt("string")); |
| 55 assertEquals(116, s.charCodeAt(null)); |
| 56 assertEquals(116, s.charCodeAt(void 0)); |
| 57 assertEquals(116, s.charCodeAt(false)); |
| 58 assertEquals(101, s.charCodeAt(true)); |
| 59 assertEquals(116, s.charCodeAt(0)); |
| 60 assertEquals(116, s.charCodeAt(-0.0)); |
| 61 assertEquals(116, s.charCodeAt(0.4)); |
| 62 assertEquals(101, s.charCodeAt(slowIndex1)); |
| 63 assertEquals(115, s.charCodeAt(slowIndex2)); |
| 64 assertEquals(116, s.charCodeAt(3)); |
| 65 assertEquals(116, s.charCodeAt(3.4)); |
| 66 assertEquals(116, s.charCodeAt(NaN)); |
| 67 assertTrue(isNaN(s.charCodeAt(-1))); |
| 68 assertTrue(isNaN(s.charCodeAt(4))); |
| 69 assertTrue(isNaN(s.charCodeAt(slowIndexOutOfRange))); |
| 70 } |
| 71 basicTest(); |
53 | 72 |
54 // Make sure enough of the one-char string cache is filled. | 73 // Make sure enough of the one-char string cache is filled. |
55 var alpha = ['@']; | 74 var alpha = ['@']; |
56 for (var i = 1; i < 128; i++) { | 75 for (var i = 1; i < 128; i++) { |
57 var c = String.fromCharCode(i); | 76 var c = String.fromCharCode(i); |
58 alpha[i] = c.charAt(0); | 77 alpha[i] = c.charAt(0); |
59 } | 78 } |
60 var alphaStr = alpha.join(""); | 79 var alphaStr = alpha.join(""); |
61 | 80 |
62 // Now test chars. | 81 // Now test chars. |
63 for (var i = 1; i < 128; i++) { | 82 for (var i = 1; i < 128; i++) { |
64 assertEquals(alpha[i], alphaStr.charAt(i)); | 83 assertEquals(alpha[i], alphaStr.charAt(i)); |
65 assertEquals(String.fromCharCode(i), alphaStr.charAt(i)); | 84 assertEquals(String.fromCharCode(i), alphaStr.charAt(i)); |
66 } | 85 } |
| 86 |
| 87 // Test stealing String.prototype.{charAt,charCodeAt}. |
| 88 var o = { |
| 89 charAt: String.prototype.charAt, |
| 90 charCodeAt: String.prototype.charCodeAt, |
| 91 toString: function() { return "012"; }, |
| 92 valueOf: function() { return "should not be called"; } |
| 93 }; |
| 94 |
| 95 function stealTest() { |
| 96 assertEquals("0", o.charAt(0)); |
| 97 assertEquals("1", o.charAt(1)); |
| 98 assertEquals("1", o.charAt(1.4)); |
| 99 assertEquals("1", o.charAt(slowIndex1)); |
| 100 assertEquals("2", o.charAt(2)); |
| 101 assertEquals("2", o.charAt(slowIndex2)); |
| 102 assertEquals(48, o.charCodeAt(0)); |
| 103 assertEquals(49, o.charCodeAt(1)); |
| 104 assertEquals(49, o.charCodeAt(1.4)); |
| 105 assertEquals(49, o.charCodeAt(slowIndex1)); |
| 106 assertEquals(50, o.charCodeAt(2)); |
| 107 assertEquals(50, o.charCodeAt(slowIndex2)); |
| 108 assertEquals("", o.charAt(-1)); |
| 109 assertEquals("", o.charAt(-1.4)); |
| 110 assertEquals("", o.charAt(10)); |
| 111 assertEquals("", o.charAt(slowIndexOutOfRange)); |
| 112 assertTrue(isNaN(o.charCodeAt(-1))); |
| 113 assertTrue(isNaN(o.charCodeAt(-1.4))); |
| 114 assertTrue(isNaN(o.charCodeAt(10))); |
| 115 assertTrue(isNaN(o.charCodeAt(slowIndexOutOfRange))); |
| 116 } |
| 117 stealTest(); |
| 118 |
| 119 // Test custom string IC-s. |
| 120 for (var i = 0; i < 20; i++) { |
| 121 basicTest(); |
| 122 stealTest(); |
| 123 } |
| 124 |
| 125 var badToString = function() { return []; }; |
| 126 |
| 127 function testBadToString_charAt() { |
| 128 var goodToString = o.toString; |
| 129 var hasCaught = false; |
| 130 var numCalls = 0; |
| 131 var result; |
| 132 try { |
| 133 for (var i = 0; i < 20; i++) { |
| 134 if (i == 10) o.toString = o.valueOf = badToString; |
| 135 result = o.charAt(1); |
| 136 numCalls++; |
| 137 } |
| 138 } catch (e) { |
| 139 hasCaught = true; |
| 140 } finally { |
| 141 o.toString = goodToString; |
| 142 } |
| 143 assertTrue(hasCaught); |
| 144 assertEquals("1", result); |
| 145 assertEquals(10, numCalls); |
| 146 } |
| 147 testBadToString_charAt(); |
| 148 |
| 149 function testBadToString_charCodeAt() { |
| 150 var goodToString = o.toString; |
| 151 var hasCaught = false; |
| 152 var numCalls = 0; |
| 153 var result; |
| 154 try { |
| 155 for (var i = 0; i < 20; i++) { |
| 156 if (i == 10) o.toString = o.valueOf = badToString; |
| 157 result = o.charCodeAt(1); |
| 158 numCalls++; |
| 159 } |
| 160 } catch (e) { |
| 161 hasCaught = true; |
| 162 } finally { |
| 163 o.toString = goodToString; |
| 164 } |
| 165 assertTrue(hasCaught); |
| 166 assertEquals(49, result); |
| 167 assertEquals(10, numCalls); |
| 168 } |
| 169 testBadToString_charCodeAt(); |
| 170 |
| 171 var badIndex = { |
| 172 toString: badToString, |
| 173 valueOf: badToString |
| 174 }; |
| 175 |
| 176 function testBadIndex_charAt() { |
| 177 var index = 1; |
| 178 var hasCaught = false; |
| 179 var numCalls = 0; |
| 180 var result; |
| 181 try { |
| 182 for (var i = 0; i < 20; i++) { |
| 183 if (i == 10) index = badIndex; |
| 184 result = o.charAt(index); |
| 185 numCalls++; |
| 186 } |
| 187 } catch (e) { |
| 188 hasCaught = true; |
| 189 } |
| 190 assertTrue(hasCaught); |
| 191 assertEquals("1", result); |
| 192 assertEquals(10, numCalls); |
| 193 } |
| 194 testBadIndex_charAt(); |
| 195 |
| 196 function testBadIndex_charCodeAt() { |
| 197 var index = 1; |
| 198 var hasCaught = false; |
| 199 var numCalls = 0; |
| 200 var result; |
| 201 try { |
| 202 for (var i = 0; i < 20; i++) { |
| 203 if (i == 10) index = badIndex; |
| 204 result = o.charCodeAt(index); |
| 205 numCalls++; |
| 206 } |
| 207 } catch (e) { |
| 208 hasCaught = true; |
| 209 } |
| 210 assertTrue(hasCaught); |
| 211 assertEquals(49, result); |
| 212 assertEquals(10, numCalls); |
| 213 } |
| 214 testBadIndex_charCodeAt(); |
| 215 |
| 216 function testPrototypeChange_charAt() { |
| 217 var result, oldResult; |
| 218 for (var i = 0; i < 20; i++) { |
| 219 if (i == 10) { |
| 220 oldResult = result; |
| 221 String.prototype.charAt = function() { return "%"; }; |
| 222 } |
| 223 result = s.charAt(1); |
| 224 } |
| 225 assertEquals("%", result); |
| 226 assertEquals("e", oldResult); |
| 227 delete String.prototype.charAt; // Restore the default. |
| 228 } |
| 229 testPrototypeChange_charAt(); |
| 230 |
| 231 function testPrototypeChange_charCodeAt() { |
| 232 var result, oldResult; |
| 233 for (var i = 0; i < 20; i++) { |
| 234 if (i == 10) { |
| 235 oldResult = result; |
| 236 String.prototype.charCodeAt = function() { return 42; }; |
| 237 } |
| 238 result = s.charCodeAt(1); |
| 239 } |
| 240 assertEquals(42, result); |
| 241 assertEquals(101, oldResult); |
| 242 delete String.prototype.charCodeAt; // Restore the default. |
| 243 } |
| 244 testPrototypeChange_charCodeAt(); |
OLD | NEW |