OLD | NEW |
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 |
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: --expose-externalize-string --expose-gc | 28 // Flags: --expose-externalize-string --expose-gc |
29 | 29 // Test data pointer caching of external strings. |
30 var size = 1024; | |
31 | 30 |
32 function test() { | 31 function test() { |
33 var str = ""; | 32 // Test string.charAt. |
34 | |
35 // Build an ascii cons string. | |
36 for (var i = 0; i < size; i++) { | |
37 str += String.fromCharCode(i & 0x7f); | |
38 } | |
39 assertTrue(isAsciiString(str)); | |
40 | |
41 var twoByteExternalWithAsciiData = | |
42 "AAAAAAAA" + (function() { return "A"; })(); | |
43 externalizeString(twoByteExternalWithAsciiData, true /* force two-byte */); | |
44 assertFalse(isAsciiString(twoByteExternalWithAsciiData)); | |
45 | |
46 var realTwoByteExternalString = | |
47 "\u1234\u1234\u1234\u1234" + (function() { return "\u1234"; })(); | |
48 externalizeString(realTwoByteExternalString); | |
49 assertFalse(isAsciiString(realTwoByteExternalString)); | |
50 | |
51 assertTrue(isAsciiString(["a", twoByteExternalWithAsciiData].join(""))); | |
52 | |
53 // Appending a two-byte string that contains only ascii chars should | |
54 // still produce an ascii cons. | |
55 var str1 = str + twoByteExternalWithAsciiData; | |
56 assertTrue(isAsciiString(str1)); | |
57 | |
58 // Force flattening of the string. | |
59 var old_length = str1.length - twoByteExternalWithAsciiData.length; | |
60 for (var i = 0; i < old_length; i++) { | |
61 assertEquals(String.fromCharCode(i & 0x7f), str1[i]); | |
62 } | |
63 for (var i = old_length; i < str1.length; i++) { | |
64 assertEquals("A", str1[i]); | |
65 } | |
66 | |
67 // Flattened string should still be ascii. | |
68 assertTrue(isAsciiString(str1)); | |
69 | |
70 // Lower-casing an ascii string should produce ascii. | |
71 assertTrue(isAsciiString(str1.toLowerCase())); | |
72 | |
73 assertFalse(isAsciiString(["a", realTwoByteExternalString].join(""))); | |
74 | |
75 // Appending a real two-byte string should produce a two-byte cons. | |
76 var str2 = str + realTwoByteExternalString; | |
77 assertFalse(isAsciiString(str2)); | |
78 | |
79 // Force flattening of the string. | |
80 old_length = str2.length - realTwoByteExternalString.length; | |
81 for (var i = 0; i < old_length; i++) { | |
82 assertEquals(String.fromCharCode(i & 0x7f), str2[i]); | |
83 } | |
84 for (var i = old_length; i < str.length; i++) { | |
85 assertEquals("\u1234", str2[i]); | |
86 } | |
87 | |
88 // Flattened string should still be two-byte. | |
89 assertFalse(isAsciiString(str2)); | |
90 | |
91 // Test buffered external strings. | |
92 var charat_str = new Array(5); | 33 var charat_str = new Array(5); |
93 charat_str[0] = "0123456789ABCDEF0123456789ABCDEF\ | 34 charat_str[0] = "0123456789ABCDEF0123456789ABCDEF\ |
94 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ | 35 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ |
95 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ | 36 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ |
96 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ | 37 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ |
97 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; | 38 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; |
98 charat_str[1] = "0123456789ABCDEF"; | 39 charat_str[1] = "0123456789ABCDEF"; |
99 for (var i = 0; i < 6; i++) charat_str[1] += charat_str[1]; | 40 for (var i = 0; i < 6; i++) charat_str[1] += charat_str[1]; |
100 try { // String can only be externalized once | 41 try { // String can only be externalized once |
101 externalizeString(charat_str[0], false); | 42 externalizeString(charat_str[0], false); |
102 externalizeString(charat_str[1], true); | 43 externalizeString(charat_str[1], true); |
103 } catch (ex) { } | 44 } catch (ex) { } |
104 charat_str[2] = charat_str[0].slice(0, -1); | 45 charat_str[2] = charat_str[0].slice(0, -1); |
105 charat_str[3] = charat_str[1].slice(0, -1); | 46 charat_str[3] = charat_str[1].slice(0, -1); |
106 charat_str[4] = charat_str[0] + charat_str[0]; | 47 charat_str[4] = charat_str[0] + charat_str[0]; |
107 | 48 |
108 for (var i = 0; i < 5; i++) { | 49 for (var i = 0; i < 5; i++) { |
109 assertEquals('B', charat_str[i].charAt(6*16 + 11)); | 50 assertEquals('B', charat_str[i].charAt(6*16 + 11)); |
110 assertEquals('C', charat_str[i].charAt(6*16 + 12)); | 51 assertEquals('C', charat_str[i].charAt(6*16 + 12)); |
111 assertEquals('A', charat_str[i].charAt(3*16 + 10)); | 52 assertEquals('A', charat_str[i].charAt(3*16 + 10)); |
112 assertEquals('B', charat_str[i].charAt(3*16 + 11)); | 53 assertEquals('B', charat_str[i].charAt(3*16 + 11)); |
113 } | 54 } |
| 55 |
| 56 // Test regexp. |
| 57 var re = /(A|B)/; |
| 58 var rere = /(T.{1,2}B)/; |
| 59 var ascii = "ABCDEFGHIJKLMNOPQRST"; |
| 60 var twobyte = "_ABCDEFGHIJKLMNOPQRST"; |
| 61 try { |
| 62 externalizeString(ascii, false); |
| 63 externalizeString(twobyte, true); |
| 64 } catch (ex) { } |
| 65 assertTrue(isAsciiString(ascii)); |
| 66 assertFalse(isAsciiString(twobyte)); |
| 67 var ascii_slice = ascii.slice(1,-1); |
| 68 var twobyte_slice = twobyte.slice(2,-1); |
| 69 var ascii_cons = ascii + ascii; |
| 70 var twobyte_cons = twobyte + twobyte; |
| 71 for (var i = 0; i < 2; i++) { |
| 72 assertEquals(["A", "A"], re.exec(ascii)); |
| 73 assertEquals(["B", "B"], re.exec(ascii_slice)); |
| 74 assertEquals(["TAB", "TAB"], rere.exec(ascii_cons)); |
| 75 assertEquals(["A", "A"], re.exec(twobyte)); |
| 76 assertEquals(["B", "B"], re.exec(twobyte_slice)); |
| 77 assertEquals(["T_AB", "T_AB"], rere.exec(twobyte_cons)); |
| 78 } |
114 } | 79 } |
115 | 80 |
116 // Run the test many times to ensure IC-s don't break things. | 81 // Run the test many times to ensure IC-s don't break things. |
117 for (var i = 0; i < 10; i++) { | 82 for (var i = 0; i < 10; i++) { |
118 test(); | 83 test(); |
119 } | 84 } |
120 | 85 |
121 // Clean up string to make Valgrind happy. | 86 // Clean up string to make Valgrind happy. |
122 gc(); | 87 gc(); |
123 gc(); | 88 gc(); |
OLD | NEW |