OLD | NEW |
1 // Copyright 2006-2009 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2009 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 4668 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
4679 | 4679 |
4680 | 4680 |
4681 static Object* Runtime_StringParseInt(Arguments args) { | 4681 static Object* Runtime_StringParseInt(Arguments args) { |
4682 NoHandleAllocation ha; | 4682 NoHandleAllocation ha; |
4683 | 4683 |
4684 CONVERT_CHECKED(String, s, args[0]); | 4684 CONVERT_CHECKED(String, s, args[0]); |
4685 CONVERT_SMI_CHECKED(radix, args[1]); | 4685 CONVERT_SMI_CHECKED(radix, args[1]); |
4686 | 4686 |
4687 s->TryFlatten(); | 4687 s->TryFlatten(); |
4688 | 4688 |
4689 RUNTIME_ASSERT(radix == 0 || (2 <= radix && radix <= 36)); | 4689 int len = s->length(); |
4690 double value = StringToInt(s, radix); | 4690 int i; |
4691 return Heap::NumberFromDouble(value); | 4691 |
| 4692 // Skip leading white space. |
| 4693 for (i = 0; i < len && Scanner::kIsWhiteSpace.get(s->Get(i)); i++) ; |
| 4694 if (i == len) return Heap::nan_value(); |
| 4695 |
| 4696 // Compute the sign (default to +). |
| 4697 int sign = 1; |
| 4698 if (s->Get(i) == '-') { |
| 4699 sign = -1; |
| 4700 i++; |
| 4701 } else if (s->Get(i) == '+') { |
| 4702 i++; |
| 4703 } |
| 4704 |
| 4705 // Compute the radix if 0. |
| 4706 if (radix == 0) { |
| 4707 radix = 10; |
| 4708 if (i < len && s->Get(i) == '0') { |
| 4709 radix = 8; |
| 4710 if (i + 1 < len) { |
| 4711 int c = s->Get(i + 1); |
| 4712 if (c == 'x' || c == 'X') { |
| 4713 radix = 16; |
| 4714 i += 2; |
| 4715 } |
| 4716 } |
| 4717 } |
| 4718 } else if (radix == 16) { |
| 4719 // Allow 0x or 0X prefix if radix is 16. |
| 4720 if (i + 1 < len && s->Get(i) == '0') { |
| 4721 int c = s->Get(i + 1); |
| 4722 if (c == 'x' || c == 'X') i += 2; |
| 4723 } |
| 4724 } |
| 4725 |
| 4726 RUNTIME_ASSERT(2 <= radix && radix <= 36); |
| 4727 double value; |
| 4728 int end_index = StringToInt(s, i, radix, &value); |
| 4729 if (end_index != i) { |
| 4730 return Heap::NumberFromDouble(sign * value); |
| 4731 } |
4692 return Heap::nan_value(); | 4732 return Heap::nan_value(); |
4693 } | 4733 } |
4694 | 4734 |
4695 | 4735 |
4696 static Object* Runtime_StringParseFloat(Arguments args) { | 4736 static Object* Runtime_StringParseFloat(Arguments args) { |
4697 NoHandleAllocation ha; | 4737 NoHandleAllocation ha; |
4698 CONVERT_CHECKED(String, str, args[0]); | 4738 CONVERT_CHECKED(String, str, args[0]); |
4699 | 4739 |
4700 // ECMA-262 section 15.1.2.3, empty string is NaN | 4740 // ECMA-262 section 15.1.2.3, empty string is NaN |
4701 double value = StringToDouble(str, ALLOW_TRAILING_JUNK, OS::nan_value()); | 4741 double value = StringToDouble(str, ALLOW_TRAILING_JUNK, OS::nan_value()); |
(...skipping 5297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
9999 } else { | 10039 } else { |
10000 // Handle last resort GC and make sure to allow future allocations | 10040 // Handle last resort GC and make sure to allow future allocations |
10001 // to grow the heap without causing GCs (if possible). | 10041 // to grow the heap without causing GCs (if possible). |
10002 Counters::gc_last_resort_from_js.Increment(); | 10042 Counters::gc_last_resort_from_js.Increment(); |
10003 Heap::CollectAllGarbage(false); | 10043 Heap::CollectAllGarbage(false); |
10004 } | 10044 } |
10005 } | 10045 } |
10006 | 10046 |
10007 | 10047 |
10008 } } // namespace v8::internal | 10048 } } // namespace v8::internal |
OLD | NEW |