]>
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" |
81345200 | 37 | #include "JSCInlines.h" |
f9bf01c6 | 38 | #include "Parser.h" |
81345200 | 39 | #include "StackVisitor.h" |
6fe7ccc8 | 40 | #include <wtf/dtoa.h> |
9dae56ea A |
41 | #include <stdio.h> |
42 | #include <stdlib.h> | |
9dae56ea A |
43 | #include <wtf/ASCIICType.h> |
44 | #include <wtf/Assertions.h> | |
ed1e77d3 | 45 | #include <wtf/HexNumber.h> |
9dae56ea | 46 | #include <wtf/MathExtras.h> |
14957cd0 | 47 | #include <wtf/StringExtras.h> |
93a37866 | 48 | #include <wtf/text/StringBuilder.h> |
9dae56ea A |
49 | #include <wtf/unicode/UTF8.h> |
50 | ||
51 | using namespace WTF; | |
52 | using namespace Unicode; | |
53 | ||
54 | namespace JSC { | |
55 | ||
ed1e77d3 A |
56 | template<unsigned charactersCount> |
57 | static Bitmap<256> makeCharacterBitmap(const char (&characters)[charactersCount]) | |
9dae56ea | 58 | { |
ed1e77d3 A |
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); | |
4e4e5a6f | 68 | if (!cstr.data()) |
81345200 | 69 | return exec->vm().throwException(exec, createURIError(exec, ASCIILiteral("String contained an illegal UTF-16 sequence."))); |
9dae56ea | 70 | |
4e4e5a6f A |
71 | JSStringBuilder builder; |
72 | const char* p = cstr.data(); | |
73 | for (size_t k = 0; k < cstr.length(); k++, p++) { | |
9dae56ea | 74 | char c = *p; |
ed1e77d3 | 75 | if (c && doNotEscape.get(static_cast<LChar>(c))) |
81345200 | 76 | builder.append(static_cast<LChar>(c)); |
9dae56ea | 77 | else { |
ed1e77d3 A |
78 | builder.append(static_cast<LChar>('%')); |
79 | appendByteAsHex(c, builder); | |
9dae56ea A |
80 | } |
81 | } | |
4e4e5a6f | 82 | return builder.build(exec); |
9dae56ea A |
83 | } |
84 | ||
6fe7ccc8 A |
85 | template <typename CharType> |
86 | ALWAYS_INLINE | |
ed1e77d3 | 87 | static JSValue decode(ExecState* exec, const CharType* characters, int length, const Bitmap<256>& doNotUnescape, bool strict) |
9dae56ea | 88 | { |
4e4e5a6f | 89 | JSStringBuilder builder; |
9dae56ea | 90 | int k = 0; |
9dae56ea | 91 | UChar u = 0; |
6fe7ccc8 A |
92 | while (k < length) { |
93 | const CharType* p = characters + k; | |
94 | CharType c = *p; | |
9dae56ea A |
95 | if (c == '%') { |
96 | int charLen = 0; | |
6fe7ccc8 A |
97 | if (k <= length - 3 && isASCIIHexDigit(p[1]) && isASCIIHexDigit(p[2])) { |
98 | const char b0 = Lexer<CharType>::convertHex(p[1], p[2]); | |
9dae56ea | 99 | const int sequenceLen = UTF8SequenceLength(b0); |
6fe7ccc8 | 100 | if (sequenceLen && k <= length - sequenceLen * 3) { |
9dae56ea A |
101 | charLen = sequenceLen * 3; |
102 | char sequence[5]; | |
103 | sequence[0] = b0; | |
104 | for (int i = 1; i < sequenceLen; ++i) { | |
6fe7ccc8 | 105 | const CharType* q = p + i * 3; |
9dae56ea | 106 | if (q[0] == '%' && isASCIIHexDigit(q[1]) && isASCIIHexDigit(q[2])) |
6fe7ccc8 | 107 | sequence[i] = Lexer<CharType>::convertHex(q[1], q[2]); |
9dae56ea A |
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. | |
f9bf01c6 | 120 | builder.append(static_cast<UChar>(0xD800 | ((character - 0x10000) >> 10))); |
9dae56ea A |
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) | |
81345200 | 129 | return exec->vm().throwException(exec, createURIError(exec, ASCIILiteral("URI error"))); |
9dae56ea A |
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. | |
6fe7ccc8 | 132 | if (k <= length - 6 && p[1] == 'u' |
9dae56ea A |
133 | && isASCIIHexDigit(p[2]) && isASCIIHexDigit(p[3]) |
134 | && isASCIIHexDigit(p[4]) && isASCIIHexDigit(p[5])) { | |
135 | charLen = 6; | |
6fe7ccc8 | 136 | u = Lexer<UChar>::convertUnicode(p[2], p[3], p[4], p[5]); |
9dae56ea A |
137 | } |
138 | } | |
ed1e77d3 | 139 | if (charLen && (u == 0 || u >= 128 || !doNotUnescape.get(static_cast<LChar>(u)))) { |
81345200 | 140 | builder.append(u); |
6fe7ccc8 A |
141 | k += charLen; |
142 | continue; | |
9dae56ea A |
143 | } |
144 | } | |
145 | k++; | |
f9bf01c6 | 146 | builder.append(c); |
9dae56ea | 147 | } |
4e4e5a6f | 148 | return builder.build(exec); |
9dae56ea A |
149 | } |
150 | ||
ed1e77d3 | 151 | static JSValue decode(ExecState* exec, const Bitmap<256>& doNotUnescape, bool strict) |
6fe7ccc8 | 152 | { |
ed1e77d3 | 153 | StringView str = exec->argument(0).toString(exec)->view(exec); |
6fe7ccc8 A |
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 | ||
9dae56ea A |
160 | bool isStrWhiteSpace(UChar c) |
161 | { | |
162 | switch (c) { | |
14957cd0 | 163 | // ECMA-262-5th 7.2 & 7.3 |
9dae56ea A |
164 | case 0x0009: |
165 | case 0x000A: | |
166 | case 0x000B: | |
167 | case 0x000C: | |
168 | case 0x000D: | |
169 | case 0x0020: | |
170 | case 0x00A0: | |
81345200 | 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. |
9dae56ea A |
172 | case 0x2028: |
173 | case 0x2029: | |
14957cd0 | 174 | case 0xFEFF: |
9dae56ea A |
175 | return true; |
176 | default: | |
81345200 | 177 | return c > 0xFF && u_charType(c) == U_SPACE_SEPARATOR; |
9dae56ea A |
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 | ||
81345200 | 197 | double parseIntOverflow(const LChar* s, unsigned length, int radix) |
9dae56ea A |
198 | { |
199 | double number = 0.0; | |
200 | double radixMultiplier = 1.0; | |
201 | ||
6fe7ccc8 A |
202 | for (const LChar* p = s + length - 1; p >= s; p--) { |
203 | if (radixMultiplier == std::numeric_limits<double>::infinity()) { | |
9dae56ea | 204 | if (*p != '0') { |
6fe7ccc8 | 205 | number = std::numeric_limits<double>::infinity(); |
9dae56ea A |
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 | ||
81345200 | 219 | static double parseIntOverflow(const UChar* s, unsigned length, int radix) |
14957cd0 A |
220 | { |
221 | double number = 0.0; | |
222 | double radixMultiplier = 1.0; | |
223 | ||
224 | for (const UChar* p = s + length - 1; p >= s; p--) { | |
6fe7ccc8 | 225 | if (radixMultiplier == std::numeric_limits<double>::infinity()) { |
14957cd0 | 226 | if (*p != '0') { |
6fe7ccc8 | 227 | number = std::numeric_limits<double>::infinity(); |
14957cd0 A |
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 | ||
81345200 A |
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 | ||
6fe7ccc8 A |
248 | // ES5.1 15.1.2.2 |
249 | template <typename CharType> | |
250 | ALWAYS_INLINE | |
ed1e77d3 | 251 | static double parseInt(StringView s, const CharType* data, int radix) |
9dae56ea | 252 | { |
6fe7ccc8 A |
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. | |
14957cd0 | 257 | int length = s.length(); |
9dae56ea | 258 | int p = 0; |
9dae56ea A |
259 | while (p < length && isStrWhiteSpace(data[p])) |
260 | ++p; | |
261 | ||
6fe7ccc8 A |
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. | |
9dae56ea A |
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 | ||
6fe7ccc8 A |
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. | |
9dae56ea A |
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; | |
6fe7ccc8 A |
289 | } else if (radix == 0) |
290 | radix = 10; | |
9dae56ea | 291 | |
6fe7ccc8 | 292 | // 8.a If R < 2 or R > 36, then return NaN. |
9dae56ea | 293 | if (radix < 2 || radix > 36) |
81345200 | 294 | return PNaN; |
6fe7ccc8 A |
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. | |
9dae56ea A |
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 | ||
6fe7ccc8 A |
315 | // 12. If Z is empty, return NaN. |
316 | if (!sawDigit) | |
81345200 | 317 | return PNaN; |
6fe7ccc8 A |
318 | |
319 | // Alternate code path for certain large numbers. | |
9dae56ea | 320 | if (number >= mantissaOverflowLowerBound) { |
6fe7ccc8 A |
321 | if (radix == 10) { |
322 | size_t parsedLength; | |
ed1e77d3 | 323 | number = parseDouble(s.substring(firstDigitPosition, p - firstDigitPosition), parsedLength); |
6fe7ccc8 | 324 | } else if (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) |
ed1e77d3 | 325 | number = parseIntOverflow(s.substring(firstDigitPosition, p - firstDigitPosition), radix); |
9dae56ea A |
326 | } |
327 | ||
6fe7ccc8 | 328 | // 15. Return sign x number. |
9dae56ea A |
329 | return sign * number; |
330 | } | |
331 | ||
ed1e77d3 | 332 | static double parseInt(StringView s, int radix) |
6fe7ccc8 A |
333 | { |
334 | if (s.is8Bit()) | |
335 | return parseInt(s, s.characters8(), radix); | |
336 | return parseInt(s, s.characters16(), radix); | |
337 | } | |
338 | ||
14957cd0 A |
339 | static const int SizeOfInfinity = 8; |
340 | ||
6fe7ccc8 A |
341 | template <typename CharType> |
342 | static bool isInfinity(const CharType* data, const CharType* end) | |
14957cd0 A |
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 | ||
ed1e77d3 A |
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 | |
6fe7ccc8 A |
400 | template <typename CharType> |
401 | static double jsHexIntegerLiteral(const CharType*& data, const CharType* end) | |
14957cd0 A |
402 | { |
403 | // Hex number. | |
404 | data += 2; | |
6fe7ccc8 | 405 | const CharType* firstDigitPosition = data; |
14957cd0 A |
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 | ||
ed1e77d3 | 421 | // See ecma-262 6th 11.8.3 |
6fe7ccc8 A |
422 | template <typename CharType> |
423 | static double jsStrDecimalLiteral(const CharType*& data, const CharType* end) | |
14957cd0 | 424 | { |
93a37866 | 425 | RELEASE_ASSERT(data < end); |
14957cd0 | 426 | |
6fe7ccc8 A |
427 | size_t parsedLength; |
428 | double number = parseDouble(data, end - data, parsedLength); | |
429 | if (parsedLength) { | |
430 | data += parsedLength; | |
14957cd0 A |
431 | return number; |
432 | } | |
433 | ||
434 | // Check for [+-]?Infinity | |
435 | switch (*data) { | |
436 | case 'I': | |
437 | if (isInfinity(data, end)) { | |
438 | data += SizeOfInfinity; | |
6fe7ccc8 | 439 | return std::numeric_limits<double>::infinity(); |
14957cd0 A |
440 | } |
441 | break; | |
442 | ||
443 | case '+': | |
444 | if (isInfinity(data + 1, end)) { | |
445 | data += SizeOfInfinity + 1; | |
6fe7ccc8 | 446 | return std::numeric_limits<double>::infinity(); |
14957cd0 A |
447 | } |
448 | break; | |
449 | ||
450 | case '-': | |
451 | if (isInfinity(data + 1, end)) { | |
452 | data += SizeOfInfinity + 1; | |
6fe7ccc8 | 453 | return -std::numeric_limits<double>::infinity(); |
14957cd0 A |
454 | } |
455 | break; | |
456 | } | |
457 | ||
458 | // Not a number. | |
81345200 | 459 | return PNaN; |
14957cd0 A |
460 | } |
461 | ||
6fe7ccc8 A |
462 | template <typename CharType> |
463 | static double toDouble(const CharType* characters, unsigned size) | |
14957cd0 | 464 | { |
6fe7ccc8 | 465 | const CharType* endCharacters = characters + size; |
14957cd0 A |
466 | |
467 | // Skip leading white space. | |
6fe7ccc8 A |
468 | for (; characters < endCharacters; ++characters) { |
469 | if (!isStrWhiteSpace(*characters)) | |
14957cd0 A |
470 | break; |
471 | } | |
6fe7ccc8 | 472 | |
14957cd0 | 473 | // Empty string. |
6fe7ccc8 | 474 | if (characters == endCharacters) |
14957cd0 | 475 | return 0.0; |
6fe7ccc8 | 476 | |
14957cd0 | 477 | double number; |
ed1e77d3 A |
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 | |
6fe7ccc8 A |
488 | number = jsStrDecimalLiteral(characters, endCharacters); |
489 | ||
14957cd0 | 490 | // Allow trailing white space. |
6fe7ccc8 A |
491 | for (; characters < endCharacters; ++characters) { |
492 | if (!isStrWhiteSpace(*characters)) | |
14957cd0 A |
493 | break; |
494 | } | |
6fe7ccc8 | 495 | if (characters != endCharacters) |
81345200 | 496 | return PNaN; |
6fe7ccc8 | 497 | |
14957cd0 A |
498 | return number; |
499 | } | |
500 | ||
ed1e77d3 A |
501 | // See ecma-262 6th 11.8.3 |
502 | double jsToNumber(StringView s) | |
6fe7ccc8 A |
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; | |
81345200 | 512 | return PNaN; |
6fe7ccc8 A |
513 | } |
514 | ||
515 | if (s.is8Bit()) | |
516 | return toDouble(s.characters8(), size); | |
517 | return toDouble(s.characters16(), size); | |
518 | } | |
519 | ||
ed1e77d3 | 520 | static double parseFloat(StringView s) |
9dae56ea | 521 | { |
14957cd0 | 522 | unsigned size = s.length(); |
9dae56ea | 523 | |
14957cd0 | 524 | if (size == 1) { |
6fe7ccc8 | 525 | UChar c = s[0]; |
14957cd0 A |
526 | if (isASCIIDigit(c)) |
527 | return c - '0'; | |
81345200 | 528 | return PNaN; |
6fe7ccc8 A |
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) | |
81345200 | 543 | return PNaN; |
6fe7ccc8 A |
544 | |
545 | return jsStrDecimalLiteral(data, end); | |
14957cd0 | 546 | } |
9dae56ea | 547 | |
6fe7ccc8 | 548 | const UChar* data = s.characters16(); |
14957cd0 | 549 | const UChar* end = data + size; |
9dae56ea | 550 | |
14957cd0 A |
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) | |
81345200 | 559 | return PNaN; |
14957cd0 A |
560 | |
561 | return jsStrDecimalLiteral(data, end); | |
9dae56ea A |
562 | } |
563 | ||
14957cd0 | 564 | EncodedJSValue JSC_HOST_CALL globalFuncEval(ExecState* exec) |
9dae56ea | 565 | { |
14957cd0 | 566 | JSValue x = exec->argument(0); |
9dae56ea | 567 | if (!x.isString()) |
14957cd0 | 568 | return JSValue::encode(x); |
9dae56ea | 569 | |
93a37866 | 570 | String s = x.toString(exec)->value(exec); |
9dae56ea | 571 | |
6fe7ccc8 A |
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 | } | |
ba379fdc | 581 | |
93a37866 | 582 | JSGlobalObject* calleeGlobalObject = exec->callee()->globalObject(); |
ed1e77d3 | 583 | EvalExecutable* eval = EvalExecutable::create(exec, makeSource(s), false, ThisTDZMode::CheckIfNeeded); |
81345200 A |
584 | if (!eval) |
585 | return JSValue::encode(jsUndefined()); | |
9dae56ea | 586 | |
93a37866 | 587 | return JSValue::encode(exec->interpreter()->execute(eval, exec, calleeGlobalObject->globalThis(), calleeGlobalObject)); |
9dae56ea A |
588 | } |
589 | ||
14957cd0 | 590 | EncodedJSValue JSC_HOST_CALL globalFuncParseInt(ExecState* exec) |
9dae56ea | 591 | { |
14957cd0 | 592 | JSValue value = exec->argument(0); |
6fe7ccc8 A |
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))); | |
9dae56ea A |
610 | } |
611 | ||
6fe7ccc8 | 612 | // If ToString throws, we shouldn't call ToInt32. |
ed1e77d3 | 613 | StringView s = value.toString(exec)->view(exec); |
6fe7ccc8 A |
614 | if (exec->hadException()) |
615 | return JSValue::encode(jsUndefined()); | |
616 | ||
617 | return JSValue::encode(jsNumber(parseInt(s, radixValue.toInt32(exec)))); | |
9dae56ea A |
618 | } |
619 | ||
14957cd0 | 620 | EncodedJSValue JSC_HOST_CALL globalFuncParseFloat(ExecState* exec) |
9dae56ea | 621 | { |
ed1e77d3 | 622 | return JSValue::encode(jsNumber(parseFloat(exec->argument(0).toString(exec)->view(exec)))); |
9dae56ea A |
623 | } |
624 | ||
14957cd0 | 625 | EncodedJSValue JSC_HOST_CALL globalFuncIsNaN(ExecState* exec) |
9dae56ea | 626 | { |
93a37866 | 627 | return JSValue::encode(jsBoolean(std::isnan(exec->argument(0).toNumber(exec)))); |
9dae56ea A |
628 | } |
629 | ||
14957cd0 | 630 | EncodedJSValue JSC_HOST_CALL globalFuncIsFinite(ExecState* exec) |
9dae56ea | 631 | { |
14957cd0 | 632 | double n = exec->argument(0).toNumber(exec); |
93a37866 | 633 | return JSValue::encode(jsBoolean(std::isfinite(n))); |
9dae56ea A |
634 | } |
635 | ||
14957cd0 | 636 | EncodedJSValue JSC_HOST_CALL globalFuncDecodeURI(ExecState* exec) |
9dae56ea | 637 | { |
ed1e77d3 A |
638 | static Bitmap<256> doNotUnescapeWhenDecodingURI = makeCharacterBitmap( |
639 | "#$&+,/:;=?@" | |
640 | ); | |
9dae56ea | 641 | |
ed1e77d3 | 642 | return JSValue::encode(decode(exec, doNotUnescapeWhenDecodingURI, true)); |
9dae56ea A |
643 | } |
644 | ||
14957cd0 | 645 | EncodedJSValue JSC_HOST_CALL globalFuncDecodeURIComponent(ExecState* exec) |
9dae56ea | 646 | { |
ed1e77d3 A |
647 | static Bitmap<256> emptyBitmap; |
648 | return JSValue::encode(decode(exec, emptyBitmap, true)); | |
9dae56ea A |
649 | } |
650 | ||
14957cd0 | 651 | EncodedJSValue JSC_HOST_CALL globalFuncEncodeURI(ExecState* exec) |
9dae56ea | 652 | { |
ed1e77d3 | 653 | static Bitmap<256> doNotEscapeWhenEncodingURI = makeCharacterBitmap( |
9dae56ea A |
654 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
655 | "abcdefghijklmnopqrstuvwxyz" | |
656 | "0123456789" | |
ed1e77d3 A |
657 | "!#$&'()*+,-./:;=?@_~" |
658 | ); | |
9dae56ea | 659 | |
ed1e77d3 | 660 | return JSValue::encode(encode(exec, doNotEscapeWhenEncodingURI)); |
9dae56ea A |
661 | } |
662 | ||
14957cd0 | 663 | EncodedJSValue JSC_HOST_CALL globalFuncEncodeURIComponent(ExecState* exec) |
9dae56ea | 664 | { |
ed1e77d3 | 665 | static Bitmap<256> doNotEscapeWhenEncodingURIComponent = makeCharacterBitmap( |
9dae56ea A |
666 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
667 | "abcdefghijklmnopqrstuvwxyz" | |
668 | "0123456789" | |
ed1e77d3 A |
669 | "!'()*-._~" |
670 | ); | |
9dae56ea | 671 | |
ed1e77d3 | 672 | return JSValue::encode(encode(exec, doNotEscapeWhenEncodingURIComponent)); |
9dae56ea A |
673 | } |
674 | ||
14957cd0 | 675 | EncodedJSValue JSC_HOST_CALL globalFuncEscape(ExecState* exec) |
9dae56ea | 676 | { |
ed1e77d3 | 677 | static Bitmap<256> doNotEscape = makeCharacterBitmap( |
9dae56ea A |
678 | "ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
679 | "abcdefghijklmnopqrstuvwxyz" | |
680 | "0123456789" | |
ed1e77d3 A |
681 | "*+-./@_" |
682 | ); | |
9dae56ea | 683 | |
4e4e5a6f | 684 | JSStringBuilder builder; |
ed1e77d3 | 685 | StringView str = exec->argument(0).toString(exec)->view(exec); |
6fe7ccc8 A |
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]; | |
ed1e77d3 | 690 | if (u && doNotEscape.get(static_cast<LChar>(u))) |
81345200 | 691 | builder.append(*c); |
6fe7ccc8 | 692 | else { |
ed1e77d3 A |
693 | builder.append(static_cast<LChar>('%')); |
694 | appendByteAsHex(static_cast<LChar>(u), builder); | |
6fe7ccc8 A |
695 | } |
696 | } | |
697 | ||
698 | return JSValue::encode(builder.build(exec)); | |
699 | } | |
700 | ||
701 | const UChar* c = str.characters16(); | |
14957cd0 | 702 | for (unsigned k = 0; k < str.length(); k++, c++) { |
9dae56ea A |
703 | int u = c[0]; |
704 | if (u > 255) { | |
ed1e77d3 A |
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))) | |
81345200 | 710 | builder.append(*c); |
9dae56ea | 711 | else { |
ed1e77d3 A |
712 | builder.append(static_cast<LChar>('%')); |
713 | appendByteAsHex(u, builder); | |
9dae56ea | 714 | } |
9dae56ea A |
715 | } |
716 | ||
14957cd0 | 717 | return JSValue::encode(builder.build(exec)); |
9dae56ea A |
718 | } |
719 | ||
14957cd0 | 720 | EncodedJSValue JSC_HOST_CALL globalFuncUnescape(ExecState* exec) |
9dae56ea | 721 | { |
93a37866 | 722 | StringBuilder builder; |
ed1e77d3 | 723 | StringView str = exec->argument(0).toString(exec)->view(exec); |
9dae56ea | 724 | int k = 0; |
14957cd0 | 725 | int len = str.length(); |
6fe7ccc8 A |
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; | |
9dae56ea | 742 | } |
6fe7ccc8 A |
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); | |
9dae56ea | 765 | } |
9dae56ea A |
766 | } |
767 | ||
93a37866 | 768 | return JSValue::encode(jsString(exec, builder.toString())); |
9dae56ea | 769 | } |
9dae56ea | 770 | |
6fe7ccc8 A |
771 | EncodedJSValue JSC_HOST_CALL globalFuncThrowTypeError(ExecState* exec) |
772 | { | |
773 | return throwVMTypeError(exec); | |
774 | } | |
775 | ||
81345200 A |
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 | ||
6fe7ccc8 A |
806 | EncodedJSValue JSC_HOST_CALL globalFuncProtoGetter(ExecState* exec) |
807 | { | |
ed1e77d3 A |
808 | if (exec->thisValue().isUndefinedOrNull()) |
809 | return throwVMError(exec, createTypeError(exec, "Can't convert undefined or null to object")); | |
810 | ||
81345200 | 811 | JSObject* thisObject = jsDynamicCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)); |
6fe7ccc8 | 812 | |
81345200 A |
813 | if (!thisObject) |
814 | return JSValue::encode(exec->thisValue().synthesizePrototype(exec)); | |
6fe7ccc8 | 815 | |
81345200 A |
816 | GlobalFuncProtoGetterFunctor functor(thisObject); |
817 | exec->iterate(functor); | |
818 | return functor.result(); | |
6fe7ccc8 A |
819 | } |
820 | ||
81345200 A |
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 | ||
ed1e77d3 A |
849 | bool checkProtoSetterAccessAllowed(ExecState* exec, JSObject* object) |
850 | { | |
851 | GlobalFuncProtoSetterFunctor functor(object); | |
852 | exec->iterate(functor); | |
853 | return functor.allowsAccess(); | |
854 | } | |
855 | ||
6fe7ccc8 A |
856 | EncodedJSValue JSC_HOST_CALL globalFuncProtoSetter(ExecState* exec) |
857 | { | |
ed1e77d3 A |
858 | if (exec->thisValue().isUndefinedOrNull()) |
859 | return throwVMError(exec, createTypeError(exec, "Can't convert undefined or null to object")); | |
860 | ||
6fe7ccc8 A |
861 | JSValue value = exec->argument(0); |
862 | ||
81345200 A |
863 | JSObject* thisObject = jsDynamicCast<JSObject*>(exec->thisValue().toThis(exec, NotStrictMode)); |
864 | ||
6fe7ccc8 | 865 | // Setting __proto__ of a primitive should have no effect. |
81345200 | 866 | if (!thisObject) |
6fe7ccc8 A |
867 | return JSValue::encode(jsUndefined()); |
868 | ||
ed1e77d3 | 869 | if (!checkProtoSetterAccessAllowed(exec, thisObject)) |
6fe7ccc8 A |
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 | ||
81345200 | 879 | if (!thisObject->setPrototypeWithCycleCheck(exec, value)) |
ed1e77d3 | 880 | exec->vm().throwException(exec, createError(exec, ASCIILiteral("cyclic __proto__ value"))); |
81345200 A |
881 | return JSValue::encode(jsUndefined()); |
882 | } | |
883 | ||
884 | EncodedJSValue JSC_HOST_CALL globalFuncBuiltinLog(ExecState* exec) | |
885 | { | |
886 | dataLog(exec->argument(0).toWTFString(exec), "\n"); | |
6fe7ccc8 A |
887 | return JSValue::encode(jsUndefined()); |
888 | } | |
889 | ||
9dae56ea | 890 | } // namespace JSC |