From 601ab7e29a242d9be951790b36a010f6c139acdf Mon Sep 17 00:00:00 2001 From: Aleksandr Veselov Date: Mon, 10 Feb 2025 14:50:48 +0300 Subject: [PATCH 1/3] Add i64 and u64 to interop types (NAPI) --- interop/src/cpp/napi/convertors-napi.cc | 41 +++++++++++++++++++------ interop/src/cpp/napi/convertors-napi.h | 32 +++++++++++++------ interop/src/cpp/types/koala-types.h | 1 + 3 files changed, 54 insertions(+), 20 deletions(-) diff --git a/interop/src/cpp/napi/convertors-napi.cc b/interop/src/cpp/napi/convertors-napi.cc index 725853dd5..7fa237507 100644 --- a/interop/src/cpp/napi/convertors-napi.cc +++ b/interop/src/cpp/napi/convertors-napi.cc @@ -12,6 +12,7 @@ * See the License for the specific language governing permissions and * limitations under the License. */ +#include #include #include #include @@ -152,17 +153,21 @@ KLong getInt64(napi_env env, napi_value value) { return static_cast(ptr64); } -// napi_value getObject(napi_env env, napi_value value) { -// if (getValueTypeChecked(env, value) != napi_valuetype::napi_object) { -// Napi::Error::New(env, "Expected Object") -// .ThrowAsJavaScriptException(); -// napi_value result; -// napi_status status = napi_get_global(env, &result); -// KOALA_NAPI_THROW_IF_FAILED(env, status, result); -// } +KULong getUInt64(napi_env env, napi_value value) { + if (getValueTypeChecked(env, value) != napi_valuetype::napi_bigint) { + napi_throw_error(env, nullptr, "cannot be coerced to uint64"); + return -1; + } -// return value; -// } + bool isWithinRange = true; + uint64_t ptr64 = 0; + napi_get_value_bigint_uint64(env, value, &ptr64, &isWithinRange); + if (!isWithinRange) { + napi_throw_error(env, nullptr, "cannot be coerced to uint64, value is too large"); + return -1; + } + return static_cast(ptr64); +} napi_value makeString(napi_env env, const KStringPtr& value) { napi_value result; @@ -204,6 +209,22 @@ napi_value makeUInt32(napi_env env, uint32_t value) { return result; } +napi_value makeInt64(napi_env env, int64_t value) { + napi_value result; + napi_status status; + status = napi_create_bigint_int64(env, value, &result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); + return result; +} + +napi_value makeUInt64(napi_env env, uint64_t value) { + napi_value result; + napi_status status; + status = napi_create_bigint_uint64(env, value, &result); + KOALA_NAPI_THROW_IF_FAILED(env, status, result); + return result; +} + napi_value makeFloat32(napi_env env, float value) { napi_value result; napi_status status; diff --git a/interop/src/cpp/napi/convertors-napi.h b/interop/src/cpp/napi/convertors-napi.h index aa4354c3c..29adb1265 100644 --- a/interop/src/cpp/napi/convertors-napi.h +++ b/interop/src/cpp/napi/convertors-napi.h @@ -393,6 +393,11 @@ inline void* getPointer(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, nullptr); return getPointer(info.Env(), info[index]); } +KULong getUInt64(napi_env env, napi_value value); +inline KULong getUInt64(const CallbackInfo& info, int index) { + NAPI_ASSERT_INDEX(info, index, 0); + return getUInt64(info.Env(), info[index]); +} KLong getInt64(napi_env env, napi_value value); inline KLong getInt64(const CallbackInfo& info, int index) { NAPI_ASSERT_INDEX(info, index, 0); @@ -503,6 +508,11 @@ inline KLong getArgument(const CallbackInfo& info, int index) { return getInt64(info, index); } +template <> +inline KULong getArgument(const CallbackInfo& info, int index) { + return getUInt64(info, index); +} + template <> inline KNativePointerArray getArgument(const CallbackInfo& info, int index) { return getPointerElements(info, index); @@ -563,6 +573,8 @@ napi_value makeString(napi_env env, const std::string& value); napi_value makeBoolean(napi_env env, KBoolean value); napi_value makeInt32(napi_env env, int32_t value); napi_value makeUInt32(napi_env env, uint32_t value); +napi_value makeInt64(napi_env env, int32_t value); +napi_value makeUInt64(napi_env env, uint32_t value); napi_value makeFloat32(napi_env env, float value); napi_value makePointer(napi_env env, void* value); napi_value makeVoid(napi_env env); @@ -590,6 +602,16 @@ inline napi_value makeResult(const CallbackInfo& info, uint32_t value) return makeUInt32(info.Env(), value); } +template <> +inline napi_value makeResult(const CallbackInfo& info, int64_t value) { + return makeInt64(info.Env(), value); +} + +template <> +inline napi_value makeResult(const CallbackInfo& info, uint64_t value) { + return makeUInt64(info.Env(), value); +} + template <> inline napi_value makeResult(const CallbackInfo& info, float value) { return makeFloat32(info.Env(), value); @@ -600,16 +622,6 @@ inline napi_value makeResult(const CallbackInfo& info, KNativePo return makePointer(info.Env(), value); } -// template <> -// inline napi_value makeResult(const CallbackInfo& info, napi_value value) { -// return value; -// } - -// template <> -// inline napi_value makeResult(const CallbackInfo& info, napi_value value) { -// return makeObject(info, value); -// } - template <> inline napi_value makeResult(const CallbackInfo& info, KVMObjectHandle value) { return InteropTypeConverter::convertTo(info.Env(), value); diff --git a/interop/src/cpp/types/koala-types.h b/interop/src/cpp/types/koala-types.h index 8b0e86f8a..44beaceee 100644 --- a/interop/src/cpp/types/koala-types.h +++ b/interop/src/cpp/types/koala-types.h @@ -124,6 +124,7 @@ typedef int32_t KInt; typedef uint32_t KUInt; typedef float KFloat; typedef int64_t KLong; +typedef uint64_t KULong; typedef double KDouble; typedef void* KNativePointer; typedef KStringPtrImpl KStringPtr; -- Gitee From fa23c7c3c8354089b3c3c4baca1602c15df68084 Mon Sep 17 00:00:00 2001 From: Aleksandr Veselov Date: Mon, 10 Feb 2025 15:03:53 +0300 Subject: [PATCH 2/3] Add serialization and deserialization for u64 --- interop/src/cpp/DeserializerBase.h | 12 ++++++++++++ interop/src/cpp/SerializerBase.h | 10 ++++++++++ interop/src/cpp/interop-types.h | 1 + 3 files changed, 23 insertions(+) diff --git a/interop/src/cpp/DeserializerBase.h b/interop/src/cpp/DeserializerBase.h index 87fe3fc39..16d49ffe7 100644 --- a/interop/src/cpp/DeserializerBase.h +++ b/interop/src/cpp/DeserializerBase.h @@ -412,6 +412,18 @@ public: memcpy(&value, data + position, 4); #else auto value = *(InteropInt64 *)(data + position); +#endif + position += 8; + return value; + } + InteropUInt64 readUInt64() + { + check(8); +#ifdef KOALA_NO_UNALIGNED_ACCESS + InteropInt64 value; + memcpy(&value, data + position, 4); +#else + auto value = *(InteropUInt64 *)(data + position); #endif position += 8; return value; diff --git a/interop/src/cpp/SerializerBase.h b/interop/src/cpp/SerializerBase.h index 8d6381466..d631a0a2a 100644 --- a/interop/src/cpp/SerializerBase.h +++ b/interop/src/cpp/SerializerBase.h @@ -140,6 +140,16 @@ public: position += 8; } + void writeUInt64(InteropUInt64 value) { + check(8); +#ifdef KOALA_NO_UNALIGNED_ACCESS + memcpy(data + position, &value, 8); +#else + *((InteropUInt64*)(data + position)) = value; +#endif + position += 8; + } + void writeFloat32(InteropFloat32 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS diff --git a/interop/src/cpp/interop-types.h b/interop/src/cpp/interop-types.h index b27e3dc18..6c23272a4 100644 --- a/interop/src/cpp/interop-types.h +++ b/interop/src/cpp/interop-types.h @@ -35,6 +35,7 @@ typedef double InteropFloat64; typedef int32_t InteropInt32; typedef unsigned int InteropUInt32; // TODO: update unsigned int typedef int64_t InteropInt64; +typedef uint64_t InteropUInt64; typedef int8_t InteropInt8; typedef uint8_t InteropUInt8; typedef int64_t InteropDate; -- Gitee From fe08f45a895d9059fdffe7933b752ec079dcebe8 Mon Sep 17 00:00:00 2001 From: Aleksandr Veselov Date: Tue, 11 Feb 2025 12:18:30 +0300 Subject: [PATCH 3/3] Fix generated code --- arkoala/framework/native/src/generated/arkoala_api_generated.h | 1 + 1 file changed, 1 insertion(+) diff --git a/arkoala/framework/native/src/generated/arkoala_api_generated.h b/arkoala/framework/native/src/generated/arkoala_api_generated.h index bfb132d27..711c23251 100644 --- a/arkoala/framework/native/src/generated/arkoala_api_generated.h +++ b/arkoala/framework/native/src/generated/arkoala_api_generated.h @@ -58,6 +58,7 @@ typedef double InteropFloat64; typedef int32_t InteropInt32; typedef unsigned int InteropUInt32; // TODO: update unsigned int typedef int64_t InteropInt64; +typedef uint64_t InteropUInt64; typedef int8_t InteropInt8; typedef uint8_t InteropUInt8; typedef int64_t InteropDate; -- Gitee