OLD | NEW |
---|---|
1 // Copyright 2011 the V8 project authors. All rights reserved. | 1 // Copyright 2011 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 // Flags: --string-slices --expose-externalize-string | 28 // Flags: --string-slices --expose-externalize-string --allow-natives-syntax |
Lasse Reichstein
2011/11/09 12:48:11
If string-slices is on by default now, we should r
| |
29 | 29 |
30 var s = 'abcdefghijklmn'; | 30 var s = 'abcdefghijklmn'; |
31 assertEquals(s, s.substr()); | 31 assertEquals(s, s.substr()); |
32 assertEquals(s, s.substr(0)); | 32 assertEquals(s, s.substr(0)); |
33 assertEquals(s, s.substr('0')); | 33 assertEquals(s, s.substr('0')); |
34 assertEquals(s, s.substr(void 0)); | 34 assertEquals(s, s.substr(void 0)); |
35 assertEquals(s, s.substr(null)); | 35 assertEquals(s, s.substr(null)); |
36 assertEquals(s, s.substr(false)); | 36 assertEquals(s, s.substr(false)); |
37 assertEquals(s, s.substr(0.9)); | 37 assertEquals(s, s.substr(0.9)); |
38 assertEquals(s, s.substr({ valueOf: function() { return 0; } })); | 38 assertEquals(s, s.substr({ valueOf: function() { return 0; } })); |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
93 var w = Math.random() * 42; // Allocate something new in new-space. | 93 var w = Math.random() * 42; // Allocate something new in new-space. |
94 assertEquals(j, z.length); | 94 assertEquals(j, z.length); |
95 for (var k = 0; k < j; k++) { | 95 for (var k = 0; k < j; k++) { |
96 assertEquals(x.charAt(i+k), z.charAt(k)); | 96 assertEquals(x.charAt(i+k), z.charAt(k)); |
97 } | 97 } |
98 } | 98 } |
99 } | 99 } |
100 | 100 |
101 // Keep creating strings to to force allocation failure on substring creation. | 101 // Keep creating strings to to force allocation failure on substring creation. |
102 var x = "0123456789ABCDEF"; | 102 var x = "0123456789ABCDEF"; |
103 x += x; // 2^5 | 103 for (var i = 0; i < 8; i++) x += x; |
104 x += x; | |
105 x += x; | |
106 x += x; | |
107 x += x; | |
108 x += x; // 2^10 | |
109 x += x; | |
110 x += x; | |
111 var xl = x.length; | 104 var xl = x.length; |
112 var cache = []; | 105 var cache = []; |
113 for (var i = 0; i < 1000; i++) { | 106 for (var i = 0; i < 1000; i++) { |
114 var z = x.substring(i % xl); | 107 var z = x.substring(i % xl); |
115 assertEquals(xl - (i % xl), z.length); | 108 assertEquals(xl - (i % xl), z.length); |
116 cache.push(z); | 109 cache.push(z); |
117 } | 110 } |
118 | 111 |
119 | 112 |
120 // Same with two-byte strings | 113 // Same with two-byte strings |
121 var x = "\u2028123456789ABCDEF"; | 114 var x = "\u2028123456789ABCDEF"; |
122 x += x; // 2^5 | 115 for (var i = 0; i < 8; i++) x += x; |
123 x += x; | |
124 x += x; | |
125 x += x; | |
126 x += x; | |
127 x += x; // 2^10 | |
128 x += x; | |
129 x += x; | |
130 var xl = x.length; | 116 var xl = x.length; |
131 var cache = []; | 117 var cache = []; |
132 for (var i = 0; i < 1000; i++) { | 118 for (var i = 0; i < 1000; i++) { |
133 var z = x.substring(i % xl); | 119 var z = x.substring(i % xl); |
134 assertEquals(xl - (i % xl), z.length); | 120 assertEquals(xl - (i % xl), z.length); |
135 cache.push(z); | 121 cache.push(z); |
136 } | 122 } |
137 | 123 |
138 // Substring of substring. | 124 // Substring of substring. |
139 var cache = []; | 125 var cache = []; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
195 assertEquals(a.slice(1,-1), b); | 181 assertEquals(a.slice(1,-1), b); |
196 | 182 |
197 assertTrue(isAsciiString(a)); | 183 assertTrue(isAsciiString(a)); |
198 externalizeString(a, true); | 184 externalizeString(a, true); |
199 assertFalse(isAsciiString(a)); | 185 assertFalse(isAsciiString(a)); |
200 | 186 |
201 assertEquals(a.slice(1,-1), b); | 187 assertEquals(a.slice(1,-1), b); |
202 assertTrue(/3456789qwe/.test(a)); | 188 assertTrue(/3456789qwe/.test(a)); |
203 assertEquals(5, a.indexOf("678")); | 189 assertEquals(5, a.indexOf("678")); |
204 assertEquals("12345", a.split("6")[0]); | 190 assertEquals("12345", a.split("6")[0]); |
191 | |
192 // Create a slice with an external string as parent string. | |
193 var c = a.slice(1,-1); | |
194 | |
195 function test_crankshaft() { | |
196 for (var i = 0; i < 20; i++) { | |
197 assertEquals(b.charAt(i), a.charAt(i + 1)); | |
198 assertEquals(b.charAt(i), c.charAt(i)); | |
199 assertEquals(b.charAt(4), c.charAt(4)); | |
200 assertTrue(/3456789qwe/.test(c)); | |
201 assertEquals(4, c.indexOf("678")); | |
202 assertEquals("2345", c.split("6")[0]); | |
203 } | |
204 } | |
205 | |
206 test_crankshaft(); | |
207 %OptimizeFunctionOnNextCall(test_crankshaft); | |
208 test_crankshaft(); | |
OLD | NEW |