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 function getTwoByteString() { return "\u1234t"; } |
| 31 function getCons() { return "testtesttesttest" + getTwoByteString() } |
| 32 |
30 var slowIndex1 = { valueOf: function() { return 1; } }; | 33 var slowIndex1 = { valueOf: function() { return 1; } }; |
31 var slowIndex2 = { toString: function() { return "2"; } }; | 34 var slowIndex2 = { toString: function() { return "2"; } }; |
32 var slowIndexOutOfRange = { valueOf: function() { return -1; } }; | 35 var slowIndexOutOfRange = { valueOf: function() { return -1; } }; |
33 | 36 |
34 function basicTest() { | 37 function basicTest(s, len) { |
35 assertEquals("t", s.charAt()); | 38 assertEquals("t", s().charAt()); |
36 assertEquals("t", s.charAt("string")); | 39 assertEquals("t", s().charAt("string")); |
37 assertEquals("t", s.charAt(null)); | 40 assertEquals("t", s().charAt(null)); |
38 assertEquals("t", s.charAt(void 0)); | 41 assertEquals("t", s().charAt(void 0)); |
39 assertEquals("t", s.charAt(false)); | 42 assertEquals("t", s().charAt(false)); |
40 assertEquals("e", s.charAt(true)); | 43 assertEquals("e", s().charAt(true)); |
41 assertEquals("", s.charAt(-1)); | 44 assertEquals("", s().charAt(-1)); |
42 assertEquals("", s.charAt(4)); | 45 assertEquals("", s().charAt(len)); |
43 assertEquals("", s.charAt(slowIndexOutOfRange)); | 46 assertEquals("", s().charAt(slowIndexOutOfRange)); |
44 assertEquals("", s.charAt(1/0)); | 47 assertEquals("", s().charAt(1/0)); |
45 assertEquals("", s.charAt(-1/0)); | 48 assertEquals("", s().charAt(-1/0)); |
46 assertEquals("t", s.charAt(0)); | 49 assertEquals("t", s().charAt(0)); |
47 assertEquals("t", s.charAt(-0.0)); | 50 assertEquals("t", s().charAt(-0.0)); |
48 assertEquals("t", s.charAt(0.4)); | 51 assertEquals("t", s().charAt(0.4)); |
49 assertEquals("e", s.charAt(slowIndex1)); | 52 assertEquals("e", s().charAt(slowIndex1)); |
50 assertEquals("s", s.charAt(slowIndex2)); | 53 assertEquals("s", s().charAt(slowIndex2)); |
51 assertEquals("t", s.charAt(3)); | 54 assertEquals("t", s().charAt(3)); |
52 assertEquals("t", s.charAt(3.4)); | 55 assertEquals("t", s().charAt(3.4)); |
53 assertEquals("t", s.charAt(NaN)); | 56 assertEquals("t", s().charAt(NaN)); |
54 | 57 |
55 assertEquals(116, s.charCodeAt()); | 58 assertEquals(116, s().charCodeAt()); |
56 assertEquals(116, s.charCodeAt("string")); | 59 assertEquals(116, s().charCodeAt("string")); |
57 assertEquals(116, s.charCodeAt(null)); | 60 assertEquals(116, s().charCodeAt(null)); |
58 assertEquals(116, s.charCodeAt(void 0)); | 61 assertEquals(116, s().charCodeAt(void 0)); |
59 assertEquals(116, s.charCodeAt(false)); | 62 assertEquals(116, s().charCodeAt(false)); |
60 assertEquals(101, s.charCodeAt(true)); | 63 assertEquals(101, s().charCodeAt(true)); |
61 assertEquals(116, s.charCodeAt(0)); | 64 assertEquals(116, s().charCodeAt(0)); |
62 assertEquals(116, s.charCodeAt(-0.0)); | 65 assertEquals(116, s().charCodeAt(-0.0)); |
63 assertEquals(116, s.charCodeAt(0.4)); | 66 assertEquals(116, s().charCodeAt(0.4)); |
64 assertEquals(101, s.charCodeAt(slowIndex1)); | 67 assertEquals(101, s().charCodeAt(slowIndex1)); |
65 assertEquals(115, s.charCodeAt(slowIndex2)); | 68 assertEquals(115, s().charCodeAt(slowIndex2)); |
66 assertEquals(116, s.charCodeAt(3)); | 69 assertEquals(116, s().charCodeAt(3)); |
67 assertEquals(116, s.charCodeAt(3.4)); | 70 assertEquals(116, s().charCodeAt(3.4)); |
68 assertEquals(116, s.charCodeAt(NaN)); | 71 assertEquals(116, s().charCodeAt(NaN)); |
69 assertTrue(isNaN(s.charCodeAt(-1))); | 72 assertTrue(isNaN(s().charCodeAt(-1))); |
70 assertTrue(isNaN(s.charCodeAt(4))); | 73 assertTrue(isNaN(s().charCodeAt(len))); |
71 assertTrue(isNaN(s.charCodeAt(slowIndexOutOfRange))); | 74 assertTrue(isNaN(s().charCodeAt(slowIndexOutOfRange))); |
72 assertTrue(isNaN(s.charCodeAt(1/0))); | 75 assertTrue(isNaN(s().charCodeAt(1/0))); |
73 assertTrue(isNaN(s.charCodeAt(-1/0))); | 76 assertTrue(isNaN(s().charCodeAt(-1/0))); |
74 } | 77 } |
75 basicTest(); | 78 basicTest(function() { return s; }, s.length); |
| 79 basicTest(getCons, getCons().length); |
76 | 80 |
77 // Make sure enough of the one-char string cache is filled. | 81 // Make sure enough of the one-char string cache is filled. |
78 var alpha = ['@']; | 82 var alpha = ['@']; |
79 for (var i = 1; i < 128; i++) { | 83 for (var i = 1; i < 128; i++) { |
80 var c = String.fromCharCode(i); | 84 var c = String.fromCharCode(i); |
81 alpha[i] = c.charAt(0); | 85 alpha[i] = c.charAt(0); |
82 } | 86 } |
83 var alphaStr = alpha.join(""); | 87 var alphaStr = alpha.join(""); |
84 | 88 |
85 // Now test chars. | 89 // Now test chars. |
(...skipping 29 matching lines...) Expand all Loading... |
115 assertEquals("", o.charAt(slowIndexOutOfRange)); | 119 assertEquals("", o.charAt(slowIndexOutOfRange)); |
116 assertTrue(isNaN(o.charCodeAt(-1))); | 120 assertTrue(isNaN(o.charCodeAt(-1))); |
117 assertTrue(isNaN(o.charCodeAt(-1.4))); | 121 assertTrue(isNaN(o.charCodeAt(-1.4))); |
118 assertTrue(isNaN(o.charCodeAt(10))); | 122 assertTrue(isNaN(o.charCodeAt(10))); |
119 assertTrue(isNaN(o.charCodeAt(slowIndexOutOfRange))); | 123 assertTrue(isNaN(o.charCodeAt(slowIndexOutOfRange))); |
120 } | 124 } |
121 stealTest(); | 125 stealTest(); |
122 | 126 |
123 // Test custom string IC-s. | 127 // Test custom string IC-s. |
124 for (var i = 0; i < 20; i++) { | 128 for (var i = 0; i < 20; i++) { |
125 basicTest(); | 129 basicTest(function() { return s; }, s.length); |
| 130 basicTest(getCons, getCons().length); |
126 stealTest(); | 131 stealTest(); |
127 } | 132 } |
128 | 133 |
129 var badToString = function() { return []; }; | 134 var badToString = function() { return []; }; |
130 | 135 |
131 function testBadToString_charAt() { | 136 function testBadToString_charAt() { |
132 var goodToString = o.toString; | 137 var goodToString = o.toString; |
133 var hasCaught = false; | 138 var hasCaught = false; |
134 var numCalls = 0; | 139 var numCalls = 0; |
135 var result; | 140 var result; |
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
239 oldResult = result; | 244 oldResult = result; |
240 String.prototype.charCodeAt = function() { return 42; }; | 245 String.prototype.charCodeAt = function() { return 42; }; |
241 } | 246 } |
242 result = s.charCodeAt(1); | 247 result = s.charCodeAt(1); |
243 } | 248 } |
244 assertEquals(42, result); | 249 assertEquals(42, result); |
245 assertEquals(101, oldResult); | 250 assertEquals(101, oldResult); |
246 delete String.prototype.charCodeAt; // Restore the default. | 251 delete String.prototype.charCodeAt; // Restore the default. |
247 } | 252 } |
248 testPrototypeChange_charCodeAt(); | 253 testPrototypeChange_charCodeAt(); |
OLD | NEW |