@@ -27805,3 +27805,117 @@ TEST(WasmStreamingAbortNoReject) {
27805
27805
streaming.Abort({});
27806
27806
CHECK_EQ(streaming.GetPromise()->State(), v8::Promise::kPending);
27807
27807
}
27808
+
27809
+ TEST(BigIntAPI) {
27810
+ LocalContext env;
27811
+ v8::Isolate* isolate = env->GetIsolate();
27812
+ v8::HandleScope scope(isolate);
27813
+ bool lossless;
27814
+ uint64_t words1[10];
27815
+ uint64_t words2[10];
27816
+
27817
+ {
27818
+ Local<Value> bi = CompileRun("12n");
27819
+ CHECK(bi->IsBigInt());
27820
+
27821
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 12);
27822
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless), 12);
27823
+ CHECK_EQ(lossless, true);
27824
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), 12);
27825
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), 12);
27826
+ CHECK_EQ(lossless, true);
27827
+ }
27828
+
27829
+ {
27830
+ Local<Value> bi = CompileRun("-12n");
27831
+ CHECK(bi->IsBigInt());
27832
+
27833
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), static_cast<uint64_t>(-12));
27834
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27835
+ static_cast<uint64_t>(-12));
27836
+ CHECK_EQ(lossless, false);
27837
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), -12);
27838
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), -12);
27839
+ CHECK_EQ(lossless, true);
27840
+ }
27841
+
27842
+ {
27843
+ Local<Value> bi = CompileRun("123456789012345678901234567890n");
27844
+ CHECK(bi->IsBigInt());
27845
+
27846
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 14083847773837265618ULL);
27847
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27848
+ 14083847773837265618ULL);
27849
+ CHECK_EQ(lossless, false);
27850
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), -4362896299872285998LL);
27851
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless),
27852
+ -4362896299872285998LL);
27853
+ CHECK_EQ(lossless, false);
27854
+ }
27855
+
27856
+ {
27857
+ Local<Value> bi = CompileRun("-123456789012345678901234567890n");
27858
+ CHECK(bi->IsBigInt());
27859
+
27860
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(), 4362896299872285998LL);
27861
+ CHECK_EQ(bi.As<v8::BigInt>()->Uint64Value(&lossless),
27862
+ 4362896299872285998LL);
27863
+ CHECK_EQ(lossless, false);
27864
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(), 4362896299872285998LL);
27865
+ CHECK_EQ(bi.As<v8::BigInt>()->Int64Value(&lossless), 4362896299872285998LL);
27866
+ CHECK_EQ(lossless, false);
27867
+ }
27868
+
27869
+ {
27870
+ Local<v8::BigInt> bi =
27871
+ v8::BigInt::NewFromWords(env.local(), 0, 0, words1).ToLocalChecked();
27872
+ CHECK_EQ(bi->Uint64Value(), 0);
27873
+ CHECK_EQ(bi->WordCount(), 0);
27874
+ }
27875
+
27876
+ {
27877
+ TryCatch try_catch(isolate);
27878
+ v8::MaybeLocal<v8::BigInt> bi = v8::BigInt::NewFromWords(
27879
+ env.local(), 0, std::numeric_limits<int>::max(), words1);
27880
+ CHECK(bi.IsEmpty());
27881
+ CHECK(try_catch.HasCaught());
27882
+ }
27883
+
27884
+ {
27885
+ TryCatch try_catch(isolate);
27886
+ v8::MaybeLocal<v8::BigInt> bi =
27887
+ v8::BigInt::NewFromWords(env.local(), 0, -1, words1);
27888
+ CHECK(bi.IsEmpty());
27889
+ CHECK(try_catch.HasCaught());
27890
+ }
27891
+
27892
+ {
27893
+ TryCatch try_catch(isolate);
27894
+ v8::MaybeLocal<v8::BigInt> bi =
27895
+ v8::BigInt::NewFromWords(env.local(), 0, 1 << 30, words1);
27896
+ CHECK(bi.IsEmpty());
27897
+ CHECK(try_catch.HasCaught());
27898
+ }
27899
+
27900
+ for (int sign_bit = 0; sign_bit <= 1; sign_bit++) {
27901
+ words1[0] = 0xffffffff00000000ULL;
27902
+ words1[1] = 0x00000000ffffffffULL;
27903
+ v8::Local<v8::BigInt> bi =
27904
+ v8::BigInt::NewFromWords(env.local(), sign_bit, 2, words1)
27905
+ .ToLocalChecked();
27906
+ CHECK_EQ(bi->Uint64Value(&lossless),
27907
+ sign_bit ? static_cast<uint64_t>(-static_cast<int64_t>(words1[0]))
27908
+ : words1[0]);
27909
+ CHECK_EQ(lossless, false);
27910
+ CHECK_EQ(bi->Int64Value(&lossless), sign_bit
27911
+ ? -static_cast<int64_t>(words1[0])
27912
+ : static_cast<int64_t>(words1[0]));
27913
+ CHECK_EQ(lossless, false);
27914
+ CHECK_EQ(bi->WordCount(), 2);
27915
+ int real_sign_bit;
27916
+ int word_count = arraysize(words2);
27917
+ bi->ToWordsArray(&real_sign_bit, &word_count, words2);
27918
+ CHECK_EQ(real_sign_bit, sign_bit);
27919
+ CHECK_EQ(word_count, 2);
27920
+ }
27921
+ }
0 commit comments