v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
runtime-bigint.cc
Go to the documentation of this file.
1// Copyright 2017 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
8
9namespace v8 {
10namespace internal {
11
12RUNTIME_FUNCTION(Runtime_BigIntCompareToNumber) {
13 SealHandleScope shs(isolate);
14 DCHECK_EQ(3, args.length());
15 int mode = args.smi_value_at(0);
16 DirectHandle<BigInt> lhs = args.at<BigInt>(1);
17 DirectHandle<Object> rhs = args.at(2);
18 bool result = ComparisonResultToBool(static_cast<Operation>(mode),
19 BigInt::CompareToNumber(lhs, rhs));
20 return *isolate->factory()->ToBoolean(result);
21}
22
23RUNTIME_FUNCTION(Runtime_BigIntCompareToString) {
24 HandleScope scope(isolate);
25 DCHECK_EQ(3, args.length());
26 int mode = args.smi_value_at(0);
27 DirectHandle<BigInt> lhs = args.at<BigInt>(1);
28 DirectHandle<String> rhs = args.at<String>(2);
29 Maybe<ComparisonResult> maybe_result =
30 BigInt::CompareToString(isolate, lhs, rhs);
31 MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
32 bool result = ComparisonResultToBool(static_cast<Operation>(mode),
33 maybe_result.FromJust());
34 return *isolate->factory()->ToBoolean(result);
35}
36
37RUNTIME_FUNCTION(Runtime_BigIntEqualToBigInt) {
38 SealHandleScope shs(isolate);
39 DCHECK_EQ(2, args.length());
40 DirectHandle<BigInt> lhs = args.at<BigInt>(0);
41 DirectHandle<BigInt> rhs = args.at<BigInt>(1);
42 bool result = BigInt::EqualToBigInt(*lhs, *rhs);
43 return *isolate->factory()->ToBoolean(result);
44}
45
46RUNTIME_FUNCTION(Runtime_BigIntEqualToNumber) {
47 SealHandleScope shs(isolate);
48 DCHECK_EQ(2, args.length());
49 DirectHandle<BigInt> lhs = args.at<BigInt>(0);
50 DirectHandle<Object> rhs = args.at(1);
51 bool result = BigInt::EqualToNumber(lhs, rhs);
52 return *isolate->factory()->ToBoolean(result);
53}
54
55RUNTIME_FUNCTION(Runtime_BigIntEqualToString) {
56 HandleScope scope(isolate);
57 DCHECK_EQ(2, args.length());
58 DirectHandle<BigInt> lhs = args.at<BigInt>(0);
59 DirectHandle<String> rhs = args.at<String>(1);
60 Maybe<bool> maybe_result = BigInt::EqualToString(isolate, lhs, rhs);
61 MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
62 return *isolate->factory()->ToBoolean(maybe_result.FromJust());
63}
64
65RUNTIME_FUNCTION(Runtime_BigIntToNumber) {
66 HandleScope scope(isolate);
67 DCHECK_EQ(1, args.length());
69 return *BigInt::ToNumber(isolate, x);
70}
71
72RUNTIME_FUNCTION(Runtime_ToBigInt) {
73 HandleScope scope(isolate);
74 DCHECK_EQ(1, args.length());
76 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
77}
78
79RUNTIME_FUNCTION(Runtime_ToBigIntConvertNumber) {
80 HandleScope scope(isolate);
81 DCHECK_EQ(1, args.length());
83
84 if (IsJSReceiver(*x)) {
86 isolate, x,
89 }
90
91 if (IsNumber(*x)) {
93 } else {
94 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
95 }
96}
97
98RUNTIME_FUNCTION(Runtime_BigIntExponentiate) {
99 HandleScope scope(isolate);
100 DCHECK_EQ(2, args.length());
101 DirectHandle<Object> left_obj = args.at(0);
102 DirectHandle<Object> right_obj = args.at(1);
103
104 if (!IsBigInt(*left_obj) || !IsBigInt(*right_obj)) {
106 isolate, NewTypeError(MessageTemplate::kBigIntMixedTypes));
107 }
108 auto left = Cast<BigInt>(left_obj);
109 auto right = Cast<BigInt>(right_obj);
110 RETURN_RESULT_OR_FAILURE(isolate, BigInt::Exponentiate(isolate, left, right));
111}
112
113RUNTIME_FUNCTION(Runtime_BigIntUnaryOp) {
114 HandleScope scope(isolate);
115 DCHECK_EQ(2, args.length());
117 int opcode = args.smi_value_at(1);
118 Operation op = static_cast<Operation>(opcode);
119
121 switch (op) {
122 case Operation::kBitwiseNot:
123 result = BigInt::BitwiseNot(isolate, x);
124 break;
125 case Operation::kNegate:
126 result = BigInt::UnaryMinus(isolate, x);
127 break;
128 case Operation::kIncrement:
129 result = BigInt::Increment(isolate, x);
130 break;
131 case Operation::kDecrement:
132 result = BigInt::Decrement(isolate, x);
133 break;
134 default:
135 UNREACHABLE();
136 }
138}
139
140} // namespace internal
141} // namespace v8
static Maybe< ComparisonResult > CompareToString(Isolate *isolate, DirectHandle< BigInt > x, DirectHandle< String > y)
Definition bigint.cc:625
static Maybe< bool > EqualToString(Isolate *isolate, DirectHandle< BigInt > x, DirectHandle< String > y)
Definition bigint.cc:643
static DirectHandle< Number > ToNumber(Isolate *isolate, DirectHandle< BigInt > x)
Definition bigint.cc:1023
static MaybeDirectHandle< BigInt > Exponentiate(Isolate *isolate, DirectHandle< BigInt > base, DirectHandle< BigInt > exponent)
Definition bigint.cc:362
static MaybeHandle< BigInt > Increment(Isolate *isolate, DirectHandle< BigInt > x)
Definition bigint.cc:599
static bool EqualToBigInt(Tagged< BigInt > x, Tagged< BigInt > y)
Definition bigint.cc:590
static bool EqualToNumber(DirectHandle< BigInt > x, DirectHandle< Object > y)
Definition bigint.cc:660
static MaybeHandle< BigInt > Decrement(Isolate *isolate, DirectHandle< BigInt > x)
Definition bigint.cc:611
static Handle< BigInt > UnaryMinus(Isolate *isolate, DirectHandle< BigInt > x)
Definition bigint.cc:341
static MaybeDirectHandle< BigInt > BitwiseNot(Isolate *isolate, DirectHandle< BigInt > x)
Definition bigint.cc:349
static ComparisonResult CompareToNumber(DirectHandle< BigInt > x, DirectHandle< Object > y)
Definition bigint.cc:679
static V8_EXPORT_PRIVATE MaybeHandle< BigInt > FromNumber(Isolate *isolate, DirectHandle< Object > number)
Definition bigint.cc:954
static V8_WARN_UNUSED_RESULT HandleType< Object >::MaybeType ToPrimitive(Isolate *isolate, HandleType< JSReceiver > receiver, ToPrimitiveHint hint=ToPrimitiveHint::kDefault)
#define RUNTIME_FUNCTION(Name)
Definition arguments.h:162
#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:284
#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call)
Definition isolate.h:294
#define MAYBE_RETURN(call, value)
Definition isolate.h:408
#define RETURN_RESULT_OR_FAILURE(isolate, call)
Definition isolate.h:264
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
ZoneVector< RpoNumber > & result
int x
bool ComparisonResultToBool(Operation op, ComparisonResult result)
Definition objects.cc:164
bool IsNumber(Tagged< Object > obj)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150
#define DCHECK_EQ(v1, v2)
Definition logging.h:485