1 // Copyright 2010 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: --expose-externalize-string --expose-gc 29 // Test data pointer caching of external strings. 30 31 function test() { 32 // Test string.charAt. 33 var charat_str = new Array(5); 34 charat_str[0] = "0123456789ABCDEF0123456789ABCDEF\ 35 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ 36 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ 37 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF\ 38 0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF0123456789ABCDEF"; 39 charat_str[1] = "0123456789ABCDEF"; 40 for (var i = 0; i < 6; i++) charat_str[1] += charat_str[1]; 41 try { // String can only be externalized once 42 externalizeString(charat_str[0], false); 43 externalizeString(charat_str[1], true); 44 } catch (ex) { } 45 charat_str[2] = charat_str[0].slice(0, -1); 46 charat_str[3] = charat_str[1].slice(0, -1); 47 charat_str[4] = charat_str[0] + charat_str[0]; 48 49 for (var i = 0; i < 5; i++) { 50 assertEquals('B', charat_str[i].charAt(6*16 + 11)); 51 assertEquals('C', charat_str[i].charAt(6*16 + 12)); 52 assertEquals('A', charat_str[i].charAt(3*16 + 10)); 53 assertEquals('B', charat_str[i].charAt(3*16 + 11)); 54 } 55 56 charat_short = "012"; 57 try { // String can only be externalized once 58 externalizeString(charat_short, true); 59 } catch (ex) { } 60 assertEquals("1", charat_short.charAt(1)); 61 62 // Test regexp and short substring. 63 var re = /(A|B)/; 64 var rere = /(T.{1,2}B)/; 65 var ascii = "ABCDEFGHIJKLMNOPQRST"; 66 var twobyte = "_ABCDEFGHIJKLMNOPQRST"; 67 try { 68 externalizeString(ascii, false); 69 externalizeString(twobyte, true); 70 } catch (ex) { } 71 assertTrue(isOneByteString(ascii)); 72 assertFalse(isOneByteString(twobyte)); 73 var ascii_slice = ascii.slice(1,-1); 74 var twobyte_slice = twobyte.slice(2,-1); 75 var ascii_cons = ascii + ascii; 76 var twobyte_cons = twobyte + twobyte; 77 for (var i = 0; i < 2; i++) { 78 assertEquals(["A", "A"], re.exec(ascii)); 79 assertEquals(["B", "B"], re.exec(ascii_slice)); 80 assertEquals(["TAB", "TAB"], rere.exec(ascii_cons)); 81 assertEquals(["A", "A"], re.exec(twobyte)); 82 assertEquals(["B", "B"], re.exec(twobyte_slice)); 83 assertEquals(["T_AB", "T_AB"], rere.exec(twobyte_cons)); 84 assertEquals("DEFG", ascii_slice.substr(2, 4)); 85 assertEquals("DEFG", twobyte_slice.substr(2, 4)); 86 assertEquals("DEFG", ascii_cons.substr(3, 4)); 87 assertEquals("DEFG", twobyte_cons.substr(4, 4)); 88 } 89 90 // Test adding external strings 91 var short_ascii = "E="; 92 var long_ascii = "MCsquared"; 93 var short_twobyte = "E\u1234"; 94 var long_twobyte = "MCsquare\u1234"; 95 try { // String can only be externalized once 96 externalizeString(short_ascii, false); 97 externalizeString(long_ascii, false); 98 externalizeString(short_twobyte, true); 99 externalizeString(long_twobyte, true); 100 assertTrue(isOneByteString(short_asii) && isOneByteString(long_ascii)); 101 assertFalse(isOneByteString(short_twobyte) || isOneByteString(long_twobyte)); 102 } catch (ex) { } 103 assertEquals("E=MCsquared", short_ascii + long_ascii); 104 assertTrue(isOneByteString(short_ascii + long_ascii)); 105 assertEquals("MCsquaredE=", long_ascii + short_ascii); 106 assertEquals("E\u1234MCsquare\u1234", short_twobyte + long_twobyte); 107 assertFalse(isOneByteString(short_twobyte + long_twobyte)); 108 assertEquals("E=MCsquared", "E=" + long_ascii); 109 assertEquals("E\u1234MCsquared", short_twobyte + "MCsquared"); 110 assertEquals("E\u1234MCsquared", short_twobyte + long_ascii); 111 assertFalse(isOneByteString(short_twobyte + long_ascii)); 112 } 113 114 // Run the test many times to ensure IC-s don't break things. 115 for (var i = 0; i < 10; i++) { 116 test(); 117 } 118 119 // Clean up string to make Valgrind happy. 120 gc(); 121 gc(); 122