Skip to content

Commit

Permalink
Add more int converters
Browse files Browse the repository at this point in the history
  • Loading branch information
zcbenz committed Apr 12, 2024
1 parent e6aa218 commit 882f9c2
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ struct Type<int64_t> {
static inline napi_status ToNode(napi_env env,
int64_t value,
napi_value* result) {
return napi_create_bigint_int64(env, value, result);
return napi_create_int64(env, value, result);
}
static inline std::optional<int64_t> FromNode(napi_env env,
napi_value value) {
int64_t result;
bool lossless;
if (napi_get_value_bigint_int64(env, value, &result, &lossless) == napi_ok)
if (napi_get_value_int64(env, value, &result) == napi_ok)
return result;
return std::nullopt;
}
Expand All @@ -168,6 +167,16 @@ struct Type<uint64_t> {
}
};

template<>
struct Type<size_t> {
static constexpr const char* name = "Integer";
static inline napi_status ToNode(napi_env env,
size_t value,
napi_value* result) {
return napi_create_int64(env, value, result);
}
};

template<>
struct Type<float> {
static constexpr const char* name = "Number";
Expand Down Expand Up @@ -451,6 +460,13 @@ inline napi_value ToNode(napi_env env, T&& value) {
return ConvertIgnoringStatus<std::decay_t<T>>(env, std::forward<T>(value));
}

template<typename T>
inline napi_value ToNode(napi_env env, std::optional<T>&& value) {
if (!value)
return Undefined(env);
return ToNode(env, *value);
}

template<size_t n>
inline napi_value ToNode(napi_env env, const char (&value)[n]) {
return ConvertIgnoringStatus<char[n]>(env, value);
Expand Down

0 comments on commit 882f9c2

Please sign in to comment.