@@ -27613,3 +27613,117 @@ TEST(WasmStreamingAbortNoReject) {
27613
27613
streaming.Abort({});
27614
27614
CHECK_EQ(streaming.GetPromise()->State(), v8::Promise::kPending);
27615
27615
}
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