NASHORN-759.js revision 877:cf4d2252d444
1/* 2 * Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24/* 25 * NASHORN-759 - Efficient string concatenation with + operator 26 * 27 * @test 28 * @run 29 */ 30 31// Check invalidation of callsite for non-string CharSequence 32function checkProps(s) { 33 print(typeof s); 34 print(typeof s.length); 35 print(s.charCodeAt === String.prototype.charCodeAt); 36 print(); 37} 38 39var s1 = "foo"; 40var s2 = "bar"; 41var s3 = s1 + s2 + s1 + s2; 42var s4 = new java.lang.StringBuilder("abc"); 43checkProps(s1); 44checkProps(s2); 45checkProps(s3); 46checkProps(s4); 47 48// Rebuild string and compare to original 49function rebuildString(s) { 50 var buf = s.split(""); 51 print(buf); 52 while (buf.length > 1) { 53 var result = []; 54 buf.reduce(function(previous, current, index) { 55 if (previous) { 56 result.push(previous + current); 57 return null; 58 } else if (index === buf.length - 1) { 59 result.push(current); 60 return null; 61 } else { 62 return current; 63 } 64 }); 65 buf = result; 66 print(buf); 67 } 68 return buf[0]; 69} 70 71var n1 = "The quick gray nashorn jumps over the lazy zebra."; 72var n2 = rebuildString(n1); 73print(n1, n2, n2.length, n1 == n2, n1 === n2, n1 < n2, n1 <= n2, n1 > n2, n1 >= n2); 74checkProps(n2); 75 76var n3 = rebuildString(rebuildString(n2)); 77print(n3, n3.length, n1 == n3, n2 == n3, n1 === n3, n2 === n3); 78checkProps(n3); 79