forked from nodejs/node
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathruntime-bigint.cc
More file actions
141 lines (124 loc) Β· 4.51 KB
/
Copy pathruntime-bigint.cc
File metadata and controls
141 lines (124 loc) Β· 4.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Copyright 2017 the V8 project authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "src/execution/arguments-inl.h"
#include "src/objects/bigint.h"
#include "src/objects/objects-inl.h"
namespace v8 {
namespace internal {
RUNTIME_FUNCTION(Runtime_BigIntCompareToNumber) {
SealHandleScope shs(isolate);
DCHECK_EQ(3, args.length());
int mode = args.smi_value_at(0);
DirectHandle<BigInt> lhs = args.at<BigInt>(1);
DirectHandle<Object> rhs = args.at(2);
bool result = ComparisonResultToBool(static_cast<Operation>(mode),
BigInt::CompareToNumber(lhs, rhs));
return *isolate->factory()->ToBoolean(result);
}
RUNTIME_FUNCTION(Runtime_BigIntCompareToString) {
HandleScope scope(isolate);
DCHECK_EQ(3, args.length());
int mode = args.smi_value_at(0);
DirectHandle<BigInt> lhs = args.at<BigInt>(1);
DirectHandle<String> rhs = args.at<String>(2);
Maybe<ComparisonResult> maybe_result =
BigInt::CompareToString(isolate, lhs, rhs);
MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
bool result = ComparisonResultToBool(static_cast<Operation>(mode),
maybe_result.FromJust());
return *isolate->factory()->ToBoolean(result);
}
RUNTIME_FUNCTION(Runtime_BigIntEqualToBigInt) {
SealHandleScope shs(isolate);
DCHECK_EQ(2, args.length());
DirectHandle<BigInt> lhs = args.at<BigInt>(0);
DirectHandle<BigInt> rhs = args.at<BigInt>(1);
bool result = BigInt::EqualToBigInt(*lhs, *rhs);
return *isolate->factory()->ToBoolean(result);
}
RUNTIME_FUNCTION(Runtime_BigIntEqualToNumber) {
SealHandleScope shs(isolate);
DCHECK_EQ(2, args.length());
DirectHandle<BigInt> lhs = args.at<BigInt>(0);
DirectHandle<Object> rhs = args.at(1);
bool result = BigInt::EqualToNumber(lhs, rhs);
return *isolate->factory()->ToBoolean(result);
}
RUNTIME_FUNCTION(Runtime_BigIntEqualToString) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
DirectHandle<BigInt> lhs = args.at<BigInt>(0);
DirectHandle<String> rhs = args.at<String>(1);
Maybe<bool> maybe_result = BigInt::EqualToString(isolate, lhs, rhs);
MAYBE_RETURN(maybe_result, ReadOnlyRoots(isolate).exception());
return *isolate->factory()->ToBoolean(maybe_result.FromJust());
}
RUNTIME_FUNCTION(Runtime_BigIntToNumber) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
DirectHandle<BigInt> x = args.at<BigInt>(0);
return *BigInt::ToNumber(isolate, x);
}
RUNTIME_FUNCTION(Runtime_ToBigInt) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
DirectHandle<Object> x = args.at(0);
RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
}
RUNTIME_FUNCTION(Runtime_ToBigIntConvertNumber) {
HandleScope scope(isolate);
DCHECK_EQ(1, args.length());
DirectHandle<Object> x = args.at(0);
if (IsJSReceiver(*x)) {
ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
isolate, x,
JSReceiver::ToPrimitive(isolate, Cast<JSReceiver>(x),
ToPrimitiveHint::kNumber));
}
if (IsNumber(*x)) {
RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromNumber(isolate, x));
} else {
RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, x));
}
}
RUNTIME_FUNCTION(Runtime_BigIntExponentiate) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
DirectHandle<Object> left_obj = args.at(0);
DirectHandle<Object> right_obj = args.at(1);
if (!IsBigInt(*left_obj) || !IsBigInt(*right_obj)) {
THROW_NEW_ERROR_RETURN_FAILURE(
isolate, NewTypeError(MessageTemplate::kBigIntMixedTypes));
}
auto left = Cast<BigInt>(left_obj);
auto right = Cast<BigInt>(right_obj);
RETURN_RESULT_OR_FAILURE(isolate, BigInt::Exponentiate(isolate, left, right));
}
RUNTIME_FUNCTION(Runtime_BigIntUnaryOp) {
HandleScope scope(isolate);
DCHECK_EQ(2, args.length());
DirectHandle<BigInt> x = args.at<BigInt>(0);
int opcode = args.smi_value_at(1);
Operation op = static_cast<Operation>(opcode);
MaybeDirectHandle<BigInt> result;
switch (op) {
case Operation::kBitwiseNot:
result = BigInt::BitwiseNot(isolate, x);
break;
case Operation::kNegate:
result = BigInt::UnaryMinus(isolate, x);
break;
case Operation::kIncrement:
result = BigInt::Increment(isolate, x);
break;
case Operation::kDecrement:
result = BigInt::Decrement(isolate, x);
break;
default:
UNREACHABLE();
}
RETURN_RESULT_OR_FAILURE(isolate, result);
}
} // namespace internal
} // namespace v8