From ab2cdae714eb288bab9391b74ca3d2415c89fe7c Mon Sep 17 00:00:00 2001 From: Keerecles Date: Thu, 12 Jun 2025 09:53:34 +0800 Subject: [PATCH 1/6] code clean: assert Signed-off-by: Keerecles Change-Id: I9b146e27c08f6a46d636f25802a1727203389506 --- .../interop/src/cpp/DeserializerBase.h | 3 +- .../koalaui/interop/src/cpp/SerializerBase.h | 11 +++-- .../koalaui/interop/src/cpp/interop-logging.h | 17 +++++-- .../interop/src/cpp/jsc/convertors-jsc.cc | 23 +++++----- .../interop/src/cpp/jsc/convertors-jsc.h | 44 ++++++++++--------- .../interop/src/cpp/wasm/convertors-wasm.h | 12 +++-- 6 files changed, 63 insertions(+), 47 deletions(-) diff --git a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h index e09c40524..669d16f9f 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h @@ -16,7 +16,6 @@ #define _DESERIALIZER_BASE_H_ #include -#include #include #include #include @@ -343,7 +342,7 @@ public: { if (position + count > length) { fprintf(stderr, "Incorrect serialized data, check for %d, buffer %d position %d\n", count, length, position); - assert(false); + ASSERT(false); abort(); } } diff --git a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h index f6cce0fae..f99efca14 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h @@ -21,13 +21,13 @@ #include #include #include -#include #include #include #include "callback-resource.h" #include "interop-types.h" #include "koala-types.h" +#include "interop-logging.h" #ifdef __arm__ #define KOALA_NO_UNALIGNED_ACCESS 1 @@ -55,7 +55,7 @@ T* allocArray(const std::array& ref) { std::size_t space = sizeof(buffer) - offset; void* ptr = buffer + offset; void* aligned_ptr = std::align(alignof(T), sizeof(T) * size, ptr, space); - assert(aligned_ptr != nullptr && "Insufficient space or alignment failed!"); + ASSERT(aligned_ptr != nullptr && "Insufficient space or alignment failed!"); offset = (char*)aligned_ptr + sizeof(T) * size - buffer; T* array = reinterpret_cast(aligned_ptr); for (size_t i = 0; i < size; ++i) { @@ -72,8 +72,8 @@ private: bool ownData; CallbackResourceHolder* resourceHolder; void resize(uint32_t newLength) { - assert(ownData); - assert(newLength > dataLength); + ASSERT(ownData); + ASSERT(newLength > dataLength); auto* newData = reinterpret_cast(malloc(newLength)); memcpy(newData, data, position); free(data); @@ -112,8 +112,7 @@ public: if (ownData) { resize(dataLength * 3 / 2 + 2); } else { - fprintf(stderr, "Buffer overrun: %d > %d\n", position + more, dataLength); - assert(false); + INTEROP_FATAL("Buffer overrun: %d > %d\n", position + more, dataLength); } } } diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.h b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.h index 4d1b7bc0f..493799010 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.h +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.h @@ -15,8 +15,15 @@ #ifndef _INTEROP_LGGING_H #define _INTEROP_LGGING_H -#include -#include +#ifdef __cplusplus + #include + #include + #include +#else + #include + #include + #include +#endif #if defined(KOALA_OHOS) #include "oh_sk_log.h" @@ -37,6 +44,10 @@ #define INTEROP_API_EXPORT __attribute__((visibility("default"))) #endif +#ifndef ASSERT + #define ASSERT(expression) assert(expression) +#endif + // Grouped logs. Keep consistent with type in ServiceGroupLogger typedef struct GroupLogger { void (*startGroupedLog)(int kind); @@ -46,6 +57,6 @@ typedef struct GroupLogger { int (*needGroupedLog)(int kind); } GroupLogger; -const GroupLogger* GetDefaultLogger(); +extern "C" INTEROP_API_EXPORT const GroupLogger* GetDefaultLogger(); #endif // _INTEROP_LOGGING_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc index b1edbde65..1ae964f27 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc @@ -16,11 +16,12 @@ #include #include <_types/_uint32_t.h> #include <_types/_uint8_t.h> -#include #include #include "convertors-jsc.h" +#include "interop-logging.h" + // See https://github.com/BabylonJS/BabylonNative/blob/master/Dependencies/napi/napi-direct/source/js_native_api_javascriptcore.cc // for convertors logic. @@ -59,7 +60,7 @@ int32_t getInt32(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); @@ -72,7 +73,7 @@ uint32_t getUInt32(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); @@ -85,7 +86,7 @@ uint8_t getUInt8(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); @@ -158,10 +159,10 @@ static uint64_t bigIntToU64(JSContextRef ctx, JSValueRef value) { JSStringRef strRef = JSValueToStringCopy(ctx, value, nullptr); size_t len = JSStringGetUTF8CString(strRef, buf, sizeof(buf)); JSStringRelease(strRef); - assert(len < sizeof(buf)); + ASSERT(len < sizeof(buf)); char* suf; uint64_t numValue = std::strtoull(buf, &suf, 10); - assert(*suf == '\0'); + ASSERT(*suf == '\0'); return numValue; } @@ -175,8 +176,8 @@ KNativePointerArray getPointerElements(JSContextRef context, JSValueRef value) { return nullptr; } - assert(JSValueIsObject(context, value)); - assert(JSValueGetTypedArrayType(context, value, nullptr) == kJSTypedArrayTypeBigUint64Array); + ASSERT(JSValueIsObject(context, value)); + ASSERT(JSValueGetTypedArrayType(context, value, nullptr) == kJSTypedArrayTypeBigUint64Array); JSObjectRef typedArray = JSValueToObject(context, value, nullptr); return reinterpret_cast(JSObjectGetTypedArrayBytesPtr(context, typedArray, nullptr)); @@ -188,7 +189,7 @@ KFloat getFloat(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); @@ -201,7 +202,7 @@ KShort getShort(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); @@ -214,7 +215,7 @@ KUShort getUShort(JSContextRef context, JSValueRef value) { return 0; } if (JSValueIsUndefined(context, value)) { - assert(false); + ASSERT(false); return 0; } double result = JSValueToNumber(context, value, &exception); diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h index b100dff7d..860ccae7e 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef CONVERTORS_JSC_H +#define CONVERTORS_JSC_H #if defined(linux) #include // For IDE completion @@ -25,9 +26,8 @@ #include #include -#include - #include "koala-types.h" +#include "interop-logging.h" template inline ElemType* getTypedElements(JSContextRef context, const JSValueRef arguments) { @@ -35,7 +35,7 @@ inline ElemType* getTypedElements(JSContextRef context, const JSValueRef argumen return nullptr; } if (JSValueIsUndefined(context, arguments)) { - assert(false); + ASSERT(false); return nullptr; } JSValueRef exception {}; @@ -46,7 +46,7 @@ inline ElemType* getTypedElements(JSContextRef context, const JSValueRef argumen template inline ElemType* getTypedElements(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getTypedElements(context, arguments[index]); } @@ -74,85 +74,85 @@ inline Type getArgument(JSContextRef context, size_t argumentCount, const JSValu template <> inline int32_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getInt32(context, arguments[index]); } template <> inline uint32_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getUInt32(context, arguments[index]); } template <> inline uint8_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getUInt8(context, arguments[index]); } template <> inline KNativePointer getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getPointer(context, arguments[index]); } template <> inline KFloat getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getFloat(context, arguments[index]); } template <> inline KStringPtr getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getString(context, arguments[index]); } template <> inline KBoolean getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getBoolean(context, arguments[index]); } template <> inline KInt* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getInt32Elements(context, arguments[index]); } template <> inline float* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getFloat32Elements(context, arguments[index]); } template <> inline KByte* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getByteElements(context, arguments[index]); } template <> inline KStringArray getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getKStringArray(context, arguments[index]); } template <> inline KUShort* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getUShortElements(context, arguments[index]); } template <> inline KNativePointerArray getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getPointerElements(context, arguments[index]); } template <> inline KShort* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - assert(index < argumentCount); + ASSERT(index < argumentCount); return getShortElements(context, arguments[index]); } @@ -711,12 +711,14 @@ void InitExports(JSGlobalContextRef globalContext); #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - /* TODO: implement*/ assert(false); \ + /* TODO: implement*/ ASSERT(false); \ return __VA_ARGS__; \ } while (0) #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ do { \ - assert(false); /* TODO: implement*/ \ + ASSERT(false); /* TODO: implement*/ \ return __VA_ARGS__; \ } while (0) + +#endif // CONVERTORS_JSC_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/wasm/convertors-wasm.h b/koala-wrapper/koalaui/interop/src/cpp/wasm/convertors-wasm.h index eea97a98e..c0fffffa1 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/wasm/convertors-wasm.h +++ b/koala-wrapper/koalaui/interop/src/cpp/wasm/convertors-wasm.h @@ -13,14 +13,16 @@ * limitations under the License. */ -#pragma once +#ifndef CONVERTORS_WASM_H +#define CONVERTORS_WASM_H #include "koala-types.h" -#include #include #define KOALA_INTEROP_EXPORT EMSCRIPTEN_KEEPALIVE extern "C" +#include "interop-logging.h" + template struct InteropTypeConverter { using InteropType = T; @@ -767,12 +769,14 @@ KOALA_INTEROP_EXPORT void name( \ #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - assert(false); /* TODO: implement*/ \ + ASSERT(false); /* TODO: implement*/ \ return __VA_ARGS__; \ } while (0) #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ do { \ - assert(false); /* TODO: implement*/ \ + ASSERT(false); /* TODO: implement*/ \ return __VA_ARGS__; \ } while (0) + +#endif // CONVERTORS_WASM_H \ No newline at end of file -- Gitee From 3ea986072653b093b7ebdab4ee8bafbb0073f194 Mon Sep 17 00:00:00 2001 From: Keerecles Date: Thu, 12 Jun 2025 11:25:48 +0800 Subject: [PATCH 2/6] clean code: safe memcpy Signed-off-by: Keerecles Change-Id: I20a092605a2e41d579489f51a46e8070f33924be --- .../interop/src/cpp/DeserializerBase.h | 63 +++++++++++++++---- .../koalaui/interop/src/cpp/SerializerBase.h | 42 ++++++++++--- .../interop/src/cpp/ani/convertors-ani.cc | 21 ------- .../interop/src/cpp/ani/convertors-ani.h | 50 --------------- .../interop/src/cpp/callback-resource.cc | 22 +++++-- .../interop/src/cpp/cangjie/convertors-cj.h | 11 ++-- .../koalaui/interop/src/cpp/common-interop.cc | 23 +++++-- .../interop/src/cpp/ets/convertors-ets.cc | 2 - .../interop/src/cpp/ets/convertors-ets.h | 6 +- .../interop/src/cpp/interop-logging.cc | 13 ++-- .../koalaui/interop/src/cpp/interop-types.h | 6 +- .../interop/src/cpp/jni/convertors-jni.h | 26 +++++--- .../interop/src/cpp/jsc/convertors-jsc.cc | 6 +- .../interop/src/cpp/napi/convertors-napi.h | 14 +---- .../interop/src/cpp/napi/win-dynamic-node.cc | 11 +++- .../koalaui/interop/src/cpp/ohos/hilog/log.h | 9 ++- .../koalaui/interop/src/cpp/ohos/oh_sk_log.cc | 17 +++-- .../koalaui/interop/src/cpp/ohos/oh_sk_log.h | 5 +- .../koalaui/interop/src/cpp/profiler.h | 6 +- .../interop/src/cpp/types/koala-types.h | 8 ++- .../interop/src/cpp/types/signatures.cc | 34 +++++----- .../koalaui/interop/src/cpp/vmloader.cc | 1 - .../src/interop/java/CallbackRegistry.java | 12 ++-- 23 files changed, 239 insertions(+), 169 deletions(-) diff --git a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h index 669d16f9f..4ff7a986c 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h @@ -23,6 +23,7 @@ #include "interop-types.h" #include "interop-logging.h" +#include "koala-types.h" void holdManagedCallbackResource(InteropInt32); void releaseManagedCallbackResource(InteropInt32); @@ -198,7 +199,11 @@ template <> inline void WriteToString(std::string *result, const InteropMaterialized *value) { char hex[20]; - std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + #ifdef __STDC_LIB_EXT1__ + std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + #else + std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + #endif result->append("\""); result->append("Materialized "); result->append(hex); @@ -309,7 +314,11 @@ public: if (length > 0) { value = malloc(length * sizeof(E)); - memset(value, 0, length * sizeof(E)); + #ifdef __STDC_LIB_EXT1__ + memset_s(value, length * sizeof(E), 0, length * sizeof(E)); + #else + memset(value, 0, length * sizeof(E)); + #endif toClean.push_back(value); } array->length = length; @@ -324,11 +333,19 @@ public: if (length > 0) { keys = malloc(length * sizeof(K)); - memset(keys, 0, length * sizeof(K)); + #ifdef __STDC_LIB_EXT1__ + memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); + #else + memset(keys, 0, length * sizeof(K)); + #endif toClean.push_back(keys); values = malloc(length * sizeof(V)); - memset(values, 0, length * sizeof(V)); + #ifdef __STDC_LIB_EXT1__ + memset_s(values, length * sizeof(V), 0, length * sizeof(V)); + #else + memset(values, 0, length * sizeof(V)); + #endif toClean.push_back(values); } map->size = length; @@ -397,7 +414,11 @@ public: check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt32 value; - memcpy(&value, data + position, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); + #else + memcpy(&value, data + position, 4); + #endif #else auto value = *(InteropInt32 *)(data + position); #endif @@ -409,7 +430,11 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; - memcpy(&value, data + position, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); + #else + memcpy(&value, data + position, 4); + #endif #else auto value = *(InteropInt64 *)(data + position); #endif @@ -421,7 +446,11 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; - memcpy(&value, data + position, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); + #else + memcpy(&value, data + position, 4); + #endif #else auto value = *(InteropUInt64 *)(data + position); #endif @@ -433,7 +462,11 @@ public: check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropFloat32 value; - memcpy(&value, data + position, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); + #else + memcpy(&value, data + position, 4); + #endif #else auto value = *(InteropFloat32 *)(data + position); #endif @@ -445,12 +478,16 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS int64_t value = 0; - memcpy(&value, data + position, 8); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 8, data + position, 8); + #else + memcpy(&value, data + position, 8); + #endif #else int64_t value = *(int64_t *)(data + position); #endif position += 8; - return reinterpret_cast(value); + return reinterpret_cast(static_cast(value)); } InteropNativePointer readPointerOrDefault(InteropNativePointer defaultValue) { @@ -578,7 +615,11 @@ inline void WriteToString(std::string *result, InteropFloat32 value) #if (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED < 130300L)) // to_chars() is not available on older macOS. char buf[20]; - snprintf(buf, sizeof buf, "%f", value); + #ifdef __STDC_LIB_EXT1__ + snprintf_s(buf, sizeof buf, "%f", value); + #else + snprintf(buf, sizeof buf, "%f", value); + #endif result->append(buf); #else std::string storage; diff --git a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h index f99efca14..f28c46252 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h @@ -75,7 +75,11 @@ private: ASSERT(ownData); ASSERT(newLength > dataLength); auto* newData = reinterpret_cast(malloc(newLength)); - memcpy(newData, data, position); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(newData, newLength, data, position); + #else + memcpy(newData, data, position); + #endif free(data); data = newData; } @@ -126,7 +130,11 @@ public: void writeInt32(InteropInt32 value) { check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS - memcpy(data + position, &value, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 4); + #else + memcpy(data + position, &value, 4); + #endif #else *((InteropInt32*)(data + position)) = value; #endif @@ -136,7 +144,11 @@ public: void writeInt64(InteropInt64 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - memcpy(data + position, &value, 8); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 8); + #else + memcpy(data + position, &value, 8); + #endif #else *((InteropInt64*)(data + position)) = value; #endif @@ -146,7 +158,11 @@ public: void writeUInt64(InteropUInt64 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - memcpy(data + position, &value, 8); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 8); + #else + memcpy(data + position, &value, 8); + #endif #else *((InteropUInt64*)(data + position)) = value; #endif @@ -156,7 +172,11 @@ public: void writeFloat32(InteropFloat32 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - memcpy(data + position, &value, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 4); + #else + memcpy(data + position, &value, 4); + #endif #else *((InteropFloat32*)(data + position)) = value; #endif @@ -166,7 +186,11 @@ public: void writePointer(InteropNativePointer value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - memcpy(data + position, &value, 8); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value64, 8); + #else + memcpy(data + position, &value, 8); + #endif #else *((int64_t*)(data + position)) = reinterpret_cast(value); #endif @@ -220,7 +244,11 @@ public: case 3: suffix = "%"; break; case 4: suffix = "lpx"; break; } - snprintf(buf, 64, "%.8f%s", value.value, suffix.c_str()); + #ifdef __STDC_LIB_EXT1__ + snprintf_s(buf, 64, "%.8f%s", value.value, suffix.c_str()); + #else + snprintf(buf, 64, "%.8f%s", value.value, suffix.c_str()); + #endif InteropString str = { buf, (InteropInt32) strlen(buf) }; writeString(str); break; diff --git a/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.cc b/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.cc index f41a2f5bf..cb9160ed0 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.cc @@ -40,7 +40,6 @@ static bool registerNatives(ani_env *env, const ani_class clazz, const std::vect ani_boolean isError = false; env->ExistUnhandledError(&isError); if (isError) { - //env->ErrorDescribe(); env->ResetError(); } } @@ -73,26 +72,6 @@ bool registerAllModules(ani_env *env) { return true; } -#if 0 -extern "C" ETS_EXPORT ets_int ETS_CALL EtsNapiOnLoad(ets_env *env) { - if (!registerAllModules(env)) { - LOGE("Failed to register ets modules"); - return ETS_ERR; - } - auto interopClasspath = AniExports::getInstance()->getClasspath("InteropNativeModule"); - auto interopClass = env->FindClass(interopClasspath.c_str()); - if (interopClass == nullptr) { - LOGE("Can not find InteropNativeModule classpath to set callback dispatcher"); - return ETS_ERR; - } - if (!setKoalaEtsNapiCallbackDispatcher(env, interopClass, callCallbackFromNative, callCallbackFromNativeSig)) { - LOGE("Failed to set koala ets callback dispatcher"); - return ETS_ERR; - } - return ETS_NAPI_VERSION_1_0; -} -#endif - AniExports* AniExports::getInstance() { static AniExports *instance = nullptr; if (instance == nullptr) { diff --git a/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.h b/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.h index 3be74d311..c3b678273 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.h +++ b/koala-wrapper/koalaui/interop/src/cpp/ani/convertors-ani.h @@ -16,7 +16,6 @@ #ifndef KOALA_ANI #define KOALA_ANI -#include #include #include #include @@ -1319,60 +1318,11 @@ bool setKoalaEtsNapiCallbackDispatcher( ); void getKoalaEtsNapiCallbackDispatcher(ani_class* clazz, ani_method* method); -#if 0 -#define KOALA_INTEROP_CALL_VOID(venv, id, length, args) \ -{ \ - ani_class clazz = nullptr; \ - ani_method method = nullptr; \ - getKoalaEtsNapiCallbackDispatcher(&clazz, &method); \ - ani_env* ani_env = reinterpret_cast(vmContext); \ - ani_env->PushLocalFrame(1); \ - ani_fixedarray_byte args_ets = ani_env->NewByteArray(length); \ - ani_env->SetByteArrayRegion(args_ets, 0, length, reinterpret_cast(args)); \ - ani_env->CallStaticIntMethod(clazz, method, id, args_ets, length); \ - ani_env->GetByteArrayRegion(args_ets, 0, length, reinterpret_cast(args)); \ - ani_env->PopLocalFrame(nullptr); \ -} - -#define KOALA_INTEROP_CALL_INT(venv, id, length, args) \ -{ \ - ani_class clazz = nullptr; \ - ani_method method = nullptr; \ - getKoalaEtsNapiCallbackDispatcher(&clazz, &method); \ - ani_env* ani_env = reinterpret_cast(venv); \ - ani_env->PushLocalFrame(1); \ - ani_fixedarray_byte args_ets = ani_env->NewByteArray(length); \ - ani_env->SetByteArrayRegion(args_ets, 0, length, reinterpret_cast(args)); \ - int32_t rv = ani_env->CallStaticIntMethod(clazz, method, id, args_ets, length); \ - ani_env->GetByteArrayRegion(args_ets, 0, length, reinterpret_cast(args)); \ - ani_env->PopLocalFrame(nullptr); \ - return rv; \ -} - -#define KOALA_INTEROP_CALL_VOID_INTS32(venv, id, argc, args) KOALA_INTEROP_CALL_VOID(venv, id, (argc) * sizeof(int32_t), args) -#define KOALA_INTEROP_CALL_INT_INTS32(venv, id, argc, args) KOALA_INTEROP_CALL_INT(venv, id, (argc) * sizeof(int32_t), args) - -#define KOALA_INTEROP_THROW(vmContext, object, ...) \ - do { \ - ani_env* env = reinterpret_cast(vmContext); \ - env->ThrowError(object); \ - return __VA_ARGS__; \ - } while (0) - -#define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ - do { \ - ani_env* env = reinterpret_cast(vmContext); \ - const static ani_class errorClass = env->FindClass("std/core/Error"); \ - env->ThrowErrorNew(errorClass, message); \ - } while (0) -#else - #define KOALA_INTEROP_CALL_VOID(venv, id, length, args) #define KOALA_INTEROP_CALL_INT(venv, id, length, args) { return 0; } #define KOALA_INTEROP_CALL_VOID_INTS32(venv, id, argc, args) { return; } #define KOALA_INTEROP_CALL_INT_INTS32(venv, id, argc, args) { return 0; } #define KOALA_INTEROP_THROW(vmContext, object, ...) { return __VA_ARGS__; } #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) { return __VA_ARGS__; } -#endif #endif // KOALA_ETS_NAPI diff --git a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc index d7851e405..f10eacf58 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc @@ -41,7 +41,8 @@ void releaseManagedCallbackResource(InteropInt32 resourceId) { callbackResourceSubqueue.push_back(resourceId); } -KInt impl_CheckCallbackEvent(KByte* result, KInt size) { +KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { + KByte* result = (KByte*)buffer; if (needReleaseFront) { switch (callbackEventsQueue.front()) @@ -64,16 +65,29 @@ KInt impl_CheckCallbackEvent(KByte* result, KInt size) { return 0; } const CallbackEventKind frontEventKind = callbackEventsQueue.front(); - memcpy(result, &frontEventKind, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(result, size, &frontEventKind, 4); + #else + memcpy(result, &frontEventKind, 4); + #endif + switch (frontEventKind) { case Event_CallCallback: - memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + #else + memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + #endif break; case Event_HoldManagedResource: case Event_ReleaseManagedResource: { const InteropInt32 resourceId = callbackResourceSubqueue.front(); - memcpy(result + 4, &resourceId, 4); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(result + 4, size, &frontEventKind, 4); + #else + memcpy(result + 4, &resourceId, 4); + #endif break; } default: diff --git a/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h b/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h index 17949006b..e3fbc8a20 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h +++ b/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h @@ -13,15 +13,16 @@ * limitations under the License. */ -#pragma once +#ifndef CONVERTORS_CJ_H +#define CONVERTORS_CJ_H #include -#include #include #include #include #include "koala-types.h" +#include "interop-logging.h" #define KOALA_INTEROP_EXPORT extern "C" @@ -864,12 +865,14 @@ KOALA_INTEROP_EXPORT void name(InteropTypeConverter::InteropType _p0, \ #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - /* TODO: implement*/ assert(false); \ + /* TODO: implement*/ ASSERT(false); \ return __VA_ARGS__; \ } while (0) #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ do { \ - /* TODO: implement*/ assert(false); \ + /* TODO: implement*/ ASSERT(false); \ return __VA_ARGS__; \ } while (0) + +#endif // CONVERTORS_CJ_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc index ec6572c6d..5d4d5b43e 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc @@ -127,7 +127,13 @@ KOALA_INTEROP_1(StringLength, KInt, KNativePointer) void impl_StringData(KNativePointer ptr, KByte* bytes, KUInt size) { string* s = reinterpret_cast(ptr); - if (s) memcpy(bytes, s->c_str(), size); + if (s) { + #ifdef __STDC_LIB_EXT1__ + memcpy_s(bytes, size, s->c_str(), size); + #else + memcpy(bytes, s->c_str(), size); + #endif + } } KOALA_INTEROP_V3(StringData, KNativePointer, KByte*, KUInt) @@ -149,7 +155,11 @@ KOALA_INTEROP_1(StringMake, KNativePointer, KStringPtr) // For slow runtimes w/o fast encoders. KInt impl_ManagedStringWrite(const KStringPtr& string, KByte* buffer, KInt offset) { - memcpy(buffer + offset, string.c_str(), string.length() + 1); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(buffer + offset, string.length() + 1, string.c_str(), string.length() + 1); + #else + memcpy(buffer + offset, string.c_str(), string.length() + 1); + #endif return string.length() + 1; } KOALA_INTEROP_3(ManagedStringWrite, KInt, KStringPtr, KByte*, KInt) @@ -373,8 +383,7 @@ KInt impl_CallForeignVM(KNativePointer foreignContextRaw, KInt function, KByte* KOALA_INTEROP_4(CallForeignVM, KInt, KNativePointer, KInt, KByte*, KInt) -#define __QUOTE(x) #x -#define QUOTE(x) __QUOTE(x) +#define QUOTE(x) #x void impl_NativeLog(const KStringPtr& str) { #ifdef KOALA_OHOS @@ -461,7 +470,11 @@ KOALA_INTEROP_CTX_1(StdStringToString, KStringPtr, KNativePointer) #if defined(KOALA_JNI) || defined(KOALA_NAPI) || defined(KOALA_CJ) KInteropReturnBuffer impl_RawReturnData(KVMContext vmContext, KInt v1, KInt v2) { void* data = new int8_t[v1]; - memset(data, v2, v1); + #ifdef __STDC_LIB_EXT1__ + memset_s(data, v1, v2, v1); + #else + memset(data, v2, v1); + #endif KInteropReturnBuffer buffer = { v1, data, [](KNativePointer ptr, KInt) { delete[] (int8_t*)ptr; }}; return buffer; } diff --git a/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.cc b/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.cc index 450804338..103c8c7de 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.cc @@ -38,7 +38,6 @@ static bool registerNatives(ets_env *env, const ets_class clazz, const std::vect if (registerByOne) { result &= env->RegisterNatives(clazz, &method, 1) >= 0; if (env->ErrorCheck()) { - //env->ErrorDescribe(); env->ErrorClear(); } } @@ -131,7 +130,6 @@ void EtsExports::setClasspath(const char* module, const char *classpath) { static std::map g_defaultClasspaths = { {"InteropNativeModule", "@koalaui/interop/InteropNativeModule/InteropNativeModule"}, - // todo leave just InteropNativeModule, define others via KOALA_ETS_INTEROP_MODULE_CLASSPATH {"TestNativeModule", "@koalaui/arkts-arkui/generated/arkts/TestNativeModule/TestNativeModule"}, {"ArkUINativeModule", "@koalaui/arkts-arkui/generated/arkts/ArkUINativeModule/ArkUINativeModule"}, {"ArkUIGeneratedNativeModule", "@koalaui/arkts-arkui/generated/arkts/ArkUIGeneratedNativeModule/ArkUIGeneratedNativeModule"}, diff --git a/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.h b/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.h index c03b5af27..b81fc1c81 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.h +++ b/koala-wrapper/koalaui/interop/src/cpp/ets/convertors-ets.h @@ -13,11 +13,11 @@ * limitations under the License. */ -#pragma once +#ifndef CONVERTORS_ETS_H +#define CONVERTORS_ETS_H #ifdef KOALA_ETS_NAPI -#include #include #include #include @@ -1347,3 +1347,5 @@ void getKoalaEtsNapiCallbackDispatcher(ets_class* clazz, ets_method* method); } while (0) #endif // KOALA_ETS_NAPI + +#endif // CONVERTORS_ETS_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc index 073878547..b99493ff6 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc @@ -14,6 +14,7 @@ */ #include #include +#include #include "interop-logging.h" @@ -27,7 +28,7 @@ struct Log { std::vector groupedLogs; void startGroupedLog(int index) { - if (index >= (int)groupedLogs.size()) { + if (index >= static_cast(groupedLogs.size())) { groupedLogs.resize(index + 1); for (int i = 0; i <= index; i++) { if (!groupedLogs[i]) groupedLogs[i] = new Log(); @@ -38,27 +39,27 @@ void startGroupedLog(int index) { } void stopGroupedLog(int index) { - if (index < (int)groupedLogs.size()) { + if (index < static_cast(groupedLogs.size())) { groupedLogs[index]->isActive = false; } } void appendGroupedLog(int index, const char* str) { - if (index < (int)groupedLogs.size()) { + if (index < static_cast(groupedLogs.size())) { groupedLogs[index]->log.append(str); } } const char* getGroupedLog(int index) { - if (index < (int)groupedLogs.size()) { - auto result = groupedLogs[index]->log.c_str(); + if (index < static_cast(groupedLogs.size())) { + const char* result = groupedLogs[index]->log.c_str(); return result; } return ""; } int needGroupedLog(int index) { - if (index < (int)groupedLogs.size()) { + if (index < static_cast(groupedLogs.size())) { return groupedLogs[index]->isActive; } return 0; diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-types.h b/koala-wrapper/koalaui/interop/src/cpp/interop-types.h index 4c5c26187..1daaaa80a 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-types.h +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-types.h @@ -16,7 +16,11 @@ #ifndef _INTEROP_TYPES_H_ #define _INTEROP_TYPES_H_ -#include +#ifdef __cplusplus + #include +#else + #include +#endif #define INTEROP_FATAL(msg, ...) do { fprintf(stderr, msg "\n", ##__VA_ARGS__); abort(); } while (0) diff --git a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h index 2ebefafe7..063a86a56 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h @@ -13,12 +13,12 @@ * limitations under the License. */ -#pragma once +#ifndef CONVERTORS_JNI_H +#define CONVERTORS_JNI_H #ifdef KOALA_JNI #include -#include #include #include @@ -145,9 +145,14 @@ struct SlowInteropTypeConverter { return result; } static InteropType convertTo(JNIEnv* env, KInteropBuffer value) { - jarray result = env->NewByteArray(value.length); + int bufferLength = value.length; + jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); - memcpy(data, value.data, value.length); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data, bufferLength, value.data, bufferLength); + #else + memcpy(data, value.data, bufferLength); + #endif env->ReleasePrimitiveArrayCritical(result, data, 0); return result; } @@ -161,11 +166,16 @@ struct SlowInteropTypeConverter { using InteropType = jarray; static inline KInteropReturnBuffer convertFrom(JNIEnv* env, InteropType value) = delete; static InteropType convertTo(JNIEnv* env, KInteropReturnBuffer value) { - jarray result = env->NewByteArray(value.length); + int bufferLength = value.length; + jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); - memcpy(data, value.data, value.length); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(data, bufferLength, value.data, bufferLength); + #else + memcpy(data, value.data, bufferLength); + #endif env->ReleasePrimitiveArrayCritical(result, data, 0); - value.dispose(value.data, value.length); + value.dispose(value.data, bufferLength); return result; } static inline void release(JNIEnv* env, InteropType value, const KInteropReturnBuffer& converted) = delete; @@ -1463,3 +1473,5 @@ void getKoalaJniCallbackDispatcher(jclass* clazz, jmethodID* method); } while (0) #endif // KOALA_JNI_CALL + +#endif // CONVERTORS_JNI_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc index 1ae964f27..246a1fd1c 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc @@ -146,7 +146,11 @@ static JSValueRef u64ToBigInt(JSContextRef context, uint64_t value) { bigint = JSObjectCallAsFunction(context, bigIntFromParts, nullptr, 2, parts, nullptr); #else char buffer[128] = {0}; - std::snprintf(buffer, sizeof(buffer) - 1, "%zun", (size_t) value); + #ifdef __STDC_LIB_EXT1__ + std::snprintf_s(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); + #else + std::snprintf(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); + #endif JSStringRef script = JSStringCreateWithUTF8CString(buffer); bigint = JSEvaluateScript(context, script, nullptr, nullptr, 0, nullptr); JSStringRelease(script); diff --git a/koala-wrapper/koalaui/interop/src/cpp/napi/convertors-napi.h b/koala-wrapper/koalaui/interop/src/cpp/napi/convertors-napi.h index 720d423f1..2446ec3a8 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/napi/convertors-napi.h +++ b/koala-wrapper/koalaui/interop/src/cpp/napi/convertors-napi.h @@ -31,7 +31,6 @@ #include "koala-types.h" #include "interop-types.h" -// TODO: switch to more generic convertors eventually. template struct InteropTypeConverter { using InteropType = T; @@ -88,9 +87,6 @@ struct InteropTypeConverter { (void*)copy, &result ); - if (status != napi_ok) { - // do smth here - } return result; }; static void release(napi_env env, InteropType value, KInteropBuffer converted) {} @@ -241,7 +237,6 @@ public: } private: napi_env _env; - // napi_callback_info _info; std::vector args; }; @@ -513,11 +508,6 @@ inline KNativePointerArray getArgument(const CallbackInfo& return getPointerElements(info, index); } -// template <> -// inline napi_value getArgument(const CallbackInfo& info, int index) { -// return getObject(info, index); -// } - template <> inline uint8_t* getArgument(const CallbackInfo& info, int index) { return getUInt8Elements(info, index); @@ -573,7 +563,6 @@ 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); -// napi_value makeObject(napi_env env, napi_value object); inline napi_value makeVoid(const CallbackInfo& info) { return makeVoid(info.Env()); @@ -656,8 +645,7 @@ public: const std::vector>& getMethods(const std::string& module); }; -#define __QUOTE(x) #x -#define QUOTE(x) __QUOTE(x) +#define QUOTE(x) #x #ifdef _MSC_VER #define MAKE_NODE_EXPORT(module, name) \ diff --git a/koala-wrapper/koalaui/interop/src/cpp/napi/win-dynamic-node.cc b/koala-wrapper/koalaui/interop/src/cpp/napi/win-dynamic-node.cc index 6ec8fcc34..ac0355338 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/napi/win-dynamic-node.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/napi/win-dynamic-node.cc @@ -12,8 +12,15 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -#include -#include + +#ifdef __cplusplus + #include + #include +#else + #include + #include +#endif + #include #include "node_api.h" diff --git a/koala-wrapper/koalaui/interop/src/cpp/ohos/hilog/log.h b/koala-wrapper/koalaui/interop/src/cpp/ohos/hilog/log.h index 7452137c2..174564bce 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ohos/hilog/log.h +++ b/koala-wrapper/koalaui/interop/src/cpp/ohos/hilog/log.h @@ -58,8 +58,13 @@ * * @since 8 */ -#include -#include + +#ifdef __cplusplus + #include +#else + #include + #include +#endif #ifdef __cplusplus extern "C" { diff --git a/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.cc b/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.cc index 6aa01fe5e..0d30f1b58 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.cc @@ -24,9 +24,13 @@ static const char* KOALAUI_OHOS_LOG_ROOT = "/data/storage/el2/base/files/logs"; -#define APPLY_LOG_FILE_PATTERN(buf, t, ms, pid) \ - sprintf(buf, "%s/%d_%d_%d_%ld.pid%d.log", \ - KOALAUI_OHOS_LOG_ROOT, t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, ms.tv_sec, pid) +#define APPLY_LOG_FILE_PATTERN(buf, bufLen, t, ms, pid) \ + #ifdef __STDC_LIB_EXT1__ \ + sprintf_s(buf, bufLen, "%s/%d_%d_%d_%lld.pid%d.log", \ + #else \ + sprintf(buf, "%s/%d_%d_%d_%lld.pid%d.log", \ + #endif \ + KOALAUI_OHOS_LOG_ROOT, (t).tm_year + 1900, (t).tm_mon + 1, (t).tm_mday, (ms).tv_sec, pid) const char* oh_sk_log_type_str(oh_sk_log_type type) { switch (type) { @@ -46,15 +50,16 @@ void oh_sk_file_log(oh_sk_log_type type, const char* msg, ...) { static char* path = nullptr; if (!path) { - path = new char[strlen(KOALAUI_OHOS_LOG_ROOT) + 100]; - APPLY_LOG_FILE_PATTERN(path, lt, ms, getpid()); + size_t len = strlen(KOALAUI_OHOS_LOG_ROOT) + 100; + path = new char[len]; + APPLY_LOG_FILE_PATTERN(path, len, lt, ms, getpid()); mkdir(KOALAUI_OHOS_LOG_ROOT, 0777); } std::unique_ptr file(fopen(path, "a"), fclose); if (!file) return; - fprintf(file.get(), "%02d-%02d %02d:%02d:%02d.%03ld %s koala: ", + fprintf(file.get(), "%02d-%02d %02d:%02d:%02d.%03lld %s koala: ", lt.tm_mon + 1, lt.tm_mday, lt.tm_hour, lt.tm_min, lt.tm_sec, ms.tv_usec / 1000, oh_sk_log_type_str(type)); diff --git a/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.h b/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.h index 961e2c0f1..6544a2e2e 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.h +++ b/koala-wrapper/koalaui/interop/src/cpp/ohos/oh_sk_log.h @@ -13,7 +13,8 @@ * limitations under the License. */ -#pragma once +#ifndef OH_SK_LOG_H +#define OH_SK_LOG_H #include @@ -55,3 +56,5 @@ const char* oh_sk_log_type_str(oh_sk_log_type type); #define OH_SK_LOG_FATAL_A(msg, ...) OH_LOG_Print(LOG_APP, LOG_FATAL, 0xFF00, "Koala", msg, ##__VA_ARGS__) #endif + +#endif // OH_SK_LOG_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/profiler.h b/koala-wrapper/koalaui/interop/src/cpp/profiler.h index a3b9da38c..cdfe518d9 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/profiler.h +++ b/koala-wrapper/koalaui/interop/src/cpp/profiler.h @@ -68,7 +68,11 @@ class InteropProfiler { auto ns = a.second.time; auto count = a.second.count; char buffer[1024]; - snprintf(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), (long long)count, (double)ns / total * 100.0, (long long)ns); + #ifdef __STDC_LIB_EXT1__ + snprintf_s(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), (long long)count, (double)ns / total * 100.0, (long long)ns); + #else + snprintf(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), (long long)count, (double)ns / total * 100.0, (long long)ns); + #endif result += buffer; }); return result; diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h index 2c5e88754..f5d684146 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h +++ b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h @@ -66,7 +66,11 @@ struct KStringPtrImpl { if (data) { if (_owned) { _value = reinterpret_cast(malloc(len + 1)); - memcpy(_value, data, len); + #ifdef __STDC_LIB_EXT1__ + memcpy_s(_value, len, data, len); + #else + memcpy(_value, data, len); + #endif _value[len] = 0; } else { _value = const_cast(data); @@ -100,7 +104,7 @@ struct KInteropNumber { // TODO: boundary check if (value == std::floor(value)) { result.tag = 102; // ARK_TAG_INT32 - result.i32 = (int)value; + result.i32 = static_cast(value); } else { result.tag = 103; // ARK_TAG_FLOAT32 result.f32 = (float)value; diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc index 7f556adaf..6c4d12e62 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc @@ -13,8 +13,14 @@ * limitations under the License. */ -#include "stdio.h" -#include +#ifdef __cplusplus + #include + #include +#else + #include + #include +#endif + #include #include "signatures.h" @@ -22,15 +28,15 @@ // For types with the same name on ets and jni #define KOALA_INTEROP_TYPEDEF(func, lang, CPP_TYPE, SIG_TYPE, CODE_TYPE) \ - if (std::strcmp(func, "sigType") == 0) if (type == CPP_TYPE) return SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0) if (type == CPP_TYPE) return CODE_TYPE; + if (std::strcmp(func, "sigType") == 0) if (type == (CPP_TYPE)) return SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0) if (type == (CPP_TYPE)) return CODE_TYPE; // For types with distinct names on ets and jni #define KOALA_INTEROP_TYPEDEF_LS(func, lang, CPP_TYPE, ETS_SIG_TYPE, ETS_CODE_TYPE, JNI_SIG_TYPE, JNI_CODE_TYPE) \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) if (type == CPP_TYPE) return ETS_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) if (type == CPP_TYPE) return ETS_CODE_TYPE; \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) if (type == CPP_TYPE) return JNI_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) if (type == CPP_TYPE) return JNI_CODE_TYPE; + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) if (type == (CPP_TYPE)) return ETS_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) if (type == (CPP_TYPE)) return ETS_CODE_TYPE; \ + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) if (type == (CPP_TYPE)) return JNI_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) if (type == (CPP_TYPE)) return JNI_CODE_TYPE; #define KOALA_INTEROP_TYPEDEFS(func, lang) \ KOALA_INTEROP_TYPEDEF(func, lang, "void", "V", "void") \ @@ -107,7 +113,7 @@ std::string convertType(const char* name, const char* koalaType) { #if KOALA_USE_PANDA_VM - for (int i = 1; i < (int)tokens.size(); i++) + for (int i = 1; i < static_cast(tokens.size()); i++) { result.append(sigType(tokens[i])); } @@ -117,7 +123,7 @@ std::string convertType(const char* name, const char* koalaType) { #elif KOALA_USE_JAVA_VM result.append("("); - for (int i = 1; i < (int)tokens.size(); i++) + for (int i = 1; i < static_cast(tokens.size()); i++) { result.append(sigType(tokens[i])); } @@ -129,24 +135,24 @@ std::string convertType(const char* name, const char* koalaType) { #ifdef KOALA_BUILD_FOR_SIGNATURES #ifdef KOALA_USE_PANDA_VM std::string params; - for (int i = 1; i < (int)tokens.size(); i++) + for (int i = 1; i < static_cast(tokens.size()); i++) { params.append("arg"); params.append(std::to_string(i)); params.append(": "); params.append(codeType(tokens[i])); - if (i < (int)(tokens.size() - 1)) + if (i < static_cast(tokens.size() - 1)) params.append(", "); } fprintf(stderr, " static native %s(%s): %s;\n", name, params.c_str(), codeType(tokens[0]).c_str()); #elif KOALA_USE_JAVA_VM std::string params; - for (int i = 1; i < (int)tokens.size(); i++) + for (int i = 1; i < static_cast(tokens.size()); i++) { params.append(codeType(tokens[i])); params.append(" arg"); params.append(std::to_string(i)); - if (i < (int)(tokens.size() - 1)) + if (i < static_cast(tokens.size() - 1)) params.append(", "); } fprintf(stderr, " public static native %s %s(%s);\n", codeType(tokens[0]).c_str(), name, params.c_str()); diff --git a/koala-wrapper/koalaui/interop/src/cpp/vmloader.cc b/koala-wrapper/koalaui/interop/src/cpp/vmloader.cc index 995d2623c..c2f5a08f9 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/vmloader.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/vmloader.cc @@ -259,7 +259,6 @@ extern "C" DLL_EXPORT KInt LoadVirtualMachine(KInt vmKind, const char* appClassP etsVMOptions.push_back({EtsOptionType::ETS_NO_JIT, nullptr}); etsVMOptions.push_back({EtsOptionType::ETS_MOBILE_LOG, (void*)ArkMobileLog}); etsVMOptions.push_back({EtsOptionType::ETS_AOT, nullptr}); - // etsVMOptions.push_back({EtsOptionType::ETS_LOG_LEVEL, "info"}); pandaVMArgs.nOptions = etsVMOptions.size(); pandaVMArgs.options = etsVMOptions.data(); g_vmEntry.vmKind = PANDA_VM_KIND; diff --git a/koala-wrapper/koalaui/interop/src/interop/java/CallbackRegistry.java b/koala-wrapper/koalaui/interop/src/interop/java/CallbackRegistry.java index 59dceae85..b246d133b 100644 --- a/koala-wrapper/koalaui/interop/src/interop/java/CallbackRegistry.java +++ b/koala-wrapper/koalaui/interop/src/interop/java/CallbackRegistry.java @@ -40,15 +40,15 @@ class CallbackRegistry { } public static Integer wrap(CallbackType callback) { - Integer id = CallbackRegistry.id++; - CallbackRegistry.callbacks.put(id, new CallbackRecord(callback, true)); - return id; + Integer tmpId = CallbackRegistry.id++; + CallbackRegistry.callbacks.put(tmpId, new CallbackRecord(callback, true)); + return tmpId; } public static Integer wrap(CallbackType callback, boolean autoDisposable) { - Integer id = CallbackRegistry.id++; - CallbackRegistry.callbacks.put(id, new CallbackRecord(callback, autoDisposable)); - return id; + Integer tmpId = CallbackRegistry.id++; + CallbackRegistry.callbacks.put(tmpId, new CallbackRecord(callback, autoDisposable)); + return tmpId; } public static int call(Integer id, byte[] args, int length) { -- Gitee From c314e5e99ce2893109ba0ae09537f2967b64a6c0 Mon Sep 17 00:00:00 2001 From: Keerecles Date: Thu, 12 Jun 2025 11:31:51 +0800 Subject: [PATCH 3/6] Add CODEOWNERS Signed-off-by: Keerecles Change-Id: I0622299a9c7058a88ab57100bb37e6721107ade2 --- .gitee/CODEOWNERS | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitee/CODEOWNERS b/.gitee/CODEOWNERS index 3a02023a8..292dd25de 100755 --- a/.gitee/CODEOWNERS +++ b/.gitee/CODEOWNERS @@ -29,6 +29,7 @@ compiler/test/transform_ut/ @lixingchi1 koala-wrapper/native/src/bridges.cc @keerecles koala-wrapper/native/src/common.cc @keerecles koala-wrapper/native/src/generated/bridges.cc @keerecles +koala-wrapper/ @keerecles # Code owners for ArkCompiler ace_ets2bundle -- Gitee From 2c1b52ba2a43b8d1d47949b0d6880d21bd829059 Mon Sep 17 00:00:00 2001 From: Keerecles Date: Thu, 12 Jun 2025 15:43:12 +0800 Subject: [PATCH 4/6] code clean Signed-off-by: Keerecles Change-Id: Iafbf0fc320dc6e10f48a68d2df671eb30a87077c --- .../interop/src/cpp/DeserializerBase.h | 100 +++++++++--------- .../koalaui/interop/src/cpp/SerializerBase.h | 76 ++++++------- .../interop/src/cpp/callback-resource.cc | 30 +++--- .../koalaui/interop/src/cpp/common-interop.cc | 30 +++--- .../interop/src/cpp/interop-logging.cc | 6 +- .../koalaui/interop/src/cpp/interop-types.h | 2 +- .../interop/src/cpp/jni/convertors-jni.h | 20 ++-- .../interop/src/cpp/jsc/convertors-jsc.cc | 10 +- .../interop/src/cpp/jsc/convertors-jsc.h | 4 +- .../koalaui/interop/src/cpp/profiler.h | 12 ++- .../interop/src/cpp/types/koala-types.h | 10 +- .../interop/src/cpp/types/signatures.cc | 12 ++- koala-wrapper/native/include/common.h | 2 +- 13 files changed, 163 insertions(+), 151 deletions(-) diff --git a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h index 4ff7a986c..2945f8da8 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h @@ -199,11 +199,11 @@ template <> inline void WriteToString(std::string *result, const InteropMaterialized *value) { char hex[20]; - #ifdef __STDC_LIB_EXT1__ - std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); - #else - std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); - #endif +#ifdef __STDC_LIB_EXT1__ + std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); +#else + std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); +#endif result->append("\""); result->append("Materialized "); result->append(hex); @@ -314,11 +314,11 @@ public: if (length > 0) { value = malloc(length * sizeof(E)); - #ifdef __STDC_LIB_EXT1__ - memset_s(value, length * sizeof(E), 0, length * sizeof(E)); - #else - memset(value, 0, length * sizeof(E)); - #endif +#ifdef __STDC_LIB_EXT1__ + memset_s(value, length * sizeof(E), 0, length * sizeof(E)); +#else + memset(value, 0, length * sizeof(E)); +#endif toClean.push_back(value); } array->length = length; @@ -333,19 +333,19 @@ public: if (length > 0) { keys = malloc(length * sizeof(K)); - #ifdef __STDC_LIB_EXT1__ - memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); - #else - memset(keys, 0, length * sizeof(K)); - #endif +#ifdef __STDC_LIB_EXT1__ + memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); +#else + memset(keys, 0, length * sizeof(K)); +#endif toClean.push_back(keys); values = malloc(length * sizeof(V)); - #ifdef __STDC_LIB_EXT1__ - memset_s(values, length * sizeof(V), 0, length * sizeof(V)); - #else - memset(values, 0, length * sizeof(V)); - #endif +#ifdef __STDC_LIB_EXT1__ + memset_s(values, length * sizeof(V), 0, length * sizeof(V)); +#else + memset(values, 0, length * sizeof(V)); +#endif toClean.push_back(values); } map->size = length; @@ -414,11 +414,11 @@ public: check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt32 value; - #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); - #else - memcpy(&value, data + position, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); +#else + memcpy(&value, data + position, 4); +#endif #else auto value = *(InteropInt32 *)(data + position); #endif @@ -430,11 +430,11 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; - #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); - #else - memcpy(&value, data + position, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); +#else + memcpy(&value, data + position, 4); +#endif #else auto value = *(InteropInt64 *)(data + position); #endif @@ -446,11 +446,11 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; - #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); - #else - memcpy(&value, data + position, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); +#else + memcpy(&value, data + position, 4); +#endif #else auto value = *(InteropUInt64 *)(data + position); #endif @@ -462,11 +462,11 @@ public: check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS InteropFloat32 value; - #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); - #else - memcpy(&value, data + position, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 4, data + position, 4); +#else + memcpy(&value, data + position, 4); +#endif #else auto value = *(InteropFloat32 *)(data + position); #endif @@ -478,11 +478,11 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS int64_t value = 0; - #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 8, data + position, 8); - #else - memcpy(&value, data + position, 8); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(&value, 8, data + position, 8); +#else + memcpy(&value, data + position, 8); +#endif #else int64_t value = *(int64_t *)(data + position); #endif @@ -615,11 +615,11 @@ inline void WriteToString(std::string *result, InteropFloat32 value) #if (defined(__MAC_OS_X_VERSION_MAX_ALLOWED) && (__MAC_OS_X_VERSION_MAX_ALLOWED < 130300L)) // to_chars() is not available on older macOS. char buf[20]; - #ifdef __STDC_LIB_EXT1__ - snprintf_s(buf, sizeof buf, "%f", value); - #else - snprintf(buf, sizeof buf, "%f", value); - #endif +#ifdef __STDC_LIB_EXT1__ + snprintf_s(buf, sizeof buf, "%f", value); +#else + snprintf(buf, sizeof buf, "%f", value); +#endif result->append(buf); #else std::string storage; diff --git a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h index f28c46252..0abea8247 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h @@ -33,6 +33,8 @@ #define KOALA_NO_UNALIGNED_ACCESS 1 #endif +constexpr int BUFFER_MAX_LEN = 64; + template inline InteropRuntimeType runtimeType(const T& value) = delete; @@ -55,7 +57,7 @@ T* allocArray(const std::array& ref) { std::size_t space = sizeof(buffer) - offset; void* ptr = buffer + offset; void* aligned_ptr = std::align(alignof(T), sizeof(T) * size, ptr, space); - ASSERT(aligned_ptr != nullptr && "Insufficient space or alignment failed!"); + ASSERT(aligned_ptr != nullptr); // "Insufficient space or alignment failed!" offset = (char*)aligned_ptr + sizeof(T) * size - buffer; T* array = reinterpret_cast(aligned_ptr); for (size_t i = 0; i < size; ++i) { @@ -75,11 +77,11 @@ private: ASSERT(ownData); ASSERT(newLength > dataLength); auto* newData = reinterpret_cast(malloc(newLength)); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(newData, newLength, data, position); - #else - memcpy(newData, data, position); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(newData, newLength, data, position); +#else + memcpy(newData, data, position); +#endif free(data); data = newData; } @@ -130,11 +132,11 @@ public: void writeInt32(InteropInt32 value) { check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 4); - #else - memcpy(data + position, &value, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 4); +#else + memcpy(data + position, &value, 4); +#endif #else *((InteropInt32*)(data + position)) = value; #endif @@ -144,11 +146,11 @@ public: void writeInt64(InteropInt64 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 8); - #else - memcpy(data + position, &value, 8); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 8); +#else + memcpy(data + position, &value, 8); +#endif #else *((InteropInt64*)(data + position)) = value; #endif @@ -158,11 +160,11 @@ public: void writeUInt64(InteropUInt64 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 8); - #else - memcpy(data + position, &value, 8); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 8); +#else + memcpy(data + position, &value, 8); +#endif #else *((InteropUInt64*)(data + position)) = value; #endif @@ -172,11 +174,11 @@ public: void writeFloat32(InteropFloat32 value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 4); - #else - memcpy(data + position, &value, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value, 4); +#else + memcpy(data + position, &value, 4); +#endif #else *((InteropFloat32*)(data + position)) = value; #endif @@ -186,11 +188,11 @@ public: void writePointer(InteropNativePointer value) { check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value64, 8); - #else - memcpy(data + position, &value, 8); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data + position, dataLength, &value64, 8); +#else + memcpy(data + position, &value, 8); +#endif #else *((int64_t*)(data + position)) = reinterpret_cast(value); #endif @@ -235,7 +237,7 @@ public: writeInt32(value.resource); break; case INTEROP_RUNTIME_STRING: { - char buf[64]; + char buf[BUFFER_MAX_LEN]; std::string suffix; switch (value.unit) { case 0: suffix = "px"; break; @@ -244,11 +246,11 @@ public: case 3: suffix = "%"; break; case 4: suffix = "lpx"; break; } - #ifdef __STDC_LIB_EXT1__ - snprintf_s(buf, 64, "%.8f%s", value.value, suffix.c_str()); - #else - snprintf(buf, 64, "%.8f%s", value.value, suffix.c_str()); - #endif +#ifdef __STDC_LIB_EXT1__ + snprintf_s(buf, BUFFER_MAX_LEN, "%.8f%s", value.value, suffix.c_str()); +#else + snprintf(buf, BUFFER_MAX_LEN, "%.8f%s", value.value, suffix.c_str()); +#endif InteropString str = { buf, (InteropInt32) strlen(buf) }; writeString(str); break; diff --git a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc index f10eacf58..0bf2128f0 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc @@ -65,29 +65,29 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { return 0; } const CallbackEventKind frontEventKind = callbackEventsQueue.front(); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(result, size, &frontEventKind, 4); - #else - memcpy(result, &frontEventKind, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(result, size, &frontEventKind, 4); +#else + memcpy(result, &frontEventKind, 4); +#endif switch (frontEventKind) { case Event_CallCallback: - #ifdef __STDC_LIB_EXT1__ - memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); - #else - memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); +#else + memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); +#endif break; case Event_HoldManagedResource: case Event_ReleaseManagedResource: { const InteropInt32 resourceId = callbackResourceSubqueue.front(); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(result + 4, size, &frontEventKind, 4); - #else - memcpy(result + 4, &resourceId, 4); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(result + 4, size, &frontEventKind, 4); +#else + memcpy(result + 4, &resourceId, 4); +#endif break; } default: diff --git a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc index 5d4d5b43e..2eb8c428f 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc @@ -128,11 +128,11 @@ KOALA_INTEROP_1(StringLength, KInt, KNativePointer) void impl_StringData(KNativePointer ptr, KByte* bytes, KUInt size) { string* s = reinterpret_cast(ptr); if (s) { - #ifdef __STDC_LIB_EXT1__ - memcpy_s(bytes, size, s->c_str(), size); - #else - memcpy(bytes, s->c_str(), size); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(bytes, size, s->c_str(), size); +#else + memcpy(bytes, s->c_str(), size); +#endif } } KOALA_INTEROP_V3(StringData, KNativePointer, KByte*, KUInt) @@ -155,11 +155,11 @@ KOALA_INTEROP_1(StringMake, KNativePointer, KStringPtr) // For slow runtimes w/o fast encoders. KInt impl_ManagedStringWrite(const KStringPtr& string, KByte* buffer, KInt offset) { - #ifdef __STDC_LIB_EXT1__ - memcpy_s(buffer + offset, string.length() + 1, string.c_str(), string.length() + 1); - #else - memcpy(buffer + offset, string.c_str(), string.length() + 1); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(buffer + offset, string.length() + 1, string.c_str(), string.length() + 1); +#else + memcpy(buffer + offset, string.c_str(), string.length() + 1); +#endif return string.length() + 1; } KOALA_INTEROP_3(ManagedStringWrite, KInt, KStringPtr, KByte*, KInt) @@ -470,11 +470,11 @@ KOALA_INTEROP_CTX_1(StdStringToString, KStringPtr, KNativePointer) #if defined(KOALA_JNI) || defined(KOALA_NAPI) || defined(KOALA_CJ) KInteropReturnBuffer impl_RawReturnData(KVMContext vmContext, KInt v1, KInt v2) { void* data = new int8_t[v1]; - #ifdef __STDC_LIB_EXT1__ - memset_s(data, v1, v2, v1); - #else - memset(data, v2, v1); - #endif +#ifdef __STDC_LIB_EXT1__ + memset_s(data, v1, v2, v1); +#else + memset(data, v2, v1); +#endif KInteropReturnBuffer buffer = { v1, data, [](KNativePointer ptr, KInt) { delete[] (int8_t*)ptr; }}; return buffer; } diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc index b99493ff6..01178d096 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc @@ -14,7 +14,11 @@ */ #include #include -#include +#ifdef __cplusplus + #include +#else + #include +#endif #include "interop-logging.h" diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-types.h b/koala-wrapper/koalaui/interop/src/cpp/interop-types.h index 1daaaa80a..911300d6b 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-types.h +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-types.h @@ -22,7 +22,7 @@ #include #endif -#define INTEROP_FATAL(msg, ...) do { fprintf(stderr, msg "\n", ##__VA_ARGS__); abort(); } while (0) +#define INTEROP_FATAL(msg, ...) do { fprintf(stderr, msg "\n", ##__VA_ARGS__);} while (0) typedef enum InteropTag { diff --git a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h index 063a86a56..cd4a2fca2 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h @@ -148,11 +148,11 @@ struct SlowInteropTypeConverter { int bufferLength = value.length; jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data, bufferLength, value.data, bufferLength); - #else - memcpy(data, value.data, bufferLength); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data, bufferLength, value.data, bufferLength); +#else + memcpy(data, value.data, bufferLength); +#endif env->ReleasePrimitiveArrayCritical(result, data, 0); return result; } @@ -169,11 +169,11 @@ struct SlowInteropTypeConverter { int bufferLength = value.length; jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(data, bufferLength, value.data, bufferLength); - #else - memcpy(data, value.data, bufferLength); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(data, bufferLength, value.data, bufferLength); +#else + memcpy(data, value.data, bufferLength); +#endif env->ReleasePrimitiveArrayCritical(result, data, 0); value.dispose(value.data, bufferLength); return result; diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc index 246a1fd1c..3b5546f4b 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc @@ -146,11 +146,11 @@ static JSValueRef u64ToBigInt(JSContextRef context, uint64_t value) { bigint = JSObjectCallAsFunction(context, bigIntFromParts, nullptr, 2, parts, nullptr); #else char buffer[128] = {0}; - #ifdef __STDC_LIB_EXT1__ - std::snprintf_s(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); - #else - std::snprintf(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); - #endif +#ifdef __STDC_LIB_EXT1__ + std::snprintf_s(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); +#else + std::snprintf(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); +#endif JSStringRef script = JSStringCreateWithUTF8CString(buffer); bigint = JSEvaluateScript(context, script, nullptr, nullptr, 0, nullptr); JSStringRelease(script); diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h index 860ccae7e..aaabacf01 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h @@ -46,8 +46,8 @@ inline ElemType* getTypedElements(JSContextRef context, const JSValueRef argumen template inline ElemType* getTypedElements(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getTypedElements(context, arguments[index]); + ASSERT(index < argumentCount); + return getTypedElements(context, arguments[index]); } uint8_t* getUInt8Elements(JSContextRef context, const JSValueRef arguments); diff --git a/koala-wrapper/koalaui/interop/src/cpp/profiler.h b/koala-wrapper/koalaui/interop/src/cpp/profiler.h index cdfe518d9..dd2483f10 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/profiler.h +++ b/koala-wrapper/koalaui/interop/src/cpp/profiler.h @@ -68,11 +68,13 @@ class InteropProfiler { auto ns = a.second.time; auto count = a.second.count; char buffer[1024]; - #ifdef __STDC_LIB_EXT1__ - snprintf_s(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), (long long)count, (double)ns / total * 100.0, (long long)ns); - #else - snprintf(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), (long long)count, (double)ns / total * 100.0, (long long)ns); - #endif +#ifdef __STDC_LIB_EXT1__ + snprintf_s(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), + (long long)count, (double)ns / total * 100.0, (long long)ns); +#else + snprintf(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), + (long long)count, (double)ns / total * 100.0, (long long)ns); +#endif result += buffer; }); return result; diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h index f5d684146..2ffa24ba7 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h +++ b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h @@ -66,11 +66,11 @@ struct KStringPtrImpl { if (data) { if (_owned) { _value = reinterpret_cast(malloc(len + 1)); - #ifdef __STDC_LIB_EXT1__ - memcpy_s(_value, len, data, len); - #else - memcpy(_value, data, len); - #endif +#ifdef __STDC_LIB_EXT1__ + memcpy_s(_value, len, data, len); +#else + memcpy(_value, data, len); +#endif _value[len] = 0; } else { _value = const_cast(data); diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc index 6c4d12e62..75fc13ee6 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc @@ -33,10 +33,14 @@ // For types with distinct names on ets and jni #define KOALA_INTEROP_TYPEDEF_LS(func, lang, CPP_TYPE, ETS_SIG_TYPE, ETS_CODE_TYPE, JNI_SIG_TYPE, JNI_CODE_TYPE) \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) if (type == (CPP_TYPE)) return ETS_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) if (type == (CPP_TYPE)) return ETS_CODE_TYPE; \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) if (type == (CPP_TYPE)) return JNI_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) if (type == (CPP_TYPE)) return JNI_CODE_TYPE; + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) \ + if (type == (CPP_TYPE)) return ETS_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) \ + if (type == (CPP_TYPE)) return ETS_CODE_TYPE; \ + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) \ + if (type == (CPP_TYPE)) return JNI_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) \ + if (type == (CPP_TYPE)) return JNI_CODE_TYPE; #define KOALA_INTEROP_TYPEDEFS(func, lang) \ KOALA_INTEROP_TYPEDEF(func, lang, "void", "V", "void") \ diff --git a/koala-wrapper/native/include/common.h b/koala-wrapper/native/include/common.h index 399d28d22..dc85f7c10 100644 --- a/koala-wrapper/native/include/common.h +++ b/koala-wrapper/native/include/common.h @@ -13,7 +13,7 @@ * limitations under the License. */ - #ifndef COMMON_H +#ifndef COMMON_H #define COMMON_H #include "dynamic-loader.h" -- Gitee From b96171079e670cd9f4dfaf523292d185dd39d82f Mon Sep 17 00:00:00 2001 From: Keerecles Date: Thu, 12 Jun 2025 16:44:38 +0800 Subject: [PATCH 5/6] code clean: safe function return Signed-off-by: Keerecles Change-Id: Id06f6bdf1d626cbe83293a8eaef13e5c2b6f2955 --- .../interop/src/cpp/DeserializerBase.h | 50 +++++++++++++++---- .../koalaui/interop/src/cpp/SerializerBase.h | 35 ++++++++++--- .../interop/src/cpp/callback-resource.cc | 15 ++++-- .../koalaui/interop/src/cpp/common-interop.cc | 15 ++++-- .../interop/src/cpp/interop-logging.cc | 3 +- .../interop/src/cpp/jni/convertors-jni.h | 10 +++- .../interop/src/cpp/jsc/convertors-jsc.cc | 5 +- .../interop/src/cpp/jsc/convertors-jsc.h | 2 +- .../koalaui/interop/src/cpp/profiler.h | 11 ++-- .../interop/src/cpp/types/koala-types.h | 5 +- 10 files changed, 118 insertions(+), 33 deletions(-) diff --git a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h index 2945f8da8..c0b7c88c7 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h @@ -200,7 +200,10 @@ inline void WriteToString(std::string *result, const InteropMaterialized *value) { char hex[20]; #ifdef __STDC_LIB_EXT1__ - std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + errno_t res = std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + if (res != EOK) { + return; + } #else std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); #endif @@ -315,7 +318,10 @@ public: { value = malloc(length * sizeof(E)); #ifdef __STDC_LIB_EXT1__ - memset_s(value, length * sizeof(E), 0, length * sizeof(E)); + errno_t res = memset_s(value, length * sizeof(E), 0, length * sizeof(E)); + if (res != EOK) { + return; + } #else memset(value, 0, length * sizeof(E)); #endif @@ -334,7 +340,10 @@ public: { keys = malloc(length * sizeof(K)); #ifdef __STDC_LIB_EXT1__ - memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); + errno_t res = memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); + if (res != EOK) { + return; + } #else memset(keys, 0, length * sizeof(K)); #endif @@ -342,7 +351,10 @@ public: values = malloc(length * sizeof(V)); #ifdef __STDC_LIB_EXT1__ - memset_s(values, length * sizeof(V), 0, length * sizeof(V)); + errno_t res = memset_s(values, length * sizeof(V), 0, length * sizeof(V)); + if (res != EOK) { + return; + } #else memset(values, 0, length * sizeof(V)); #endif @@ -415,7 +427,10 @@ public: #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt32 value; #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); + errno_t res = memcpy_s(&value, 4, data + position, 4); + if (res != EOK) { + return value; + } #else memcpy(&value, data + position, 4); #endif @@ -431,7 +446,10 @@ public: #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); + errno_t res = memcpy_s(&value, 4, data + position, 4); + if (res != EOK) { + return value; + } #else memcpy(&value, data + position, 4); #endif @@ -447,7 +465,10 @@ public: #ifdef KOALA_NO_UNALIGNED_ACCESS InteropInt64 value; #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); + errno_t res = memcpy_s(&value, 4, data + position, 4); + if (res != EOK) { + return value; + } #else memcpy(&value, data + position, 4); #endif @@ -463,7 +484,10 @@ public: #ifdef KOALA_NO_UNALIGNED_ACCESS InteropFloat32 value; #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 4, data + position, 4); + errno_t res = memcpy_s(&value, 4, data + position, 4); + if (res != EOK) { + return value; + } #else memcpy(&value, data + position, 4); #endif @@ -479,7 +503,10 @@ public: #ifdef KOALA_NO_UNALIGNED_ACCESS int64_t value = 0; #ifdef __STDC_LIB_EXT1__ - memcpy_s(&value, 8, data + position, 8); + errno_t res = memcpy_s(&value, 8, data + position, 8); + if (res != EOK) { + return value; + } #else memcpy(&value, data + position, 8); #endif @@ -616,7 +643,10 @@ inline void WriteToString(std::string *result, InteropFloat32 value) // to_chars() is not available on older macOS. char buf[20]; #ifdef __STDC_LIB_EXT1__ - snprintf_s(buf, sizeof buf, "%f", value); + errno_t res = snprintf_s(buf, sizeof buf, "%f", value); + if (res != EOK) { + return; + } #else snprintf(buf, sizeof buf, "%f", value); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h index 0abea8247..321169abf 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/SerializerBase.h @@ -78,7 +78,10 @@ private: ASSERT(newLength > dataLength); auto* newData = reinterpret_cast(malloc(newLength)); #ifdef __STDC_LIB_EXT1__ - memcpy_s(newData, newLength, data, position); + errno_t res = memcpy_s(newData, newLength, data, position); + if (res != EOK) { + return; + } #else memcpy(newData, data, position); #endif @@ -133,7 +136,10 @@ public: check(4); #ifdef KOALA_NO_UNALIGNED_ACCESS #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 4); + errno_t res = memcpy_s(data + position, dataLength, &value, 4); + if (res != EOK) { + return; + } #else memcpy(data + position, &value, 4); #endif @@ -147,7 +153,10 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 8); + errno_t res = memcpy_s(data + position, dataLength, &value, 8); + if (res != EOK) { + return; + } #else memcpy(data + position, &value, 8); #endif @@ -161,7 +170,10 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 8); + errno_t res = memcpy_s(data + position, dataLength, &value, 8); + if (res != EOK) { + return; + } #else memcpy(data + position, &value, 8); #endif @@ -175,7 +187,10 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value, 4); + errno_t res = memcpy_s(data + position, dataLength, &value, 4); + if (res != EOK) { + return; + } #else memcpy(data + position, &value, 4); #endif @@ -189,7 +204,10 @@ public: check(8); #ifdef KOALA_NO_UNALIGNED_ACCESS #ifdef __STDC_LIB_EXT1__ - memcpy_s(data + position, dataLength, &value64, 8); + errno_t res = memcpy_s(data + position, dataLength, &value64, 8); + if (res != EOK) { + return; + } #else memcpy(data + position, &value, 8); #endif @@ -247,7 +265,10 @@ public: case 4: suffix = "lpx"; break; } #ifdef __STDC_LIB_EXT1__ - snprintf_s(buf, BUFFER_MAX_LEN, "%.8f%s", value.value, suffix.c_str()); + errno_t res = snprintf_s(buf, BUFFER_MAX_LEN, "%.8f%s", value.value, suffix.c_str()); + if (res != EOK) { + return; + } #else snprintf(buf, BUFFER_MAX_LEN, "%.8f%s", value.value, suffix.c_str()); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc index 0bf2128f0..6f246712d 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc @@ -66,7 +66,10 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { } const CallbackEventKind frontEventKind = callbackEventsQueue.front(); #ifdef __STDC_LIB_EXT1__ - memcpy_s(result, size, &frontEventKind, 4); + errno_t res = memcpy_s(result, size, &frontEventKind, 4); + if (res != EOK) { + return 0; + } #else memcpy(result, &frontEventKind, 4); #endif @@ -75,7 +78,10 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { { case Event_CallCallback: #ifdef __STDC_LIB_EXT1__ - memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + errno_t res = memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + if (res != EOK) { + return 0; + } #else memcpy(result + 4, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); #endif @@ -84,7 +90,10 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { case Event_ReleaseManagedResource: { const InteropInt32 resourceId = callbackResourceSubqueue.front(); #ifdef __STDC_LIB_EXT1__ - memcpy_s(result + 4, size, &frontEventKind, 4); + errno_t res = memcpy_s(result + 4, size, &frontEventKind, 4); + if (res != EOK) { + return 0; + } #else memcpy(result + 4, &resourceId, 4); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc index 2eb8c428f..27f8cf3b6 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/common-interop.cc @@ -129,7 +129,10 @@ void impl_StringData(KNativePointer ptr, KByte* bytes, KUInt size) { string* s = reinterpret_cast(ptr); if (s) { #ifdef __STDC_LIB_EXT1__ - memcpy_s(bytes, size, s->c_str(), size); + errno_t res = memcpy_s(bytes, size, s->c_str(), size); + if (res != EOK) { + return; + } #else memcpy(bytes, s->c_str(), size); #endif @@ -156,7 +159,10 @@ KOALA_INTEROP_1(StringMake, KNativePointer, KStringPtr) // For slow runtimes w/o fast encoders. KInt impl_ManagedStringWrite(const KStringPtr& string, KByte* buffer, KInt offset) { #ifdef __STDC_LIB_EXT1__ - memcpy_s(buffer + offset, string.length() + 1, string.c_str(), string.length() + 1); + errno_t res = memcpy_s(buffer + offset, string.length() + 1, string.c_str(), string.length() + 1); + if (res != EOK) { + return 0; + } #else memcpy(buffer + offset, string.c_str(), string.length() + 1); #endif @@ -471,7 +477,10 @@ KOALA_INTEROP_CTX_1(StdStringToString, KStringPtr, KNativePointer) KInteropReturnBuffer impl_RawReturnData(KVMContext vmContext, KInt v1, KInt v2) { void* data = new int8_t[v1]; #ifdef __STDC_LIB_EXT1__ - memset_s(data, v1, v2, v1); + errno_t res = memset_s(data, v1, v2, v1); + if (res != EOK) { + LOGE("RawReturnData failed"); + } #else memset(data, v2, v1); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc index 01178d096..20d47b8bb 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc @@ -56,8 +56,7 @@ void appendGroupedLog(int index, const char* str) { const char* getGroupedLog(int index) { if (index < static_cast(groupedLogs.size())) { - const char* result = groupedLogs[index]->log.c_str(); - return result; + return groupedLogs[index]->log.c_str(); } return ""; } diff --git a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h index cd4a2fca2..2b89aeb46 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jni/convertors-jni.h @@ -149,7 +149,10 @@ struct SlowInteropTypeConverter { jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); #ifdef __STDC_LIB_EXT1__ - memcpy_s(data, bufferLength, value.data, bufferLength); + errno_t res = memcpy_s(data, bufferLength, value.data, bufferLength); + if (res != EOK) { + return result; + } #else memcpy(data, value.data, bufferLength); #endif @@ -170,7 +173,10 @@ struct SlowInteropTypeConverter { jarray result = env->NewByteArray(bufferLength); void* data = env->GetPrimitiveArrayCritical(result, nullptr); #ifdef __STDC_LIB_EXT1__ - memcpy_s(data, bufferLength, value.data, bufferLength); + errno_t res = memcpy_s(data, bufferLength, value.data, bufferLength); + if (res != EOK) { + return result; + } #else memcpy(data, value.data, bufferLength); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc index 3b5546f4b..5d4114829 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.cc @@ -147,7 +147,10 @@ static JSValueRef u64ToBigInt(JSContextRef context, uint64_t value) { #else char buffer[128] = {0}; #ifdef __STDC_LIB_EXT1__ - std::snprintf_s(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); + errno_t res = std::snprintf_s(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); + if (res != EOK) { + return bigint; + } #else std::snprintf(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); #endif diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h index aaabacf01..022d8e4d6 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h @@ -711,7 +711,7 @@ void InitExports(JSGlobalContextRef globalContext); #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - /* TODO: implement*/ ASSERT(false); \ + ASSERT(false); /* TODO: implement*/ \ return __VA_ARGS__; \ } while (0) diff --git a/koala-wrapper/koalaui/interop/src/cpp/profiler.h b/koala-wrapper/koalaui/interop/src/cpp/profiler.h index dd2483f10..d3b7d5106 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/profiler.h +++ b/koala-wrapper/koalaui/interop/src/cpp/profiler.h @@ -25,6 +25,8 @@ #include #include +constexpr double PERCENTAGE_FACTOR = 100.0; + struct InteropProfilerRecord { int64_t time; int64_t count; @@ -69,11 +71,14 @@ class InteropProfiler { auto count = a.second.count; char buffer[1024]; #ifdef __STDC_LIB_EXT1__ - snprintf_s(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), - (long long)count, (double)ns / total * 100.0, (long long)ns); + errno_t res = snprintf_s(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), + (long long)count, (double)ns / total * PERCENTAGE_FACTOR, (long long)ns); + if (res != EOK) { + return ""; + } #else snprintf(buffer, sizeof buffer, "for %s[%lld]: %.01f%% (%lld)\n", a.first.c_str(), - (long long)count, (double)ns / total * 100.0, (long long)ns); + (long long)count, (double)ns / total * PERCENTAGE_FACTOR, (long long)ns); #endif result += buffer; }); diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h index 2ffa24ba7..710e77370 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h +++ b/koala-wrapper/koalaui/interop/src/cpp/types/koala-types.h @@ -67,7 +67,10 @@ struct KStringPtrImpl { if (_owned) { _value = reinterpret_cast(malloc(len + 1)); #ifdef __STDC_LIB_EXT1__ - memcpy_s(_value, len, data, len); + errno_t res = memcpy_s(_value, len, data, len); + if (res != EOK) { + return; + } #else memcpy(_value, data, len); #endif -- Gitee From 12f171ba3ec9a8f24e79a63b5f580d624a789f83 Mon Sep 17 00:00:00 2001 From: Keerecles Date: Mon, 7 Jul 2025 11:17:51 +0800 Subject: [PATCH 6/6] code clean Signed-off-by: Keerecles Change-Id: I8413df0cae5ecd3e366d6f8698294d9a8e5ed47a --- .../interop/src/cpp/DeserializerBase.h | 71 +++++++++---------- .../interop/src/cpp/callback-resource.cc | 30 ++++---- .../interop/src/cpp/cangjie/convertors-cj.h | 8 +-- .../interop/src/cpp/interop-logging.cc | 3 - .../interop/src/cpp/jsc/convertors-jsc.h | 62 ++++++++-------- .../interop/src/cpp/types/signatures.cc | 20 +++--- 6 files changed, 96 insertions(+), 98 deletions(-) diff --git a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h index c0b7c88c7..a6e2da56d 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h +++ b/koala-wrapper/koalaui/interop/src/cpp/DeserializerBase.h @@ -198,19 +198,19 @@ inline void WriteToString(std::string *result, const InteropFunction* value) template <> inline void WriteToString(std::string *result, const InteropMaterialized *value) { - char hex[20]; -#ifdef __STDC_LIB_EXT1__ - errno_t res = std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); - if (res != EOK) { - return; - } -#else - std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); -#endif - result->append("\""); - result->append("Materialized "); - result->append(hex); - result->append("\""); + char hex[20]; + #ifdef __STDC_LIB_EXT1__ + errno_t res = std::snprintf_s(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + if (res != EOK) { + return; + } + #else + std::snprintf(hex, sizeof(hex), "0x%llx", (long long)value->ptr); + #endif + result->append("\""); + result->append("Materialized "); + result->append(hex); + result->append("\""); } // TODO: generate! @@ -336,29 +336,28 @@ public: { void *keys = nullptr; void *values = nullptr; - if (length > 0) - { - keys = malloc(length * sizeof(K)); -#ifdef __STDC_LIB_EXT1__ - errno_t res = memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); - if (res != EOK) { - return; - } -#else - memset(keys, 0, length * sizeof(K)); -#endif - toClean.push_back(keys); - - values = malloc(length * sizeof(V)); -#ifdef __STDC_LIB_EXT1__ - errno_t res = memset_s(values, length * sizeof(V), 0, length * sizeof(V)); - if (res != EOK) { - return; - } -#else - memset(values, 0, length * sizeof(V)); -#endif - toClean.push_back(values); + if (length > 0) { + keys = malloc(length * sizeof(K)); + #ifdef __STDC_LIB_EXT1__ + errno_t res = memset_s(keys, length * sizeof(K), 0, length * sizeof(K)); + if (res != EOK) { + return; + } + #else + memset(keys, 0, length * sizeof(K)); + #endif + toClean.push_back(keys); + + values = malloc(length * sizeof(V)); + #ifdef __STDC_LIB_EXT1__ + errno_t res = memset_s(values, length * sizeof(V), 0, length * sizeof(V)); + if (res != EOK) { + return; + } + #else + memset(values, 0, length * sizeof(V)); + #endif + toClean.push_back(values); } map->size = length; map->keys = reinterpret_cast(keys); diff --git a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc index 6f246712d..b26d1a3e9 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/callback-resource.cc @@ -26,27 +26,29 @@ static std::deque callbackEventsQueue; static std::deque callbackCallSubqueue; static std::deque callbackResourceSubqueue; -void enqueueCallback(const CallbackBuffer* event) { +void enqueueCallback(const CallbackBuffer* event) +{ callbackEventsQueue.push_back(Event_CallCallback); callbackCallSubqueue.push_back(*event); } -void holdManagedCallbackResource(InteropInt32 resourceId) { +void holdManagedCallbackResource(InteropInt32 resourceId) +{ callbackEventsQueue.push_back(Event_HoldManagedResource); callbackResourceSubqueue.push_back(resourceId); } -void releaseManagedCallbackResource(InteropInt32 resourceId) { +void releaseManagedCallbackResource(InteropInt32 resourceId) +{ callbackEventsQueue.push_back(Event_ReleaseManagedResource); callbackResourceSubqueue.push_back(resourceId); } -KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { +KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) +{ KByte* result = (KByte*)buffer; - if (needReleaseFront) - { - switch (callbackEventsQueue.front()) - { + if (needReleaseFront) { + switch (callbackEventsQueue.front()) { case Event_CallCallback: callbackCallSubqueue.front().resourceHolder.release(); callbackCallSubqueue.pop_front(); @@ -74,11 +76,11 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { memcpy(result, &frontEventKind, 4); #endif - switch (frontEventKind) - { + switch (frontEventKind) { case Event_CallCallback: #ifdef __STDC_LIB_EXT1__ - errno_t res = memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, sizeof(CallbackBuffer::buffer)); + errno_t res = memcpy_s(result + 4, size, callbackCallSubqueue.front().buffer, + sizeof(CallbackBuffer::buffer)); if (res != EOK) { return 0; } @@ -107,12 +109,14 @@ KInt impl_CheckCallbackEvent(KByte* buffer, KInt size) { } KOALA_INTEROP_2(CheckCallbackEvent, KInt, KByte*, KInt) -void impl_ReleaseCallbackResource(InteropInt32 resourceId) { +void impl_ReleaseCallbackResource(InteropInt32 resourceId) +{ releaseManagedCallbackResource(resourceId); } KOALA_INTEROP_V1(ReleaseCallbackResource, KInt) -void impl_HoldCallbackResource(InteropInt32 resourceId) { +void impl_HoldCallbackResource(InteropInt32 resourceId) +{ holdManagedCallbackResource(resourceId); } KOALA_INTEROP_V1(HoldCallbackResource, KInt) diff --git a/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h b/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h index e3fbc8a20..7295002dd 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h +++ b/koala-wrapper/koalaui/interop/src/cpp/cangjie/convertors-cj.h @@ -865,14 +865,14 @@ KOALA_INTEROP_EXPORT void name(InteropTypeConverter::InteropType _p0, \ #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - /* TODO: implement*/ ASSERT(false); \ - return __VA_ARGS__; \ + ASSERT(false); \ + return __VA_ARGS__; \ } while (0) #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ do { \ - /* TODO: implement*/ ASSERT(false); \ - return __VA_ARGS__; \ + ASSERT(false); \ + return __VA_ARGS__; \ } while (0) #endif // CONVERTORS_CJ_H \ No newline at end of file diff --git a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc index 20d47b8bb..459d05f06 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/interop-logging.cc @@ -55,9 +55,6 @@ void appendGroupedLog(int index, const char* str) { } const char* getGroupedLog(int index) { - if (index < static_cast(groupedLogs.size())) { - return groupedLogs[index]->log.c_str(); - } return ""; } diff --git a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h index 022d8e4d6..85509ad8b 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h +++ b/koala-wrapper/koalaui/interop/src/cpp/jsc/convertors-jsc.h @@ -74,86 +74,86 @@ inline Type getArgument(JSContextRef context, size_t argumentCount, const JSValu template <> inline int32_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getInt32(context, arguments[index]); + ASSERT(index < argumentCount); + return getInt32(context, arguments[index]); } template <> inline uint32_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getUInt32(context, arguments[index]); + ASSERT(index < argumentCount); + return getUInt32(context, arguments[index]); } template <> inline uint8_t getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getUInt8(context, arguments[index]); + ASSERT(index < argumentCount); + return getUInt8(context, arguments[index]); } template <> inline KNativePointer getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getPointer(context, arguments[index]); + ASSERT(index < argumentCount); + return getPointer(context, arguments[index]); } template <> inline KFloat getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getFloat(context, arguments[index]); + ASSERT(index < argumentCount); + return getFloat(context, arguments[index]); } template <> inline KStringPtr getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getString(context, arguments[index]); + ASSERT(index < argumentCount); + return getString(context, arguments[index]); } template <> inline KBoolean getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getBoolean(context, arguments[index]); + ASSERT(index < argumentCount); + return getBoolean(context, arguments[index]); } template <> inline KInt* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getInt32Elements(context, arguments[index]); + ASSERT(index < argumentCount); + return getInt32Elements(context, arguments[index]); } template <> inline float* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getFloat32Elements(context, arguments[index]); + ASSERT(index < argumentCount); + return getFloat32Elements(context, arguments[index]); } template <> inline KByte* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getByteElements(context, arguments[index]); + ASSERT(index < argumentCount); + return getByteElements(context, arguments[index]); } template <> inline KStringArray getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getKStringArray(context, arguments[index]); + ASSERT(index < argumentCount); + return getKStringArray(context, arguments[index]); } template <> inline KUShort* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getUShortElements(context, arguments[index]); + ASSERT(index < argumentCount); + return getUShortElements(context, arguments[index]); } template <> inline KNativePointerArray getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getPointerElements(context, arguments[index]); + ASSERT(index < argumentCount); + return getPointerElements(context, arguments[index]); } template <> inline KShort* getArgument(JSContextRef context, size_t argumentCount, const JSValueRef arguments[], int index) { - ASSERT(index < argumentCount); - return getShortElements(context, arguments[index]); + ASSERT(index < argumentCount); + return getShortElements(context, arguments[index]); } JSValueRef makeInt32(JSContextRef context, int32_t value); @@ -657,7 +657,6 @@ void InitExports(JSGlobalContextRef globalContext); } \ MAKE_JSC_EXPORT(name) -// todo: implement properly #define KOALA_INTEROP_CTX_3(name, Ret, P0, P1, P2) \ JSValueRef Jsc_##name(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef *exception) \ { \ @@ -683,7 +682,6 @@ void InitExports(JSGlobalContextRef globalContext); } \ MAKE_JSC_EXPORT(name) -// todo: implement properly #define KOALA_INTEROP_CTX_V3(name, P0, P1, P2) \ JSValueRef Jsc_##name(JSContextRef ctx, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) { \ printf("TODO: implement KOALA_INTEROP_CTX_V3 for jsc"); \ @@ -711,13 +709,13 @@ void InitExports(JSGlobalContextRef globalContext); #define KOALA_INTEROP_THROW(vmContext, object, ...) \ do { \ - ASSERT(false); /* TODO: implement*/ \ + ASSERT(false); \ return __VA_ARGS__; \ } while (0) #define KOALA_INTEROP_THROW_STRING(vmContext, message, ...) \ do { \ - ASSERT(false); /* TODO: implement*/ \ + ASSERT(false); \ return __VA_ARGS__; \ } while (0) diff --git a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc index 75fc13ee6..34253a767 100644 --- a/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc +++ b/koala-wrapper/koalaui/interop/src/cpp/types/signatures.cc @@ -28,19 +28,19 @@ // For types with the same name on ets and jni #define KOALA_INTEROP_TYPEDEF(func, lang, CPP_TYPE, SIG_TYPE, CODE_TYPE) \ - if (std::strcmp(func, "sigType") == 0) if (type == (CPP_TYPE)) return SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0) if (type == (CPP_TYPE)) return CODE_TYPE; + if (std::strcmp(func, "sigType") == 0) if (type == (CPP_TYPE)) return SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0) if (type == (CPP_TYPE)) return CODE_TYPE; // For types with distinct names on ets and jni #define KOALA_INTEROP_TYPEDEF_LS(func, lang, CPP_TYPE, ETS_SIG_TYPE, ETS_CODE_TYPE, JNI_SIG_TYPE, JNI_CODE_TYPE) \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) \ - if (type == (CPP_TYPE)) return ETS_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) \ - if (type == (CPP_TYPE)) return ETS_CODE_TYPE; \ - if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) \ - if (type == (CPP_TYPE)) return JNI_SIG_TYPE; \ - if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) \ - if (type == (CPP_TYPE)) return JNI_CODE_TYPE; + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "ets") == 0) \ + if (type == (CPP_TYPE)) return ETS_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "ets") == 0) \ + if (type == (CPP_TYPE)) return ETS_CODE_TYPE; \ + if (std::strcmp(func, "sigType") == 0 && std::strcmp(lang, "jni") == 0) \ + if (type == (CPP_TYPE)) return JNI_SIG_TYPE; \ + if (std::strcmp(func, "codeType") == 0 && std::strcmp(lang, "jni") == 0) \ + if (type == (CPP_TYPE)) return JNI_CODE_TYPE; #define KOALA_INTEROP_TYPEDEFS(func, lang) \ KOALA_INTEROP_TYPEDEF(func, lang, "void", "V", "void") \ -- Gitee