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
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.
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.
28 // Flags: --allow-natives-syntax
30 // Test String.fromCharCode.
32 // Test char codes larger than 0xffff.
34 for (var i = 100; i < 500; i++) {
35 expected += String.fromCharCode(i);
38 function testCharCodeTruncation() {
40 for (var i = 0x100000 + 100; i < 0x100000 + 500; i++) {
41 result += String.fromCharCode(i);
43 assertEquals(String.fromCharCode(0xFFFF), String.fromCharCode(0xFFFFFFFF));
47 assertEquals(expected, testCharCodeTruncation());
48 assertEquals(expected, testCharCodeTruncation());
49 %OptimizeFunctionOnNextCall(testCharCodeTruncation);
50 assertEquals(expected, testCharCodeTruncation());
52 // Test various receivers and arguments passed to String.fromCharCode.
54 Object.prototype.fromCharCode = function(x) { return this; };
56 var fcc = String.fromCharCode;
59 function constFun(x) { return function(y) { return x; }; }
62 assertEquals(" ", String.fromCharCode(0x20));
63 assertEquals(" ", String.fromCharCode(0x20 + 0x10000));
64 assertEquals(" ", String.fromCharCode(0x20 - 0x10000));
65 assertEquals(" ", String.fromCharCode(0x20 + 0.5));
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));
72 assertEquals(" ", String.fromCharCode(0x20, 0x20));
73 assertEquals(" ", String.fromCharCode(0x20 + 0.5, 0x20));
75 assertEquals(" ", fcc(0x20));
76 assertEquals(" ", fcc(0x20 + 0x10000));
77 assertEquals(" ", fcc(0x20 - 0x10000));
78 assertEquals(" ", fcc(0x20 + 0.5));
80 assertEquals("\u1234", fcc(0x1234));
81 assertEquals("\u1234", fcc(0x1234 + 0x10000));
82 assertEquals("\u1234", fcc(0x1234 - 0x10000));
83 assertEquals("\u1234", fcc(0x1234 + 0.5));
85 assertEquals(" ", fcc(0x20, 0x20));
86 assertEquals(" ", fcc(0x20 + 0.5, 0x20));
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));
101 // Use loop to test the custom IC.
102 for (var i = 0; i < 10; i++) {
106 assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65));
107 assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65));
108 %OptimizeFunctionOnNextCall(String.fromCharCode);
109 assertEquals("AAAA", String.fromCharCode(65, 65, 65, 65));
111 // Test the custom IC works correctly when the map changes.
112 for (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));