From 2d0d5196dacc8f3211ea85a2c3a3239ad0796616 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Fri, 12 Sep 2025 18:48:26 +0300 Subject: [PATCH 1/5] include header --- interop/src/cpp/ani/convertors-ani.cc | 4 ++++ interop/src/cpp/kotlin/convertors-kotlin.h | 4 ++++ interop/src/cpp/types/koala-types.h | 7 ++++++- 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/interop/src/cpp/ani/convertors-ani.cc b/interop/src/cpp/ani/convertors-ani.cc index c9ece6e17..f4065c3ae 100644 --- a/interop/src/cpp/ani/convertors-ani.cc +++ b/interop/src/cpp/ani/convertors-ani.cc @@ -15,7 +15,11 @@ #include "convertors-ani.h" #include +#ifdef __cplusplus +#include +#else #include +#endif #include "interop-types.h" #include "signatures.h" diff --git a/interop/src/cpp/kotlin/convertors-kotlin.h b/interop/src/cpp/kotlin/convertors-kotlin.h index 05df34381..c8d4d65ec 100644 --- a/interop/src/cpp/kotlin/convertors-kotlin.h +++ b/interop/src/cpp/kotlin/convertors-kotlin.h @@ -21,7 +21,11 @@ #include #include #include +#ifdef __cplusplus +#include +#else #include +#endif #include "interop-logging.h" #include "interop-utils.h" diff --git a/interop/src/cpp/types/koala-types.h b/interop/src/cpp/types/koala-types.h index d9856c2a8..14d916cb3 100644 --- a/interop/src/cpp/types/koala-types.h +++ b/interop/src/cpp/types/koala-types.h @@ -20,9 +20,14 @@ #include #include #include -#include #include +#ifdef __cplusplus +#include +#else +#include +#endif + #include "interop-types.h" #include "interop-utils.h" -- Gitee From 5caa9a5dce3fe92222681e9c811ff7967d9291a9 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Fri, 12 Sep 2025 18:52:33 +0300 Subject: [PATCH 2/5] edited header --- interop/src/cpp/interop-logging.cc | 5 +++++ interop/src/cpp/ohos/hilog/log.h | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/interop/src/cpp/interop-logging.cc b/interop/src/cpp/interop-logging.cc index 4fb30b938..756067c89 100644 --- a/interop/src/cpp/interop-logging.cc +++ b/interop/src/cpp/interop-logging.cc @@ -14,7 +14,12 @@ */ #include "interop-logging.h" +#ifdef __cplusplus +#include +#else #include +#endif + #include #include diff --git a/interop/src/cpp/ohos/hilog/log.h b/interop/src/cpp/ohos/hilog/log.h index 058d09e26..97fd0f541 100644 --- a/interop/src/cpp/ohos/hilog/log.h +++ b/interop/src/cpp/ohos/hilog/log.h @@ -58,7 +58,12 @@ * * @since 8 */ +#ifdef __cplusplus +#include +#else #include +#endif + #include #ifdef __cplusplus -- Gitee From 8224aced3a9f181c42306fa6ee11212b321d23d6 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Fri, 12 Sep 2025 18:54:45 +0300 Subject: [PATCH 3/5] edit argument name (to avoid Redefinition) --- gn/script/npm.py | 12 ++++++------ ui2abc/gn/command/gen_sdk.py | 4 ++-- ui2abc/libarkts/gn/command/copy.py | 4 ++-- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/gn/script/npm.py b/gn/script/npm.py index 7bcc018a3..147c61aed 100755 --- a/gn/script/npm.py +++ b/gn/script/npm.py @@ -54,19 +54,19 @@ if args.stdlib_path: os.environ["ETS_STDLIB_PATH"] = args.stdlib_path if args.panda_sdk_path: os.environ["PANDA_SDK_PATH"] = args.panda_sdk_path -# os.environ["PANDA_SDK_PATH"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../ui2abc/build/sdk") +# Note: os.environ["PANDA_SDK_PATH"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../ui2abc/build/sdk") -def run(args, dir = None): +def run(args_list, dir = None): os.chdir(dir or project_path) if os.environ.get("KOALA_LOG_STDOUT"): - subprocess.run(["npm"] + args, env=os.environ, text=True, check=True, stderr=subprocess.STDOUT) + subprocess.run(["npm"] + args_list, env=os.environ, text=True, check=True, stderr=subprocess.STDOUT) return - result = subprocess.run(["npm"] + args, capture_output=True, env=os.environ, text=True) + result = subprocess.run(["npm"] + args_list, capture_output=True, env=os.environ, text=True) with open(koala_log, "a+") as f: - f.write(f"npm args: {args}; project: {project_path}:\n" + result.stdout) + f.write(f"npm args: {args_list}; project: {project_path}:\n" + result.stdout) if result.returncode != 0: - f.write(f"npm args: {args}; project: {project_path}:\n" + result.stderr) + f.write(f"npm args: {args_list}; project: {project_path}:\n" + result.stderr) print(open(koala_log, "r").read()) raise Exception("npm failed") f.close() diff --git a/ui2abc/gn/command/gen_sdk.py b/ui2abc/gn/command/gen_sdk.py index f9bb97ea2..1a08504a7 100755 --- a/ui2abc/gn/command/gen_sdk.py +++ b/ui2abc/gn/command/gen_sdk.py @@ -23,8 +23,8 @@ def load_config(config_file): config = json.load(f) return config -def get_compiler_type(os, cpu): - if (os == 'mingw' and cpu == 'x86_64'): +def get_compiler_type(os_name, cpu): + if (os_name == 'mingw' and cpu == 'x86_64'): return 'mingw_x86_64' return 'clang_x64' diff --git a/ui2abc/libarkts/gn/command/copy.py b/ui2abc/libarkts/gn/command/copy.py index c42c37f14..70cd12dcb 100755 --- a/ui2abc/libarkts/gn/command/copy.py +++ b/ui2abc/libarkts/gn/command/copy.py @@ -19,8 +19,8 @@ import shutil import subprocess import sys -def library_ext(os, cpu): - if (os == 'mingw' and cpu == 'x86_64'): +def library_ext(os_name, cpu): + if (os_name == 'mingw' and cpu == 'x86_64'): return 'dll' return 'node' -- Gitee From 4c1c55f96beca54bc8f87c3d53fcb8c2bef51f29 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Fri, 12 Sep 2025 19:18:17 +0300 Subject: [PATCH 4/5] avoid magic numbers --- interop/src/cpp/DeserializerBase.h | 9 +++++---- interop/src/cpp/jni/convertors-jni.cc | 9 ++++++--- interop/src/cpp/napi/convertors-napi.h | 13 +++++++++---- interop/src/cpp/ohos/oh_sk_log.cc | 3 ++- 4 files changed, 22 insertions(+), 12 deletions(-) diff --git a/interop/src/cpp/DeserializerBase.h b/interop/src/cpp/DeserializerBase.h index 360ccaea6..16c8df852 100644 --- a/interop/src/cpp/DeserializerBase.h +++ b/interop/src/cpp/DeserializerBase.h @@ -531,17 +531,18 @@ inline void WriteToString(std::string* result, InteropUInt32 value) { result->append(std::to_string(value)); } +const int BUFFER_FLOAT_NUMBER = 20; template<> 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]; + char buf[BUFFER_FLOAT_NUMBER]; interop_snprintf(buf, sizeof buf, "%f", value); result->append(buf); #else std::string storage; - storage.resize(20); + storage.resize(BUFFER_FLOAT_NUMBER); // We use to_chars() to avoid locale issues. auto rc = std::to_chars(storage.data(), storage.data() + storage.size(), value); storage.resize(rc.ptr - storage.data()); @@ -553,12 +554,12 @@ inline void WriteToString(std::string* result, InteropFloat64 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]; + char buf[BUFFER_FLOAT_NUMBER]; interop_snprintf(buf, sizeof buf, "%f", value); result->append(buf); #else std::string storage; - storage.resize(20); + storage.resize(BUFFER_FLOAT_NUMBER); // We use to_chars() to avoid locale issues. auto rc = std::to_chars(storage.data(), storage.data() + storage.size(), value); storage.resize(rc.ptr - storage.data()); diff --git a/interop/src/cpp/jni/convertors-jni.cc b/interop/src/cpp/jni/convertors-jni.cc index 7918b9721..1c16a4ebd 100644 --- a/interop/src/cpp/jni/convertors-jni.cc +++ b/interop/src/cpp/jni/convertors-jni.cc @@ -26,6 +26,9 @@ static const char* callCallbackFromNative = "callCallbackFromNative"; static const char* callCallbackFromNativeSig = "(I[BI)I"; const bool registerByOne = true; +const size_t METHOD_NAME_IDX = 0; +const size_t METHOD_SIGNATURE_IDX = 1; +const size_t METHOD_FN_PTR_IDX = 2; static bool registerNatives( JNIEnv* env, jclass clazz, const std::vector> impls) @@ -34,9 +37,9 @@ static bool registerNatives( JNINativeMethod* methods = new JNINativeMethod[numMethods]; bool result = true; for (size_t i = 0; i < numMethods; i++) { - methods[i].name = (char*)std::get<0>(impls[i]).c_str(); - methods[i].signature = (char*)std::get<1>(impls[i]).c_str(); - methods[i].fnPtr = std::get<2>(impls[i]); + methods[i].name = (char*)std::get(impls[i]).c_str(); + methods[i].signature = (char*)std::get(impls[i]).c_str(); + methods[i].fnPtr = std::get(impls[i]); if (registerByOne) { result &= (env->RegisterNatives(clazz, methods + i, 1) >= 0); if (env->ExceptionCheck()) { diff --git a/interop/src/cpp/napi/convertors-napi.h b/interop/src/cpp/napi/convertors-napi.h index 4b86ca1eb..6026d4b55 100644 --- a/interop/src/cpp/napi/convertors-napi.h +++ b/interop/src/cpp/napi/convertors-napi.h @@ -203,16 +203,21 @@ struct InteropTypeConverter { static inline void release(napi_env env, InteropType value, KVMObjectHandle converted) {} }; +const UINT8_ARRAY_SIZE = 1; +const UINT16_ARRAY_SIZE = 2; +const UINT32_ARRAY_SIZE = 4; +const BIGUINT64_ARRAY_SIZE = 8; + inline napi_typedarray_type getNapiType(KInt size) { switch (size) { - case 1: + case UINT8_ARRAY_SIZE: return napi_uint8_array; - case 2: + case UINT16_ARRAY_SIZE: return napi_uint16_array; - case 4: + case UINT32_ARRAY_SIZE: return napi_uint32_array; - case 8: + case BIGUINT64_ARRAY_SIZE: return napi_biguint64_array; default: break; diff --git a/interop/src/cpp/ohos/oh_sk_log.cc b/interop/src/cpp/ohos/oh_sk_log.cc index c17e605f7..5a4320249 100644 --- a/interop/src/cpp/ohos/oh_sk_log.cc +++ b/interop/src/cpp/ohos/oh_sk_log.cc @@ -26,6 +26,7 @@ #include "interop-utils.h" static const char* KOALAUI_OHOS_LOG_ROOT = "/data/storage/el2/base/files/logs"; +const int RATIO = 1000; #define APPLY_LOG_FILE_PATTERN(buf, bufLen, t, ms, pid) \ interop_sprintf(buf, bufLen, "%s/%d_%d_%d_%lld.pid%d.log", KOALAUI_OHOS_LOG_ROOT, (t).tm_year + 1900, \ @@ -67,7 +68,7 @@ void oh_sk_file_log(oh_sk_log_type type, const char* msg, ...) return; 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)); + lt.tm_sec, ms.tv_usec / RATIO, oh_sk_log_type_str(type)); va_list args; va_start(args, msg); -- Gitee From 654e16d50d671bb17e46763d34ac249d5f9a6869 Mon Sep 17 00:00:00 2001 From: twx1232375 Date: Fri, 12 Sep 2025 19:26:45 +0300 Subject: [PATCH 5/5] other --- gn/command/npm_util.py | 10 +++++----- interop/src/cpp/SerializerBase.h | 4 +++- interop/src/cpp/jsc/convertors-jsc.cc | 5 +++-- interop/src/cpp/napi/convertors-napi.h | 8 ++++---- interop/src/cpp/ohos/hilog/log.h | 3 +-- 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/gn/command/npm_util.py b/gn/command/npm_util.py index 2dec5865c..eebddb8a8 100755 --- a/gn/command/npm_util.py +++ b/gn/command/npm_util.py @@ -53,17 +53,17 @@ if args.stdlib_path: os.environ["PANDA_SDK_PATH"] = os.path.join(os.path.dirname(os.path.realpath(__file__)), "../../ui2abc/build/sdk") -def run(args, dir = None): +def run(args_list, dir = None): os.chdir(dir or project_path) if os.environ.get("KOALA_LOG_STDOUT"): - subprocess.run(["npm"] + args, env=os.environ, text=True, check=True, stderr=subprocess.STDOUT) + subprocess.run(["npm"] + args_list, env=os.environ, text=True, check=True, stderr=subprocess.STDOUT) return - result = subprocess.run(["npm"] + args, capture_output=True, env=os.environ, text=True) + result = subprocess.run(["npm"] + args_list, capture_output=True, env=os.environ, text=True) with open(koala_log, "a+") as f: - f.write(f"npm args: {args}; project: {project_path}:\n" + result.stdout) + f.write(f"npm args: {args_list}; project: {project_path}:\n" + result.stdout) if result.returncode != 0: - f.write(f"npm args: {args}; project: {project_path}:\n" + result.stderr) + f.write(f"npm args: {args_list}; project: {project_path}:\n" + result.stderr) print(open(koala_log, "r").read()) raise Exception("npm failed") f.close() diff --git a/interop/src/cpp/SerializerBase.h b/interop/src/cpp/SerializerBase.h index 2b9e484e1..df8149192 100644 --- a/interop/src/cpp/SerializerBase.h +++ b/interop/src/cpp/SerializerBase.h @@ -54,6 +54,8 @@ inline InteropRuntimeType runtimeType(const InteropObject& value) return INTEROP_RUNTIME_OBJECT; } +const uint32_t INITIAL_SERIALIZER_DATA_LENGTH = 256; + class SerializerBase { private: uint8_t* data; @@ -79,7 +81,7 @@ public: SerializerBase(CallbackResourceHolder* resourceHolder = nullptr) : position(0), ownData(true), resourceHolder(resourceHolder) { - this->dataLength = 256; + this->dataLength = INITIAL_SERIALIZER_DATA_LENGTH; this->data = reinterpret_cast(malloc(this->dataLength)); if (!this->data) { INTEROP_FATAL("Cannot allocate memory"); diff --git a/interop/src/cpp/jsc/convertors-jsc.cc b/interop/src/cpp/jsc/convertors-jsc.cc index 98d3a994e..86a87195c 100644 --- a/interop/src/cpp/jsc/convertors-jsc.cc +++ b/interop/src/cpp/jsc/convertors-jsc.cc @@ -152,11 +152,12 @@ static JSValueRef u64ToBigInt(JSContextRef context, uint64_t value) #ifdef KOALA_JSC_USE_CALLBACK_CAST // Improve: benchmark this JSObjectRef bigIntFromParts = getBigIntFromParts(context); - JSValueRef parts[2] = { + const int partsSize = 2; + JSValueRef parts[partsSize] = { JSValueMakeNumber(context, (double)(value >> 32)), JSValueMakeNumber(context, (double)(value & 0xFFFFFFFF)), }; - bigint = JSObjectCallAsFunction(context, bigIntFromParts, nullptr, 2, parts, nullptr); + bigint = JSObjectCallAsFunction(context, bigIntFromParts, nullptr, partsSize, parts, nullptr); #else char buffer[128] = { 0 }; interop_snprintf(buffer, sizeof(buffer) - 1, "%zun", static_cast(value)); diff --git a/interop/src/cpp/napi/convertors-napi.h b/interop/src/cpp/napi/convertors-napi.h index 6026d4b55..5a930c1b0 100644 --- a/interop/src/cpp/napi/convertors-napi.h +++ b/interop/src/cpp/napi/convertors-napi.h @@ -203,10 +203,10 @@ struct InteropTypeConverter { static inline void release(napi_env env, InteropType value, KVMObjectHandle converted) {} }; -const UINT8_ARRAY_SIZE = 1; -const UINT16_ARRAY_SIZE = 2; -const UINT32_ARRAY_SIZE = 4; -const BIGUINT64_ARRAY_SIZE = 8; +const KInt UINT8_ARRAY_SIZE = 1; +const KInt UINT16_ARRAY_SIZE = 2; +const KInt UINT32_ARRAY_SIZE = 4; +const KInt BIGUINT64_ARRAY_SIZE = 8; inline napi_typedarray_type getNapiType(KInt size) { diff --git a/interop/src/cpp/ohos/hilog/log.h b/interop/src/cpp/ohos/hilog/log.h index 97fd0f541..c1beac5b6 100644 --- a/interop/src/cpp/ohos/hilog/log.h +++ b/interop/src/cpp/ohos/hilog/log.h @@ -62,9 +62,8 @@ #include #else #include -#endif - #include +#endif #ifdef __cplusplus extern "C" { -- Gitee