1// Copyright 2011 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 --allow-natives-syntax 29 30var s = 'abcdefghijklmn'; 31assertEquals(s, s.substr()); 32assertEquals(s, s.substr(0)); 33assertEquals(s, s.substr('0')); 34assertEquals(s, s.substr(void 0)); 35assertEquals(s, s.substr(null)); 36assertEquals(s, s.substr(false)); 37assertEquals(s, s.substr(0.9)); 38assertEquals(s, s.substr({ valueOf: function() { return 0; } })); 39assertEquals(s, s.substr({ toString: function() { return '0'; } })); 40 41var s1 = s.substring(1); 42assertEquals(s1, s.substr(1)); 43assertEquals(s1, s.substr('1')); 44assertEquals(s1, s.substr(true)); 45assertEquals(s1, s.substr(1.1)); 46assertEquals(s1, s.substr({ valueOf: function() { return 1; } })); 47assertEquals(s1, s.substr({ toString: function() { return '1'; } })); 48 49 50assertEquals(s.substring(s.length - 1), s.substr(-1)); 51assertEquals(s.substring(s.length - 1), s.substr(-1.2)); 52assertEquals(s.substring(s.length - 1), s.substr(-1.7)); 53assertEquals(s.substring(s.length - 2), s.substr(-2)); 54assertEquals(s.substring(s.length - 2), s.substr(-2.3)); 55assertEquals(s.substring(s.length - 2, s.length - 1), s.substr(-2, 1)); 56assertEquals(s, s.substr(-100)); 57assertEquals('abc', s.substr(-100, 3)); 58assertEquals(s1, s.substr(-s.length + 1)); 59 60// assertEquals('', s.substr(0, void 0)); // smjs and rhino 61assertEquals('abcdefghijklmn', s.substr(0, void 0)); // kjs and v8 62assertEquals('', s.substr(0, null)); 63assertEquals(s, s.substr(0, String(s.length))); 64assertEquals('a', s.substr(0, true)); 65 66 67// Test substrings of different lengths and alignments. 68// First ASCII. 69var x = "ASCII"; 70for (var i = 0; i < 25; i++) { 71 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); 72} 73/x/.exec(x); // Try to force a flatten. 74for (var i = 5; i < 25; i++) { 75 for (var j = 0; j < 25; j++) { 76 var z = x.substring(i, i+j); 77 var w = Math.random() * 42; // Allocate something new in new-space. 78 assertEquals(j, z.length); 79 for (var k = 0; k < j; k++) { 80 assertEquals(x.charAt(i+k), z.charAt(k)); 81 } 82 } 83} 84// Then two-byte strings. 85x = "UC16\u2028"; // Non-ascii char forces two-byte string. 86for (var i = 0; i < 25; i++) { 87 x += (i >> 4).toString(16) + (i & 0x0f).toString(16); 88} 89/x/.exec(x); // Try to force a flatten. 90for (var i = 5; i < 25; i++) { 91 for (var j = 0; j < 25; j++) { 92 var z = x.substring(i, i + j); 93 var w = Math.random() * 42; // Allocate something new in new-space. 94 assertEquals(j, z.length); 95 for (var k = 0; k < j; k++) { 96 assertEquals(x.charAt(i+k), z.charAt(k)); 97 } 98 } 99} 100 101// Keep creating strings to to force allocation failure on substring creation. 102var x = "0123456789ABCDEF"; 103for (var i = 0; i < 8; i++) x += x; 104var xl = x.length; 105var cache = []; 106for (var i = 0; i < 1000; i++) { 107 var z = x.substring(i % xl); 108 assertEquals(xl - (i % xl), z.length); 109 cache.push(z); 110} 111 112 113// Same with two-byte strings 114var x = "\u2028123456789ABCDEF"; 115for (var i = 0; i < 8; i++) x += x; 116var xl = x.length; 117var cache = []; 118for (var i = 0; i < 1000; i++) { 119 var z = x.substring(i % xl); 120 assertEquals(xl - (i % xl), z.length); 121 cache.push(z); 122} 123 124// Substring of substring. 125var cache = []; 126var last = x; 127var offset = 0; 128for (var i = 0; i < 64; i++) { 129 var z = last.substring(i); 130 last = z; 131 cache.push(z); 132 offset += i; 133} 134for (var i = 63; i >= 0; i--) { 135 var z = cache.pop(); 136 assertTrue(/\u2028123456789ABCDEF/.test(z)); 137 assertEquals(xl - offset, z.length); 138 assertEquals(x.charAt(i*(i+1)/2), z.charAt(0)); 139 offset -= i; 140} 141 142// Test charAt for different strings. 143function f(s1, s2, s3, i) { 144 assertEquals(String.fromCharCode(97+i%11), s1.charAt(i%11)); 145 assertEquals(String.fromCharCode(97+i%11), s2.charAt(i%11)); 146 assertEquals(String.fromCharCode(98+i%11), s3.charAt(i%11)); 147 assertEquals(String.fromCharCode(101), s3.charAt(3)); 148} 149 150flat = "abcdefghijkl12345"; 151cons = flat + flat.toUpperCase(); 152slice = "abcdefghijklmn12345".slice(1, -1); 153for ( var i = 0; i < 1000; i++) { 154 f(flat, cons, slice, i); 155} 156flat = "abcdefghijkl1\u20232345"; 157cons = flat + flat.toUpperCase(); 158slice = "abcdefghijklmn1\u20232345".slice(1, -1); 159for ( var i = 0; i < 1000; i++) { 160 f(flat, cons, slice, i); 161} 162 163// Short substrings. 164flat = "abcdefghijkl12345"; 165cons = flat + flat.toUpperCase(); 166/x/.exec(cons); // Flatten cons 167slice = "abcdefghijklmn12345".slice(1, -1); 168assertEquals("cdefg", flat.substr(2, 5)); 169assertEquals("cdefg", cons.substr(2, 5)); 170assertEquals("cdefg", slice.substr(1, 5)); 171 172flat = "abc\u1234defghijkl12345"; 173cons = flat + flat.toUpperCase(); 174/x/.exec(cons); // Flatten cons 175slice = "abc\u1234defghijklmn12345".slice(1, -1); 176assertEquals("c\u1234def", flat.substr(2, 5)); 177assertEquals("c\u1234def", cons.substr(2, 5)); 178assertEquals("c\u1234def", slice.substr(1, 5)); 179 180// Concatenate substrings. 181var ascii = 'abcdefghijklmnop'; 182var utf = '\u03B1\u03B2\u03B3\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9\u03BA\u03BB'; 183assertEquals("klmno", ascii.substring(10,15) + ascii.substring(16)); 184assertEquals("\u03B4\u03B7", utf.substring(3,4) + utf.substring(6,7)); 185assertEquals("klp", ascii.substring(10,12) + ascii.substring(15,16)); 186assertEquals("\u03B1\u03B4\u03B5", utf.substring(0,1) + utf.substring(5,3)); 187assertEquals("", ascii.substring(16) + utf.substring(16)); 188assertEquals("bcdef\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9", 189 ascii.substring(1,6) + utf.substring(3,9)); 190assertEquals("\u03B4\u03B5\u03B6\u03B7\u03B8\u03B9abcdefghijklmnop", 191 utf.substring(3,9) + ascii); 192assertEquals("\u03B2\u03B3\u03B4\u03B5\u03B4\u03B5\u03B6\u03B7", 193 utf.substring(5,1) + utf.substring(3,7)); 194 195// Externalizing strings. 196var a = "123456789" + "qwertyuiopasdfghjklzxcvbnm"; 197var b = "23456789qwertyuiopasdfghjklzxcvbn" 198assertEquals(a.slice(1,-1), b); 199 200assertTrue(isAsciiString(a)); 201externalizeString(a, true); 202assertFalse(isAsciiString(a)); 203 204assertEquals(a.slice(1,-1), b); 205assertTrue(/3456789qwe/.test(a)); 206assertEquals(5, a.indexOf("678")); 207assertEquals("12345", a.split("6")[0]); 208 209// Create a slice with an external string as parent string. 210var c = a.slice(1,-1); 211 212function test_crankshaft() { 213 for (var i = 0; i < 20; i++) { 214 assertEquals(b.charAt(i), a.charAt(i + 1)); 215 assertEquals(b.charAt(i), c.charAt(i)); 216 assertEquals(b.charAt(4), c.charAt(4)); 217 assertTrue(/3456789qwe/.test(c)); 218 assertEquals(4, c.indexOf("678")); 219 assertEquals("2345", c.split("6")[0]); 220 } 221} 222 223test_crankshaft(); 224%OptimizeFunctionOnNextCall(test_crankshaft); 225test_crankshaft();