V8 API Reference, 7.2.502.16 (for Deno 0.2.4)
runtime-numbers.cc
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 RUNTIME_FUNCTION(Runtime_IsValidSmi) {
16  SealHandleScope shs(isolate);
17  DCHECK_EQ(1, args.length());
18 
19  CONVERT_NUMBER_CHECKED(int32_t, number, Int32, args[0]);
20  return isolate->heap()->ToBoolean(Smi::IsValid(number));
21 }
22 
23 
24 RUNTIME_FUNCTION(Runtime_StringToNumber) {
25  HandleScope handle_scope(isolate);
26  DCHECK_EQ(1, args.length());
27  CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
28  return *String::ToNumber(isolate, subject);
29 }
30 
31 
32 // ES6 18.2.5 parseInt(string, radix) slow path
33 RUNTIME_FUNCTION(Runtime_StringParseInt) {
34  HandleScope handle_scope(isolate);
35  DCHECK_EQ(2, args.length());
36  CONVERT_ARG_HANDLE_CHECKED(Object, string, 0);
37  CONVERT_ARG_HANDLE_CHECKED(Object, radix, 1);
38 
39  // Convert {string} to a String first, and flatten it.
40  Handle<String> subject;
41  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, subject,
42  Object::ToString(isolate, string));
43  subject = String::Flatten(isolate, subject);
44 
45  // Convert {radix} to Int32.
46  if (!radix->IsNumber()) {
47  ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
48  Object::ToNumber(isolate, radix));
49  }
50  int radix32 = DoubleToInt32(radix->Number());
51  if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
52  return ReadOnlyRoots(isolate).nan_value();
53  }
54 
55  double result = StringToInt(isolate, subject, radix32);
56  return *isolate->factory()->NewNumber(result);
57 }
58 
59 
60 // ES6 18.2.4 parseFloat(string)
61 RUNTIME_FUNCTION(Runtime_StringParseFloat) {
62  HandleScope shs(isolate);
63  DCHECK_EQ(1, args.length());
64  CONVERT_ARG_HANDLE_CHECKED(String, subject, 0);
65 
66  double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
67  std::numeric_limits<double>::quiet_NaN());
68 
69  return *isolate->factory()->NewNumber(value);
70 }
71 
72 RUNTIME_FUNCTION(Runtime_NumberToString) {
73  HandleScope scope(isolate);
74  DCHECK_EQ(1, args.length());
75  CONVERT_NUMBER_ARG_HANDLE_CHECKED(number, 0);
76 
77  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 RUNTIME_FUNCTION(Runtime_SmiLexicographicCompare) {
87  SealHandleScope shs(isolate);
88  DCHECK_EQ(2, args.length());
89  CONVERT_ARG_CHECKED(Smi, x_value, 0);
90  CONVERT_ARG_CHECKED(Smi, y_value, 1);
91 
92  return reinterpret_cast<Object*>(
93  Smi::LexicographicCompare(isolate, x_value, y_value));
94 }
95 
96 RUNTIME_FUNCTION(Runtime_MaxSmi) {
97  SealHandleScope shs(isolate);
98  DCHECK_EQ(0, args.length());
99  return Smi::FromInt(Smi::kMaxValue);
100 }
101 
102 
103 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 RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
112  HandleScope scope(isolate);
113  DCHECK_EQ(0, args.length());
114  return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
115 }
116 
117 
118 RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
119  HandleScope scope(isolate);
120  DCHECK_EQ(0, args.length());
121  return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
122 }
123 
124 } // namespace internal
125 } // namespace v8
Definition: libplatform.h:13