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