From 05d91522ceb37e5d52cb71616855fef79f7510cd Mon Sep 17 00:00:00 2001 From: xLuxy <67131061+xLuxy@users.noreply.github.com> Date: Thu, 15 Feb 2024 02:18:52 +0100 Subject: [PATCH] fix(shared): Fix BigInts not being parsed properly --- server/src/classes/Metric.cpp | 4 ++-- shared/src/helpers/Convert.h | 26 ++++++++++++++++++++------ 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/server/src/classes/Metric.cpp b/server/src/classes/Metric.cpp index d6cea5318..2600f9303 100644 --- a/server/src/classes/Metric.cpp +++ b/server/src/classes/Metric.cpp @@ -38,7 +38,7 @@ static void ValueSetter(js::PropertyContext& ctx) if(!ctx.CheckExtraInternalFieldValue()) return; alt::Metric* metric = ctx.GetExtraInternalFieldValue(); - uint32_t value; + uint64_t value; if(!ctx.GetValue(value)) return; metric->SetValue(value); @@ -65,7 +65,7 @@ static void Add(js::FunctionContext& ctx) if(!ctx.CheckExtraInternalFieldValue()) return; alt::Metric* metric = ctx.GetExtraInternalFieldValue(); - uint32_t value; + uint64_t value; if(!ctx.GetArg(0, value)) return; metric->Add(value); diff --git a/shared/src/helpers/Convert.h b/shared/src/helpers/Convert.h index 4bbba2bd6..d077054f5 100644 --- a/shared/src/helpers/Convert.h +++ b/shared/src/helpers/Convert.h @@ -311,12 +311,26 @@ namespace js else if constexpr(std::is_same_v || std::is_same_v) { constexpr bool isSigned = std::is_same_v; - v8::Local bigInt; - v8::MaybeLocal maybeBigInt = val->ToBigInt(v8::Isolate::GetCurrent()->GetEnteredOrMicrotaskContext()); - if(!maybeBigInt.ToLocal(&bigInt)) return std::nullopt; - if constexpr(isSigned) return bigInt->Int64Value(); - else - return bigInt->Uint64Value(); + + if (val->IsBigInt()) + { + v8::Local bigInt; + v8::MaybeLocal maybeBigInt = val->ToBigInt(v8::Isolate::GetCurrent()->GetEnteredOrMicrotaskContext()); + if(!maybeBigInt.ToLocal(&bigInt)) return std::nullopt; + + if constexpr(isSigned) + return bigInt->Int64Value(); + else + return bigInt->Uint64Value(); + } + + using Type = std::conditional_t; + const auto _val = CppValue(val); + + if (_val.has_value()) + { + return static_cast(_val.value()); + } } else if constexpr(std::is_integral_v || std::is_floating_point_v || std::is_enum_v) {