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
3 // found in the LICENSE file.
4
5 #include "src/base/bits.h"
6 #include "src/execution/arguments-inl.h"
7 #include "src/execution/isolate-inl.h"
8 #include "src/heap/heap-inl.h" // For ToBoolean. TODO(jkummerow): Drop.
9 #include "src/init/bootstrapper.h"
10 #include "src/logging/counters.h"
11 #include "src/runtime/runtime-utils.h"
12
13 namespace v8 {
14 namespace internal {
15
RUNTIME_FUNCTION(Runtime_StringToNumber)16 RUNTIME_FUNCTION(Runtime_StringToNumber) {
17 HandleScope handle_scope(isolate);
18 DCHECK_EQ(1, args.length());
19 Handle<String> subject = args.at<String>(0);
20 return *String::ToNumber(isolate, subject);
21 }
22
23
24 // ES6 18.2.5 parseInt(string, radix) slow path
RUNTIME_FUNCTION(Runtime_StringParseInt)25 RUNTIME_FUNCTION(Runtime_StringParseInt) {
26 HandleScope handle_scope(isolate);
27 DCHECK_EQ(2, args.length());
28 Handle<Object> string = args.at(0);
29 Handle<Object> radix = args.at(1);
30
31 // Convert {string} to a String first, and flatten it.
32 Handle<String> subject;
33 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, subject,
34 Object::ToString(isolate, string));
35 subject = String::Flatten(isolate, subject);
36
37 // Convert {radix} to Int32.
38 if (!radix->IsNumber()) {
39 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
40 Object::ToNumber(isolate, radix));
41 }
42 int radix32 = DoubleToInt32(radix->Number());
43 if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
44 return ReadOnlyRoots(isolate).nan_value();
45 }
46
47 double result = StringToInt(isolate, subject, radix32);
48 return *isolate->factory()->NewNumber(result);
49 }
50
51
52 // ES6 18.2.4 parseFloat(string)
RUNTIME_FUNCTION(Runtime_StringParseFloat)53 RUNTIME_FUNCTION(Runtime_StringParseFloat) {
54 HandleScope shs(isolate);
55 DCHECK_EQ(1, args.length());
56 Handle<String> subject = args.at<String>(0);
57
58 double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
59 std::numeric_limits<double>::quiet_NaN());
60
61 return *isolate->factory()->NewNumber(value);
62 }
63
RUNTIME_FUNCTION(Runtime_NumberToStringSlow)64 RUNTIME_FUNCTION(Runtime_NumberToStringSlow) {
65 HandleScope scope(isolate);
66 DCHECK_EQ(1, args.length());
67 return *isolate->factory()->NumberToString(args.at(0),
68 NumberCacheMode::kSetOnly);
69 }
70
RUNTIME_FUNCTION(Runtime_MaxSmi)71 RUNTIME_FUNCTION(Runtime_MaxSmi) {
72 SealHandleScope shs(isolate);
73 DCHECK_EQ(0, args.length());
74 return Smi::FromInt(Smi::kMaxValue);
75 }
76
77
RUNTIME_FUNCTION(Runtime_IsSmi)78 RUNTIME_FUNCTION(Runtime_IsSmi) {
79 SealHandleScope shs(isolate);
80 DCHECK_EQ(1, args.length());
81 Object obj = args[0];
82 return isolate->heap()->ToBoolean(obj.IsSmi());
83 }
84
85
RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper)86 RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
87 HandleScope scope(isolate);
88 DCHECK_EQ(0, args.length());
89 return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
90 }
91
92
RUNTIME_FUNCTION(Runtime_GetHoleNaNLower)93 RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
94 HandleScope scope(isolate);
95 DCHECK_EQ(0, args.length());
96 return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
97 }
98
99 } // namespace internal
100 } // namespace v8
101