]>
Commit | Line | Data |
---|---|---|
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, 2012 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 "JSFunction.h" | |
31 | #include "JSGlobalObject.h" | |
32 | #include "JSString.h" | |
33 | #include "JSStringBuilder.h" | |
34 | #include "Lexer.h" | |
35 | #include "LiteralParser.h" | |
36 | #include "Nodes.h" | |
37 | #include "JSCInlines.h" | |
38 | #include "Parser.h" | |
39 | #include "StackVisitor.h" | |
40 | #include <wtf/dtoa.h> | |
41 | #include <stdio.h> | |
42 | #include <stdlib.h> | |
43 | #include <wtf/ASCIICType.h> | |
44 | #include <wtf/Assertions.h> | |
45 | #include <wtf/HexNumber.h> | |
46 | #include <wtf/MathExtras.h> | |
47 | #include <wtf/StringExtras.h> | |
48 | #include <wtf/text/StringBuilder.h> | |
49 | #include <wtf/unicode/UTF8.h> | |
50 | ||
51 | using namespace WTF; | |
52 | using namespace Unicode; | |
53 | ||
54 | namespace JSC { | |
55 | ||
56 | template<unsigned charactersCount> | |
57 | static Bitmap<256> makeCharacterBitmap(const char (&characters)[charactersCount]) | |
58 | { | |
59 | Bitmap<256> bitmap; | |
60 | for (unsigned i = 0; i < charactersCount; ++i) | |
61 | bitmap.set(characters[i]); | |
62 | return bitmap; | |
63 | } | |
64 | ||
65 | static JSValue encode(ExecState* exec, const Bitmap<256>& doNotEscape) | |
66 | { | |
67 | CString cstr = exec->argument(0).toString(exec)->view(exec).get().utf8(StrictConversion); | |
68 | if (!cstr.data()) | |
69 | return exec->vm().throwException(exec, createURIError(exec, ASCIILiteral("String contained an illegal UTF-16 sequence."))); | |
70 | ||
71 | JSStringBuilder builder; | |
72 | const char* p = cstr.data(); | |
73 | for (size_t k = 0; k < cstr.length(); k++, p++) { | |
74 | char c = *p; | |
75 | if (c && doNotEscape.get(static_cast<LChar>(c))) | |
76 | builder.append(static_cast<LChar>(c)); | |
77 | else { | |
78 | builder.append(static_cast<LChar>('%')); | |
79 | appendByteAsHex(c, builder); | |
80 | } | |
81 | } | |
82 | return builder.build(exec); | |
83 | } | |
84 | ||
85 | template <typename CharType> | |
86 | ALWAYS_INLINE | |
87 | static JSValue decode(ExecState* exec, const CharType* characters, int length, const Bitmap<256>& doNotUnescape, bool strict) | |
88 | { | |
89 | JSStringBuilder builder; | |
90 | int k = 0; | |
91 | UChar u = 0; | |
92 | while (k < length) { | |
93 | const CharType* p = characters + k; | |
94 | CharType c = *p; | |
95 | if (c == '%') { | |
96 | int charLen = 0; | |
97 | if (k <= length - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) { | |
98 | const char b0 = Lexer<CharType>::convertHex(p[1], p[2]); | |
99 | const int sequenceLen = UTF8SequenceLength(b0); | |
100 | if (sequenceLen && k <= length - sequenceLen * 3) { | |
101 | charLen = sequenceLen * 3; | |
102 | char sequence[5]; | |
103 | sequence[0] = b0; | |
104 | for (int i = 1; i < sequenceLen; ++i) { | |
105 | const CharType* q = p + i * 3; | |
106 | if (q[0] == '%' && isASCIIHexDigit(q[1]) && isASCIIHexDigit(q[2])) | |
107 | sequence[i] = Lexer<CharType>::convertHex(q[1], q[2]); | |
108 | else { | |
109 | charLen = 0; | |
110 | break; | |
111 | } | |
112 | } | |
113 | if (charLen != 0) { | |
114 | sequence[sequenceLen] = 0; | |
115 | const int character = decodeUTF8Sequence(sequence); | |
116 | if (character < 0 || character >= 0x110000) | |
117 | charLen = 0; | |
118 | else if (character >= 0x10000) { | |
119 | // Convert to surrogate pair. | |
120 | builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10))); | |
121 | u = static_cast<UChar>(0xDC00 | ((character - 0x10000) & 0x3FF)); | |
122 | } else | |
123 | u = static_cast<UChar>(character); | |
124 | } | |
125 | } | |
126 | } | |
127 | if (charLen == 0) { | |
128 | if (strict) | |
129 | return exec->vm().throwException(exec, createURIError(exec, ASCIILiteral("URI error"))); | |
130 | // The only case where we don't use "strict" mode is the "unescape" function. | |
131 | // For that, it's good to support the wonky "%u" syntax for compatibility with WinIE. | |
132 | if (k <= length - 6 && p[1] == 'u' | |
133 | && isASCIIHexDigit(p[2]) && isASCIIHexDigit(p[3]) | |
134 | && isASCIIHexDigit(p[4]) && isASCIIHexDigit(p[5])) { | |
135 | charLen = 6; | |
136 | u = Lexer<UChar>::convertUnicode(p[2], p[3], p[4], p[5]); | |
137 | } | |
138 | } | |
139 | if (charLen && (u == 0 || u >= 128 || !doNotUnescape.get(static_cast<LChar>(u)))) { | |
140 | builder.append(u); | |
141 | k += charLen; | |
142 | continue; | |
143 | } | |
144 | } | |
145 | k++; | |
146 | builder.append(c); | |
147 | } | |
148 | return builder.build(exec); | |
149 | } | |
150 | ||
151 | static JSValue decode(ExecState* exec, const Bitmap<256>& doNotUnescape, bool strict) | |
152 | { | |
153 | StringView str = exec->argument(0).toString(exec)->view(exec); | |
154 | ||
155 | if (str.is8Bit()) | |
156 | return decode(exec, str.characters8(), str.length(), doNotUnescape, strict); | |
157 | return decode(exec, str.characters16(), str.length(), doNotUnescape, strict); | |
158 | } | |
159 | ||
160 | bool isStrWhiteSpace(UChar c) | |
161 | { | |
162 | switch (c) { | |
163 | // ECMA-262-5th 7.2 & 7.3 | |
164 | case 0x0009: | |
165 | case 0x000A: | |
166 | case 0x000B: | |
167 | case 0x000C: | |
168 | case 0x000D: | |
169 | case 0x0020: | |
170 | case 0x00A0: | |
171 | case 0x180E: // This character used to be in Zs category before Unicode 6.3, and EcmaScript says that we should keep treating it as such. | |
172 | case 0x2028: | |
173 | case 0x2029: | |
174 | case 0xFEFF: | |
175 | return true; | |
176 | default: | |
177 | return c > 0xFF && u_charType(c) == U_SPACE_SEPARATOR; | |
178 | } | |
179 | } | |
180 | ||
181 | static int parseDigit(unsigned short c, int radix) | |
182 | { | |
183 | int digit = -1; | |
184 | ||
185 | if (c >= '0' && c <= '9') | |
186 | digit = c - '0'; | |
187 | else if (c >= 'A' && c <= 'Z') | |
188 | digit = c - 'A' + 10; | |
189 | else if (c >= 'a' && c <= 'z') | |
190 | digit = c - 'a' + 10; | |
191 | ||
192 | if (digit >= radix) | |
193 | return -1; | |
194 | return digit; | |
195 | } | |
196 | ||
197 | double parseIntOverflow(const LChar* s, unsigned length, int radix) | |
198 | { | |
199 | double number = 0.0; | |
200 | double radixMultiplier = 1.0; | |
201 | ||
202 | for (const LChar* p = s + length - 1; p >= s; p--) { | |
203 | if (radixMultiplier == std::numeric_limits<double>::infinity()) { | |
204 | if (*p != '0') { | |
205 | number = std::numeric_limits<double>::infinity(); | |
206 | break; | |
207 | } | |
208 | } else { | |
209 | int digit = parseDigit(*p, radix); | |
210 | number += digit * radixMultiplier; | |
211 | } | |
212 | ||
213 | radixMultiplier *= radix; | |
214 | } | |
215 | ||
216 | return number; | |
217 | } | |
218 | ||
219 | static double parseIntOverflow(const UChar* s, unsigned length, int radix) | |
220 | { | |
221 | double number = 0.0; | |
222 | double radixMultiplier = 1.0; | |
223 | ||
224 | for (const UChar* p = s + length - 1; p >= s; p--) { | |
225 | if (radixMultiplier == std::numeric_limits<double>::infinity()) { | |
226 | if (*p != '0') { | |
227 | number = std::numeric_limits<double>::infinity(); | |
228 | break; | |
229 | } | |
230 | } else { | |
231 | int digit = parseDigit(*p, radix); | |
232 | number += digit * radixMultiplier; | |
233 | } | |
234 | ||
235 | radixMultiplier *= radix; | |
236 | } | |
237 | ||
238 | return number; | |
239 | } | |
240 | ||
241 | static double parseIntOverflow(StringView string, int radix) | |
242 | { | |
243 | if (string.is8Bit()) | |
244 | return parseIntOverflow(string.characters8(), string.length(), radix); | |
245 | return parseIntOverflow(string.characters16(), string.length(), radix); | |
246 | } | |
247 | ||
248 | // ES5.1 15.1.2.2 | |
249 | template <typename CharType> | |
250 | ALWAYS_INLINE | |
251 | static double parseInt(StringView s, const CharType* data, int radix) | |
252 | { | |
253 | // 1. Let inputString be ToString(string). | |
254 | // 2. Let S be a newly created substring of inputString consisting of the first character that is not a | |
255 | // StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white | |
256 | // space.) If inputString does not contain any such characters, let S be the empty string. | |
257 | int length = s.length(); | |
258 | int p = 0; | |
259 | while (p < length && isStrWhiteSpace(data[p])) | |
260 | ++p; | |
261 | ||
262 | // 3. Let sign be 1. | |
263 | // 4. If S is not empty and the first character of S is a minus sign -, let sign be -1. | |
264 | // 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. | |
265 | double sign = 1; | |
266 | if (p < length) { | |
267 | if (data[p] == '+') | |
268 | ++p; | |
269 | else if (data[p] == '-') { | |
270 | sign = -1; | |
271 | ++p; | |
272 | } | |
273 | } | |
274 | ||
275 | // 6. Let R = ToInt32(radix). | |
276 | // 7. Let stripPrefix be true. | |
277 | // 8. If R != 0,then | |
278 | // b. If R != 16, let stripPrefix be false. | |
279 | // 9. Else, R == 0 | |
280 | // a. LetR = 10. | |
281 | // 10. If stripPrefix is true, then | |
282 | // a. If the length of S is at least 2 and the first two characters of S are either ―0x or ―0X, | |
283 | // then remove the first two characters from S and let R = 16. | |
284 | // 11. If S contains any character that is not a radix-R digit, then let Z be the substring of S | |
285 | // consisting of all characters before the first such character; otherwise, let Z be S. | |
286 | if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) { | |
287 | radix = 16; | |
288 | p += 2; | |
289 | } else if (radix == 0) | |
290 | radix = 10; | |
291 | ||
292 | // 8.a If R < 2 or R > 36, then return NaN. | |
293 | if (radix < 2 || radix > 36) | |
294 | return PNaN; | |
295 | ||
296 | // 13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters | |
297 | // A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant | |
298 | // digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; | |
299 | // and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the | |
300 | // mathematical integer value that is represented by Z in radix-R notation.) | |
301 | // 14. Let number be the Number value for mathInt. | |
302 | int firstDigitPosition = p; | |
303 | bool sawDigit = false; | |
304 | double number = 0; | |
305 | while (p < length) { | |
306 | int digit = parseDigit(data[p], radix); | |
307 | if (digit == -1) | |
308 | break; | |
309 | sawDigit = true; | |
310 | number *= radix; | |
311 | number += digit; | |
312 | ++p; | |
313 | } | |
314 | ||
315 | // 12. If Z is empty, return NaN. | |
316 | if (!sawDigit) | |
317 | return PNaN; | |
318 | ||
319 | // Alternate code path for certain large numbers. | |
320 | if (number >= mantissaOverflowLowerBound) { | |
321 | if (radix == 10) { | |
322 | size_t parsedLength; | |
323 | number = parseDouble(s.substring(firstDigitPosition, p - firstDigitPosition), parsedLength); | |
324 | } else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) | |
325 | number = parseIntOverflow(s.substring(firstDigitPosition, p - firstDigitPosition), radix); | |
326 | } | |
327 | ||
328 | // 15. Return sign x number. | |
329 | return sign * number; | |
330 | } | |
331 | ||
332 | static double parseInt(StringView s, int radix) | |
333 | { | |
334 | if (s.is8Bit()) | |
335 | return parseInt(s, s.characters8(), radix); | |
336 | return parseInt(s, s.characters16(), radix); | |
337 | } | |
338 | ||
339 | static const int SizeOfInfinity = 8; | |
340 | ||
341 | template <typename CharType> | |
342 | static bool isInfinity(const CharType* data, const CharType* end) | |
343 | { | |
344 | return (end - data) >= SizeOfInfinity | |
345 | && data[0] == 'I' | |
346 | && data[1] == 'n' | |
347 | && data[2] == 'f' | |
348 | && data[3] == 'i' | |
349 | && data[4] == 'n' | |
350 | && data[5] == 'i' | |
351 | && data[6] == 't' | |
352 | && data[7] == 'y'; | |
353 | } | |
354 | ||
355 | // See ecma-262 6th 11.8.3 | |
356 | template <typename CharType> | |
357 | static double jsBinaryIntegerLiteral(const CharType*& data, const CharType* end) | |
358 | { | |
359 | // Binary number. | |
360 | data += 2; | |
361 | const CharType* firstDigitPosition = data; | |
362 | double number = 0; | |
363 | while (true) { | |
364 | number = number * 2 + (*data - '0'); | |
365 | ++data; | |
366 | if (data == end) | |
367 | break; | |
368 | if (!isASCIIBinaryDigit(*data)) | |
369 | break; | |
370 | } | |
371 | if (number >= mantissaOverflowLowerBound) | |
372 | number = parseIntOverflow(firstDigitPosition, data - firstDigitPosition, 2); | |
373 | ||
374 | return number; | |
375 | } | |
376 | ||
377 | // See ecma-262 6th 11.8.3 | |
378 | template <typename CharType> | |
379 | static double jsOctalIntegerLiteral(const CharType*& data, const CharType* end) | |
380 | { | |
381 | // Octal number. | |
382 | data += 2; | |
383 | const CharType* firstDigitPosition = data; | |
384 | double number = 0; | |
385 | while (true) { | |
386 | number = number * 8 + (*data - '0'); | |
387 | ++data; | |
388 | if (data == end) | |
389 | break; | |
390 | if (!isASCIIOctalDigit(*data)) | |
391 | break; | |
392 | } | |
393 | if (number >= mantissaOverflowLowerBound) | |
394 | number = parseIntOverflow(firstDigitPosition, data - firstDigitPosition, 8); | |
395 | ||
396 | return number; | |
397 | } | |
398 | ||
399 | // See ecma-262 6th 11.8.3 | |
400 | template <typename CharType> | |
401 | static double jsHexIntegerLiteral(const CharType*& data, const CharType* end) | |
402 | { | |
403 | // Hex number. | |
404 | data += 2; | |
405 | const CharType* firstDigitPosition = data; | |
406 | double number = 0; | |
407 | while (true) { | |
408 | number = number * 16 + toASCIIHexValue(*data); | |
409 | ++data; | |
410 | if (data == end) | |
411 | break; | |
412 | if (!isASCIIHexDigit(*data)) | |
413 | break; | |
414 | } | |
415 | if (number >= mantissaOverflowLowerBound) | |
416 | number = parseIntOverflow(firstDigitPosition, data - firstDigitPosition, 16); | |
417 | ||
418 | return number; | |
419 | } | |
420 | ||
421 | // See ecma-262 6th 11.8.3 | |
422 | template <typename CharType> | |
423 | static double jsStrDecimalLiteral(const CharType*& data, const CharType* end) | |
424 | { | |
425 | RELEASE_ASSERT(data < end); | |
426 | ||
427 | size_t parsedLength; | |
428 | double number = parseDouble(data, end - data, parsedLength); | |
429 | if (parsedLength) { | |
430 | data += parsedLength; | |
431 | return number; | |
432 | } | |
433 | ||
434 | // Check for [+-]?Infinity | |
435 | switch (*data) { | |
436 | case 'I': | |
437 | if (isInfinity(data, end)) { | |
438 | data += SizeOfInfinity; | |
439 | return std::numeric_limits<double>::infinity(); | |
440 | } | |
441 | break; | |
442 | ||
443 | case '+': | |
444 | if (isInfinity(data + 1, end)) { | |
445 | data += SizeOfInfinity + 1; | |
446 | return std::numeric_limits<double>::infinity(); | |
447 | } | |
448 | break; | |
449 | ||
450 | case '-': | |
451 | if (isInfinity(data + 1, end)) { | |
452 | data += SizeOfInfinity + 1; | |
453 | return -std::numeric_limits<double>::infinity(); | |
454 | } | |
455 | break; | |
456 | } | |
457 | ||
458 | // Not a number. | |
459 | return PNaN; | |
460 | } | |
461 | ||
462 | template <typename CharType> | |
463 | static double toDouble(const CharType* characters, unsigned size) | |
464 | { | |
465 | const CharType* endCharacters = characters + size; | |
466 | ||
467 | // Skip leading white space. | |
468 | for (; characters < endCharacters; ++characters) { | |
469 | if (!isStrWhiteSpace(*characters)) | |
470 | break; | |
471 | } | |
472 | ||
473 | // Empty string. | |
474 | if (characters == endCharacters) | |
475 | return 0.0; | |
476 | ||
477 | double number; | |
478 | if (characters[0] == '0' && characters + 2 < endCharacters) { | |
479 | if ((characters[1] | 0x20) == 'x' && isASCIIHexDigit(characters[2])) | |
480 | number = jsHexIntegerLiteral(characters, endCharacters); | |
481 | else if ((characters[1] | 0x20) == 'o' && isASCIIOctalDigit(characters[2])) | |
482 | number = jsOctalIntegerLiteral(characters, endCharacters); | |
483 | else if ((characters[1] | 0x20) == 'b' && isASCIIBinaryDigit(characters[2])) | |
484 | number = jsBinaryIntegerLiteral(characters, endCharacters); | |
485 | else | |
486 | number = jsStrDecimalLiteral(characters, endCharacters); | |
487 | } else | |
488 | number = jsStrDecimalLiteral(characters, endCharacters); | |
489 | ||
490 | // Allow trailing white space. | |
491 | for (; characters < endCharacters; ++characters) { | |
492 | if (!isStrWhiteSpace(*characters)) | |
493 | break; | |
494 | } | |
495 | if (characters != endCharacters) | |
496 | return PNaN; | |
497 | ||
498 | return number; | |
499 | } | |
500 | ||
501 | // See ecma-262 6th 11.8.3 | |
502 | double jsToNumber(StringView s) | |
503 | { | |
504 | unsigned size = s.length(); | |
505 | ||
506 | if (size == 1) { | |
507 | UChar c = s[0]; | |
508 | if (isASCIIDigit(c)) | |
509 | return c - '0'; | |
510 | if (isStrWhiteSpace(c)) | |
511 | return 0; | |
512 | return PNaN; | |
513 | } | |
514 | ||
515 | if (s.is8Bit()) | |
516 | return toDouble(s.characters8(), size); | |
517 | return toDouble(s.characters16(), size); | |
518 | } | |
519 | ||
520 | static double parseFloat(StringView s) | |
521 | { | |
522 | unsigned size = s.length(); | |
523 | ||
524 | if (size == 1) { | |
525 | UChar c = s[0]; | |
526 | if (isASCIIDigit(c)) | |
527 | return c - '0'; | |
528 | return PNaN; | |
529 | } | |
530 | ||
531 | if (s.is8Bit()) { | |
532 | const LChar* data = s.characters8(); | |
533 | const LChar* end = data + size; | |
534 | ||
535 | // Skip leading white space. | |
536 | for (; data < end; ++data) { | |
537 | if (!isStrWhiteSpace(*data)) | |
538 | break; | |
539 | } | |
540 | ||
541 | // Empty string. | |
542 | if (data == end) | |
543 | return PNaN; | |
544 | ||
545 | return jsStrDecimalLiteral(data, end); | |
546 | } | |
547 | ||
548 | const UChar* data = s.characters16(); | |
549 | const UChar* end = data + size; | |
550 | ||
551 | // Skip leading white space. | |
552 | for (; data < end; ++data) { | |
553 | if (!isStrWhiteSpace(*data)) | |
554 | break; | |
555 | } | |
556 | ||
557 | // Empty string. | |
558 | if (data == end) | |
559 | return PNaN; | |
560 | ||
561 | return jsStrDecimalLiteral(data, end); | |
562 | } | |
563 | ||
564 | EncodedJSValue JSC_HOST_CALL globalFuncEval(ExecState* exec) | |
565 | { | |
566 | JSValue x = exec->argument(0); | |
567 | if (!x.isString()) | |
568 | return JSValue::encode(x); | |
569 | ||
570 | String s = x.toString(exec)->value(exec); | |
571 | ||
572 | if (s.is8Bit()) { | |
573 | LiteralParser<LChar> preparser(exec, s.characters8(), s.length(), NonStrictJSON); | |
574 | if (JSValue parsedObject = preparser.tryLiteralParse()) | |
575 | return JSValue::encode(parsedObject); | |
576 | } else { | |
577 | LiteralParser<UChar> preparser(exec, s.characters16(), s.length(), NonStrictJSON); | |
578 | if (JSValue parsedObject = preparser.tryLiteralParse()) | |
579 | return JSValue::encode(parsedObject); | |
580 | } | |
581 | ||
582 | JSGlobalObject* calleeGlobalObject = exec->callee()->globalObject(); | |
583 | EvalExecutable* eval = EvalExecutable::create(exec, makeSource(s), false, ThisTDZMode::CheckIfNeeded); | |
584 | if (!eval) | |
585 | return JSValue::encode(jsUndefined()); | |
586 | ||
587 | return JSValue::encode(exec->interpreter()->execute(eval, exec, calleeGlobalObject->globalThis(), calleeGlobalObject)); | |
588 | } | |
589 | ||
590 | EncodedJSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec) | |
591 | { | |
592 | JSValue value = exec->argument(0); | |
593 | JSValue radixValue = exec->argument(1); | |
594 | ||
595 | // Optimized handling for numbers: | |
596 | // If the argument is 0 or a number in range 10^-6 <= n < INT_MAX+1, then parseInt | |
597 | // results in a truncation to integer. In the case of -0, this is converted to 0. | |
598 | // | |
599 | // This is also a truncation for values in the range INT_MAX+1 <= n < 10^21, | |
600 | // however these values cannot be trivially truncated to int since 10^21 exceeds | |
601 | // even the int64_t range. Negative numbers are a little trickier, the case for | |
602 | // values in the range -10^21 < n <= -1 are similar to those for integer, but | |
603 | // values in the range -1 < n <= -10^-6 need to truncate to -0, not 0. | |
604 | static const double tenToTheMinus6 = 0.000001; | |
605 | static const double intMaxPlusOne = 2147483648.0; | |
606 | if (value.isNumber()) { | |
607 | double n = value.asNumber(); | |
608 | if (((n < intMaxPlusOne && n >= tenToTheMinus6) || !n) && radixValue.isUndefinedOrNull()) | |
609 | return JSValue::encode(jsNumber(static_cast<int32_t>(n))); | |
610 | } | |
611 | ||
612 | // If ToString throws, we shouldn't call ToInt32. | |
613 | StringView s = value.toString(exec)->view(exec); | |
614 | if (exec->hadException()) | |
615 | return JSValue::encode(jsUndefined()); | |
616 | ||
617 | return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec)))); | |
618 | } | |
619 | ||
620 | EncodedJSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec) | |
621 | { | |
622 | return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec)->view(exec)))); | |
623 | } | |
624 | ||
625 | EncodedJSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec) | |
626 | { | |
627 | return JSValue::encode(jsBoolean(std::isnan(exec->argument(0).toNumber(exec)))); | |
628 | } | |
629 | ||
630 | EncodedJSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec) | |
631 | { | |
632 | double n = exec->argument(0).toNumber(exec); | |
633 | return JSValue::encode(jsBoolean(std::isfinite(n))); | |
634 | } | |
635 | ||
636 | EncodedJSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec) | |
637 | { | |
638 | static Bitmap<256> doNotUnescapeWhenDecodingURI = makeCharacterBitmap( | |
639 | "#$&+,/:;=?@" | |
640 | ); | |
641 | ||
642 | return JSValue::encode(decode(exec, doNotUnescapeWhenDecodingURI, true)); | |
643 | } | |
644 | ||
645 | EncodedJSValue JSC_HOST_CALL globalFuncDecodeURIComponent(ExecState* exec) | |
646 | { | |
647 | static Bitmap<256> emptyBitmap; | |
648 | return JSValue::encode(decode(exec, emptyBitmap, true)); | |
649 | } | |
650 | ||
651 | EncodedJSValue JSC_HOST_CALL globalFuncEncodeURI(ExecState* exec) | |
652 | { | |
653 | static Bitmap<256> doNotEscapeWhenEncodingURI = makeCharacterBitmap( | |
654 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
655 | "abcdefghijklmnopqrstuvwxyz" | |
656 | "0123456789" | |
657 | "!#$&'()*+,-./:;=?@_~" | |
658 | ); | |
659 | ||
660 | return JSValue::encode(encode(exec, doNotEscapeWhenEncodingURI)); | |
661 | } | |
662 | ||
663 | EncodedJSValue JSC_HOST_CALL globalFuncEncodeURIComponent(ExecState* exec) | |
664 | { | |
665 | static Bitmap<256> doNotEscapeWhenEncodingURIComponent = makeCharacterBitmap( | |
666 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
667 | "abcdefghijklmnopqrstuvwxyz" | |
668 | "0123456789" | |
669 | "!'()*-._~" | |
670 | ); | |
671 | ||
672 | return JSValue::encode(encode(exec, doNotEscapeWhenEncodingURIComponent)); | |
673 | } | |
674 | ||
675 | EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec) | |
676 | { | |
677 | static Bitmap<256> doNotEscape = makeCharacterBitmap( | |
678 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" | |
679 | "abcdefghijklmnopqrstuvwxyz" | |
680 | "0123456789" | |
681 | "*+-./@_" | |
682 | ); | |
683 | ||
684 | JSStringBuilder builder; | |
685 | StringView str = exec->argument(0).toString(exec)->view(exec); | |
686 | if (str.is8Bit()) { | |
687 | const LChar* c = str.characters8(); | |
688 | for (unsigned k = 0; k < str.length(); k++, c++) { | |
689 | int u = c[0]; | |
690 | if (u && doNotEscape.get(static_cast<LChar>(u))) | |
691 | builder.append(*c); | |
692 | else { | |
693 | builder.append(static_cast<LChar>('%')); | |
694 | appendByteAsHex(static_cast<LChar>(u), builder); | |
695 | } | |
696 | } | |
697 | ||
698 | return JSValue::encode(builder.build(exec)); | |
699 | } | |
700 | ||
701 | const UChar* c = str.characters16(); | |
702 | for (unsigned k = 0; k < str.length(); k++, c++) { | |
703 | int u = c[0]; | |
704 | if (u > 255) { | |
705 | builder.append(static_cast<LChar>('%')); | |
706 | builder.append(static_cast<LChar>('u')); | |
707 | appendByteAsHex(u >> 8, builder); | |
708 | appendByteAsHex(u & 0xFF, builder); | |
709 | } else if (u != 0 && doNotEscape.get(static_cast<LChar>(u))) | |
710 | builder.append(*c); | |
711 | else { | |
712 | builder.append(static_cast<LChar>('%')); | |
713 | appendByteAsHex(u, builder); | |
714 | } | |
715 | } | |
716 | ||
717 | return JSValue::encode(builder.build(exec)); | |
718 | } | |
719 | ||
720 | EncodedJSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec) | |
721 | { | |
722 | StringBuilder builder; | |
723 | StringView str = exec->argument(0).toString(exec)->view(exec); | |
724 | int k = 0; | |
725 | int len = str.length(); | |
726 | ||
727 | if (str.is8Bit()) { | |
728 | const LChar* characters = str.characters8(); | |
729 | LChar convertedLChar; | |
730 | while (k < len) { | |
731 | const LChar* c = characters + k; | |
732 | if (c[0] == '%' && k <= len - 6 && c[1] == 'u') { | |
733 | if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) { | |
734 | builder.append(Lexer<UChar>::convertUnicode(c[2], c[3], c[4], c[5])); | |
735 | k += 6; | |
736 | continue; | |
737 | } | |
738 | } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) { | |
739 | convertedLChar = LChar(Lexer<LChar>::convertHex(c[1], c[2])); | |
740 | c = &convertedLChar; | |
741 | k += 2; | |
742 | } | |
743 | builder.append(*c); | |
744 | k++; | |
745 | } | |
746 | } else { | |
747 | const UChar* characters = str.characters16(); | |
748 | ||
749 | while (k < len) { | |
750 | const UChar* c = characters + k; | |
751 | UChar convertedUChar; | |
752 | if (c[0] == '%' && k <= len - 6 && c[1] == 'u') { | |
753 | if (isASCIIHexDigit(c[2]) && isASCIIHexDigit(c[3]) && isASCIIHexDigit(c[4]) && isASCIIHexDigit(c[5])) { | |
754 | convertedUChar = Lexer<UChar>::convertUnicode(c[2], c[3], c[4], c[5]); | |
755 | c = &convertedUChar; | |
756 | k += 5; | |
757 | } | |
758 | } else if (c[0] == '%' && k <= len - 3 && isASCIIHexDigit(c[1]) && isASCIIHexDigit(c[2])) { | |
759 | convertedUChar = UChar(Lexer<UChar>::convertHex(c[1], c[2])); | |
760 | c = &convertedUChar; | |
761 | k += 2; | |
762 | } | |
763 | k++; | |
764 | builder.append(*c); | |
765 | } | |
766 | } | |
767 | ||
768 | return JSValue::encode(jsString(exec, builder.toString())); | |
769 | } | |
770 | ||
771 | EncodedJSValue JSC_HOST_CALL globalFuncThrowTypeError(ExecState* exec) | |
772 | { | |
773 | return throwVMTypeError(exec); | |
774 | } | |
775 | ||
776 | class GlobalFuncProtoGetterFunctor { | |
777 | public: | |
778 | GlobalFuncProtoGetterFunctor(JSObject* thisObject) | |
779 | : m_hasSkippedFirstFrame(false) | |
780 | , m_thisObject(thisObject) | |
781 | , m_result(JSValue::encode(jsUndefined())) | |
782 | { | |
783 | } | |
784 | ||
785 | EncodedJSValue result() { return m_result; } | |
786 | ||
787 | StackVisitor::Status operator()(StackVisitor& visitor) | |
788 | { | |
789 | if (!m_hasSkippedFirstFrame) { | |
790 | m_hasSkippedFirstFrame = true; | |
791 | return StackVisitor::Continue; | |
792 | } | |
793 | ||
794 | if (m_thisObject->allowsAccessFrom(visitor->callFrame())) | |
795 | m_result = JSValue::encode(m_thisObject->prototype()); | |
796 | ||
797 | return StackVisitor::Done; | |
798 | } | |
799 | ||
800 | private: | |
801 | bool m_hasSkippedFirstFrame; | |
802 | JSObject* m_thisObject; | |
803 | EncodedJSValue m_result; | |
804 | }; | |
805 | ||
806 | EncodedJSValue JSC_HOST_CALL globalFuncProtoGetter(ExecState* exec) | |
807 | { | |
808 | if (exec->thisValue().isUndefinedOrNull()) | |
809 | return throwVMError(exec, createTypeError(exec, "Can't convert undefined or null to object")); | |
810 | ||
811 | JSObject* thisObject = jsDynamicCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)); | |
812 | ||
813 | if (!thisObject) | |
814 | return JSValue::encode(exec->thisValue().synthesizePrototype(exec)); | |
815 | ||
816 | GlobalFuncProtoGetterFunctor functor(thisObject); | |
817 | exec->iterate(functor); | |
818 | return functor.result(); | |
819 | } | |
820 | ||
821 | class GlobalFuncProtoSetterFunctor { | |
822 | public: | |
823 | GlobalFuncProtoSetterFunctor(JSObject* thisObject) | |
824 | : m_hasSkippedFirstFrame(false) | |
825 | , m_allowsAccess(false) | |
826 | , m_thisObject(thisObject) | |
827 | { | |
828 | } | |
829 | ||
830 | bool allowsAccess() const { return m_allowsAccess; } | |
831 | ||
832 | StackVisitor::Status operator()(StackVisitor& visitor) | |
833 | { | |
834 | if (!m_hasSkippedFirstFrame) { | |
835 | m_hasSkippedFirstFrame = true; | |
836 | return StackVisitor::Continue; | |
837 | } | |
838 | ||
839 | m_allowsAccess = m_thisObject->allowsAccessFrom(visitor->callFrame()); | |
840 | return StackVisitor::Done; | |
841 | } | |
842 | ||
843 | private: | |
844 | bool m_hasSkippedFirstFrame; | |
845 | bool m_allowsAccess; | |
846 | JSObject* m_thisObject; | |
847 | }; | |
848 | ||
849 | bool checkProtoSetterAccessAllowed(ExecState* exec, JSObject* object) | |
850 | { | |
851 | GlobalFuncProtoSetterFunctor functor(object); | |
852 | exec->iterate(functor); | |
853 | return functor.allowsAccess(); | |
854 | } | |
855 | ||
856 | EncodedJSValue JSC_HOST_CALL globalFuncProtoSetter(ExecState* exec) | |
857 | { | |
858 | if (exec->thisValue().isUndefinedOrNull()) | |
859 | return throwVMError(exec, createTypeError(exec, "Can't convert undefined or null to object")); | |
860 | ||
861 | JSValue value = exec->argument(0); | |
862 | ||
863 | JSObject* thisObject = jsDynamicCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)); | |
864 | ||
865 | // Setting __proto__ of a primitive should have no effect. | |
866 | if (!thisObject) | |
867 | return JSValue::encode(jsUndefined()); | |
868 | ||
869 | if (!checkProtoSetterAccessAllowed(exec, thisObject)) | |
870 | return JSValue::encode(jsUndefined()); | |
871 | ||
872 | // Setting __proto__ to a non-object, non-null value is silently ignored to match Mozilla. | |
873 | if (!value.isObject() && !value.isNull()) | |
874 | return JSValue::encode(jsUndefined()); | |
875 | ||
876 | if (!thisObject->isExtensible()) | |
877 | return throwVMError(exec, createTypeError(exec, StrictModeReadonlyPropertyWriteError)); | |
878 | ||
879 | if (!thisObject->setPrototypeWithCycleCheck(exec, value)) | |
880 | exec->vm().throwException(exec, createError(exec, ASCIILiteral("cyclic __proto__ value"))); | |
881 | return JSValue::encode(jsUndefined()); | |
882 | } | |
883 | ||
884 | EncodedJSValue JSC_HOST_CALL globalFuncBuiltinLog(ExecState* exec) | |
885 | { | |
886 | dataLog(exec->argument(0).toWTFString(exec), "\n"); | |
887 | return JSValue::encode(jsUndefined()); | |
888 | } | |
889 | ||
890 | } // namespace JSC |