From ff90b81ec3c0e79ce13bf616c5362d9c14d0f09e Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 24 Jun 2023 10:38:35 -0500 Subject: [PATCH] Add some utilitites for dealing with unsigned integers in janet.h --- src/core/capi.c | 10 +++++++++- src/core/util.c | 9 ++++++++- src/core/vm.c | 8 ++++---- src/include/janet.h | 3 +++ 4 files changed, 24 insertions(+), 6 deletions(-) diff --git a/src/core/capi.c b/src/core/capi.c index 6984d3ad..c1109c5f 100644 --- a/src/core/capi.c +++ b/src/core/capi.c @@ -273,6 +273,14 @@ int32_t janet_getinteger(const Janet *argv, int32_t n) { return janet_unwrap_integer(x); } +uint32_t janet_getuinteger(const Janet *argv, int32_t n) { + Janet x = argv[n]; + if (!janet_checkuint(x)) { + janet_panicf("bad slot #%d, expected 32 bit signed integer, got %v", n, x); + } + return janet_unwrap_integer(x); +} + int64_t janet_getinteger64(const Janet *argv, int32_t n) { #ifdef JANET_INT_TYPES return janet_unwrap_s64(argv[n]); @@ -290,7 +298,7 @@ uint64_t janet_getuinteger64(const Janet *argv, int32_t n) { return janet_unwrap_u64(argv[n]); #else Janet x = argv[n]; - if (!janet_checkint64(x)) { + if (!janet_checkuint64(x)) { janet_panicf("bad slot #%d, expected 64 bit unsigned integer, got %v", n, x); } return (uint64_t) janet_unwrap_number(x); diff --git a/src/core/util.c b/src/core/util.c index c0e3e564..2dfd11e3 100644 --- a/src/core/util.c +++ b/src/core/util.c @@ -805,6 +805,13 @@ int janet_checkint(Janet x) { return janet_checkintrange(dval); } +int janet_checkuint(Janet x) { + if (!janet_checktype(x, JANET_NUMBER)) + return 0; + double dval = janet_unwrap_number(x); + return janet_checkuintrange(dval); +} + int janet_checkint64(Janet x) { if (!janet_checktype(x, JANET_NUMBER)) return 0; @@ -816,7 +823,7 @@ int janet_checkuint64(Janet x) { if (!janet_checktype(x, JANET_NUMBER)) return 0; double dval = janet_unwrap_number(x); - return dval >= 0 && dval <= JANET_INTMAX_DOUBLE && dval == (uint64_t) dval; + return janet_checkuint64range(dval); } int janet_checksize(Janet x) { diff --git a/src/core/vm.c b/src/core/vm.c index ab73499d..dd8b7e57 100644 --- a/src/core/vm.c +++ b/src/core/vm.c @@ -147,8 +147,8 @@ stack[A] = janet_mcall(#op, 2, _argv);\ vm_checkgc_pcnext();\ } else {\ - type1 x1 = (type1) janet_unwrap_integer(op1);\ - stack[A] = janet_wrap_integer(x1 op CS);\ + type1 x1 = (type1) janet_unwrap_number(op1);\ + stack[A] = janet_wrap_number((type1) (x1 op CS));\ vm_pcnext();\ }\ } @@ -175,9 +175,9 @@ Janet op1 = stack[B];\ Janet op2 = stack[C];\ if (janet_checktype(op1, JANET_NUMBER) && janet_checktype(op2, JANET_NUMBER)) {\ - type1 x1 = (type1) janet_unwrap_integer(op1);\ + type1 x1 = (type1) janet_unwrap_number(op1);\ int32_t x2 = janet_unwrap_integer(op2);\ - stack[A] = janet_wrap_integer(x1 op x2);\ + stack[A] = janet_wrap_number((type1) (x1 op x2));\ vm_pcnext();\ } else {\ vm_commit();\ diff --git a/src/include/janet.h b/src/include/janet.h index 509b194e..3f6b8982 100644 --- a/src/include/janet.h +++ b/src/include/janet.h @@ -868,12 +868,15 @@ JANET_API Janet janet_nanbox32_from_tagp(uint32_t tag, void *pointer); #endif JANET_API int janet_checkint(Janet x); +JANET_API int janet_checkuint(Janet x); JANET_API int janet_checkint64(Janet x); JANET_API int janet_checkuint64(Janet x); JANET_API int janet_checksize(Janet x); JANET_API JanetAbstract janet_checkabstract(Janet x, const JanetAbstractType *at); #define janet_checkintrange(x) ((x) >= INT32_MIN && (x) <= INT32_MAX && (x) == (int32_t)(x)) +#define janet_checkuintrange(x) ((x) >= 0 && (x) <= UINT32_MAX && (x) == (uint32_t)(x)) #define janet_checkint64range(x) ((x) >= JANET_INTMIN_DOUBLE && (x) <= JANET_INTMAX_DOUBLE && (x) == (int64_t)(x)) +#define janet_checkuint64range(x) ((x) >= 0 && (x) <= JANET_INTMAX_DOUBLE && (x) == (uint64_t)(x)) #define janet_unwrap_integer(x) ((int32_t) janet_unwrap_number(x)) #define janet_wrap_integer(x) janet_wrap_number((int32_t)(x))