| OLD | NEW |
| 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
| 3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
| 4 // met: | 4 // met: |
| 5 // | 5 // |
| 6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
| 7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
| 8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
| 9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
| 10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 | 93 |
| 94 static inline void ReleaseCString(const char* original, const char* str) { | 94 static inline void ReleaseCString(const char* original, const char* str) { |
| 95 } | 95 } |
| 96 | 96 |
| 97 | 97 |
| 98 static inline void ReleaseCString(String* original, const char* str) { | 98 static inline void ReleaseCString(String* original, const char* str) { |
| 99 DeleteArray(const_cast<char *>(str)); | 99 DeleteArray(const_cast<char *>(str)); |
| 100 } | 100 } |
| 101 | 101 |
| 102 | 102 |
| 103 static inline bool IsSpace(const char* str, int index) { | 103 static inline bool IsSpace(const char* str, int index, V8Context* v8context) { |
| 104 ASSERT(index >= 0 && index < StrLength(str)); | 104 ASSERT(index >= 0 && index < StrLength(str)); |
| 105 return Scanner::kIsWhiteSpace.get(str[index]); | 105 return v8context->scanner_data_.kIsWhiteSpace_.get(str[index]); |
| 106 } | 106 } |
| 107 | 107 |
| 108 | 108 |
| 109 static inline bool IsSpace(String* str, int index) { | 109 static inline bool IsSpace(String* str, int index, V8Context* v8context) { |
| 110 return Scanner::kIsWhiteSpace.get(str->Get(index)); | 110 return v8context->scanner_data_.kIsWhiteSpace_.get(str->Get(index)); |
| 111 } | 111 } |
| 112 | 112 |
| 113 | 113 |
| 114 static inline bool SubStringEquals(const char* str, | 114 static inline bool SubStringEquals(const char* str, |
| 115 int index, | 115 int index, |
| 116 const char* other) { | 116 const char* other) { |
| 117 return strncmp(str + index, other, strlen(other)) != 0; | 117 return strncmp(str + index, other, strlen(other)) != 0; |
| 118 } | 118 } |
| 119 | 119 |
| 120 | 120 |
| (...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 265 // char* or a String*. | 265 // char* or a String*. |
| 266 template<class S> | 266 template<class S> |
| 267 static double InternalStringToDouble(S* str, | 267 static double InternalStringToDouble(S* str, |
| 268 int flags, | 268 int flags, |
| 269 double empty_string_val) { | 269 double empty_string_val) { |
| 270 double result = 0.0; | 270 double result = 0.0; |
| 271 int index = 0; | 271 int index = 0; |
| 272 | 272 |
| 273 int len = GetLength(str); | 273 int len = GetLength(str); |
| 274 | 274 |
| 275 V8Context* const v8context = v8_context(); |
| 275 // Skip leading spaces. | 276 // Skip leading spaces. |
| 276 while ((index < len) && IsSpace(str, index)) index++; | 277 while ((index < len) && IsSpace(str, index, v8context)) index++; |
| 277 | 278 |
| 278 // Is the string empty? | 279 // Is the string empty? |
| 279 if (index >= len) return empty_string_val; | 280 if (index >= len) return empty_string_val; |
| 280 | 281 |
| 281 // Get the first character. | 282 // Get the first character. |
| 282 uint16_t first = GetChar(str, index); | 283 uint16_t first = GetChar(str, index); |
| 283 | 284 |
| 284 // Numbers can only start with '-', '+', '.', 'I' (Infinity), or a digit. | 285 // Numbers can only start with '-', '+', '.', 'I' (Infinity), or a digit. |
| 285 if (first != '-' && first != '+' && first != '.' && first != 'I' && | 286 if (first != '-' && first != '+' && first != '.' && first != 'I' && |
| 286 (first > '9' || first < '0')) { | 287 (first > '9' || first < '0')) { |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 327 index++; | 328 index++; |
| 328 if (!SubStringEquals(str, index, "Infinity")) | 329 if (!SubStringEquals(str, index, "Infinity")) |
| 329 return JUNK_STRING_VALUE; | 330 return JUNK_STRING_VALUE; |
| 330 result = is_negative ? -V8_INFINITY : V8_INFINITY; | 331 result = is_negative ? -V8_INFINITY : V8_INFINITY; |
| 331 index += 8; | 332 index += 8; |
| 332 } | 333 } |
| 333 } | 334 } |
| 334 | 335 |
| 335 if ((flags & ALLOW_TRAILING_JUNK) == 0) { | 336 if ((flags & ALLOW_TRAILING_JUNK) == 0) { |
| 336 // skip trailing spaces | 337 // skip trailing spaces |
| 337 while ((index < len) && IsSpace(str, index)) index++; | 338 while ((index < len) && IsSpace(str, index, v8context)) index++; |
| 338 // string ending with junk? | 339 // string ending with junk? |
| 339 if (index < len) return JUNK_STRING_VALUE; | 340 if (index < len) return JUNK_STRING_VALUE; |
| 340 } | 341 } |
| 341 | 342 |
| 342 return sign * result; | 343 return sign * result; |
| 343 } | 344 } |
| 344 | 345 |
| 345 | 346 |
| 346 double StringToDouble(String* str, int flags, double empty_string_val) { | 347 double StringToDouble(String* str, int flags, double empty_string_val) { |
| 347 return InternalStringToDouble(str, flags, empty_string_val); | 348 return InternalStringToDouble(str, flags, empty_string_val); |
| 348 } | 349 } |
| 349 | 350 |
| 350 | 351 |
| 351 double StringToDouble(const char* str, int flags, double empty_string_val) { | 352 double StringToDouble(const char* str, int flags, double empty_string_val) { |
| 352 return InternalStringToDouble(str, flags, empty_string_val); | 353 return InternalStringToDouble(str, flags, empty_string_val); |
| 353 } | 354 } |
| 354 | 355 |
| 356 static MutexLockAdapter mutex_lock(OS::CreateMutex()); |
| 355 | 357 |
| 356 extern "C" char* dtoa(double d, int mode, int ndigits, | 358 extern "C" char* dtoa(double d, int mode, int ndigits, |
| 357 int* decpt, int* sign, char** rve); | 359 int* decpt, int* sign, char** rve); |
| 358 | 360 |
| 359 extern "C" void freedtoa(char* s); | 361 extern "C" void freedtoa(char* s); |
| 360 | 362 |
| 361 const char* DoubleToCString(double v, Vector<char> buffer) { | 363 const char* DoubleToCString(double v, Vector<char> buffer) { |
| 362 StringBuilder builder(buffer.start(), buffer.length()); | 364 StringBuilder builder(buffer.start(), buffer.length()); |
| 363 | 365 |
| 364 switch (fpclassify(v)) { | 366 switch (fpclassify(v)) { |
| 365 case FP_NAN: | 367 case FP_NAN: |
| 366 builder.AddString("NaN"); | 368 builder.AddString("NaN"); |
| 367 break; | 369 break; |
| 368 | 370 |
| 369 case FP_INFINITE: | 371 case FP_INFINITE: |
| 370 if (v < 0.0) { | 372 if (v < 0.0) { |
| 371 builder.AddString("-Infinity"); | 373 builder.AddString("-Infinity"); |
| 372 } else { | 374 } else { |
| 373 builder.AddString("Infinity"); | 375 builder.AddString("Infinity"); |
| 374 } | 376 } |
| 375 break; | 377 break; |
| 376 | 378 |
| 377 case FP_ZERO: | 379 case FP_ZERO: |
| 378 builder.AddCharacter('0'); | 380 builder.AddCharacter('0'); |
| 379 break; | 381 break; |
| 380 | 382 |
| 381 default: { | 383 default: { |
| 382 int decimal_point; | 384 int decimal_point; |
| 383 int sign; | 385 int sign; |
| 384 | 386 V8SharedStateLocker dtoa_locker(&mutex_lock); |
| 385 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); | 387 char* decimal_rep = dtoa(v, 0, 0, &decimal_point, &sign, NULL); |
| 386 int length = StrLength(decimal_rep); | 388 int length = StrLength(decimal_rep); |
| 387 | 389 |
| 388 if (sign) builder.AddCharacter('-'); | 390 if (sign) builder.AddCharacter('-'); |
| 389 | 391 |
| 390 if (length <= decimal_point && decimal_point <= 21) { | 392 if (length <= decimal_point && decimal_point <= 21) { |
| 391 // ECMA-262 section 9.8.1 step 6. | 393 // ECMA-262 section 9.8.1 step 6. |
| 392 builder.AddString(decimal_rep); | 394 builder.AddString(decimal_rep); |
| 393 builder.AddPadding('0', decimal_point - length); | 395 builder.AddPadding('0', decimal_point - length); |
| 394 | 396 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 457 | 459 |
| 458 if (abs_value >= 1e21) { | 460 if (abs_value >= 1e21) { |
| 459 char arr[100]; | 461 char arr[100]; |
| 460 Vector<char> buffer(arr, ARRAY_SIZE(arr)); | 462 Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 461 return StrDup(DoubleToCString(value, buffer)); | 463 return StrDup(DoubleToCString(value, buffer)); |
| 462 } | 464 } |
| 463 | 465 |
| 464 // Find a sufficiently precise decimal representation of n. | 466 // Find a sufficiently precise decimal representation of n. |
| 465 int decimal_point; | 467 int decimal_point; |
| 466 int sign; | 468 int sign; |
| 469 V8SharedStateLocker dtoa_locker(&mutex_lock); |
| 467 char* decimal_rep = dtoa(abs_value, 3, f, &decimal_point, &sign, NULL); | 470 char* decimal_rep = dtoa(abs_value, 3, f, &decimal_point, &sign, NULL); |
| 468 int decimal_rep_length = StrLength(decimal_rep); | 471 int decimal_rep_length = StrLength(decimal_rep); |
| 469 | 472 |
| 470 // Create a representation that is padded with zeros if needed. | 473 // Create a representation that is padded with zeros if needed. |
| 471 int zero_prefix_length = 0; | 474 int zero_prefix_length = 0; |
| 472 int zero_postfix_length = 0; | 475 int zero_postfix_length = 0; |
| 473 | 476 |
| 474 if (decimal_point <= 0) { | 477 if (decimal_point <= 0) { |
| 475 zero_prefix_length = -decimal_point + 1; | 478 zero_prefix_length = -decimal_point + 1; |
| 476 decimal_point = 1; | 479 decimal_point = 1; |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 545 bool negative = false; | 548 bool negative = false; |
| 546 if (value < 0) { | 549 if (value < 0) { |
| 547 value = -value; | 550 value = -value; |
| 548 negative = true; | 551 negative = true; |
| 549 } | 552 } |
| 550 | 553 |
| 551 // Find a sufficiently precise decimal representation of n. | 554 // Find a sufficiently precise decimal representation of n. |
| 552 int decimal_point; | 555 int decimal_point; |
| 553 int sign; | 556 int sign; |
| 554 char* decimal_rep = NULL; | 557 char* decimal_rep = NULL; |
| 558 V8SharedStateLocker dtoa_locker(&mutex_lock); |
| 555 if (f == -1) { | 559 if (f == -1) { |
| 556 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); | 560 decimal_rep = dtoa(value, 0, 0, &decimal_point, &sign, NULL); |
| 557 f = StrLength(decimal_rep) - 1; | 561 f = StrLength(decimal_rep) - 1; |
| 558 } else { | 562 } else { |
| 559 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); | 563 decimal_rep = dtoa(value, 2, f + 1, &decimal_point, &sign, NULL); |
| 560 } | 564 } |
| 561 int decimal_rep_length = StrLength(decimal_rep); | 565 int decimal_rep_length = StrLength(decimal_rep); |
| 562 ASSERT(decimal_rep_length > 0); | 566 ASSERT(decimal_rep_length > 0); |
| 563 ASSERT(decimal_rep_length <= f + 1); | 567 ASSERT(decimal_rep_length <= f + 1); |
| 564 USE(decimal_rep_length); | 568 USE(decimal_rep_length); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 578 | 582 |
| 579 bool negative = false; | 583 bool negative = false; |
| 580 if (value < 0) { | 584 if (value < 0) { |
| 581 value = -value; | 585 value = -value; |
| 582 negative = true; | 586 negative = true; |
| 583 } | 587 } |
| 584 | 588 |
| 585 // Find a sufficiently precise decimal representation of n. | 589 // Find a sufficiently precise decimal representation of n. |
| 586 int decimal_point; | 590 int decimal_point; |
| 587 int sign; | 591 int sign; |
| 592 V8SharedStateLocker dtoa_locker(&mutex_lock); |
| 588 char* decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL); | 593 char* decimal_rep = dtoa(value, 2, p, &decimal_point, &sign, NULL); |
| 589 int decimal_rep_length = StrLength(decimal_rep); | 594 int decimal_rep_length = StrLength(decimal_rep); |
| 590 ASSERT(decimal_rep_length <= p); | 595 ASSERT(decimal_rep_length <= p); |
| 591 | 596 |
| 592 int exponent = decimal_point - 1; | 597 int exponent = decimal_point - 1; |
| 593 | 598 |
| 594 char* result = NULL; | 599 char* result = NULL; |
| 595 | 600 |
| 596 if (exponent < -6 || exponent >= p) { | 601 if (exponent < -6 || exponent >= p) { |
| 597 result = | 602 result = |
| (...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 700 // Allocate result and fill in the parts. | 705 // Allocate result and fill in the parts. |
| 701 StringBuilder builder(result_size + 1); | 706 StringBuilder builder(result_size + 1); |
| 702 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 707 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
| 703 if (decimal_pos > 0) builder.AddCharacter('.'); | 708 if (decimal_pos > 0) builder.AddCharacter('.'); |
| 704 builder.AddSubstring(decimal_buffer, decimal_pos); | 709 builder.AddSubstring(decimal_buffer, decimal_pos); |
| 705 return builder.Finalize(); | 710 return builder.Finalize(); |
| 706 } | 711 } |
| 707 | 712 |
| 708 | 713 |
| 709 } } // namespace v8::internal | 714 } } // namespace v8::internal |
| OLD | NEW |