From f488dd13f84f69a41af14e2420a04c3cb03dfcfd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 14 Aug 2025 20:08:59 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E6=80=A7=E8=83=BD?= =?UTF-8?q?=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Icad5dc6153765ebc22f6f11ee26cb2d8c10dc56e Signed-off-by: 姜小林 --- .../js/src/common/ani_helper/ani_helper.h | 61 ++++++------ .../src/common/ani_helper/ani_signature.cpp | 2 +- .../js/src/common/ani_helper/error_handler.h | 12 +-- .../class_atomicfile/ani/atomicfile_ani.cpp | 6 +- .../mod_fs/class_file/ani/file_wrapper.cpp | 13 +-- .../ani/randomaccessfile_ani.cpp | 47 ++++----- .../ani/reader_iterator_result_ani.cpp | 4 +- .../mod_fs/class_stat/ani/stat_wrapper.cpp | 97 +++++++------------ .../ani/task_signal_wrapper.cpp | 2 +- .../ani/create_randomaccessfile_ani.cpp | 4 +- .../class_hashstream/ani/hashstream_ani.cpp | 4 +- 11 files changed, 104 insertions(+), 148 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_helper.h b/interfaces/kits/js/src/common/ani_helper/ani_helper.h index f600a41df..8a2e69ec5 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_helper.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_helper.h @@ -33,7 +33,8 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; -inline shared_ptr& GetMainHandler() { +inline shared_ptr &GetMainHandler() +{ thread_local shared_ptr mainHandler; return mainHandler; } @@ -41,60 +42,54 @@ inline shared_ptr& GetMainHandler() { class AniHelper { public: template - static ani_status SetFieldValue( - ani_env *env, const ani_class &cls, ani_object &obj, const char *fieldName, const T &value) + static ani_status SetFieldValue(ani_env *env, ani_object &obj, const string_view &key, const T &value) { - ani_field field; - auto status = env->Class_FindField(cls, fieldName, &field); - if (status != ANI_OK) { - return status; - } - + ani_status status = ANI_ERROR; if constexpr (is_same_v || is_same_v || is_same_v) { - status = env->Object_SetField_Int(obj, field, value); - } else if constexpr (is_same_v || is_same_v) { - status = env->Object_SetField_Long(obj, field, value); + status = env->Object_SetFieldByName_Int(obj, key.data(), static_cast(value)); + } else if constexpr (is_same_v || is_same_v || is_same_v) { + status = env->Object_SetFieldByName_Long(obj, key.data(), static_cast(value)); } else if constexpr (is_same_v || is_same_v) { - status = env->Object_SetField_Double(obj, field, value); + status = env->Object_SetFieldByName_Double(obj, key.data(), static_cast(value)); } else if constexpr (is_same_v || is_same_v) { - status = env->Object_SetField_Boolean(obj, field, value); + status = env->Object_SetFieldByName_Boolean(obj, key.data(), static_cast(value)); } else if constexpr (is_same_v || is_same_v) { auto [succ, aniStr] = TypeConverter::ToAniString(env, value); if (!succ) { return ANI_ERROR; } - status = env->Object_SetField_Ref(obj, field, move(aniStr)); + status = env->Object_SetFieldByName_Ref(obj, key.data(), move(aniStr)); } else if constexpr (is_base_of_v) { - status = env->Object_SetField_Ref(obj, field, value); + status = env->Object_SetFieldByName_Ref(obj, key.data(), value); } else { + HILOGE("Invalid ani value type!"); return ANI_INVALID_TYPE; } return status; } template - static ani_status SetPropertyValue( - ani_env *env, const ani_class &cls, ani_object &obj, const string &property, const T &value) + static ani_status SetPropertyValue(ani_env *env, ani_object &obj, const string_view &key, const T &value) { - ani_method method; - string setter = "" + property; - auto status = env->Class_FindMethod(cls, setter.c_str(), nullptr, &method); - if (status != ANI_OK) { - return status; - } - - if constexpr (is_same_v || is_same_v) { + ani_status status = ANI_ERROR; + if constexpr (is_same_v || is_same_v || is_same_v) { + status = env->Object_SetPropertyByName_Int(obj, key.data(), static_cast(value)); + } else if constexpr (is_same_v || is_same_v || is_same_v) { + status = env->Object_SetPropertyByName_Long(obj, key.data(), static_cast(value)); + } else if constexpr (is_same_v || is_same_v) { + status = env->Object_SetPropertyByName_Double(obj, key.data(), static_cast(value)); + } else if constexpr (is_same_v || is_same_v) { + status = env->Object_SetPropertyByName_Boolean(obj, key.data(), static_cast(value)); + } else if constexpr (is_same_v || is_same_v) { auto [succ, aniStr] = TypeConverter::ToAniString(env, value); if (!succ) { return ANI_ERROR; } - status = env->Object_CallMethod_Void(obj, method, move(aniStr)); - } else if constexpr (is_base_of_v || is_same_v || is_same_v || - is_same_v || is_same_v || is_same_v || - is_same_v || is_same_v || is_same_v || - is_same_v) { - status = env->Object_CallMethod_Void(obj, method, value); + status = env->Object_SetPropertyByName_Ref(obj, key.data(), move(aniStr)); + } else if constexpr (is_base_of_v) { + status = env->Object_SetPropertyByName_Ref(obj, key.data(), value); } else { + HILOGE("Invalid ani value type!"); return ANI_INVALID_TYPE; } return status; @@ -186,7 +181,7 @@ public: return false; } - auto& mainHandler = GetMainHandler(); + auto &mainHandler = GetMainHandler(); if (mainHandler == nullptr) { shared_ptr runner = OHOS::AppExecFwk::EventRunner::GetMainEventRunner(); if (!runner) { diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp index a632f6f88..dcac37802 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -64,7 +64,7 @@ const string BuiltInTypes::ArrayBuffer::ctorSig = Builder::BuildSignatureDescrip // BuiltInTypes::BigInt const Type BuiltInTypes::BigInt::classType = Builder::BuildClass("escompat.BigInt"); const string BuiltInTypes::BigInt::classDesc = BuiltInTypes::BigInt::classType.Descriptor(); -const string BuiltInTypes::BigInt::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::doubleType }); +const string BuiltInTypes::BigInt::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); // BuiltInTypes::BusinessError const Type BuiltInTypes::BusinessError::classType = Builder::BuildClass("@ohos.base.BusinessError"); const string BuiltInTypes::BusinessError::classDesc = BuiltInTypes::BusinessError::classType.Descriptor(); diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.h b/interfaces/kits/js/src/common/ani_helper/error_handler.h index b1c1c7f5a..c9a9f7a49 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -106,28 +106,22 @@ private: return { ANI_ERROR, nullptr }; } - auto [succ, message] = TypeConverter::ToAniString(env, errMsg); - if (!succ) { - HILOGE("Convert errMsg to ani string failed"); - return { ANI_ERROR, nullptr }; - } - ani_status status = ANI_ERROR; - status = env->Object_SetPropertyByName_Ref(obj, "message", message); + status = AniHelper::SetPropertyValue(env, obj, "message", errMsg); if (status != ANI_OK) { HILOGE("Set property 'message' value failed"); return { status, nullptr }; } - status = env->Object_SetPropertyByName_Int(obj, "code", code); + status = AniHelper::SetPropertyValue(env, obj, "code", code); if (status != ANI_OK) { HILOGE("Set property 'code' value failed"); return { status, nullptr }; } if (errData.has_value()) { - status = env->Object_SetPropertyByName_Ref(obj, "data", errData.value()); + status = AniHelper::SetPropertyValue(env, obj, "data", errData.value()); if (status != ANI_OK) { HILOGE("Set property 'data' value failed"); return { status, nullptr }; diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index 71666f6ea..b93dd102e 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -53,9 +53,9 @@ void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj return; } - if (ANI_OK != - env->Object_SetFieldByName_Long(obj, "nativePtr", - static_cast(reinterpret_cast(ret.GetData().value())))) { + ani_long ptr = static_cast(reinterpret_cast(ret.GetData().value())); + + if (ANI_OK != AniHelper::SetFieldValue(env, obj, "nativePtr", ptr)) { HILOGE("Failed to wrap entity for obj AtomicFile"); ErrorHandler::Throw(env, EIO); return; diff --git a/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp index b314615db..6ae6ae46e 100644 --- a/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_file/ani/file_wrapper.cpp @@ -50,9 +50,10 @@ static bool SetFileProperties(ani_env *env, ani_class cls, ani_object obj, const return false; } + ani_status ret; const auto &fd = fdRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", fd)) { - HILOGE("Set fd field value failed!"); + if ((ret = AniHelper::SetPropertyValue(env, obj, "fd", fd)) != ANI_OK) { + HILOGE("Set fd field value failed! ret: %{public}d", ret); return false; } @@ -63,8 +64,8 @@ static bool SetFileProperties(ani_env *env, ani_class cls, ani_object obj, const } const auto &path = pathRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "path", path)) { - HILOGE("Set path field value failed!"); + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "path", path)) { + HILOGE("Set path field value failed! ret: %{public}d", ret); return false; } @@ -75,8 +76,8 @@ static bool SetFileProperties(ani_env *env, ani_class cls, ani_object obj, const } const auto &name = nameRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "name", name)) { - HILOGE("Set name field value failed!"); + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "name", name)) { + HILOGE("Set name field value failed! ret: %{public}d", ret); return false; } return true; diff --git a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp index b89bec3eb..2109da97b 100644 --- a/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_randomaccessfile/ani/randomaccessfile_ani.cpp @@ -305,26 +305,20 @@ static ani_object CreateReadStreamOptions(ani_env *env, int64_t start, int64_t e return nullptr; } - ani_field startField = nullptr; - if (ANI_OK != env->Class_FindField(cls, "start", &startField)) { - HILOGE("Cannot find start in class %s", className); - return nullptr; - } - - ani_field endField = nullptr; - if (ANI_OK != env->Class_FindField(cls, "end", &endField)) { - HILOGE("Cannot find end in class %s", className); - return nullptr; - } - if (start >= 0) { - env->Object_SetField_Int(obj, startField, start); + auto ret = AniHelper::SetFieldValue(env, obj, "start", start); + if (ret != ANI_OK) { + HILOGE("Set 'start' field value failed! ret: %{public}d", ret); + return nullptr; + } } + if (end >= 0) { - env->Object_SetField_Int(obj, endField, end); - } - if (obj == nullptr) { - HILOGE("CreateReadStreamOptions is nullptr"); + auto ret = AniHelper::SetFieldValue(env, obj, "end", end); + if (ret != ANI_OK) { + HILOGE("Set 'end' field value failed! ret: %{public}d", ret); + return nullptr; + } } return move(obj); @@ -351,21 +345,18 @@ static ani_object CreateWriteStreamOptions(ani_env *env, int64_t start, int flag return nullptr; } - ani_field modeField = nullptr; - if (ANI_OK != env->Class_FindField(cls, "mode", &modeField)) { - HILOGE("Cannot find mode in class %s", className); - return nullptr; - } - - ani_field startField = nullptr; - if (ANI_OK != env->Class_FindField(cls, "start", &startField)) { - HILOGE("Cannot find start in class %s", className); + auto ret = AniHelper::SetFieldValue(env, obj, "mode", flags); + if (ret != ANI_OK) { + HILOGE("Set 'mode' field value failed! ret: %{public}d", ret); return nullptr; } - env->Object_SetField_Int(obj, modeField, flags); if (start >= 0) { - env->Object_SetField_Int(obj, startField, start); + ret = AniHelper::SetFieldValue(env, obj, "start", start); + if (ret != ANI_OK) { + HILOGE("Set 'start' field value failed! ret: %{public}d", ret); + return nullptr; + } } return move(obj); diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp index b8efe806e..c36e63060 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_result_ani.cpp @@ -58,13 +58,13 @@ ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResul } const auto &done = result->done; - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "done", static_cast(done))) { + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "done", done)) { HILOGE("Set 'done' field value failed!"); return nullptr; } const auto &value = result->value; - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "value", value)) { + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "value", value)) { HILOGE("Set 'value' field value failed!"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp index 656e8e861..43f91bb0b 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp @@ -32,21 +32,7 @@ using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; -static ani_status SetNumProperty( - ani_env *env, const ani_class &cls, ani_object &object, const char *name, ani_long &value) -{ - ani_method setter; - ani_status ret; - if ((ret = env->Class_FindMethod(cls, name, nullptr, &setter)) != ANI_OK) { - HILOGE("Class_FindMethod Fail %{private}s, err: %{private}d", name, ret); - return ret; - } - - return env->Object_CallMethod_Void(object, setter, value); -} - -static ani_status SetBigIntProperty( - ani_env *env, const ani_class &statCls, ani_object &statObject, const char *name, ani_double &value) +static ani_status SetBigIntProperty(ani_env *env, ani_object &statObject, const char *name, int64_t value) { ani_object object = {}; auto classDesc = BuiltInTypes::BigInt::classDesc.c_str(); @@ -71,13 +57,13 @@ static ani_status SetBigIntProperty( return ret; } - ani_method setter; - if ((ret = env->Class_FindMethod(statCls, name, nullptr, &setter)) != ANI_OK) { - HILOGE("Class_FindMethod Fail %{private}s, err: %{private}d", name, ret); + ret = AniHelper::SetPropertyValue(env, statObject, name, object); + if (ret != ANI_OK) { + HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", name, ret); return ret; } - return env->Object_CallMethod_Void(statObject, setter, object); + return ANI_OK; } static ani_enum_item GetLocationEnumIndex(ani_env *env, const Location &value) @@ -107,75 +93,64 @@ static ani_enum_item GetLocationEnumIndex(ani_env *env, const Location &value) return enumItem; } -static ani_status SetEnumLocation( - ani_env *env, const ani_class &cls, ani_object &object, const char *name, const Location &value) +static ani_status SetEnumLocation(ani_env *env, ani_object &object, const char *name, const Location &value) { - ani_method setter; - ani_status ret; - if ((ret = env->Class_FindMethod(cls, name, nullptr, &setter)) != ANI_OK) { - HILOGE("Class_FindMethod Fail %{private}s, err: %{private}d", name, ret); + ani_enum_item location = GetLocationEnumIndex(env, value); + if (location == nullptr) { + return ANI_ERROR; + } + + ani_status ret = AniHelper::SetPropertyValue(env, object, name, location); + if (ret != ANI_OK) { + HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", name, ret); return ret; } - return env->Object_CallMethod_Void(object, setter, GetLocationEnumIndex(env, value)); + return ANI_OK; } -const static string MODE_SETTER = Builder::BuildSetterName("mode"); -const static string UID_SETTER = Builder::BuildSetterName("uid"); -const static string GID_SETTER = Builder::BuildSetterName("gid"); -const static string SIZE_SETTER = Builder::BuildSetterName("size"); -const static string ATIME_SETTER = Builder::BuildSetterName("atime"); -const static string MTIME_SETTER = Builder::BuildSetterName("mtime"); -const static string CTIME_SETTER = Builder::BuildSetterName("ctime"); -const static string INO_SETTER = Builder::BuildSetterName("ino"); -const static string ATIME_NS_SETTER = Builder::BuildSetterName("atimeNs"); -const static string MTIME_NS_SETTER = Builder::BuildSetterName("mtimeNs"); -const static string CTIME_NS_SETTER = Builder::BuildSetterName("ctimeNs"); -const static string LOCATION_SETTER = Builder::BuildSetterName("location"); - -static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object &statObject, FsStat *fsStat) +static ani_status SetProperties(ani_env *env, ani_object &statObject, FsStat *fsStat) { ani_status ret; - vector> numProperties = { - { MODE_SETTER, fsStat->GetMode() }, - { UID_SETTER, fsStat->GetUid() }, - { GID_SETTER, fsStat->GetGid() }, - { SIZE_SETTER, fsStat->GetSize() }, - { ATIME_SETTER, fsStat->GetAtime() }, - { MTIME_SETTER, fsStat->GetMtime() }, - { CTIME_SETTER, fsStat->GetCtime() }, + vector> numProperties = { + { "mode", fsStat->GetMode() }, + { "uid", fsStat->GetUid() }, + { "gid", fsStat->GetGid() }, + { "size", fsStat->GetSize() }, + { "atime", fsStat->GetAtime() }, + { "mtime", fsStat->GetMtime() }, + { "ctime", fsStat->GetCtime() }, }; for (auto iter : numProperties) { auto key = iter.first.data(); auto value = iter.second; - ret = SetNumProperty(env, cls, statObject, key, value); + ret = AniHelper::SetPropertyValue(env, statObject, key, value); if (ret != ANI_OK) { - HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", key, ret); + HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", key, ret); return ret; } } - vector> bigIntProperties = { - { INO_SETTER, ani_double(static_cast(fsStat->GetIno())) }, - { ATIME_NS_SETTER, ani_double(static_cast(fsStat->GetAtimeNs())) }, - { MTIME_NS_SETTER, ani_double(static_cast(fsStat->GetMtimeNs())) }, - { CTIME_NS_SETTER, ani_double(static_cast(fsStat->GetCtimeNs())) }, + vector> bigIntProperties = { + { "ino", fsStat->GetIno() }, + { "atimeNs", fsStat->GetAtimeNs() }, + { "mtimeNs", fsStat->GetMtimeNs() }, + { "ctimeNs", fsStat->GetCtimeNs() }, }; for (auto iter : bigIntProperties) { auto key = iter.first.data(); auto value = iter.second; - ret = SetBigIntProperty(env, cls, statObject, key, value); + ret = SetBigIntProperty(env, statObject, key, value); if (ret != ANI_OK) { - HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", key, ret); + HILOGE("SetBigIntProperty Fail %{private}s, err: %{private}d", key, ret); return ret; } } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - auto key = LOCATION_SETTER.c_str(); - if ((ret = SetEnumLocation(env, cls, statObject, key, static_cast(fsStat->GetLocation()))) != ANI_OK) { - HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", key, ret); + if ((ret = SetEnumLocation(env, statObject, "location", static_cast(fsStat->GetLocation()))) != ANI_OK) { + HILOGE("SetEnumLocation Fail, err: %{private}d", ret); return ret; } #endif @@ -212,7 +187,7 @@ ani_object StatWrapper::Wrap(ani_env *env, FsStat *fsStat) return nullptr; } - if ((ret = SetProperties(env, cls, statObject, fsStat)) != ANI_OK) { + if ((ret = SetProperties(env, statObject, fsStat)) != ANI_OK) { HILOGE("SetProperties Fail, err: %{private}d", ret); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp index 79be64231..3a38f9d74 100644 --- a/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_tasksignal/ani/task_signal_wrapper.cpp @@ -56,7 +56,7 @@ bool TaskSignalWrapper::Wrap(ani_env *env, ani_object object, const FsTaskSignal ani_long ptr = static_cast(reinterpret_cast(signal)); - auto status = env->Object_SetFieldByName_Long(object, "nativeTaskSignal", ptr); + auto status = AniHelper::SetFieldValue(env, object, "nativeTaskSignal", ptr); if (status != ANI_OK) { HILOGE("Wrap taskSignal obj failed! status: %{public}d", status); return false; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp index f2a7ecc9c..51e7ac53d 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_randomaccessfile_ani.cpp @@ -66,7 +66,7 @@ static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) } const auto &fd = fdRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", fd)) { + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "fd", fd)) { HILOGE("Set fd field value failed!"); return nullptr; } @@ -78,7 +78,7 @@ static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) } const auto &fp = fpRet.GetData().value(); - if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "filePointer", fp)) { + if (ANI_OK != AniHelper::SetPropertyValue(env, obj, "filePointer", fp)) { HILOGE("Set fp field value failed!"); return nullptr; } diff --git a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp index 49079f114..440f5c6a5 100644 --- a/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp +++ b/interfaces/kits/js/src/mod_hash/class_hashstream/ani/hashstream_ani.cpp @@ -109,8 +109,8 @@ void HashStreamAni::Constructor(ani_env *env, ani_object obj, ani_string alg) return; } - if (ANI_OK != - env->Object_SetFieldByName_Long(obj, "nativePtr", reinterpret_cast(ret.GetData().value()))) { + ani_long ptr = static_cast(reinterpret_cast(ret.GetData().value())); + if (ANI_OK != AniHelper::SetFieldValue(env, obj, "nativePtr", ptr)) { HILOGE("Failed to wrap entity for obj HashStream"); ErrorHandler::Throw(env, EIO); return; -- Gitee From 1e0d1a726955a3d7106239b70a51312a190a9eb3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Thu, 14 Aug 2025 21:48:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=A0=B9=E6=8D=AE=E6=A3=80=E8=A7=86?= =?UTF-8?q?=E6=84=8F=E8=A7=81=E8=BF=9B=E8=A1=8C=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: I166aa5cd1cf73ff8796b7957f2a9af9836b8e4b8 Signed-off-by: 姜小林 --- .../js/src/common/ani_helper/ani_helper.h | 4 +-- .../class_atomicfile/ani/atomicfile_ani.cpp | 1 - .../mod_fs/class_stat/ani/stat_wrapper.cpp | 30 +++++++++---------- 3 files changed, 17 insertions(+), 18 deletions(-) diff --git a/interfaces/kits/js/src/common/ani_helper/ani_helper.h b/interfaces/kits/js/src/common/ani_helper/ani_helper.h index 8a2e69ec5..bd5ca58ff 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_helper.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_helper.h @@ -59,7 +59,7 @@ public: return ANI_ERROR; } status = env->Object_SetFieldByName_Ref(obj, key.data(), move(aniStr)); - } else if constexpr (is_base_of_v) { + } else if constexpr (std::is_pointer_v && std::is_base_of_v<__ani_ref, std::remove_pointer_t>) { status = env->Object_SetFieldByName_Ref(obj, key.data(), value); } else { HILOGE("Invalid ani value type!"); @@ -86,7 +86,7 @@ public: return ANI_ERROR; } status = env->Object_SetPropertyByName_Ref(obj, key.data(), move(aniStr)); - } else if constexpr (is_base_of_v) { + } else if constexpr (std::is_pointer_v && std::is_base_of_v<__ani_ref, std::remove_pointer_t>) { status = env->Object_SetPropertyByName_Ref(obj, key.data(), value); } else { HILOGE("Invalid ani value type!"); diff --git a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp index b93dd102e..614c53008 100644 --- a/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_atomicfile/ani/atomicfile_ani.cpp @@ -54,7 +54,6 @@ void AtomicFileAni::Constructor(ani_env *env, ani_object obj, ani_string pathObj } ani_long ptr = static_cast(reinterpret_cast(ret.GetData().value())); - if (ANI_OK != AniHelper::SetFieldValue(env, obj, "nativePtr", ptr)) { HILOGE("Failed to wrap entity for obj AtomicFile"); ErrorHandler::Throw(env, EIO); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp index 43f91bb0b..63daa0de9 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.cpp @@ -40,7 +40,7 @@ static ani_status SetBigIntProperty(ani_env *env, ani_object &statObject, const ani_status ret; if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); + HILOGE("Not found %{public}s, err: %{public}d", classDesc, ret); return ret; } @@ -48,18 +48,18 @@ static ani_status SetBigIntProperty(ani_env *env, ani_object &statObject, const auto ctorSig = BuiltInTypes::BigInt::ctorSig.c_str(); ani_method ctor; if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { - HILOGE("Not found ctor, err: %{private}d", ret); + HILOGE("Not found ctor, err: %{public}d", ret); return ret; } if ((ret = env->Object_New(cls, ctor, &object, value)) != ANI_OK) { - HILOGE("New BigIntProperty Fail, err: %{private}d", ret); + HILOGE("New BigIntProperty Fail, err: %{public}d", ret); return ret; } ret = AniHelper::SetPropertyValue(env, statObject, name, object); if (ret != ANI_OK) { - HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", name, ret); + HILOGE("SetPropertyValue Fail %{public}s, err: %{public}d", name, ret); return ret; } @@ -72,7 +72,7 @@ static ani_enum_item GetLocationEnumIndex(ani_env *env, const Location &value) auto classDesc = FS::LocationType::classDesc.c_str(); ani_status ret = env->FindEnum(classDesc, &enumType); if (ret != ANI_OK) { - HILOGE("FindEnum %{private}s failed, err: %{private}d", classDesc, ret); + HILOGE("FindEnum %{public}s failed, err: %{public}d", classDesc, ret); return nullptr; } @@ -87,7 +87,7 @@ static ani_enum_item GetLocationEnumIndex(ani_env *env, const Location &value) ani_enum_item enumItem; ret = env->Enum_GetEnumItemByIndex(enumType, index, &enumItem); if (ret != ANI_OK) { - HILOGE("Enum_GetEnumItemByIndex failed, index: %{private}zu, err: %{private}d", index, ret); + HILOGE("Enum_GetEnumItemByIndex failed, index: %{public}zu, err: %{public}d", index, ret); return nullptr; } return enumItem; @@ -102,7 +102,7 @@ static ani_status SetEnumLocation(ani_env *env, ani_object &object, const char * ani_status ret = AniHelper::SetPropertyValue(env, object, name, location); if (ret != ANI_OK) { - HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", name, ret); + HILOGE("SetPropertyValue Fail %{public}s, err: %{public}d", name, ret); return ret; } @@ -127,7 +127,7 @@ static ani_status SetProperties(ani_env *env, ani_object &statObject, FsStat *fs auto value = iter.second; ret = AniHelper::SetPropertyValue(env, statObject, key, value); if (ret != ANI_OK) { - HILOGE("SetPropertyValue Fail %{private}s, err: %{private}d", key, ret); + HILOGE("SetPropertyValue Fail %{public}s, err: %{public}d", key, ret); return ret; } } @@ -143,14 +143,14 @@ static ani_status SetProperties(ani_env *env, ani_object &statObject, FsStat *fs auto value = iter.second; ret = SetBigIntProperty(env, statObject, key, value); if (ret != ANI_OK) { - HILOGE("SetBigIntProperty Fail %{private}s, err: %{private}d", key, ret); + HILOGE("SetBigIntProperty Fail %{public}s, err: %{public}d", key, ret); return ret; } } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) if ((ret = SetEnumLocation(env, statObject, "location", static_cast(fsStat->GetLocation()))) != ANI_OK) { - HILOGE("SetEnumLocation Fail, err: %{private}d", ret); + HILOGE("SetEnumLocation Fail, err: %{public}d", ret); return ret; } #endif @@ -170,7 +170,7 @@ ani_object StatWrapper::Wrap(ani_env *env, FsStat *fsStat) ani_status ret; if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); + HILOGE("Not found %{public}s, err: %{public}d", classDesc, ret); return nullptr; } @@ -178,17 +178,17 @@ ani_object StatWrapper::Wrap(ani_env *env, FsStat *fsStat) auto ctorSig = FS::StatInner::ctorSig.c_str(); ani_method ctor; if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { - HILOGE("Not found ctor, err: %{private}d", ret); + HILOGE("Not found ctor, err: %{public}d", ret); return nullptr; } if ((ret = env->Object_New(cls, ctor, &statObject, reinterpret_cast(fsStat))) != ANI_OK) { - HILOGE("New StatInner Fail, err: %{private}d", ret); + HILOGE("New StatInner Fail, err: %{public}d", ret); return nullptr; } if ((ret = SetProperties(env, statObject, fsStat)) != ANI_OK) { - HILOGE("SetProperties Fail, err: %{private}d", ret); + HILOGE("SetProperties Fail, err: %{public}d", ret); return nullptr; } @@ -200,7 +200,7 @@ FsStat *StatWrapper::Unwrap(ani_env *env, ani_object object) ani_long fsStat; auto ret = env->Object_GetFieldByName_Long(object, "nativeStat", &fsStat); if (ret != ANI_OK) { - HILOGE("Unwrap fsStat err: %{private}d", ret); + HILOGE("Unwrap fsStat err: %{public}d", ret); return nullptr; } return reinterpret_cast(fsStat); -- Gitee