summaryrefslogtreecommitdiff
path: root/gi/arg-inl.h
diff options
context:
space:
mode:
authorMarco Trevisan (TreviƱo) <mail@3v1n0.net>2020-10-08 04:25:26 +0200
committerPhilip Chimento <philip.chimento@gmail.com>2022-02-03 22:07:09 -0800
commit20da419bf09d0d73b4b08d58cbe2da9f22b13677 (patch)
tree4299b6fb0c15faa0349bdd63ca810dcbe3e9a262 /gi/arg-inl.h
parent9454bc99a7b5aab0c2590ac3c22224602c8fe61e (diff)
downloadgjs-20da419bf09d0d73b4b08d58cbe2da9f22b13677.tar.gz
value: Support converting BigInt to (u)int64 values
BigInt values are now accepted as parameters to C functions that take 64-bit integers. For now BigInts are only accepted for 64-bit parameters. We may allow BigInt for any parameter in the future (once we've checked that we don't go out-of-range), for now we assume that BigInt values are used only for functions taking a type that can mostly handle them. For bounds checks, given that we don't have a bigger value holder than (u)int64_t, we have to create a new wrapper to avoid taking the fast path in gjs_arg_set_from_js_value(), and without having to hack type_has_js_getter(). As per this, now for 64bit integers we use TypeWrapper as the type holder that has conversion operators to the base type, and for which we can then add a js_value_to_c_checked() specialization. See: #271
Diffstat (limited to 'gi/arg-inl.h')
-rw-r--r--gi/arg-inl.h17
1 files changed, 2 insertions, 15 deletions
diff --git a/gi/arg-inl.h b/gi/arg-inl.h
index 19de766b..5fb19e3b 100644
--- a/gi/arg-inl.h
+++ b/gi/arg-inl.h
@@ -174,19 +174,6 @@ template <typename T, GITypeTag TAG = GI_TYPE_TAG_VOID>
// Implementation to store rounded (u)int64_t numbers into double
template <typename BigT>
-[[nodiscard]] inline constexpr BigT max_safe_big_number() {
- return (BigT(1) << std::numeric_limits<double>::digits) - 1;
-}
-
-template <typename BigT>
-[[nodiscard]] inline constexpr BigT min_safe_big_number() {
- if constexpr (std::is_signed_v<BigT>)
- return -(max_safe_big_number<BigT>());
-
- return std::numeric_limits<BigT>::lowest();
-}
-
-template <typename BigT>
[[nodiscard]] inline std::enable_if_t<std::is_integral_v<BigT> &&
(std::numeric_limits<BigT>::max() >
std::numeric_limits<int32_t>::max()),
@@ -194,8 +181,8 @@ template <typename BigT>
gjs_arg_get_maybe_rounded(GIArgument* arg) {
BigT val = gjs_arg_get<BigT>(arg);
- if (val < min_safe_big_number<BigT>() ||
- val > max_safe_big_number<BigT>()) {
+ if (val < Gjs::min_safe_big_number<BigT>() ||
+ val > Gjs::max_safe_big_number<BigT>()) {
g_warning(
"Value %s cannot be safely stored in a JS Number "
"and may be rounded",