Skip to content

Commit a5233c7

Browse files
devsnektargos
authored andcommitted
deps: cherry-pick 477df06 from upstream v8
Original commit message: [API] Expand BigInt API Provide a more complete BigInt API. Bug: v8:7712 Cq-Include-Trybots: luci.chromium.try:linux_chromium_rel_ng Change-Id: Ic8562d616f3125deabdf8b52c7019b191bef0e07 Reviewed-on: chromium-review.googlesource.com/1101198 Commit-Queue: Yang Guo <[email protected]> Reviewed-by: Jakob Kummerow <[email protected]> Reviewed-by: Yang Guo <[email protected]> Cr-Commit-Position: refs/heads/master@{#54122} PR-URL: #21644 Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent cec166e commit a5233c7

File tree

8 files changed

+271
-1
lines changed

8 files changed

+271
-1
lines changed

common.gypi

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
# Reset this number to 0 on major V8 upgrades.
3030
# Increment by one for each non-official patch applied to deps/v8.
31-
'v8_embedder_string': '-node.13',
31+
'v8_embedder_string': '-node.14',
3232

3333
# Enable disassembler for `--print-code` v8 options
3434
'v8_enable_disassembler': 1,

deps/v8/include/v8.h

+42
Original file line numberDiff line numberDiff line change
@@ -3048,6 +3048,48 @@ class V8_EXPORT Uint32 : public Integer {
30483048
class V8_EXPORT BigInt : public Primitive {
30493049
public:
30503050
static Local<BigInt> New(Isolate* isolate, int64_t value);
3051+
static Local<BigInt> NewFromUnsigned(Isolate* isolate, uint64_t value);
3052+
/**
3053+
* Creates a new BigInt object using a specified sign bit and a
3054+
* specified list of digits/words.
3055+
* The resulting number is calculated as:
3056+
*
3057+
* (-1)^sign_bit * (words[0] * (2^64)^0 + words[1] * (2^64)^1 + ...)
3058+
*/
3059+
static MaybeLocal<BigInt> NewFromWords(Local<Context> context, int sign_bit,
3060+
int word_count, const uint64_t* words);
3061+
3062+
/**
3063+
* Returns the value of this BigInt as an unsigned 64-bit integer.
3064+
* If `lossless` is provided, it will reflect whether the return value was
3065+
* truncated or wrapped around. In particular, it is set to `false` if this
3066+
* BigInt is negative.
3067+
*/
3068+
uint64_t Uint64Value(bool* lossless = nullptr) const;
3069+
3070+
/**
3071+
* Returns the value of this BigInt as a signed 64-bit integer.
3072+
* If `lossless` is provided, it will reflect whether this BigInt was
3073+
* truncated or not.
3074+
*/
3075+
int64_t Int64Value(bool* lossless = nullptr) const;
3076+
3077+
/**
3078+
* Returns the number of 64-bit words needed to store the result of
3079+
* ToWordsArray().
3080+
*/
3081+
int WordCount() const;
3082+
3083+
/**
3084+
* Writes the contents of this BigInt to a specified memory location.
3085+
* `sign_bit` must be provided and will be set to 1 if this BigInt is
3086+
* negative.
3087+
* `*word_count` has to be initialized to the length of the `words` array.
3088+
* Upon return, it will be set to the actual number of words that would
3089+
* be needed to store this BigInt (i.e. the return value of `WordCount()`).
3090+
*/
3091+
void ToWordsArray(int* sign_bit, int* word_count, uint64_t* words) const;
3092+
30513093
V8_INLINE static BigInt* Cast(v8::Value* obj);
30523094

30533095
private:

deps/v8/src/api.cc

+43
Original file line numberDiff line numberDiff line change
@@ -8121,6 +8121,49 @@ Local<BigInt> v8::BigInt::New(Isolate* isolate, int64_t value) {
81218121
return Utils::ToLocal(result);
81228122
}
81238123

8124+
Local<BigInt> v8::BigInt::NewFromUnsigned(Isolate* isolate, uint64_t value) {
8125+
CHECK(i::FLAG_harmony_bigint);
8126+
i::Isolate* internal_isolate = reinterpret_cast<i::Isolate*>(isolate);
8127+
ENTER_V8_NO_SCRIPT_NO_EXCEPTION(internal_isolate);
8128+
i::Handle<i::BigInt> result = i::BigInt::FromUint64(internal_isolate, value);
8129+
return Utils::ToLocal(result);
8130+
}
8131+
8132+
MaybeLocal<BigInt> v8::BigInt::NewFromWords(Local<Context> context,
8133+
int sign_bit, int word_count,
8134+
const uint64_t* words) {
8135+
CHECK(i::FLAG_harmony_bigint);
8136+
i::Isolate* isolate = reinterpret_cast<i::Isolate*>(context->GetIsolate());
8137+
ENTER_V8_NO_SCRIPT(isolate, context, BigInt, NewFromWords,
8138+
MaybeLocal<BigInt>(), InternalEscapableScope);
8139+
i::MaybeHandle<i::BigInt> result =
8140+
i::BigInt::FromWords64(isolate, sign_bit, word_count, words);
8141+
has_pending_exception = result.is_null();
8142+
RETURN_ON_FAILED_EXECUTION(BigInt);
8143+
RETURN_ESCAPED(Utils::ToLocal(result.ToHandleChecked()));
8144+
}
8145+
8146+
uint64_t v8::BigInt::Uint64Value(bool* lossless) const {
8147+
i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
8148+
return handle->AsUint64(lossless);
8149+
}
8150+
8151+
int64_t v8::BigInt::Int64Value(bool* lossless) const {
8152+
i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
8153+
return handle->AsInt64(lossless);
8154+
}
8155+
8156+
int BigInt::WordCount() const {
8157+
i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
8158+
return handle->Words64Count();
8159+
}
8160+
8161+
void BigInt::ToWordsArray(int* sign_bit, int* word_count,
8162+
uint64_t* words) const {
8163+
i::Handle<i::BigInt> handle = Utils::OpenHandle(this);
8164+
return handle->ToWordsArray64(sign_bit, word_count, words);
8165+
}
8166+
81248167
void Isolate::ReportExternalAllocationLimitReached() {
81258168
i::Heap* heap = reinterpret_cast<i::Isolate*>(this)->heap();
81268169
if (heap->gc_state() != i::Heap::NOT_IN_GC) return;

deps/v8/src/api.h

+1
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ class RegisteredExtension {
114114
V(Promise, JSPromise) \
115115
V(Primitive, Object) \
116116
V(PrimitiveArray, FixedArray) \
117+
V(BigInt, BigInt) \
117118
V(ScriptOrModule, Script)
118119

119120
class Utils {

deps/v8/src/counters.h

+1
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,7 @@ class RuntimeCallTimer final {
692692
V(ArrayBuffer_New) \
693693
V(Array_CloneElementAt) \
694694
V(Array_New) \
695+
V(BigInt_NewFromWords) \
695696
V(BigInt64Array_New) \
696697
V(BigUint64Array_New) \
697698
V(BigIntObject_New) \

deps/v8/src/objects/bigint.cc

+64
Original file line numberDiff line numberDiff line change
@@ -2205,6 +2205,70 @@ Handle<BigInt> BigInt::FromUint64(Isolate* isolate, uint64_t n) {
22052205
return MutableBigInt::MakeImmutable(result);
22062206
}
22072207

2208+
MaybeHandle<BigInt> BigInt::FromWords64(Isolate* isolate, int sign_bit,
2209+
int words64_count,
2210+
const uint64_t* words) {
2211+
if (words64_count < 0 || words64_count > kMaxLength / (64 / kDigitBits)) {
2212+
THROW_NEW_ERROR(isolate, NewRangeError(MessageTemplate::kBigIntTooBig),
2213+
BigInt);
2214+
}
2215+
if (words64_count == 0) return MutableBigInt::Zero(isolate);
2216+
STATIC_ASSERT(kDigitBits == 64 || kDigitBits == 32);
2217+
int length = (64 / kDigitBits) * words64_count;
2218+
DCHECK_GT(length, 0);
2219+
if (kDigitBits == 32 && words[words64_count - 1] <= (1ULL << 32)) length--;
2220+
2221+
Handle<MutableBigInt> result;
2222+
if (!MutableBigInt::New(isolate, length).ToHandle(&result)) {
2223+
return MaybeHandle<BigInt>();
2224+
}
2225+
2226+
result->set_sign(sign_bit);
2227+
if (kDigitBits == 64) {
2228+
for (int i = 0; i < length; ++i) {
2229+
result->set_digit(i, static_cast<digit_t>(words[i]));
2230+
}
2231+
} else {
2232+
for (int i = 0; i < length; i += 2) {
2233+
digit_t lo = static_cast<digit_t>(words[i / 2]);
2234+
digit_t hi = static_cast<digit_t>(words[i / 2] >> 32);
2235+
result->set_digit(i, lo);
2236+
if (i + 1 < length) result->set_digit(i + 1, hi);
2237+
}
2238+
}
2239+
2240+
return MutableBigInt::MakeImmutable(result);
2241+
}
2242+
2243+
int BigInt::Words64Count() {
2244+
STATIC_ASSERT(kDigitBits == 64 || kDigitBits == 32);
2245+
return length() / (64 / kDigitBits) +
2246+
(kDigitBits == 32 && length() % 2 == 1 ? 1 : 0);
2247+
}
2248+
2249+
void BigInt::ToWordsArray64(int* sign_bit, int* words64_count,
2250+
uint64_t* words) {
2251+
DCHECK_NE(sign_bit, nullptr);
2252+
DCHECK_NE(words64_count, nullptr);
2253+
*sign_bit = sign();
2254+
int available_words = *words64_count;
2255+
*words64_count = Words64Count();
2256+
if (available_words == 0) return;
2257+
DCHECK_NE(words, nullptr);
2258+
2259+
int len = length();
2260+
if (kDigitBits == 64) {
2261+
for (int i = 0; i < len && i < available_words; ++i) words[i] = digit(i);
2262+
} else {
2263+
for (int i = 0; i < len && available_words > 0; i += 2) {
2264+
uint64_t lo = digit(i);
2265+
uint64_t hi = (i + 1) < len ? digit(i + 1) : 0;
2266+
words[i / 2] = lo | (hi << 32);
2267+
available_words--;
2268+
}
2269+
}
2270+
}
2271+
22082272
uint64_t MutableBigInt::GetRawBits(BigIntBase* x, bool* lossless) {
22092273
if (lossless != nullptr) *lossless = true;
22102274
if (x->is_zero()) return 0;

deps/v8/src/objects/bigint.h

+5
Original file line numberDiff line numberDiff line change
@@ -144,8 +144,13 @@ class V8_EXPORT_PRIVATE BigInt : public BigIntBase {
144144

145145
static Handle<BigInt> FromInt64(Isolate* isolate, int64_t n);
146146
static Handle<BigInt> FromUint64(Isolate* isolate, uint64_t n);
147+
static MaybeHandle<BigInt> FromWords64(Isolate* isolate, int sign_bit,
148+
int words64_count,
149+
const uint64_t* words);
147150
int64_t AsInt64(bool* lossless = nullptr);
148151
uint64_t AsUint64(bool* lossless = nullptr);
152+
int Words64Count();
153+
void ToWordsArray64(int* sign_bit, int* words64_count, uint64_t* words);
149154

150155
DECL_CAST(BigInt)
151156
DECL_VERIFIER(BigInt)

deps/v8/test/cctest/test-api.cc

+114
Original file line numberDiff line numberDiff line change
@@ -27613,3 +27613,117 @@ TEST(WasmStreamingAbortNoReject) {
2761327613
streaming.Abort({});
2761427614
CHECK_EQ(streaming.GetPromise()->State(), v8::Promise::kPending);
2761527615
}
27616+
27617+
TEST(BigIntAPI) {
27618+
LocalContext env;
27619+
v8::Isolate* isolate = env->GetIsolate();
27620+
v8::HandleScope scope(isolate);
27621+
bool lossless;
27622+
uint64_t words1[10];
27623+
uint64_t words2[10];
27624+
27625+
{
27626+
Local<Value> bi = CompileRun("12n");
27627+
CHECK(bi->IsBigInt());
27628+
27629+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 12);
27630+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless), 12);
27631+
CHECK_EQ(lossless, true);
27632+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), 12);
27633+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), 12);
27634+
CHECK_EQ(lossless, true);
27635+
}
27636+
27637+
{
27638+
Local<Value> bi = CompileRun("-12n");
27639+
CHECK(bi->IsBigInt());
27640+
27641+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), static_cast<uint64_t>(-12));
27642+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27643+
static_cast<uint64_t>(-12));
27644+
CHECK_EQ(lossless, false);
27645+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), -12);
27646+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), -12);
27647+
CHECK_EQ(lossless, true);
27648+
}
27649+
27650+
{
27651+
Local<Value> bi = CompileRun("123456789012345678901234567890n");
27652+
CHECK(bi->IsBigInt());
27653+
27654+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 14083847773837265618ULL);
27655+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27656+
14083847773837265618ULL);
27657+
CHECK_EQ(lossless, false);
27658+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), -4362896299872285998LL);
27659+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless),
27660+
-4362896299872285998LL);
27661+
CHECK_EQ(lossless, false);
27662+
}
27663+
27664+
{
27665+
Local<Value> bi = CompileRun("-123456789012345678901234567890n");
27666+
CHECK(bi->IsBigInt());
27667+
27668+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 4362896299872285998LL);
27669+
CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27670+
4362896299872285998LL);
27671+
CHECK_EQ(lossless, false);
27672+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), 4362896299872285998LL);
27673+
CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), 4362896299872285998LL);
27674+
CHECK_EQ(lossless, false);
27675+
}
27676+
27677+
{
27678+
Local<v8::BigInt> bi =
27679+
v8::BigInt::NewFromWords(env.local(), 0, 0, words1).ToLocalChecked();
27680+
CHECK_EQ(bi->Uint64Value(), 0);
27681+
CHECK_EQ(bi->WordCount(), 0);
27682+
}
27683+
27684+
{
27685+
TryCatch try_catch(isolate);
27686+
v8::MaybeLocal<v8::BigInt> bi = v8::BigInt::NewFromWords(
27687+
env.local(), 0, std::numeric_limits<int>::max(), words1);
27688+
CHECK(bi.IsEmpty());
27689+
CHECK(try_catch.HasCaught());
27690+
}
27691+
27692+
{
27693+
TryCatch try_catch(isolate);
27694+
v8::MaybeLocal<v8::BigInt> bi =
27695+
v8::BigInt::NewFromWords(env.local(), 0, -1, words1);
27696+
CHECK(bi.IsEmpty());
27697+
CHECK(try_catch.HasCaught());
27698+
}
27699+
27700+
{
27701+
TryCatch try_catch(isolate);
27702+
v8::MaybeLocal<v8::BigInt> bi =
27703+
v8::BigInt::NewFromWords(env.local(), 0, 1 << 30, words1);
27704+
CHECK(bi.IsEmpty());
27705+
CHECK(try_catch.HasCaught());
27706+
}
27707+
27708+
for (int sign_bit = 0; sign_bit <= 1; sign_bit++) {
27709+
words1[0] = 0xffffffff00000000ULL;
27710+
words1[1] = 0x00000000ffffffffULL;
27711+
v8::Local<v8::BigInt> bi =
27712+
v8::BigInt::NewFromWords(env.local(), sign_bit, 2, words1)
27713+
.ToLocalChecked();
27714+
CHECK_EQ(bi->Uint64Value(&lossless),
27715+
sign_bit ? static_cast<uint64_t>(-static_cast<int64_t>(words1[0]))
27716+
: words1[0]);
27717+
CHECK_EQ(lossless, false);
27718+
CHECK_EQ(bi->Int64Value(&lossless), sign_bit
27719+
? -static_cast<int64_t>(words1[0])
27720+
: static_cast<int64_t>(words1[0]));
27721+
CHECK_EQ(lossless, false);
27722+
CHECK_EQ(bi->WordCount(), 2);
27723+
int real_sign_bit;
27724+
int word_count = arraysize(words2);
27725+
bi->ToWordsArray(&real_sign_bit, &word_count, words2);
27726+
CHECK_EQ(real_sign_bit, sign_bit);
27727+
CHECK_EQ(word_count, 2);
27728+
}
27729+
}

0 commit comments

Comments
 (0)