tizen beta release
[framework/web/webkit-efl.git] / Source / JavaScriptCore / runtime / JSGlobalObjectFunctions.cpp
1 /*
2  *  Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3  *  Copyright (C) 2001 Peter Kelly (pmk@post.com)
4  *  Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
5  *  Copyright (C) 2007 Cameron Zwarich (cwzwarich@uwaterloo.ca)
6  *  Copyright (C) 2007 Maks Orlovich
7  *
8  *  This library is free software; you can redistribute it and/or
9  *  modify it under the terms of the GNU Library General Public
10  *  License as published by the Free Software Foundation; either
11  *  version 2 of the License, or (at your option) any later version.
12  *
13  *  This library is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  *  Library General Public License for more details.
17  *
18  *  You should have received a copy of the GNU Library General Public License
19  *  along with this library; see the file COPYING.LIB.  If not, write to
20  *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21  *  Boston, MA 02110-1301, USA.
22  *
23  */
24
25 #include "config.h"
26 #include "JSGlobalObjectFunctions.h"
27
28 #include "CallFrame.h"
29 #include "Interpreter.h"
30 #include "JSGlobalObject.h"
31 #include "JSString.h"
32 #include "JSStringBuilder.h"
33 #include "Lexer.h"
34 #include "LiteralParser.h"
35 #include "Nodes.h"
36 #include "Parser.h"
37 #include "UStringBuilder.h"
38 #include "dtoa.h"
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <wtf/ASCIICType.h>
42 #include <wtf/Assertions.h>
43 #include <wtf/MathExtras.h>
44 #include <wtf/StringExtras.h>
45 #include <wtf/unicode/UTF8.h>
46
47 using namespace WTF;
48 using namespace Unicode;
49
50 namespace JSC {
51
52 static JSValue encode(ExecState* exec, const char* doNotEscape)
53 {
54     UString str = exec->argument(0).toString(exec);
55     CString cstr = str.utf8(true);
56     if (!cstr.data())
57         return throwError(exec, createURIError(exec, "String contained an illegal UTF-16 sequence."));
58
59     JSStringBuilder builder;
60     const char* p = cstr.data();
61     for (size_t k = 0; k < cstr.length(); k++, p++) {
62         char c = *p;
63         if (c && strchr(doNotEscape, c))
64             builder.append(c);
65         else {
66             char tmp[4];
67             snprintf(tmp, sizeof(tmp), "%%%02X", static_cast<unsigned char>(c));
68             builder.append(tmp);
69         }
70     }
71     return builder.build(exec);
72 }
73
74 static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict)
75 {
76     JSStringBuilder builder;
77     UString str = exec->argument(0).toString(exec);
78     int k = 0;
79     int len = str.length();
80     const UChar* d = str.characters();
81     UChar u = 0;
82     while (k < len) {
83         const UChar* p = d + k;
84         UChar c = *p;
85         if (c == '%') {
86             int charLen = 0;
87             if (k <= len - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) {
88                 const char b0 = Lexer<UChar>::convertHex(p[1], p[2]);
89                 const int sequenceLen = UTF8SequenceLength(b0);
90                 if (sequenceLen != 0 && k <= len - sequenceLen * 3) {
91                     charLen = sequenceLen * 3;
92                     char sequence[5];
93                     sequence[0] = b0;
94                     for (int i = 1; i < sequenceLen; ++i) {
95                         const UChar* q = p + i * 3;
96                         if (q[0] == '%' && isASCIIHexDigit(q[1]) && isASCIIHexDigit(q[2]))
97                             sequence[i] = Lexer<UChar>::convertHex(q[1], q[2]);
98                         else {
99                             charLen = 0;
100                             break;
101                         }
102                     }
103                     if (charLen != 0) {
104                         sequence[sequenceLen] = 0;
105                         const int character = decodeUTF8Sequence(sequence);
106                         if (character < 0 || character >= 0x110000)
107                             charLen = 0;
108                         else if (character >= 0x10000) {
109                             // Convert to surrogate pair.
110                             builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
111                             u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF));
112                         } else
113                             u = static_cast<UChar>(character);
114                     }
115                 }
116             }
117             if (charLen == 0) {
118                 if (strict)
119                     return throwError(exec, createURIError(exec, "URI error"));
120                 // The only case where we don't use "strict" mode is the "unescape" function.
121                 // For that, it's good to support the wonky "%u" syntax for compatibility with WinIE.
122                 if (k <= len - 6 && p[1] == 'u'
123                         && isASCIIHexDigit(p[2]) && isASCIIHexDigit(p[3])
124                         && isASCIIHexDigit(p[4]) && isASCIIHexDigit(p[5])) {
125                     charLen = 6;
126                     u = Lexer<UChar>::convertUnicode(p[2], p[3], p[4], p[5]);
127                 }
128             }
129             if (charLen && (u == 0 || u >= 128 || !strchr(doNotUnescape, u))) {
130                 c = u;
131                 k += charLen - 1;
132             }
133         }
134         k++;
135         builder.append(c);
136     }
137     return builder.build(exec);
138 }
139
140 bool isStrWhiteSpace(UChar c)
141 {
142     switch (c) {
143         // ECMA-262-5th 7.2 & 7.3
144         case 0x0009:
145         case 0x000A:
146         case 0x000B:
147         case 0x000C:
148         case 0x000D:
149         case 0x0020:
150         case 0x00A0:
151         case 0x2028:
152         case 0x2029:
153         case 0xFEFF:
154             return true;
155         default:
156             return c > 0xff && isSeparatorSpace(c);
157     }
158 }
159
160 static int parseDigit(unsigned short c, int radix)
161 {
162     int digit = -1;
163
164     if (c >= '0' && c <= '9')
165         digit = c - '0';
166     else if (c >= 'A' && c <= 'Z')
167         digit = c - 'A' + 10;
168     else if (c >= 'a' && c <= 'z')
169         digit = c - 'a' + 10;
170
171     if (digit >= radix)
172         return -1;
173     return digit;
174 }
175
176 double parseIntOverflow(const LChar* s, int length, int radix)
177 {
178     double number = 0.0;
179     double radixMultiplier = 1.0;
180
181     for (const LChar* p = s + length - 1; p >= s; p--) {
182         if (radixMultiplier == std::numeric_limits<double>::infinity()) {
183             if (*p != '0') {
184                 number = std::numeric_limits<double>::infinity();
185                 break;
186             }
187         } else {
188             int digit = parseDigit(*p, radix);
189             number += digit * radixMultiplier;
190         }
191
192         radixMultiplier *= radix;
193     }
194
195     return number;
196 }
197
198 double parseIntOverflow(const UChar* s, int length, int radix)
199 {
200     double number = 0.0;
201     double radixMultiplier = 1.0;
202
203     for (const UChar* p = s + length - 1; p >= s; p--) {
204         if (radixMultiplier == std::numeric_limits<double>::infinity()) {
205             if (*p != '0') {
206                 number = std::numeric_limits<double>::infinity();
207                 break;
208             }
209         } else {
210             int digit = parseDigit(*p, radix);
211             number += digit * radixMultiplier;
212         }
213
214         radixMultiplier *= radix;
215     }
216
217     return number;
218 }
219
220 static double parseInt(const UString& s, int radix)
221 {
222     int length = s.length();
223     const UChar* data = s.characters();
224     int p = 0;
225
226     while (p < length && isStrWhiteSpace(data[p]))
227         ++p;
228
229     double sign = 1;
230     if (p < length) {
231         if (data[p] == '+')
232             ++p;
233         else if (data[p] == '-') {
234             sign = -1;
235             ++p;
236         }
237     }
238
239     if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) {
240         radix = 16;
241         p += 2;
242     } else if (radix == 0) {
243         if (p < length && data[p] == '0')
244             radix = 8;
245         else
246             radix = 10;
247     }
248
249     if (radix < 2 || radix > 36)
250         return std::numeric_limits<double>::quiet_NaN();
251
252     int firstDigitPosition = p;
253     bool sawDigit = false;
254     double number = 0;
255     while (p < length) {
256         int digit = parseDigit(data[p], radix);
257         if (digit == -1)
258             break;
259         sawDigit = true;
260         number *= radix;
261         number += digit;
262         ++p;
263     }
264
265     if (number >= mantissaOverflowLowerBound) {
266         if (radix == 10)
267             number = WTF::strtod(s.substringSharingImpl(firstDigitPosition, p - firstDigitPosition).utf8().data(), 0);
268         else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32)
269             number = parseIntOverflow(s.substringSharingImpl(firstDigitPosition, p - firstDigitPosition).utf8().data(), p - firstDigitPosition, radix);
270     }
271
272     if (!sawDigit)
273         return std::numeric_limits<double>::quiet_NaN();
274
275     return sign * number;
276 }
277
278 static const int SizeOfInfinity = 8;
279
280 template <typename CharType>
281 static bool isInfinity(const CharType* data, const CharType* end)
282 {
283     return (end - data) >= SizeOfInfinity
284         && data[0] == 'I'
285         && data[1] == 'n'
286         && data[2] == 'f'
287         && data[3] == 'i'
288         && data[4] == 'n'
289         && data[5] == 'i'
290         && data[6] == 't'
291         && data[7] == 'y';
292 }
293
294 // See ecma-262 9.3.1
295 template <typename CharType>
296 static double jsHexIntegerLiteral(const CharType*& data, const CharType* end)
297 {
298     // Hex number.
299     data += 2;
300     const CharType* firstDigitPosition = data;
301     double number = 0;
302     while (true) {
303         number = number * 16 + toASCIIHexValue(*data);
304         ++data;
305         if (data == end)
306             break;
307         if (!isASCIIHexDigit(*data))
308             break;
309     }
310     if (number >= mantissaOverflowLowerBound)
311         number = parseIntOverflow(firstDigitPosition, data - firstDigitPosition, 16);
312
313     return number;
314 }
315
316 // See ecma-262 9.3.1
317 template <typename CharType>
318 static double jsStrDecimalLiteral(const CharType*& data, const CharType* end)
319 {
320     ASSERT(data < end);
321
322     // Copy the sting into a null-terminated byte buffer, and call strtod.
323     Vector<char, 32> byteBuffer;
324     for (const CharType* characters = data; characters < end; ++characters) {
325         CharType character = *characters;
326         byteBuffer.append(isASCII(character) ? static_cast<char>(character) : 0);
327     }
328     byteBuffer.append(0);
329     char* endOfNumber;
330     double number = WTF::strtod(byteBuffer.data(), &endOfNumber);
331
332     // Check if strtod found a number; if so return it.
333     ptrdiff_t consumed = endOfNumber - byteBuffer.data();
334     if (consumed) {
335         data += consumed;
336         return number;
337     }
338
339     // Check for [+-]?Infinity
340     switch (*data) {
341     case 'I':
342         if (isInfinity(data, end)) {
343             data += SizeOfInfinity;
344             return std::numeric_limits<double>::infinity();
345         }
346         break;
347
348     case '+':
349         if (isInfinity(data + 1, end)) {
350             data += SizeOfInfinity + 1;
351             return std::numeric_limits<double>::infinity();
352         }
353         break;
354
355     case '-':
356         if (isInfinity(data + 1, end)) {
357             data += SizeOfInfinity + 1;
358             return -std::numeric_limits<double>::infinity();
359         }
360         break;
361     }
362
363     // Not a number.
364     return std::numeric_limits<double>::quiet_NaN();
365 }
366
367 template <typename CharType>
368 static double toDouble(const CharType* characters, unsigned size)
369 {
370     const CharType* endCharacters = characters + size;
371
372     // Skip leading white space.
373     for (; characters < endCharacters; ++characters) {
374         if (!isStrWhiteSpace(*characters))
375             break;
376     }
377     
378     // Empty string.
379     if (characters == endCharacters)
380         return 0.0;
381     
382     double number;
383     if (characters[0] == '0' && characters + 2 < endCharacters && (characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2]))
384         number = jsHexIntegerLiteral(characters, endCharacters);
385     else
386         number = jsStrDecimalLiteral(characters, endCharacters);
387     
388     // Allow trailing white space.
389     for (; characters < endCharacters; ++characters) {
390         if (!isStrWhiteSpace(*characters))
391             break;
392     }
393     if (characters != endCharacters)
394         return std::numeric_limits<double>::quiet_NaN();
395     
396     return number;
397 }
398
399 // See ecma-262 9.3.1
400 double jsToNumber(const UString& s)
401 {
402     unsigned size = s.length();
403
404     if (size == 1) {
405         UChar c = s[0];
406         if (isASCIIDigit(c))
407             return c - '0';
408         if (isStrWhiteSpace(c))
409             return 0;
410         return std::numeric_limits<double>::quiet_NaN();
411     }
412
413     if (s.is8Bit())
414         return toDouble(s.characters8(), size);
415     return toDouble(s.characters16(), size);
416 }
417
418 static double parseFloat(const UString& s)
419 {
420     unsigned size = s.length();
421
422     if (size == 1) {
423         UChar c = s[0];
424         if (isASCIIDigit(c))
425             return c - '0';
426         return std::numeric_limits<double>::quiet_NaN();
427     }
428
429     const UChar* data = s.characters();
430     const UChar* end = data + size;
431
432     // Skip leading white space.
433     for (; data < end; ++data) {
434         if (!isStrWhiteSpace(*data))
435             break;
436     }
437
438     // Empty string.
439     if (data == end)
440         return std::numeric_limits<double>::quiet_NaN();
441
442     return jsStrDecimalLiteral(data, end);
443 }
444
445 EncodedJSValue JSC_HOST_CALL globalFuncEval(ExecState* exec)
446 {
447     JSObject* thisObject = exec->hostThisValue().toThisObject(exec);
448     JSObject* unwrappedObject = thisObject->unwrappedObject();
449     if (!unwrappedObject->isGlobalObject() || static_cast<JSGlobalObject*>(unwrappedObject)->evalFunction() != exec->callee())
450         return throwVMError(exec, createEvalError(exec, "The \"this\" value passed to eval must be the global object from which eval originated"));
451
452     JSValue x = exec->argument(0);
453     if (!x.isString())
454         return JSValue::encode(x);
455
456     UString s = x.toString(exec);
457
458     if (s.is8Bit()) {
459         LiteralParser<LChar> preparser(exec, s.characters8(), s.length(), NonStrictJSON);
460         if (JSValue parsedObject = preparser.tryLiteralParse())
461             return JSValue::encode(parsedObject);
462     } else {
463         LiteralParser<UChar> preparser(exec, s.characters16(), s.length(), NonStrictJSON);
464         if (JSValue parsedObject = preparser.tryLiteralParse())
465             return JSValue::encode(parsedObject);        
466     }
467
468     EvalExecutable* eval = EvalExecutable::create(exec, makeSource(s), false);
469     JSObject* error = eval->compile(exec, static_cast<JSGlobalObject*>(unwrappedObject)->globalScopeChain());
470     if (error)
471         return throwVMError(exec, error);
472
473     return JSValue::encode(exec->interpreter()->execute(eval, exec, thisObject, static_cast<JSGlobalObject*>(unwrappedObject)->globalScopeChain()));
474 }
475
476 EncodedJSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec)
477 {
478     JSValue value = exec->argument(0);
479     JSValue radixValue = exec->argument(1);
480
481     // Optimized handling for numbers:
482     // If the argument is 0 or a number in range 10^-6 <= n < INT_MAX+1, then parseInt
483     // results in a truncation to integer. In the case of -0, this is converted to 0.
484     //
485     // This is also a truncation for values in the range INT_MAX+1 <= n < 10^21,
486     // however these values cannot be trivially truncated to int since 10^21 exceeds
487     // even the int64_t range. Negative numbers are a little trickier, the case for
488     // values in the range -10^21 < n <= -1 are similar to those for integer, but
489     // values in the range -1 < n <= -10^-6 need to truncate to -0, not 0.
490     static const double tenToTheMinus6 = 0.000001;
491     static const double intMaxPlusOne = 2147483648.0;
492     if (value.isNumber()) {
493         double n = value.asNumber();
494         if (((n < intMaxPlusOne && n >= tenToTheMinus6) || !n) && radixValue.isUndefinedOrNull())
495             return JSValue::encode(jsNumber(static_cast<int32_t>(n)));
496     }
497
498     // If ToString throws, we shouldn't call ToInt32.
499     UString s = value.toString(exec);
500     if (exec->hadException())
501         return JSValue::encode(jsUndefined());
502
503     return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec))));
504 }
505
506 EncodedJSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec)
507 {
508     return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec))));
509 }
510
511 EncodedJSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec)
512 {
513     return JSValue::encode(jsBoolean(isnan(exec->argument(0).toNumber(exec))));
514 }
515
516 EncodedJSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec)
517 {
518     double n = exec->argument(0).toNumber(exec);
519     return JSValue::encode(jsBoolean(isfinite(n)));
520 }
521
522 EncodedJSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec)
523 {
524     static const char do_not_unescape_when_decoding_URI[] =
525         "#$&+,/:;=?@";
526
527     return JSValue::encode(decode(exec, do_not_unescape_when_decoding_URI, true));
528 }
529
530 EncodedJSValue JSC_HOST_CALL globalFuncDecodeURIComponent(ExecState* exec)
531 {
532     return JSValue::encode(decode(exec, "", true));
533 }
534
535 EncodedJSValue JSC_HOST_CALL globalFuncEncodeURI(ExecState* exec)
536 {
537     static const char do_not_escape_when_encoding_URI[] =
538         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
539         "abcdefghijklmnopqrstuvwxyz"
540         "0123456789"
541         "!#$&'()*+,-./:;=?@_~";
542
543     return JSValue::encode(encode(exec, do_not_escape_when_encoding_URI));
544 }
545
546 EncodedJSValue JSC_HOST_CALL globalFuncEncodeURIComponent(ExecState* exec)
547 {
548     static const char do_not_escape_when_encoding_URI_component[] =
549         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
550         "abcdefghijklmnopqrstuvwxyz"
551         "0123456789"
552         "!'()*-._~";
553
554     return JSValue::encode(encode(exec, do_not_escape_when_encoding_URI_component));
555 }
556
557 EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec)
558 {
559     static const char do_not_escape[] =
560         "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
561         "abcdefghijklmnopqrstuvwxyz"
562         "0123456789"
563         "*+-./@_";
564
565     JSStringBuilder builder;
566     UString str = exec->argument(0).toString(exec);
567     const UChar* c = str.characters();
568     for (unsigned k = 0; k < str.length(); k++, c++) {
569         int u = c[0];
570         if (u > 255) {
571             char tmp[7];
572             snprintf(tmp, sizeof(tmp), "%%u%04X", u);
573             builder.append(tmp);
574         } else if (u != 0 && strchr(do_not_escape, static_cast<char>(u)))
575             builder.append(c, 1);
576         else {
577             char tmp[4];
578             snprintf(tmp, sizeof(tmp), "%%%02X", u);
579             builder.append(tmp);
580         }
581     }
582
583     return JSValue::encode(builder.build(exec));
584 }
585
586 EncodedJSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec)
587 {
588     UStringBuilder builder;
589     UString str = exec->argument(0).toString(exec);
590     int k = 0;
591     int len = str.length();
592     while (k < len) {
593         const UChar* c = str.characters() + k;
594         UChar u;
595         if (c[0] == '%' && k <= len - 6 && c[1] == 'u') {
596             if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) {
597                 u = Lexer<UChar>::convertUnicode(c[2], c[3], c[4], c[5]);
598                 c = &u;
599                 k += 5;
600             }
601         } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) {
602             u = UChar(Lexer<UChar>::convertHex(c[1], c[2]));
603             c = &u;
604             k += 2;
605         }
606         k++;
607         builder.append(*c);
608     }
609
610     return JSValue::encode(jsString(exec, builder.toUString()));
611 }
612
613 EncodedJSValue JSC_HOST_CALL globalFuncThrowTypeError(ExecState* exec)
614 {
615     return throwVMTypeError(exec);
616 }
617
618 } // namespace JSC