v8
V8 is Google’s open source high-performance JavaScript and WebAssembly engine, written in C++.
Loading...
Searching...
No Matches
builtins-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
10#ifdef V8_INTL_SUPPORT
12#endif
13
14namespace v8 {
15namespace internal {
16
17BUILTIN(BigIntConstructor) {
18 HandleScope scope(isolate);
19 if (!IsUndefined(*args.new_target(), isolate)) { // [[Construct]]
21 isolate, NewTypeError(MessageTemplate::kNotConstructor,
22 isolate->factory()->BigInt_string()));
23 }
24 // [[Call]]
25 Handle<Object> value = args.atOrUndefined(isolate, 1);
26
27 if (IsJSReceiver(*value)) {
29 isolate, value,
32 }
33
34 if (IsNumber(*value)) {
35 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromNumber(isolate, value));
36 } else {
37 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, value));
38 }
39}
40
41BUILTIN(BigIntAsUintN) {
42 HandleScope scope(isolate);
43 Handle<Object> bits_obj = args.atOrUndefined(isolate, 1);
44 Handle<Object> bigint_obj = args.atOrUndefined(isolate, 2);
45
48 isolate, bits,
49 Object::ToIndex(isolate, bits_obj, MessageTemplate::kInvalidIndex));
50
53 BigInt::FromObject(isolate, bigint_obj));
54
56 isolate, BigInt::AsUintN(isolate, Object::NumberValue(*bits), bigint));
57}
58
59BUILTIN(BigIntAsIntN) {
60 HandleScope scope(isolate);
61 Handle<Object> bits_obj = args.atOrUndefined(isolate, 1);
62 Handle<Object> bigint_obj = args.atOrUndefined(isolate, 2);
63
66 isolate, bits,
67 Object::ToIndex(isolate, bits_obj, MessageTemplate::kInvalidIndex));
68
71 BigInt::FromObject(isolate, bigint_obj));
72
73 return *BigInt::AsIntN(isolate, Object::NumberValue(*bits), bigint);
74}
75
76namespace {
77
78MaybeHandle<BigInt> ThisBigIntValue(Isolate* isolate, Handle<Object> value,
79 const char* caller) {
80 // 1. If Type(value) is BigInt, return value.
81 if (IsBigInt(*value)) return Cast<BigInt>(value);
82 // 2. If Type(value) is Object and value has a [[BigIntData]] internal slot:
83 if (IsJSPrimitiveWrapper(*value)) {
84 // 2a. Assert: value.[[BigIntData]] is a BigInt value.
85 // 2b. Return value.[[BigIntData]].
86 Tagged<Object> data = Cast<JSPrimitiveWrapper>(*value)->value();
87 if (IsBigInt(data)) return handle(Cast<BigInt>(data), isolate);
88 }
89 // 3. Throw a TypeError exception.
91 isolate,
92 NewTypeError(MessageTemplate::kNotGeneric,
93 isolate->factory()->NewStringFromAsciiChecked(caller),
94 isolate->factory()->BigInt_string()));
95}
96
97Tagged<Object> BigIntToStringImpl(Handle<Object> receiver, Handle<Object> radix,
98 Isolate* isolate, const char* builtin_name) {
99 // 1. Let x be ? thisBigIntValue(this value).
100 DirectHandle<BigInt> x;
102 isolate, x, ThisBigIntValue(isolate, receiver, builtin_name));
103 // 2. If radix is not present, let radixNumber be 10.
104 // 3. Else if radix is undefined, let radixNumber be 10.
105 int radix_number = 10;
106 if (!IsUndefined(*radix, isolate)) {
107 // 4. Else, let radixNumber be ? ToInteger(radix).
108 double radix_double;
110 isolate, radix_double, Object::IntegerValue(isolate, radix));
111 // 5. If radixNumber < 2 or radixNumber > 36, throw a RangeError exception.
112 if (radix_double < 2 || radix_double > 36) {
114 isolate, NewRangeError(MessageTemplate::kToRadixFormatRange));
115 }
116 radix_number = static_cast<int>(radix_double);
117 }
118 // Return the String representation of this Number value using the radix
119 // specified by radixNumber.
120 RETURN_RESULT_OR_FAILURE(isolate, BigInt::ToString(isolate, x, radix_number));
121}
122
123} // namespace
124
125BUILTIN(BigIntPrototypeToLocaleString) {
126 HandleScope scope(isolate);
127 const char* method_name = "BigInt.prototype.toLocaleString";
128#ifdef V8_INTL_SUPPORT
129 // 1. Let x be ? thisBigIntValue(this value).
132 isolate, x, ThisBigIntValue(isolate, args.receiver(), method_name));
133
135 isolate,
136 Intl::NumberToLocaleString(isolate, x, args.atOrUndefined(isolate, 1),
137 args.atOrUndefined(isolate, 2), method_name));
138 // Fallbacks to old toString implementation if no V8_INTL_SUPPORT.
139#endif // V8_INTL_SUPPORT
140 Handle<Object> radix = isolate->factory()->undefined_value();
141 return BigIntToStringImpl(args.receiver(), radix, isolate, method_name);
142}
143
144BUILTIN(BigIntPrototypeToString) {
145 HandleScope scope(isolate);
146 Handle<Object> radix = args.atOrUndefined(isolate, 1);
147 return BigIntToStringImpl(args.receiver(), radix, isolate,
148 "BigInt.prototype.toString");
149}
150
151BUILTIN(BigIntPrototypeValueOf) {
152 HandleScope scope(isolate);
154 isolate,
155 ThisBigIntValue(isolate, args.receiver(), "BigInt.prototype.valueOf"));
156}
157
158} // namespace internal
159} // namespace v8
#define BUILTIN(name)
static MaybeDirectHandle< BigInt > AsUintN(Isolate *isolate, uint64_t n, DirectHandle< BigInt > x)
Definition bigint.cc:1309
static MaybeHandle< String > ToString(Isolate *isolate, DirectHandle< BigInt > bigint, int radix=10, ShouldThrow should_throw=kThrowOnError)
Definition bigint.cc:835
static DirectHandle< BigInt > AsIntN(Isolate *isolate, uint64_t n, DirectHandle< BigInt > x)
Definition bigint.cc:1294
static V8_EXPORT_PRIVATE MaybeHandle< BigInt > FromNumber(Isolate *isolate, DirectHandle< Object > number)
Definition bigint.cc:954
static V8_WARN_UNUSED_RESULT MaybeDirectHandle< String > NumberToLocaleString(Isolate *isolate, Handle< Object > num, DirectHandle< Object > locales, DirectHandle< Object > options, const char *method_name)
static V8_WARN_UNUSED_RESULT HandleType< Object >::MaybeType ToPrimitive(Isolate *isolate, HandleType< JSReceiver > receiver, ToPrimitiveHint hint=ToPrimitiveHint::kDefault)
static V8_WARN_UNUSED_RESULT HandleType< Object >::MaybeType ToIndex(Isolate *isolate, HandleType< T > input, MessageTemplate error_index)
static double NumberValue(Tagged< Number > obj)
static V8_WARN_UNUSED_RESULT Maybe< double > IntegerValue(Isolate *isolate, HandleType< T > input)
#define ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:284
#define THROW_NEW_ERROR(isolate, call)
Definition isolate.h:307
#define THROW_NEW_ERROR_RETURN_FAILURE(isolate, call)
Definition isolate.h:294
#define RETURN_RESULT_OR_FAILURE(isolate, call)
Definition isolate.h:264
#define MAYBE_ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, dst, call)
Definition isolate.h:448
base::Vector< const DirectHandle< Object > > args
Definition execution.cc:74
TNode< Object > receiver
int x
V8_INLINE IndirectHandle< T > handle(Tagged< T > object, Isolate *isolate)
Definition handles-inl.h:72
bool IsNumber(Tagged< Object > obj)
Tagged< To > Cast(Tagged< From > value, const v8::SourceLocation &loc=INIT_SOURCE_LOCATION_IN_DEBUG)
Definition casting.h:150