1 // Copyright 2008 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
31 * @fileoverview Check all sorts of borderline cases for charCodeAt.
35 return "Te" + "st testing 123";
43 a += "ting testing 123";
49 return "testing Testing testing 123456789012345".substring(8, 22);
54 return "Testing testing 123";
58 return "Te" + "\u1234t testing 123";
66 a += "ting testing 123";
71 function Slice16Beginning() {
72 return "Te\u1234t testing testing 123".substring(0, 14);
76 function Slice16Middle() {
77 return "test Te\u1234t testing testing 123".substring(5, 19);
81 function Slice16End() {
82 return "test Te\u1234t".substring(5, 9);
87 return "Te\u1234ting testing 123";
95 function NotAString() {
97 n.toString = function() { return "Test"; };
98 n.charCodeAt = String.prototype.charCodeAt;
103 function NotAString16() {
105 n.toString = function() { return "Te\u1234t"; };
106 n.charCodeAt = String.prototype.charCodeAt;
111 function TestStringType(generator, sixteen) {
113 var len = g().toString().length;
114 var t = sixteen ? "t" : "f"
116 assertTrue(isNaN(g().charCodeAt(-1e19)), 1 + t);
117 assertTrue(isNaN(g().charCodeAt(-0x80000001)), 2 + t);
118 assertTrue(isNaN(g().charCodeAt(-0x80000000)), 3 + t);
119 assertTrue(isNaN(g().charCodeAt(-0x40000000)), 4 + t);
120 assertTrue(isNaN(g().charCodeAt(-1)), 5 + t);
121 assertTrue(isNaN(g().charCodeAt(len)), 6 + t);
122 assertTrue(isNaN(g().charCodeAt(len + 1)), 7 + t);
123 assertTrue(isNaN(g().charCodeAt(0x3fffffff)), 8 + t);
124 assertTrue(isNaN(g().charCodeAt(0x7fffffff)), 9 + t);
125 assertTrue(isNaN(g().charCodeAt(0x80000000)), 10 + t);
126 assertTrue(isNaN(g().charCodeAt(1e9)), 11 + t);
127 assertEquals(84, g().charCodeAt(0), 12 + t);
128 assertEquals(84, g().charCodeAt("test"), 13 + t);
129 assertEquals(84, g().charCodeAt(""), 14 + t);
130 assertEquals(84, g().charCodeAt(null), 15 + t);
131 assertEquals(84, g().charCodeAt(undefined), 16 + t);
132 assertEquals(84, g().charCodeAt(), 17 + t);
133 assertEquals(84, g().charCodeAt(void 0), 18 + t);
134 assertEquals(84, g().charCodeAt(false), 19 + t);
135 assertEquals(101, g().charCodeAt(true), 20 + t);
136 assertEquals(101, g().charCodeAt(1), 21 + t);
137 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2), 22 + t);
138 assertEquals(116, g().charCodeAt(3), 23 + t);
139 assertEquals(101, g().charCodeAt(1.1), 24 + t);
140 assertEquals(sixteen ? 0x1234 : 115, g().charCodeAt(2.1718), 25 + t);
141 assertEquals(116, g().charCodeAt(3.14159), 26 + t);
145 TestStringType(Cons, false);
146 TestStringType(Deep, false);
147 TestStringType(Slice, false);
148 TestStringType(Flat, false);
149 TestStringType(NotAString, false);
150 TestStringType(Cons16, true);
151 TestStringType(Deep16, true);
152 TestStringType(Slice16Beginning, true);
153 TestStringType(Slice16Middle, true);
154 TestStringType(Slice16End, true);
155 TestStringType(Flat16, true);
156 TestStringType(NotAString16, true);
159 function ConsNotSmiIndex() {
161 assertTrue(isNaN(str.charCodeAt(0x7fffffff)));
164 for (var i = 0; i < 5; i++) {
167 %OptimizeFunctionOnNextCall(ConsNotSmiIndex);
171 for (var i = 0; i != 10; i++) {
172 assertEquals(101, Cons16().charCodeAt(1.1));
173 assertEquals('e', Cons16().charAt(1.1));
176 function StupidThing() {
177 // Doesn't return a string from toString!
178 this.toString = function() { return 42; }
179 this.charCodeAt = String.prototype.charCodeAt;
182 assertEquals(52, new StupidThing().charCodeAt(0), 27);
183 assertEquals(50, new StupidThing().charCodeAt(1), 28);
184 assertTrue(isNaN(new StupidThing().charCodeAt(2)), 29);
185 assertTrue(isNaN(new StupidThing().charCodeAt(-1)), 30);
188 // Medium (>255) and long (>65535) strings.
190 var medium = "12345678";
191 medium += medium; // 16.
192 medium += medium; // 32.
193 medium += medium; // 64.
194 medium += medium; // 128.
195 medium += medium; // 256.
198 long += long + long + long; // 1024.
199 long += long + long + long; // 4096.
200 long += long + long + long; // 16384.
201 long += long + long + long; // 65536.
203 assertTrue(isNaN(medium.charCodeAt(-1)), 31);
204 assertEquals(49, medium.charCodeAt(0), 32);
205 assertEquals(56, medium.charCodeAt(255), 33);
206 assertTrue(isNaN(medium.charCodeAt(256)), 34);
208 assertTrue(isNaN(long.charCodeAt(-1)), 35);
209 assertEquals(49, long.charCodeAt(0), 36);
210 assertEquals(56, long.charCodeAt(65535), 37);
211 assertTrue(isNaN(long.charCodeAt(65536)), 38);
214 // Test crankshaft code when the function is set directly on the
215 // string prototype object instead of the hidden prototype object.
216 // See http://code.google.com/p/v8/issues/detail?id=1070
218 String.prototype.x = String.prototype.charCodeAt;
220 function directlyOnPrototype() {
221 assertEquals(97, "a".x(0));
222 assertEquals(98, "b".x(0));
223 assertEquals(99, "c".x(0));
224 assertEquals(97, "a".x(0));
225 assertEquals(98, "b".x(0));
226 assertEquals(99, "c".x(0));
229 for (var i = 0; i < 5; i++) {
230 directlyOnPrototype();
232 %OptimizeFunctionOnNextCall(directlyOnPrototype);
233 directlyOnPrototype();
235 assertTrue(isNaN(%_StringCharCodeAt("ABC", -1)));
236 assertTrue(isNaN(%_StringCharCodeAt("ABC", 4)));