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