v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
runtime-numbers.cc
Go to the documentation of this file.
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
7#include "src/heap/heap-inl.h" // For ToBoolean. TODO(jkummerow): Drop.
9
10namespace v8 {
11namespace internal {
12
13RUNTIME_FUNCTION(Runtime_StringToNumber) {
14 // When this is called from Wasm code, clear the "thread in wasm" flag,
15 // which is important in case any GC needs to happen.
16 // TODO(40192807): Find a better fix, likely by replacing the global flag.
17 SaveAndClearThreadInWasmFlag clear_wasm_flag(isolate);
18
19 HandleScope handle_scope(isolate);
20 DCHECK_EQ(1, args.length());
21 Handle<String> subject = args.at<String>(0);
22 return *String::ToNumber(isolate, subject);
23}
24
25
26// ES6 18.2.5 parseInt(string, radix) slow path
27RUNTIME_FUNCTION(Runtime_StringParseInt) {
28 HandleScope handle_scope(isolate);
29 DCHECK_EQ(2, args.length());
30 Handle<Object> string = args.at(0);
31 Handle<Object> radix = args.at(1);
32
33 // Convert {string} to a String first, and flatten it.
34 Handle<String> subject;
36 Object::ToString(isolate, string));
37 subject = String::Flatten(isolate, subject);
38
39 // Convert {radix} to Int32.
40 if (!IsNumber(*radix)) {
42 Object::ToNumber(isolate, radix));
43 }
44 int radix32 = DoubleToInt32(Object::NumberValue(*radix));
45 if (radix32 != 0 && (radix32 < 2 || radix32 > 36)) {
46 return ReadOnlyRoots(isolate).nan_value();
47 }
48
49 double result = StringToInt(isolate, subject, radix32);
50 return *isolate->factory()->NewNumber(result);
51}
52
53
54// ES6 18.2.4 parseFloat(string)
55RUNTIME_FUNCTION(Runtime_StringParseFloat) {
56 HandleScope shs(isolate);
57 DCHECK_EQ(1, args.length());
58 DirectHandle<String> subject = args.at<String>(0);
59
60 double value = StringToDouble(isolate, subject, ALLOW_TRAILING_JUNK,
61 std::numeric_limits<double>::quiet_NaN());
62
63 return *isolate->factory()->NewNumber(value);
64}
65
66RUNTIME_FUNCTION(Runtime_NumberToStringSlow) {
67 // When this is called from Wasm code, clear the "thread in wasm" flag,
68 // which is important in case any GC needs to happen.
69 // TODO(40192807): Find a better fix, likely by replacing the global flag.
70 SaveAndClearThreadInWasmFlag clear_wasm_flag(isolate);
71
72 HandleScope scope(isolate);
73 DCHECK_EQ(1, args.length());
74 return *isolate->factory()->NumberToString(args.at(0),
76}
77
78RUNTIME_FUNCTION(Runtime_MaxSmi) {
79 SealHandleScope shs(isolate);
80 DCHECK_EQ(0, args.length());
82}
83
84
85RUNTIME_FUNCTION(Runtime_IsSmi) {
86 SealHandleScope shs(isolate);
87 DCHECK_EQ(1, args.length());
88 Tagged<Object> obj = args[0];
89 return isolate->heap()->ToBoolean(IsSmi(obj));
90}
91
92
93RUNTIME_FUNCTION(Runtime_GetHoleNaNUpper) {
94 HandleScope scope(isolate);
95 DCHECK_EQ(0, args.length());
96 return *isolate->factory()->NewNumberFromUint(kHoleNanUpper32);
97}
98
99
100RUNTIME_FUNCTION(Runtime_GetHoleNaNLower) {
101 HandleScope scope(isolate);
102 DCHECK_EQ(0, args.length());
103 return *isolate->factory()->NewNumberFromUint(kHoleNanLower32);
104}
105
106} // namespace internal
107} // namespace v8
static V8_WARN_UNUSED_RESULT HandleType< String >::MaybeType ToString(Isolate *isolate, HandleType< T > input)
static V8_WARN_UNUSED_RESULT HandleType< Number >::MaybeType ToNumber(Isolate *isolate, HandleType< T > input)
static double NumberValue(Tagged< Number > obj)
static constexpr Tagged< Smi > FromInt(int value)
Definition smi.h:38
static constexpr int kMaxValue
Definition smi.h:101
static V8_INLINE HandleType< String > Flatten(Isolate *isolate, HandleType< T > string, AllocationType allocation=AllocationType::kYoung)
static HandleType< Number > ToNumber(Isolate *isolate, HandleType< String > subject)
Definition string.cc:661
#define RUNTIME_FUNCTION(Name)
Definition arguments.h:162
#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:284
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
ZoneVector< RpoNumber > & result
bool IsNumber(Tagged< Object > obj)
double StringToInt(Isolate *isolate, DirectHandle< String > string, int radix)
V8_INLINE constexpr bool IsSmi(TaggedImpl< kRefType, StorageType > obj)
Definition objects.h:665
int32_t DoubleToInt32(double x)
constexpr uint32_t kHoleNanLower32
Definition globals.h:1953
constexpr uint32_t kHoleNanUpper32
Definition globals.h:1952
double StringToDouble(const char *str, ConversionFlag flags, double empty_string_val)
#define DCHECK_EQ(v1, v2)
Definition logging.h:485