]> git.saurik.com Git - apple/javascriptcore.git/blame - runtime/JSGlobalObjectFunctions.cpp
JavaScriptCore-1097.13.tar.gz
[apple/javascriptcore.git] / runtime / JSGlobalObjectFunctions.cpp
CommitLineData
9dae56ea
A
1/*
2 * Copyright (C) 1999-2002 Harri Porten (porten@kde.org)
3 * Copyright (C) 2001 Peter Kelly (pmk@post.com)
6fe7ccc8 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2012 Apple Inc. All rights reserved.
9dae56ea
A
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"
f9bf01c6 29#include "Interpreter.h"
9dae56ea
A
30#include "JSGlobalObject.h"
31#include "JSString.h"
4e4e5a6f 32#include "JSStringBuilder.h"
9dae56ea 33#include "Lexer.h"
f9bf01c6 34#include "LiteralParser.h"
9dae56ea 35#include "Nodes.h"
f9bf01c6 36#include "Parser.h"
14957cd0 37#include "UStringBuilder.h"
6fe7ccc8 38#include <wtf/dtoa.h>
9dae56ea
A
39#include <stdio.h>
40#include <stdlib.h>
9dae56ea
A
41#include <wtf/ASCIICType.h>
42#include <wtf/Assertions.h>
43#include <wtf/MathExtras.h>
14957cd0 44#include <wtf/StringExtras.h>
9dae56ea
A
45#include <wtf/unicode/UTF8.h>
46
47using namespace WTF;
48using namespace Unicode;
49
50namespace JSC {
51
14957cd0 52static JSValue encode(ExecState* exec, const char* doNotEscape)
9dae56ea 53{
6fe7ccc8 54 CString cstr = exec->argument(0).toString(exec)->value(exec).utf8(true);
4e4e5a6f 55 if (!cstr.data())
14957cd0 56 return throwError(exec, createURIError(exec, "String contained an illegal UTF-16 sequence."));
9dae56ea 57
4e4e5a6f
A
58 JSStringBuilder builder;
59 const char* p = cstr.data();
60 for (size_t k = 0; k < cstr.length(); k++, p++) {
9dae56ea
A
61 char c = *p;
62 if (c && strchr(doNotEscape, c))
f9bf01c6 63 builder.append(c);
9dae56ea
A
64 else {
65 char tmp[4];
66 snprintf(tmp, sizeof(tmp), "%%%02X", static_cast<unsigned char>(c));
4e4e5a6f 67 builder.append(tmp);
9dae56ea
A
68 }
69 }
4e4e5a6f 70 return builder.build(exec);
9dae56ea
A
71}
72
6fe7ccc8
A
73template <typename CharType>
74ALWAYS_INLINE
75static JSValue decode(ExecState* exec, const CharType* characters, int length, const char* doNotUnescape, bool strict)
9dae56ea 76{
4e4e5a6f 77 JSStringBuilder builder;
9dae56ea 78 int k = 0;
9dae56ea 79 UChar u = 0;
6fe7ccc8
A
80 while (k < length) {
81 const CharType* p = characters + k;
82 CharType c = *p;
9dae56ea
A
83 if (c == '%') {
84 int charLen = 0;
6fe7ccc8
A
85 if (k <= length - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) {
86 const char b0 = Lexer<CharType>::convertHex(p[1], p[2]);
9dae56ea 87 const int sequenceLen = UTF8SequenceLength(b0);
6fe7ccc8 88 if (sequenceLen && k <= length - sequenceLen * 3) {
9dae56ea
A
89 charLen = sequenceLen * 3;
90 char sequence[5];
91 sequence[0] = b0;
92 for (int i = 1; i < sequenceLen; ++i) {
6fe7ccc8 93 const CharType* q = p + i * 3;
9dae56ea 94 if (q[0] == '%' && isASCIIHexDigit(q[1]) && isASCIIHexDigit(q[2]))
6fe7ccc8 95 sequence[i] = Lexer<CharType>::convertHex(q[1], q[2]);
9dae56ea
A
96 else {
97 charLen = 0;
98 break;
99 }
100 }
101 if (charLen != 0) {
102 sequence[sequenceLen] = 0;
103 const int character = decodeUTF8Sequence(sequence);
104 if (character < 0 || character >= 0x110000)
105 charLen = 0;
106 else if (character >= 0x10000) {
107 // Convert to surrogate pair.
f9bf01c6 108 builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10)));
9dae56ea
A
109 u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF));
110 } else
111 u = static_cast<UChar>(character);
112 }
113 }
114 }
115 if (charLen == 0) {
116 if (strict)
14957cd0 117 return throwError(exec, createURIError(exec, "URI error"));
9dae56ea
A
118 // The only case where we don't use "strict" mode is the "unescape" function.
119 // For that, it's good to support the wonky "%u" syntax for compatibility with WinIE.
6fe7ccc8 120 if (k <= length - 6 && p[1] == 'u'
9dae56ea
A
121 && isASCIIHexDigit(p[2]) && isASCIIHexDigit(p[3])
122 && isASCIIHexDigit(p[4]) && isASCIIHexDigit(p[5])) {
123 charLen = 6;
6fe7ccc8 124 u = Lexer<UChar>::convertUnicode(p[2], p[3], p[4], p[5]);
9dae56ea
A
125 }
126 }
127 if (charLen && (u == 0 || u >= 128 || !strchr(doNotUnescape, u))) {
6fe7ccc8
A
128 if (u < 256)
129 builder.append(static_cast<LChar>(u));
130 else
131 builder.append(u);
132 k += charLen;
133 continue;
9dae56ea
A
134 }
135 }
136 k++;
f9bf01c6 137 builder.append(c);
9dae56ea 138 }
4e4e5a6f 139 return builder.build(exec);
9dae56ea
A
140}
141
6fe7ccc8
A
142static JSValue decode(ExecState* exec, const char* doNotUnescape, bool strict)
143{
144 JSStringBuilder builder;
145 UString str = exec->argument(0).toString(exec)->value(exec);
146
147 if (str.is8Bit())
148 return decode(exec, str.characters8(), str.length(), doNotUnescape, strict);
149 return decode(exec, str.characters16(), str.length(), doNotUnescape, strict);
150}
151
9dae56ea
A
152bool isStrWhiteSpace(UChar c)
153{
154 switch (c) {
14957cd0 155 // ECMA-262-5th 7.2 & 7.3
9dae56ea
A
156 case 0x0009:
157 case 0x000A:
158 case 0x000B:
159 case 0x000C:
160 case 0x000D:
161 case 0x0020:
162 case 0x00A0:
163 case 0x2028:
164 case 0x2029:
14957cd0 165 case 0xFEFF:
9dae56ea
A
166 return true;
167 default:
168 return c > 0xff && isSeparatorSpace(c);
169 }
170}
171
172static int parseDigit(unsigned short c, int radix)
173{
174 int digit = -1;
175
176 if (c >= '0' && c <= '9')
177 digit = c - '0';
178 else if (c >= 'A' && c <= 'Z')
179 digit = c - 'A' + 10;
180 else if (c >= 'a' && c <= 'z')
181 digit = c - 'a' + 10;
182
183 if (digit >= radix)
184 return -1;
185 return digit;
186}
187
6fe7ccc8 188double parseIntOverflow(const LChar* s, int length, int radix)
9dae56ea
A
189{
190 double number = 0.0;
191 double radixMultiplier = 1.0;
192
6fe7ccc8
A
193 for (const LChar* p = s + length - 1; p >= s; p--) {
194 if (radixMultiplier == std::numeric_limits<double>::infinity()) {
9dae56ea 195 if (*p != '0') {
6fe7ccc8 196 number = std::numeric_limits<double>::infinity();
9dae56ea
A
197 break;
198 }
199 } else {
200 int digit = parseDigit(*p, radix);
201 number += digit * radixMultiplier;
202 }
203
204 radixMultiplier *= radix;
205 }
206
207 return number;
208}
209
14957cd0
A
210double parseIntOverflow(const UChar* s, int length, int radix)
211{
212 double number = 0.0;
213 double radixMultiplier = 1.0;
214
215 for (const UChar* p = s + length - 1; p >= s; p--) {
6fe7ccc8 216 if (radixMultiplier == std::numeric_limits<double>::infinity()) {
14957cd0 217 if (*p != '0') {
6fe7ccc8 218 number = std::numeric_limits<double>::infinity();
14957cd0
A
219 break;
220 }
221 } else {
222 int digit = parseDigit(*p, radix);
223 number += digit * radixMultiplier;
224 }
225
226 radixMultiplier *= radix;
227 }
228
229 return number;
230}
231
6fe7ccc8
A
232// ES5.1 15.1.2.2
233template <typename CharType>
234ALWAYS_INLINE
235static double parseInt(const UString& s, const CharType* data, int radix)
9dae56ea 236{
6fe7ccc8
A
237 // 1. Let inputString be ToString(string).
238 // 2. Let S be a newly created substring of inputString consisting of the first character that is not a
239 // StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white
240 // space.) If inputString does not contain any such characters, let S be the empty string.
14957cd0 241 int length = s.length();
9dae56ea 242 int p = 0;
9dae56ea
A
243 while (p < length && isStrWhiteSpace(data[p]))
244 ++p;
245
6fe7ccc8
A
246 // 3. Let sign be 1.
247 // 4. If S is not empty and the first character of S is a minus sign -, let sign be -1.
248 // 5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S.
9dae56ea
A
249 double sign = 1;
250 if (p < length) {
251 if (data[p] == '+')
252 ++p;
253 else if (data[p] == '-') {
254 sign = -1;
255 ++p;
256 }
257 }
258
6fe7ccc8
A
259 // 6. Let R = ToInt32(radix).
260 // 7. Let stripPrefix be true.
261 // 8. If R != 0,then
262 // b. If R != 16, let stripPrefix be false.
263 // 9. Else, R == 0
264 // a. LetR = 10.
265 // 10. If stripPrefix is true, then
266 // a. If the length of S is at least 2 and the first two characters of S are either ―0x or ―0X,
267 // then remove the first two characters from S and let R = 16.
268 // 11. If S contains any character that is not a radix-R digit, then let Z be the substring of S
269 // consisting of all characters before the first such character; otherwise, let Z be S.
9dae56ea
A
270 if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) {
271 radix = 16;
272 p += 2;
6fe7ccc8
A
273 } else if (radix == 0)
274 radix = 10;
9dae56ea 275
6fe7ccc8 276 // 8.a If R < 2 or R > 36, then return NaN.
9dae56ea 277 if (radix < 2 || radix > 36)
6fe7ccc8
A
278 return std::numeric_limits<double>::quiet_NaN();
279
280 // 13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters
281 // A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant
282 // digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation;
283 // and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the
284 // mathematical integer value that is represented by Z in radix-R notation.)
285 // 14. Let number be the Number value for mathInt.
9dae56ea
A
286 int firstDigitPosition = p;
287 bool sawDigit = false;
288 double number = 0;
289 while (p < length) {
290 int digit = parseDigit(data[p], radix);
291 if (digit == -1)
292 break;
293 sawDigit = true;
294 number *= radix;
295 number += digit;
296 ++p;
297 }
298
6fe7ccc8
A
299 // 12. If Z is empty, return NaN.
300 if (!sawDigit)
301 return std::numeric_limits<double>::quiet_NaN();
302
303 // Alternate code path for certain large numbers.
9dae56ea 304 if (number >= mantissaOverflowLowerBound) {
6fe7ccc8
A
305 if (radix == 10) {
306 size_t parsedLength;
307 number = parseDouble(s.characters() + firstDigitPosition, p - firstDigitPosition, parsedLength);
308 } else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32)
14957cd0 309 number = parseIntOverflow(s.substringSharingImpl(firstDigitPosition, p - firstDigitPosition).utf8().data(), p - firstDigitPosition, radix);
9dae56ea
A
310 }
311
6fe7ccc8 312 // 15. Return sign x number.
9dae56ea
A
313 return sign * number;
314}
315
6fe7ccc8
A
316static double parseInt(const UString& s, int radix)
317{
318 if (s.is8Bit())
319 return parseInt(s, s.characters8(), radix);
320 return parseInt(s, s.characters16(), radix);
321}
322
14957cd0
A
323static const int SizeOfInfinity = 8;
324
6fe7ccc8
A
325template <typename CharType>
326static bool isInfinity(const CharType* data, const CharType* end)
14957cd0
A
327{
328 return (end - data) >= SizeOfInfinity
329 && data[0] == 'I'
330 && data[1] == 'n'
331 && data[2] == 'f'
332 && data[3] == 'i'
333 && data[4] == 'n'
334 && data[5] == 'i'
335 && data[6] == 't'
336 && data[7] == 'y';
337}
338
339// See ecma-262 9.3.1
6fe7ccc8
A
340template <typename CharType>
341static double jsHexIntegerLiteral(const CharType*& data, const CharType* end)
14957cd0
A
342{
343 // Hex number.
344 data += 2;
6fe7ccc8 345 const CharType* firstDigitPosition = data;
14957cd0
A
346 double number = 0;
347 while (true) {
348 number = number * 16 + toASCIIHexValue(*data);
349 ++data;
350 if (data == end)
351 break;
352 if (!isASCIIHexDigit(*data))
353 break;
354 }
355 if (number >= mantissaOverflowLowerBound)
356 number = parseIntOverflow(firstDigitPosition, data - firstDigitPosition, 16);
357
358 return number;
359}
360
361// See ecma-262 9.3.1
6fe7ccc8
A
362template <typename CharType>
363static double jsStrDecimalLiteral(const CharType*& data, const CharType* end)
14957cd0
A
364{
365 ASSERT(data < end);
366
6fe7ccc8
A
367 size_t parsedLength;
368 double number = parseDouble(data, end - data, parsedLength);
369 if (parsedLength) {
370 data += parsedLength;
14957cd0
A
371 return number;
372 }
373
374 // Check for [+-]?Infinity
375 switch (*data) {
376 case 'I':
377 if (isInfinity(data, end)) {
378 data += SizeOfInfinity;
6fe7ccc8 379 return std::numeric_limits<double>::infinity();
14957cd0
A
380 }
381 break;
382
383 case '+':
384 if (isInfinity(data + 1, end)) {
385 data += SizeOfInfinity + 1;
6fe7ccc8 386 return std::numeric_limits<double>::infinity();
14957cd0
A
387 }
388 break;
389
390 case '-':
391 if (isInfinity(data + 1, end)) {
392 data += SizeOfInfinity + 1;
6fe7ccc8 393 return -std::numeric_limits<double>::infinity();
14957cd0
A
394 }
395 break;
396 }
397
398 // Not a number.
6fe7ccc8 399 return std::numeric_limits<double>::quiet_NaN();
14957cd0
A
400}
401
6fe7ccc8
A
402template <typename CharType>
403static double toDouble(const CharType* characters, unsigned size)
14957cd0 404{
6fe7ccc8 405 const CharType* endCharacters = characters + size;
14957cd0
A
406
407 // Skip leading white space.
6fe7ccc8
A
408 for (; characters < endCharacters; ++characters) {
409 if (!isStrWhiteSpace(*characters))
14957cd0
A
410 break;
411 }
6fe7ccc8 412
14957cd0 413 // Empty string.
6fe7ccc8 414 if (characters == endCharacters)
14957cd0 415 return 0.0;
6fe7ccc8 416
14957cd0 417 double number;
6fe7ccc8
A
418 if (characters[0] == '0' && characters + 2 < endCharacters && (characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2]))
419 number = jsHexIntegerLiteral(characters, endCharacters);
14957cd0 420 else
6fe7ccc8
A
421 number = jsStrDecimalLiteral(characters, endCharacters);
422
14957cd0 423 // Allow trailing white space.
6fe7ccc8
A
424 for (; characters < endCharacters; ++characters) {
425 if (!isStrWhiteSpace(*characters))
14957cd0
A
426 break;
427 }
6fe7ccc8
A
428 if (characters != endCharacters)
429 return std::numeric_limits<double>::quiet_NaN();
430
14957cd0
A
431 return number;
432}
433
6fe7ccc8
A
434// See ecma-262 9.3.1
435double jsToNumber(const UString& s)
436{
437 unsigned size = s.length();
438
439 if (size == 1) {
440 UChar c = s[0];
441 if (isASCIIDigit(c))
442 return c - '0';
443 if (isStrWhiteSpace(c))
444 return 0;
445 return std::numeric_limits<double>::quiet_NaN();
446 }
447
448 if (s.is8Bit())
449 return toDouble(s.characters8(), size);
450 return toDouble(s.characters16(), size);
451}
452
9dae56ea
A
453static double parseFloat(const UString& s)
454{
14957cd0 455 unsigned size = s.length();
9dae56ea 456
14957cd0 457 if (size == 1) {
6fe7ccc8 458 UChar c = s[0];
14957cd0
A
459 if (isASCIIDigit(c))
460 return c - '0';
6fe7ccc8
A
461 return std::numeric_limits<double>::quiet_NaN();
462 }
463
464 if (s.is8Bit()) {
465 const LChar* data = s.characters8();
466 const LChar* end = data + size;
467
468 // Skip leading white space.
469 for (; data < end; ++data) {
470 if (!isStrWhiteSpace(*data))
471 break;
472 }
473
474 // Empty string.
475 if (data == end)
476 return std::numeric_limits<double>::quiet_NaN();
477
478 return jsStrDecimalLiteral(data, end);
14957cd0 479 }
9dae56ea 480
6fe7ccc8 481 const UChar* data = s.characters16();
14957cd0 482 const UChar* end = data + size;
9dae56ea 483
14957cd0
A
484 // Skip leading white space.
485 for (; data < end; ++data) {
486 if (!isStrWhiteSpace(*data))
487 break;
488 }
489
490 // Empty string.
491 if (data == end)
6fe7ccc8 492 return std::numeric_limits<double>::quiet_NaN();
14957cd0
A
493
494 return jsStrDecimalLiteral(data, end);
9dae56ea
A
495}
496
14957cd0 497EncodedJSValue JSC_HOST_CALL globalFuncEval(ExecState* exec)
9dae56ea 498{
14957cd0 499 JSObject* thisObject = exec->hostThisValue().toThisObject(exec);
9dae56ea 500 JSObject* unwrappedObject = thisObject->unwrappedObject();
6fe7ccc8 501 if (!unwrappedObject->isGlobalObject() || jsCast<JSGlobalObject*>(unwrappedObject)->evalFunction() != exec->callee())
14957cd0 502 return throwVMError(exec, createEvalError(exec, "The \"this\" value passed to eval must be the global object from which eval originated"));
9dae56ea 503
14957cd0 504 JSValue x = exec->argument(0);
9dae56ea 505 if (!x.isString())
14957cd0 506 return JSValue::encode(x);
9dae56ea 507
6fe7ccc8 508 UString s = x.toString(exec)->value(exec);
9dae56ea 509
6fe7ccc8
A
510 if (s.is8Bit()) {
511 LiteralParser<LChar> preparser(exec, s.characters8(), s.length(), NonStrictJSON);
512 if (JSValue parsedObject = preparser.tryLiteralParse())
513 return JSValue::encode(parsedObject);
514 } else {
515 LiteralParser<UChar> preparser(exec, s.characters16(), s.length(), NonStrictJSON);
516 if (JSValue parsedObject = preparser.tryLiteralParse())
517 return JSValue::encode(parsedObject);
518 }
ba379fdc 519
14957cd0 520 EvalExecutable* eval = EvalExecutable::create(exec, makeSource(s), false);
6fe7ccc8 521 JSObject* error = eval->compile(exec, jsCast<JSGlobalObject*>(unwrappedObject)->globalScopeChain());
f9bf01c6 522 if (error)
14957cd0 523 return throwVMError(exec, error);
9dae56ea 524
6fe7ccc8 525 return JSValue::encode(exec->interpreter()->execute(eval, exec, thisObject, jsCast<JSGlobalObject*>(unwrappedObject)->globalScopeChain()));
9dae56ea
A
526}
527
14957cd0 528EncodedJSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec)
9dae56ea 529{
14957cd0 530 JSValue value = exec->argument(0);
6fe7ccc8
A
531 JSValue radixValue = exec->argument(1);
532
533 // Optimized handling for numbers:
534 // If the argument is 0 or a number in range 10^-6 <= n < INT_MAX+1, then parseInt
535 // results in a truncation to integer. In the case of -0, this is converted to 0.
536 //
537 // This is also a truncation for values in the range INT_MAX+1 <= n < 10^21,
538 // however these values cannot be trivially truncated to int since 10^21 exceeds
539 // even the int64_t range. Negative numbers are a little trickier, the case for
540 // values in the range -10^21 < n <= -1 are similar to those for integer, but
541 // values in the range -1 < n <= -10^-6 need to truncate to -0, not 0.
542 static const double tenToTheMinus6 = 0.000001;
543 static const double intMaxPlusOne = 2147483648.0;
544 if (value.isNumber()) {
545 double n = value.asNumber();
546 if (((n < intMaxPlusOne && n >= tenToTheMinus6) || !n) && radixValue.isUndefinedOrNull())
547 return JSValue::encode(jsNumber(static_cast<int32_t>(n)));
9dae56ea
A
548 }
549
6fe7ccc8
A
550 // If ToString throws, we shouldn't call ToInt32.
551 UString s = value.toString(exec)->value(exec);
552 if (exec->hadException())
553 return JSValue::encode(jsUndefined());
554
555 return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec))));
9dae56ea
A
556}
557
14957cd0 558EncodedJSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec)
9dae56ea 559{
6fe7ccc8 560 return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec)->value(exec))));
9dae56ea
A
561}
562
14957cd0 563EncodedJSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec)
9dae56ea 564{
14957cd0 565 return JSValue::encode(jsBoolean(isnan(exec->argument(0).toNumber(exec))));
9dae56ea
A
566}
567
14957cd0 568EncodedJSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec)
9dae56ea 569{
14957cd0 570 double n = exec->argument(0).toNumber(exec);
6fe7ccc8 571 return JSValue::encode(jsBoolean(isfinite(n)));
9dae56ea
A
572}
573
14957cd0 574EncodedJSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec)
9dae56ea
A
575{
576 static const char do_not_unescape_when_decoding_URI[] =
577 "#$&+,/:;=?@";
578
14957cd0 579 return JSValue::encode(decode(exec, do_not_unescape_when_decoding_URI, true));
9dae56ea
A
580}
581
14957cd0 582EncodedJSValue JSC_HOST_CALL globalFuncDecodeURIComponent(ExecState* exec)
9dae56ea 583{
14957cd0 584 return JSValue::encode(decode(exec, "", true));
9dae56ea
A
585}
586
14957cd0 587EncodedJSValue JSC_HOST_CALL globalFuncEncodeURI(ExecState* exec)
9dae56ea
A
588{
589 static const char do_not_escape_when_encoding_URI[] =
590 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
591 "abcdefghijklmnopqrstuvwxyz"
592 "0123456789"
593 "!#$&'()*+,-./:;=?@_~";
594
14957cd0 595 return JSValue::encode(encode(exec, do_not_escape_when_encoding_URI));
9dae56ea
A
596}
597
14957cd0 598EncodedJSValue JSC_HOST_CALL globalFuncEncodeURIComponent(ExecState* exec)
9dae56ea
A
599{
600 static const char do_not_escape_when_encoding_URI_component[] =
601 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
602 "abcdefghijklmnopqrstuvwxyz"
603 "0123456789"
604 "!'()*-._~";
605
14957cd0 606 return JSValue::encode(encode(exec, do_not_escape_when_encoding_URI_component));
9dae56ea
A
607}
608
14957cd0 609EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec)
9dae56ea
A
610{
611 static const char do_not_escape[] =
612 "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
613 "abcdefghijklmnopqrstuvwxyz"
614 "0123456789"
615 "*+-./@_";
616
4e4e5a6f 617 JSStringBuilder builder;
6fe7ccc8
A
618 UString str = exec->argument(0).toString(exec)->value(exec);
619 if (str.is8Bit()) {
620 const LChar* c = str.characters8();
621 for (unsigned k = 0; k < str.length(); k++, c++) {
622 int u = c[0];
623 if (u && strchr(do_not_escape, static_cast<char>(u)))
624 builder.append(c, 1);
625 else {
626 char tmp[4];
627 snprintf(tmp, sizeof(tmp), "%%%02X", u);
628 builder.append(tmp);
629 }
630 }
631
632 return JSValue::encode(builder.build(exec));
633 }
634
635 const UChar* c = str.characters16();
14957cd0 636 for (unsigned k = 0; k < str.length(); k++, c++) {
9dae56ea
A
637 int u = c[0];
638 if (u > 255) {
639 char tmp[7];
640 snprintf(tmp, sizeof(tmp), "%%u%04X", u);
4e4e5a6f 641 builder.append(tmp);
9dae56ea 642 } else if (u != 0 && strchr(do_not_escape, static_cast<char>(u)))
4e4e5a6f 643 builder.append(c, 1);
9dae56ea
A
644 else {
645 char tmp[4];
646 snprintf(tmp, sizeof(tmp), "%%%02X", u);
4e4e5a6f 647 builder.append(tmp);
9dae56ea 648 }
9dae56ea
A
649 }
650
14957cd0 651 return JSValue::encode(builder.build(exec));
9dae56ea
A
652}
653
14957cd0 654EncodedJSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec)
9dae56ea 655{
14957cd0 656 UStringBuilder builder;
6fe7ccc8 657 UString str = exec->argument(0).toString(exec)->value(exec);
9dae56ea 658 int k = 0;
14957cd0 659 int len = str.length();
6fe7ccc8
A
660
661 if (str.is8Bit()) {
662 const LChar* characters = str.characters8();
663 LChar convertedLChar;
664 while (k < len) {
665 const LChar* c = characters + k;
666 if (c[0] == '%' && k <= len - 6 && c[1] == 'u') {
667 if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) {
668 builder.append(Lexer<UChar>::convertUnicode(c[2], c[3], c[4], c[5]));
669 k += 6;
670 continue;
671 }
672 } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) {
673 convertedLChar = LChar(Lexer<LChar>::convertHex(c[1], c[2]));
674 c = &convertedLChar;
675 k += 2;
9dae56ea 676 }
6fe7ccc8
A
677 builder.append(*c);
678 k++;
679 }
680 } else {
681 const UChar* characters = str.characters16();
682
683 while (k < len) {
684 const UChar* c = characters + k;
685 UChar convertedUChar;
686 if (c[0] == '%' && k <= len - 6 && c[1] == 'u') {
687 if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) {
688 convertedUChar = Lexer<UChar>::convertUnicode(c[2], c[3], c[4], c[5]);
689 c = &convertedUChar;
690 k += 5;
691 }
692 } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) {
693 convertedUChar = UChar(Lexer<UChar>::convertHex(c[1], c[2]));
694 c = &convertedUChar;
695 k += 2;
696 }
697 k++;
698 builder.append(*c);
9dae56ea 699 }
9dae56ea
A
700 }
701
14957cd0 702 return JSValue::encode(jsString(exec, builder.toUString()));
9dae56ea 703}
9dae56ea 704
6fe7ccc8
A
705EncodedJSValue JSC_HOST_CALL globalFuncThrowTypeError(ExecState* exec)
706{
707 return throwVMTypeError(exec);
708}
709
710EncodedJSValue JSC_HOST_CALL globalFuncProtoGetter(ExecState* exec)
711{
712 if (!exec->thisValue().isObject())
713 return JSValue::encode(exec->thisValue().synthesizePrototype(exec));
714
715 JSObject* thisObject = asObject(exec->thisValue());
716 if (!thisObject->allowsAccessFrom(exec->trueCallerFrame()))
717 return JSValue::encode(jsUndefined());
718
719 return JSValue::encode(thisObject->prototype());
720}
721
722EncodedJSValue JSC_HOST_CALL globalFuncProtoSetter(ExecState* exec)
723{
724 JSValue value = exec->argument(0);
725
726 // Setting __proto__ of a primitive should have no effect.
727 if (!exec->thisValue().isObject())
728 return JSValue::encode(jsUndefined());
729
730 JSObject* thisObject = asObject(exec->thisValue());
731 if (!thisObject->allowsAccessFrom(exec->trueCallerFrame()))
732 return JSValue::encode(jsUndefined());
733
734 // Setting __proto__ to a non-object, non-null value is silently ignored to match Mozilla.
735 if (!value.isObject() && !value.isNull())
736 return JSValue::encode(jsUndefined());
737
738 if (!thisObject->isExtensible())
739 return throwVMError(exec, createTypeError(exec, StrictModeReadonlyPropertyWriteError));
740
741 if (!thisObject->setPrototypeWithCycleCheck(exec->globalData(), value))
742 throwError(exec, createError(exec, "cyclic __proto__ value"));
743 return JSValue::encode(jsUndefined());
744}
745
9dae56ea 746} // namespace JSC