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: --allow-natives-syntax 29 30// Test String.fromCharCode. 31 32// Test char codes larger than 0xffff. 33var expected = ""; 34for (var i = 100; i < 500; i++) { 35 expected += String.fromCharCode(i); 36} 37 38function testCharCodeTruncation() { 39 var result = ""; 40 for (var i = 0x100000 + 100; i < 0x100000 + 500; i++) { 41 result += String.fromCharCode(i); 42 } 43 assertEquals(String.fromCharCode(0xFFFF), String.fromCharCode(0xFFFFFFFF)); 44 return result; 45} 46 47assertEquals(expected, testCharCodeTruncation()); 48assertEquals(expected, testCharCodeTruncation()); 49%OptimizeFunctionOnNextCall(testCharCodeTruncation); 50assertEquals(expected, testCharCodeTruncation()); 51 52// Test various receivers and arguments passed to String.fromCharCode. 53 54Object.prototype.fromCharCode = function(x) { return this; }; 55 56var fcc = String.fromCharCode; 57var fcc2 = fcc; 58 59function constFun(x) { return function(y) { return x; }; } 60 61function test(num) { 62 assertEquals(" ", String.fromCharCode(0x20)); 63 assertEquals(" ", String.fromCharCode(0x20 + 0x10000)); 64 assertEquals(" ", String.fromCharCode(0x20 - 0x10000)); 65 assertEquals(" ", String.fromCharCode(0x20 + 0.5)); 66 67 assertEquals("\u1234", String.fromCharCode(0x1234)); 68 assertEquals("\u1234", String.fromCharCode(0x1234 + 0x10000)); 69 assertEquals("\u1234", String.fromCharCode(0x1234 - 0x10000)); 70 assertEquals("\u1234", String.fromCharCode(0x1234 + 0.5)); 71 72 assertEquals(" ", String.fromCharCode(0x20, 0x20)); 73 assertEquals(" ", String.fromCharCode(0x20 + 0.5, 0x20)); 74 75 assertEquals(" ", fcc(0x20)); 76 assertEquals(" ", fcc(0x20 + 0x10000)); 77 assertEquals(" ", fcc(0x20 - 0x10000)); 78 assertEquals(" ", fcc(0x20 + 0.5)); 79 80 assertEquals("\u1234", fcc(0x1234)); 81 assertEquals("\u1234", fcc(0x1234 + 0x10000)); 82 assertEquals("\u1234", fcc(0x1234 - 0x10000)); 83 assertEquals("\u1234", fcc(0x1234 + 0.5)); 84 85 assertEquals(" ", fcc(0x20, 0x20)); 86 assertEquals(" ", fcc(0x20 + 0.5, 0x20)); 87 88 var receiver = (num < 5) ? String : (num < 9) ? "dummy" : 42; 89 fcc2 = (num < 5) ? fcc 90 : (num < 9) ? constFun(Object("dummy")) 91 : constFun(Object(42)); 92 var expected = (num < 5) ? " " : (num < 9) ? Object("dummy") : Object(42); 93 assertEquals(expected, receiver.fromCharCode(0x20)); 94 assertEquals(expected, receiver.fromCharCode(0x20 - 0x10000)); 95 assertEquals(expected, receiver.fromCharCode(0x20 + 0.5)); 96 assertEquals(expected, fcc2(0x20)); 97 assertEquals(expected, fcc2(0x20 - 0x10000)); 98 assertEquals(expected, fcc2(0x20 + 0.5)); 99} 100 101// Use loop to test the custom IC. 102for (var i = 0; i < 10; i++) { 103 test(i); 104} 105 106assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65)); 107assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65)); 108%OptimizeFunctionOnNextCall(String.fromCharCode); 109assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65)); 110 111// Test the custom IC works correctly when the map changes. 112for (var i = 0; i < 10; i++) { 113 var expected = (i < 5) ? " " : 42; 114 if (i == 5) String.fromCharCode = function() { return 42; }; 115 assertEquals(expected, String.fromCharCode(0x20)); 116} 117