Line data Source code
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/arguments-inl.h"
6 : #include "src/base/bits.h"
7 : #include "src/bootstrapper.h"
8 : #include "src/counters.h"
9 : #include "src/isolate-inl.h"
10 : #include "src/runtime/runtime-utils.h"
11 :
12 : namespace v8 {
13 : namespace internal {
14 :
15 5 : RUNTIME_FUNCTION(Runtime_IsValidSmi) {
16 : SealHandleScope shs(isolate);
17 : DCHECK_EQ(1, args.length());
18 :
19 10 : CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]);
20 5 : return isolate->heap()->ToBoolean(Smi::IsValid(number));
21 : }
22 :
23 :
24 3759030 : RUNTIME_FUNCTION(Runtime_StringToNumber) {
25 3759030 : HandleScope handle_scope(isolate);
26 : DCHECK_EQ(1, args.length());
27 7518060 : CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
28 7518060 : return *String::ToNumber(isolate, subject);
29 : }
30 :
31 :
32 : // ES6 18.2.5 parseInt(string, radix) slow path
33 1341148 : RUNTIME_FUNCTION(Runtime_StringParseInt) {
34 1341148 : HandleScope handle_scope(isolate);
35 : DCHECK_EQ(2, args.length());
36 1341148 : CONVERT_ARG_HANDLE_CHECKED(Object, string, 0);
37 1341148 : CONVERT_ARG_HANDLE_CHECKED(Object, radix, 1);
38 :
39 : // Convert {string} to a String first, and flatten it.
40 : Handle<String> subject;
41 2682296 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, subject,
42 : Object::ToString(isolate, string));
43 1341121 : subject = String::Flatten(isolate, subject);
44 :
45 : // Convert {radix} to Int32.
46 2682242 : if (!radix->IsNumber()) {
47 2681152 : ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
48 : Object::ToNumber(isolate, radix));
49 : }
50 1341103 : int radix32 = DoubleToInt32(radix->Number());
51 1341103 : if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
52 : return ReadOnlyRoots(isolate).nan_value();
53 : }
54 :
55 1341094 : double result = StringToInt(isolate, subject, radix32);
56 2682188 : return *isolate->factory()->NewNumber(result);
57 : }
58 :
59 :
60 : // ES6 18.2.4 parseFloat(string)
61 576 : RUNTIME_FUNCTION(Runtime_StringParseFloat) {
62 576 : HandleScope shs(isolate);
63 : DCHECK_EQ(1, args.length());
64 1152 : CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
65 :
66 : double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
67 576 : std::numeric_limits<double>::quiet_NaN());
68 :
69 1152 : return *isolate->factory()->NewNumber(value);
70 : }
71 :
72 27499387 : RUNTIME_FUNCTION(Runtime_NumberToString) {
73 27499387 : HandleScope scope(isolate);
74 : DCHECK_EQ(1, args.length());
75 54998774 : CONVERT_NUMBER_ARG_HANDLE_CHECKED(number, 0);
76 :
77 54998774 : return *isolate->factory()->NumberToString(number);
78 : }
79 :
80 : // Compare two Smis x, y as if they were converted to strings and then
81 : // compared lexicographically. Returns:
82 : // -1 if x < y
83 : // 0 if x == y
84 : // 1 if x > y
85 : // TODO(szuend): Remove once the call-site in src/js/array.js is gone.
86 0 : RUNTIME_FUNCTION(Runtime_SmiLexicographicCompare) {
87 : SealHandleScope shs(isolate);
88 : DCHECK_EQ(2, args.length());
89 0 : CONVERT_ARG_CHECKED(Smi, x_value, 0);
90 0 : CONVERT_ARG_CHECKED(Smi, y_value, 1);
91 :
92 0 : return Object(Smi::LexicographicCompare(isolate, x_value, y_value));
93 : }
94 :
95 443 : RUNTIME_FUNCTION(Runtime_MaxSmi) {
96 : SealHandleScope shs(isolate);
97 : DCHECK_EQ(0, args.length());
98 443 : return Smi::FromInt(Smi::kMaxValue);
99 : }
100 :
101 :
102 10 : RUNTIME_FUNCTION(Runtime_IsSmi) {
103 : SealHandleScope shs(isolate);
104 : DCHECK_EQ(1, args.length());
105 20 : CONVERT_ARG_CHECKED(Object, obj, 0);
106 20 : return isolate->heap()->ToBoolean(obj->IsSmi());
107 : }
108 :
109 :
110 36 : RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
111 36 : HandleScope scope(isolate);
112 : DCHECK_EQ(0, args.length());
113 72 : return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
114 : }
115 :
116 :
117 36 : RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
118 36 : HandleScope scope(isolate);
119 : DCHECK_EQ(0, args.length());
120 72 : return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
121 : }
122 :
123 : } // namespace internal
124 183867 : } // namespace v8
|