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
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/**
31 * @fileoverview Check all sorts of borderline cases for charCodeAt.
32 */
33
34function Cons() {
35  return "Te" + "st testing 123";
36}
37
38
39function Deep() {
40  var a = "T";
41  a += "e";
42  a += "s";
43  a += "ting testing 123";
44  return a;
45}
46
47
48function Slice() {
49  return "testing Testing testing 123456789012345".substring(8, 22);
50}
51
52
53function Flat() {
54  return "Testing testing 123";
55}
56
57function Cons16() {
58  return "Te" + "\u1234t testing 123";
59}
60
61
62function Deep16() {
63  var a = "T";
64  a += "e";
65  a += "\u1234";
66  a += "ting testing 123";
67  return a;
68}
69
70
71function Slice16Beginning() {
72  return "Te\u1234t testing testing 123".substring(0, 14);
73}
74
75
76function Slice16Middle() {
77  return "test Te\u1234t testing testing 123".substring(5, 19);
78}
79
80
81function Slice16End() {
82  return "test Te\u1234t".substring(5, 9);
83}
84
85
86function Flat16() {
87  return "Te\u1234ting testing 123";
88}
89
90
91function Thing() {
92}
93
94
95function NotAString() {
96  var n = new Thing();
97  n.toString = function() { return "Test"; };
98  n.charCodeAt = String.prototype.charCodeAt;
99  return n;
100}
101
102
103function NotAString16() {
104  var n = new Thing();
105  n.toString = function() { return "Te\u1234t"; };
106  n.charCodeAt = String.prototype.charCodeAt;
107  return n;
108}
109
110
111function TestStringType(generator, sixteen) {
112  var g = generator;
113  var len = g().toString().length;
114  var t = sixteen ? "t" : "f"
115  t += generator.name;
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);
142}
143
144
145TestStringType(Cons, false);
146TestStringType(Deep, false);
147TestStringType(Slice, false);
148TestStringType(Flat, false);
149TestStringType(NotAString, false);
150TestStringType(Cons16, true);
151TestStringType(Deep16, true);
152TestStringType(Slice16Beginning, true);
153TestStringType(Slice16Middle, true);
154TestStringType(Slice16End, true);
155TestStringType(Flat16, true);
156TestStringType(NotAString16, true);
157
158
159function ConsNotSmiIndex() {
160  var str = Cons();
161  assertTrue(isNaN(str.charCodeAt(0x7fffffff)));
162}
163
164for (var i = 0; i < 5; i++) {
165  ConsNotSmiIndex();
166}
167%OptimizeFunctionOnNextCall(ConsNotSmiIndex);
168ConsNotSmiIndex();
169
170
171for (var i = 0; i != 10; i++) {
172  assertEquals(101, Cons16().charCodeAt(1.1));
173  assertEquals('e', Cons16().charAt(1.1));
174}
175
176function StupidThing() {
177  // Doesn't return a string from toString!
178  this.toString = function() { return 42; }
179  this.charCodeAt = String.prototype.charCodeAt;
180}
181
182assertEquals(52, new StupidThing().charCodeAt(0), 27);
183assertEquals(50, new StupidThing().charCodeAt(1), 28);
184assertTrue(isNaN(new StupidThing().charCodeAt(2)), 29);
185assertTrue(isNaN(new StupidThing().charCodeAt(-1)), 30);
186
187
188// Medium (>255) and long (>65535) strings.
189
190var medium = "12345678";
191medium += medium; // 16.
192medium += medium; // 32.
193medium += medium; // 64.
194medium += medium; // 128.
195medium += medium; // 256.
196
197var long = medium;
198long += long + long + long;     // 1024.
199long += long + long + long;     // 4096.
200long += long + long + long;     // 16384.
201long += long + long + long;     // 65536.
202
203assertTrue(isNaN(medium.charCodeAt(-1)), 31);
204assertEquals(49, medium.charCodeAt(0), 32);
205assertEquals(56, medium.charCodeAt(255), 33);
206assertTrue(isNaN(medium.charCodeAt(256)), 34);
207
208assertTrue(isNaN(long.charCodeAt(-1)), 35);
209assertEquals(49, long.charCodeAt(0), 36);
210assertEquals(56, long.charCodeAt(65535), 37);
211assertTrue(isNaN(long.charCodeAt(65536)), 38);
212
213
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
217
218String.prototype.x = String.prototype.charCodeAt;
219
220function 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));
227}
228
229for (var i = 0; i < 5; i++) {
230  directlyOnPrototype();
231}
232%OptimizeFunctionOnNextCall(directlyOnPrototype);
233directlyOnPrototype();
234