5 #include "src/builtins/builtins-utils-inl.h" 6 #include "src/builtins/builtins.h" 7 #include "src/conversions.h" 8 #include "src/counters.h" 9 #include "src/objects-inl.h" 14 BUILTIN(BigIntConstructor) {
15 HandleScope scope(isolate);
16 if (!args.new_target()->IsUndefined(isolate)) {
17 THROW_NEW_ERROR_RETURN_FAILURE(
18 isolate, NewTypeError(MessageTemplate::kNotConstructor,
19 isolate->factory()->BigInt_string()));
22 Handle<Object> value = args.atOrUndefined(isolate, 1);
24 if (value->IsJSReceiver()) {
25 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
27 JSReceiver::ToPrimitive(Handle<JSReceiver>::cast(value),
28 ToPrimitiveHint::kNumber));
31 if (value->IsNumber()) {
32 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromNumber(isolate, value));
34 RETURN_RESULT_OR_FAILURE(isolate, BigInt::FromObject(isolate, value));
38 BUILTIN(BigIntAsUintN) {
39 HandleScope scope(isolate);
40 Handle<Object> bits_obj = args.atOrUndefined(isolate, 1);
41 Handle<Object> bigint_obj = args.atOrUndefined(isolate, 2);
44 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
46 Object::ToIndex(isolate, bits_obj, MessageTemplate::kInvalidIndex));
48 Handle<BigInt> bigint;
49 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, bigint,
50 BigInt::FromObject(isolate, bigint_obj));
52 RETURN_RESULT_OR_FAILURE(isolate,
53 BigInt::AsUintN(isolate, bits->Number(), bigint));
56 BUILTIN(BigIntAsIntN) {
57 HandleScope scope(isolate);
58 Handle<Object> bits_obj = args.atOrUndefined(isolate, 1);
59 Handle<Object> bigint_obj = args.atOrUndefined(isolate, 2);
62 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
64 Object::ToIndex(isolate, bits_obj, MessageTemplate::kInvalidIndex));
66 Handle<BigInt> bigint;
67 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, bigint,
68 BigInt::FromObject(isolate, bigint_obj));
70 return *BigInt::AsIntN(isolate, bits->Number(), bigint);
75 MaybeHandle<BigInt> ThisBigIntValue(Isolate* isolate, Handle<Object> value,
78 if (value->IsBigInt())
return Handle<BigInt>::cast(value);
80 if (value->IsJSValue()) {
83 Object* data = JSValue::cast(*value)->value();
84 if (data->IsBigInt())
return handle(BigInt::cast(data), isolate);
89 NewTypeError(MessageTemplate::kNotGeneric,
90 isolate->factory()->NewStringFromAsciiChecked(caller),
91 isolate->factory()->NewStringFromStaticChars(
"BigInt")),
95 Object* BigIntToStringImpl(Handle<Object> receiver, Handle<Object> radix,
96 Isolate* isolate,
const char* builtin_name) {
99 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(
100 isolate, x, ThisBigIntValue(isolate, receiver, builtin_name));
104 if (radix->IsUndefined(isolate)) {
108 ASSIGN_RETURN_FAILURE_ON_EXCEPTION(isolate, radix,
109 Object::ToInteger(isolate, radix));
110 radix_number =
static_cast<int>(radix->Number());
113 if (radix_number < 2 || radix_number > 36) {
114 THROW_NEW_ERROR_RETURN_FAILURE(
115 isolate, NewRangeError(MessageTemplate::kToRadixFormatRange));
119 RETURN_RESULT_OR_FAILURE(isolate, BigInt::ToString(isolate, x, radix_number));
124 BUILTIN(BigIntPrototypeToLocaleString) {
125 HandleScope scope(isolate);
126 Handle<Object> radix = isolate->factory()->undefined_value();
127 return BigIntToStringImpl(args.receiver(), radix, isolate,
128 "BigInt.prototype.toLocaleString");
131 BUILTIN(BigIntPrototypeToString) {
132 HandleScope scope(isolate);
133 Handle<Object> radix = args.atOrUndefined(isolate, 1);
134 return BigIntToStringImpl(args.receiver(), radix, isolate,
135 "BigInt.prototype.toString");
138 BUILTIN(BigIntPrototypeValueOf) {
139 HandleScope scope(isolate);
140 RETURN_RESULT_OR_FAILURE(
142 ThisBigIntValue(isolate, args.receiver(),
"BigInt.prototype.valueOf"));