OLD | NEW |
1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "src/runtime/runtime-utils.h" | 5 #include "src/runtime/runtime-utils.h" |
6 | 6 |
7 #include "src/arguments.h" | 7 #include "src/arguments.h" |
8 #include "src/base/bits.h" | 8 #include "src/base/bits.h" |
9 #include "src/bootstrapper.h" | 9 #include "src/bootstrapper.h" |
10 #include "src/codegen.h" | 10 #include "src/codegen.h" |
11 #include "src/isolate-inl.h" | 11 #include "src/isolate-inl.h" |
12 | 12 |
13 namespace v8 { | 13 namespace v8 { |
14 namespace internal { | 14 namespace internal { |
15 | 15 |
| 16 RUNTIME_FUNCTION(Runtime_NumberToRadixString) { |
| 17 HandleScope scope(isolate); |
| 18 DCHECK(args.length() == 2); |
| 19 CONVERT_SMI_ARG_CHECKED(radix, 1); |
| 20 CHECK(2 <= radix && radix <= 36); |
| 21 |
| 22 // Fast case where the result is a one character string. |
| 23 if (args[0]->IsSmi()) { |
| 24 int value = args.smi_at(0); |
| 25 if (value >= 0 && value < radix) { |
| 26 // Character array used for conversion. |
| 27 static const char kCharTable[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 28 return *isolate->factory()->LookupSingleCharacterStringFromCode( |
| 29 kCharTable[value]); |
| 30 } |
| 31 } |
| 32 |
| 33 // Slow case. |
| 34 CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
| 35 if (std::isnan(value)) { |
| 36 return isolate->heap()->nan_string(); |
| 37 } |
| 38 if (std::isinf(value)) { |
| 39 if (value < 0) { |
| 40 return isolate->heap()->minus_infinity_string(); |
| 41 } |
| 42 return isolate->heap()->infinity_string(); |
| 43 } |
| 44 char* str = DoubleToRadixCString(value, radix); |
| 45 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); |
| 46 DeleteArray(str); |
| 47 return *result; |
| 48 } |
| 49 |
| 50 |
| 51 RUNTIME_FUNCTION(Runtime_NumberToFixed) { |
| 52 HandleScope scope(isolate); |
| 53 DCHECK(args.length() == 2); |
| 54 |
| 55 CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
| 56 CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
| 57 int f = FastD2IChecked(f_number); |
| 58 // See DoubleToFixedCString for these constants: |
| 59 CHECK(f >= 0 && f <= 20); |
| 60 CHECK(!Double(value).IsSpecial()); |
| 61 char* str = DoubleToFixedCString(value, f); |
| 62 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); |
| 63 DeleteArray(str); |
| 64 return *result; |
| 65 } |
| 66 |
| 67 |
| 68 RUNTIME_FUNCTION(Runtime_NumberToExponential) { |
| 69 HandleScope scope(isolate); |
| 70 DCHECK(args.length() == 2); |
| 71 |
| 72 CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
| 73 CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
| 74 int f = FastD2IChecked(f_number); |
| 75 CHECK(f >= -1 && f <= 20); |
| 76 CHECK(!Double(value).IsSpecial()); |
| 77 char* str = DoubleToExponentialCString(value, f); |
| 78 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); |
| 79 DeleteArray(str); |
| 80 return *result; |
| 81 } |
| 82 |
| 83 |
| 84 RUNTIME_FUNCTION(Runtime_NumberToPrecision) { |
| 85 HandleScope scope(isolate); |
| 86 DCHECK(args.length() == 2); |
| 87 |
| 88 CONVERT_DOUBLE_ARG_CHECKED(value, 0); |
| 89 CONVERT_DOUBLE_ARG_CHECKED(f_number, 1); |
| 90 int f = FastD2IChecked(f_number); |
| 91 CHECK(f >= 1 && f <= 21); |
| 92 CHECK(!Double(value).IsSpecial()); |
| 93 char* str = DoubleToPrecisionCString(value, f); |
| 94 Handle<String> result = isolate->factory()->NewStringFromAsciiChecked(str); |
| 95 DeleteArray(str); |
| 96 return *result; |
| 97 } |
| 98 |
| 99 |
16 RUNTIME_FUNCTION(Runtime_IsValidSmi) { | 100 RUNTIME_FUNCTION(Runtime_IsValidSmi) { |
17 SealHandleScope shs(isolate); | 101 SealHandleScope shs(isolate); |
18 DCHECK(args.length() == 1); | 102 DCHECK(args.length() == 1); |
19 | 103 |
20 CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]); | 104 CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]); |
21 return isolate->heap()->ToBoolean(Smi::IsValid(number)); | 105 return isolate->heap()->ToBoolean(Smi::IsValid(number)); |
22 } | 106 } |
23 | 107 |
24 | 108 |
25 RUNTIME_FUNCTION(Runtime_StringToNumber) { | 109 RUNTIME_FUNCTION(Runtime_StringToNumber) { |
(...skipping 191 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
217 | 301 |
218 RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) { | 302 RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) { |
219 HandleScope scope(isolate); | 303 HandleScope scope(isolate); |
220 DCHECK(args.length() == 0); | 304 DCHECK(args.length() == 0); |
221 return *isolate->factory()->NewNumberFromUint(kHoleNanLower32); | 305 return *isolate->factory()->NewNumberFromUint(kHoleNanLower32); |
222 } | 306 } |
223 | 307 |
224 | 308 |
225 } // namespace internal | 309 } // namespace internal |
226 } // namespace v8 | 310 } // namespace v8 |
OLD | NEW |