From 9106d3e0d2e4353b7e6d19c58b6e4a062ef2dadf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=A7=9C=E5=B0=8F=E6=9E=97?= Date: Fri, 18 Apr 2025 17:59:09 +0800 Subject: [PATCH] =?UTF-8?q?Ani=20Signature=E6=94=B9=E9=80=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Change-Id: Ifdfc03bcb0b125d6d662b8e3094af607be928dbc Signed-off-by: 姜小林 --- interfaces/kits/js/BUILD.gn | 18 +- .../js/src/common/ani_helper/ani_helper.h | 12 +- .../src/common/ani_helper/ani_signature.cpp | 134 +++++++++++ .../js/src/common/ani_helper/ani_signature.h | 212 ++++++++++++++++++ .../src/common/ani_helper/error_handler.cpp | 4 +- .../js/src/common/ani_helper/error_handler.h | 6 +- .../src/common/ani_helper/type_converter.cpp | 26 ++- .../ani/bind_function_class.cpp | 6 +- .../js/src/mod_fs/ani/bind_function_class.cpp | 58 ++--- .../js/src/mod_fs/ani/ets/@ohos.file.fs.ets | 36 +-- .../mod_fs/class_file/ani/file_wrapper.cpp | 70 +++--- .../ani/randomaccessfile_ani.cpp | 10 +- .../ani/reader_iterator_ani.cpp | 28 ++- .../ani/reader_iterator_result_ani.cpp | 24 +- .../src/mod_fs/class_stat/ani/lstat_ani.cpp | 6 +- .../js/src/mod_fs/class_stat/ani/stat_ani.cpp | 10 +- .../mod_fs/class_stat/ani/stat_wrapper.cpp | 98 +++++--- .../src/mod_fs/class_stat/ani/stat_wrapper.h | 4 +- .../mod_fs/class_stream/ani/stream_ani.cpp | 10 +- .../class_stream/ani/stream_wrapper.cpp | 22 +- .../class_watcher/ani/fs_watcher_wrapper.cpp | 22 +- .../class_watcher/ani/watch_event_wrapper.cpp | 18 +- .../src/mod_fs/properties/ani/close_ani.cpp | 20 +- .../js/src/mod_fs/properties/ani/copy_ani.cpp | 16 +- .../mod_fs/properties/ani/copy_dir_ani.cpp | 31 ++- .../ani/create_randomaccessfile_ani.cpp | 46 ++-- .../properties/ani/create_stream_ani.cpp | 2 + .../mod_fs/properties/ani/listfile_ani.cpp | 10 +- .../src/mod_fs/properties/ani/movedir_ani.cpp | 31 ++- .../js/src/mod_fs/properties/ani/open_ani.cpp | 2 + .../src/mod_fs/properties/ani/watcher_ani.cpp | 2 + .../src/mod_fs/properties/ani/write_ani.cpp | 8 +- .../src/mod_hash/ani/bind_function_class.cpp | 9 +- .../ani/bind_function_class.cpp | 14 +- .../mod_statvfs/ani/bind_function_class.cpp | 6 +- 35 files changed, 784 insertions(+), 247 deletions(-) create mode 100644 interfaces/kits/js/src/common/ani_helper/ani_signature.cpp create mode 100644 interfaces/kits/js/src/common/ani_helper/ani_signature.h diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index aa649c938..7e2ccedbe 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -681,6 +681,7 @@ ohos_shared_library("ani_file_fs") { "src/mod_fs/properties/copy_listener", ] sources = [ + "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", @@ -806,6 +807,7 @@ ohos_shared_library("ani_file_fs") { "ipc:ipc_core", "libuv:uv", "runtime_core:ani", + "runtime_core:ani_helpers", "runtime_core:libarkruntime", "samgr:samgr_proxy", ] @@ -846,6 +848,7 @@ ohos_shared_library("ani_file_hash") { "src/mod_hash/ani", ] sources = [ + "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", @@ -867,6 +870,7 @@ ohos_shared_library("ani_file_hash") { "libuv:uv", "openssl:libcrypto_shared", "runtime_core:ani", + "runtime_core:ani_helpers", "runtime_core:libarkruntime", ] use_exceptions = true @@ -907,6 +911,7 @@ ohos_shared_library("ani_file_securitylabel") { "src/mod_securitylabel", ] sources = [ + "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", @@ -926,6 +931,7 @@ ohos_shared_library("ani_file_securitylabel") { "hilog:libhilog", "libuv:uv", "runtime_core:ani", + "runtime_core:ani_helpers", "runtime_core:libarkruntime", ] use_exceptions = true @@ -966,13 +972,14 @@ ohos_shared_library("ani_file_environment") { "src/mod_environment", ] sources = [ + "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", - "src/mod_fs/fs_utils.cpp", "src/mod_environment/ani/bind_function_class.cpp", "src/mod_environment/ani/environment_ani.cpp", "src/mod_environment/environment_core.cpp", + "src/mod_fs/fs_utils.cpp", ] deps = [ @@ -985,12 +992,13 @@ ohos_shared_library("ani_file_environment") { "access_token:libaccesstoken_sdk", "access_token:libtokenid_sdk", "hilog:libhilog", - "libuv:uv", - "ipc:ipc_core", "init:libbegetutil", + "ipc:ipc_core", + "libuv:uv", "openssl:libcrypto_shared", "os_account:os_account_innerkits", "runtime_core:ani", + "runtime_core:ani_helpers", "runtime_core:libarkruntime", ] use_exceptions = true @@ -1031,6 +1039,7 @@ ohos_shared_library("ani_file_statvfs") { "src/mod_statvfs", ] sources = [ + "src/common/ani_helper/ani_signature.cpp", "src/common/ani_helper/error_handler.cpp", "src/common/ani_helper/type_converter.cpp", "src/common/file_helper/fd_guard.cpp", @@ -1050,6 +1059,7 @@ ohos_shared_library("ani_file_statvfs") { "hilog:libhilog", "libuv:uv", "runtime_core:ani", + "runtime_core:ani_helpers", "runtime_core:libarkruntime", ] use_exceptions = true @@ -1090,4 +1100,4 @@ ohos_prebuilt_etc("ohos_file_statvfs_abc_etc") { subsystem_name = "filemanagement" part_name = "file_api" deps = [ ":ohos_file_statvfs_abc" ] -} \ No newline at end of file +} 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 11083ff86..654b3b9c2 100644 --- a/interfaces/kits/js/src/common/ani_helper/ani_helper.h +++ b/interfaces/kits/js/src/common/ani_helper/ani_helper.h @@ -22,6 +22,7 @@ #include +#include "ani_signature.h" #include "event_handler.h" #include "event_runner.h" #include "file_utils.h" @@ -30,6 +31,7 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static thread_local shared_ptr mainHandler; @@ -107,8 +109,10 @@ public: if (isUndefined) { return { true, nullopt }; } + static const string longValueSig = Builder::BuildSignatureDescriptor({}, BasicTypes::longType); ani_long value; - status = env->Object_CallMethodByName_Long(static_cast(property), "longValue", ":J", &value); + status = env->Object_CallMethodByName_Long( + static_cast(property), "longValue", longValueSig.c_str(), &value); if (status != ANI_OK) { return { false, nullopt }; } @@ -147,8 +151,7 @@ public: return env; } - ani_option interopEnabled { "--interop=enable", nullptr }; - ani_options aniArgs { 1, &interopEnabled }; + ani_options aniArgs { 0, nullptr }; auto status = vm->AttachCurrentThread(&aniArgs, ANI_VERSION_1, &env); if (status != ANI_OK) { status = vm->GetEnv(ANI_VERSION_1, &env); @@ -156,10 +159,7 @@ public: HILOGE("vm GetEnv, err: %{private}d", status); return nullptr; } - - return env; } - return env; } diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp new file mode 100644 index 000000000..87ee3dfe3 --- /dev/null +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.cpp @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ani_signature.h" + +namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature { +// BasicTypes +const Type BasicTypes::undefinedType = Builder::BuildUndefined(); +const Type BasicTypes::nullType = Builder::BuildNull(); +const Type BasicTypes::booleanType = Builder::BuildBoolean(); +const Type BasicTypes::byteType = Builder::BuildByte(); +const Type BasicTypes::charType = Builder::BuildChar(); +const Type BasicTypes::shortType = Builder::BuildShort(); +const Type BasicTypes::intType = Builder::BuildInt(); +const Type BasicTypes::longType = Builder::BuildLong(); +const Type BasicTypes::floatType = Builder::BuildFloat(); +const Type BasicTypes::doubleType = Builder::BuildDouble(); +// BoxedTypes::Boolean +const Type BoxedTypes::Boolean::classType = Builder::BuildClass("std.core.Boolean"); +const string BoxedTypes::Boolean::classDesc = BoxedTypes::Double::classType.Descriptor(); +const string BoxedTypes::Boolean::unboxedDesc = "unboxed"; +const string BoxedTypes::Boolean::unboxedSig = Builder::BuildSignatureDescriptor({}, BasicTypes::booleanType); +// BoxedTypes::Int +const Type BoxedTypes::Int::classType = Builder::BuildClass("std.core.Int"); +const string BoxedTypes::Int::classDesc = BoxedTypes::Int::classType.Descriptor(); +// BoxedTypes::Double +const Type BoxedTypes::Double::classType = Builder::BuildClass("std.core.Double"); +const string BoxedTypes::Double::classDesc = BoxedTypes::Double::classType.Descriptor(); +// BaseType +const string BaseType::ctorDesc = Builder::BuildConstructorName(); +const string BaseType::ctorSig0 = Builder::BuildSignatureDescriptor({}); +// BuiltInTypes::Object +const Type BuiltInTypes::Object::classType = Builder::BuildClass("std.core.Object"); +const string BuiltInTypes::Object::classDesc = BuiltInTypes::Object::classType.Descriptor(); +// BuiltInTypes::String +const Type BuiltInTypes::String::classType = Builder::BuildClass("std.core.String"); +const string BuiltInTypes::String::classDesc = BuiltInTypes::String::classType.Descriptor(); +// BuiltInTypes::Array +const Type BuiltInTypes::Array::classType = Builder::BuildClass("escompat.Array"); +const string BuiltInTypes::Array::classDesc = BuiltInTypes::Array::classType.Descriptor(); +const string BuiltInTypes::Array::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::intType }); +const string BuiltInTypes::Array::getterDesc = "$_get"; +const string BuiltInTypes::Array::setterDesc = "$_set"; +const string BuiltInTypes::Array::objectGetterSig = + Builder::BuildSignatureDescriptor({ BasicTypes::intType }, BuiltInTypes::objectType); +const string BuiltInTypes::Array::objectSetterSig = + Builder::BuildSignatureDescriptor({ BasicTypes::intType, BuiltInTypes::objectType }); +// BuiltInTypes::ArrayBuffer +const Type BuiltInTypes::ArrayBuffer::classType = Builder::BuildClass("escompat.ArrayBuffer"); +const string BuiltInTypes::ArrayBuffer::classDesc = BuiltInTypes::ArrayBuffer::classType.Descriptor(); +// 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 }); +// BuiltInTypes::BusinessError +const Type BuiltInTypes::BusinessError::classType = Builder::BuildClass("@ohos.base.BusinessError"); +const string BuiltInTypes::BusinessError::classDesc = BuiltInTypes::BusinessError::classType.Descriptor(); +// FS::ConflictFilesInner +const Type FS::ConflictFilesInner::classType = Builder::BuildClass("@ohos.file.fs.ConflictFilesInner"); +const string FS::ConflictFilesInner::classDesc = FS::ConflictFilesInner::classType.Descriptor(); +const string FS::ConflictFilesInner::ctorSig = + Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BuiltInTypes::stringType }); +// FS::FileInner +const Type FS::FileInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.FileInner"); +const string FS::FileInner::classDesc = FS::FileInner::classType.Descriptor(); +const string FS::FileInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::ProgressInner +const Type FS::ProgressInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ProgressInner"); +const string FS::ProgressInner::classDesc = FS::ProgressInner::classType.Descriptor(); +const string FS::ProgressInner::ctorSig = + Builder::BuildSignatureDescriptor({ BasicTypes::doubleType, BasicTypes::doubleType }); +// FS::RandomAccessFileInner +const Type FS::RandomAccessFileInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.RandomAccessFileInner"); +const string FS::RandomAccessFileInner::classDesc = FS::RandomAccessFileInner::classType.Descriptor(); +const string FS::RandomAccessFileInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::ReaderIteratorInner +const Type FS::ReaderIteratorInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.ReaderIteratorInner"); +const string FS::ReaderIteratorInner::classDesc = FS::ReaderIteratorInner::classType.Descriptor(); +const string FS::ReaderIteratorInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::ReaderIteratorResultInner +const Type FS::ReaderIteratorResultInner::classType = + Builder::BuildClass("@ohos.file.fs.fileIo.ReaderIteratorResultInner"); +const string FS::ReaderIteratorResultInner::classDesc = FS::ReaderIteratorResultInner::classType.Descriptor(); +const string FS::ReaderIteratorResultInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::StatInner +const Type FS::StatInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.StatInner"); +const string FS::StatInner::classDesc = FS::StatInner::classType.Descriptor(); +const string FS::StatInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::StreamInner +const Type FS::StreamInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.StreamInner"); +const string FS::StreamInner::classDesc = FS::StreamInner::classType.Descriptor(); +const string FS::StreamInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::TaskSignal +const Type FS::TaskSignal::classType = Builder::BuildClass("@ohos.file.fs.fileIo.TaskSignal"); +const string FS::TaskSignal::classDesc = FS::TaskSignal::classType.Descriptor(); +const string FS::TaskSignal::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::WatcherInner +const Type FS::WatcherInner::classType = Builder::BuildClass("@ohos.file.fs.fileIo.WatcherInner"); +const string FS::WatcherInner::classDesc = FS::WatcherInner::classType.Descriptor(); +const string FS::WatcherInner::ctorSig = Builder::BuildSignatureDescriptor({ BasicTypes::longType }); +// FS::WatchEventInner +const Type FS::WatchEventInner::classType = Builder::BuildClass("@ohos.file.fs.WatchEventInner"); +const string FS::WatchEventInner::classDesc = FS::WatchEventInner::classType.Descriptor(); +const string FS::WatchEventInner::ctorSig = + Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::doubleType, BasicTypes::doubleType }); +// Impl::EnvironmentImpl +const Type Impl::EnvironmentImpl::classType = Builder::BuildClass("@ohos.file.environment.EnvironmentImpl"); +const string Impl::EnvironmentImpl::classDesc = Impl::EnvironmentImpl::classType.Descriptor(); +// Impl::FileIoImpl +const Type Impl::FileIoImpl::classType = Builder::BuildClass("@ohos.file.fs.FileIoImpl"); +const string Impl::FileIoImpl::classDesc = Impl::FileIoImpl::classType.Descriptor(); +// Impl::HashImpl +const Type Impl::HashImpl::classType = Builder::BuildClass("@ohos.file.hash.HashImpl"); +const string Impl::HashImpl::classDesc = Impl::HashImpl::classType.Descriptor(); +// Impl::SecurityLabelImpl +const Type Impl::SecurityLabelImpl::classType = Builder::BuildClass("@ohos.file.securityLabel.SecurityLabelImpl"); +const string Impl::SecurityLabelImpl::classDesc = Impl::SecurityLabelImpl::classType.Descriptor(); +// Impl::StatvfsImpl +const Type Impl::StatvfsImpl::classType = Builder::BuildClass("@ohos.file.statvfs.StatvfsImpl"); +const string Impl::StatvfsImpl::classDesc = Impl::StatvfsImpl::classType.Descriptor(); + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature \ No newline at end of file diff --git a/interfaces/kits/js/src/common/ani_helper/ani_signature.h b/interfaces/kits/js/src/common/ani_helper/ani_signature.h new file mode 100644 index 000000000..c05470dc0 --- /dev/null +++ b/interfaces/kits/js/src/common/ani_helper/ani_signature.h @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H +#define INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H + +#pragma once + +#include +#include + +namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature { +using namespace std; +using namespace arkts::ani_signature; + +struct BasicTypes { + static const Type undefinedType; + static const Type nullType; + static const Type booleanType; + static const Type byteType; + static const Type charType; + static const Type shortType; + static const Type intType; + static const Type longType; + static const Type floatType; + static const Type doubleType; +}; + +struct BaseType { + static const string ctorDesc; + static const string ctorSig0; +}; + +namespace BoxedTypes { + +struct Boolean : public BaseType { + static const Type classType; + static const string classDesc; + static const string unboxedDesc; + static const string unboxedSig; +}; + +struct Int : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct Double : public BaseType { + static const Type classType; + static const string classDesc; +}; + +} // namespace BoxedTypes + +namespace BuiltInTypes { + +struct Object : public BaseType { + static const Type classType; + static const string classDesc; +}; + +inline const Type &objectType = BuiltInTypes::Object::classType; + +struct String : public BaseType { + static const Type classType; + static const string classDesc; +}; + +inline const Type &stringType = BuiltInTypes::String::classType; + +struct Array : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; + static const string getterDesc; + static const string setterDesc; + static const string objectGetterSig; + static const string objectSetterSig; +}; + +struct ArrayBuffer : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct BigInt : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct BusinessError : public BaseType { + static const Type classType; + static const string classDesc; +}; + +}; // namespace BuiltInTypes + +namespace FS { + +struct ConflictFilesInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct FileInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct ProgressInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct RandomAccessFileInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct ReaderIteratorInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct ReaderIteratorResultInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct StatInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct StreamInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct TaskSignal : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct WatcherInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +struct WatchEventInner : public BaseType { + static const Type classType; + static const string classDesc; + static const string ctorSig; +}; + +} // namespace FS + +namespace Impl { + +struct EnvironmentImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct FileIoImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct HashImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct SecurityLabelImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +struct StatvfsImpl : public BaseType { + static const Type classType; + static const string classDesc; +}; + +} // namespace Impl + +} // namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature + +#endif // INTERFACES_KITS_JS_SRC_COMMON_ANI_HELPER_ANI_SIGNATURE_H \ No newline at end of file diff --git a/interfaces/kits/js/src/common/ani_helper/error_handler.cpp b/interfaces/kits/js/src/common/ani_helper/error_handler.cpp index d300bb8c2..c56a4dc0d 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.cpp +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.cpp @@ -20,8 +20,8 @@ namespace OHOS::FileManagement::ModuleFileIO::ANI { ani_status ErrorHandler::Throw( ani_env *env, int32_t code, const std::string &errMsg, const std::optional &errData) { - const char *className = "L@ohos/base/BusinessError;"; - return Throw(env, className, code, errMsg, errData); + auto classDesc = BuiltInTypes::BusinessError::classDesc.c_str(); + return Throw(env, classDesc, code, errMsg, errData); } } // namespace OHOS::FileManagement::ModuleFileIO::ANI \ No newline at end of file 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 cab44cc1c..2138f075d 100644 --- a/interfaces/kits/js/src/common/ani_helper/error_handler.h +++ b/interfaces/kits/js/src/common/ani_helper/error_handler.h @@ -20,11 +20,13 @@ #include #include #include "ani_helper.h" +#include "ani_signature.h" #include "filemgmt_libhilog.h" #include "fs_error.h" #include "type_converter.h" namespace OHOS::FileManagement::ModuleFileIO::ANI { +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; class ErrorHandler { public: @@ -90,8 +92,10 @@ private: return { ANI_NOT_FOUND, nullptr }; } + auto ctorDesc = BaseType::ctorDesc.c_str(); + auto ctorSig = BaseType::ctorSig0.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", ":V", &ctor)) { + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { HILOGE("Cannot find constructor for class '%{private}s'", className); return { ANI_NOT_FOUND, nullptr }; } diff --git a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp index bfa635e0e..2e995d41e 100644 --- a/interfaces/kits/js/src/common/ani_helper/type_converter.cpp +++ b/interfaces/kits/js/src/common/ani_helper/type_converter.cpp @@ -21,10 +21,12 @@ #include +#include "ani_signature.h" #include "file_utils.h" #include "filemgmt_libhilog.h" namespace OHOS::FileManagement::ModuleFileIO::ANI { +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; std::tuple TypeConverter::ToUTF8String(ani_env *env, const ani_string &path) { @@ -155,9 +157,10 @@ static std::tuple ParseFd(ani_env *env, const ani_object &pathOrF { ani_boolean isFd = false; - ani_class DoubleClass; - env->FindClass("Lstd/core/Double;", &DoubleClass); - env->Object_InstanceOf(pathOrFd, DoubleClass, &isFd); + auto doubleClassDesc = BoxedTypes::Double::classDesc.c_str(); + ani_class doubleClass; + env->FindClass(doubleClassDesc, &doubleClass); + env->Object_InstanceOf(pathOrFd, doubleClass, &isFd); if (isFd) { ani_double doubleValue; if (ANI_OK != env->Object_CallMethodByName_Double(pathOrFd, "doubleValue", nullptr, &doubleValue)) { @@ -168,9 +171,10 @@ static std::tuple ParseFd(ani_env *env, const ani_object &pathOrF return { true, fd }; } - ani_class IntClass; - env->FindClass("Lstd/core/Int;", &IntClass); - env->Object_InstanceOf(pathOrFd, IntClass, &isFd); + auto intClassDesc = BoxedTypes::Int::classDesc.c_str(); + ani_class intClass; + env->FindClass(intClassDesc, &intClass); + env->Object_InstanceOf(pathOrFd, intClass, &isFd); if (isFd) { ani_int fd; if (ANI_OK != env->Object_CallMethodByName_Int(pathOrFd, "intValue", nullptr, &fd)) { @@ -190,8 +194,9 @@ std::tuple TypeConverter::ToFileInfo(ani_env *env, const ani_obj return { false, FileInfo { false, {}, {} } }; } + auto stringClassDesc = BuiltInTypes::String::classDesc.c_str(); ani_class stringClass; - env->FindClass("Lstd/core/String;", &stringClass); + env->FindClass(stringClassDesc, &stringClass); ani_boolean isPath = false; env->Object_InstanceOf(pathOrFd, stringClass, &isPath); @@ -246,12 +251,13 @@ std::tuple TypeConverter::ToAniStringList( return { false, nullptr }; } + auto classDesc = BuiltInTypes::String::classDesc.c_str(); ani_array_ref result = nullptr; - ani_class itemCls = nullptr; - if (env->FindClass("Lstd/core/String;", &itemCls) != ANI_OK) { + ani_class cls = nullptr; + if (env->FindClass(classDesc, &cls) != ANI_OK) { return { false, result }; } - if (env->Array_New_Ref(itemCls, length, nullptr, &result) != ANI_OK) { + if (env->Array_New_Ref(cls, length, nullptr, &result) != ANI_OK) { return { false, result }; } for (uint32_t i = 0; i < length; i++) { diff --git a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp index 86e43e7e1..dfda60113 100644 --- a/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_environment/ani/bind_function_class.cpp @@ -14,14 +14,16 @@ */ #include +#include "ani_signature.h" #include "bind_function.h" #include "environment_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/environment/EnvironmentImpl;"; + auto classDesc = Impl::EnvironmentImpl::classDesc.c_str(); std::array methods = { ani_native_function { "getStorageDataDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetStorageDataDirSync) }, @@ -38,7 +40,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "getUserHomeDirSync", nullptr, reinterpret_cast(EnvironmentAni::GetUserHomeDirSync) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) diff --git a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp index 5086029c2..10e86d4ba 100644 --- a/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_fs/ani/bind_function_class.cpp @@ -18,6 +18,7 @@ #include #include "access_ani.h" +#include "ani_signature.h" #include "bind_function.h" #include "close_ani.h" #include "connectdfs_ani.h" @@ -61,10 +62,11 @@ #include "xattr_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_status BindRafFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/RandomAccessFileInner;"; + auto classDesc = FS::RandomAccessFileInner::classDesc.c_str(); std::array methods = { ani_native_function { @@ -77,24 +79,24 @@ static ani_status BindRafFileMethods(ani_env *env) "getWriteStream", nullptr, reinterpret_cast(RandomAccessFileAni::GetWriteStream) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindWatcherClassMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/WatcherInner;"; + auto classDesc = FS::WatcherInner::classDesc.c_str(); std::array methods = { ani_native_function { "start", nullptr, reinterpret_cast(FsWatcherAni::Start) }, ani_native_function { "stop", nullptr, reinterpret_cast(FsWatcherAni::Stop) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindFileMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/FileInner;"; + auto classDesc = FS::FileInner::classDesc.c_str(); std::array methods = { ani_native_function { "getParent", nullptr, reinterpret_cast(FileAni::GetParent) }, @@ -103,40 +105,40 @@ static ani_status BindFileMethods(ani_env *env) ani_native_function { "unlock", nullptr, reinterpret_cast(FileAni::UnLock) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindReaderIteratorMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorInner;"; + auto classDesc = FS::ReaderIteratorInner::classDesc.c_str(); std::array methods = { ani_native_function { "next", nullptr, reinterpret_cast(ReaderIteratorAni::Next) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindStatClassMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/StatInner;"; + auto classDesc = FS::StatInner::classDesc.c_str(); std::array methods = { - ani_native_function { "isBlockDevice", ":Z", reinterpret_cast(StatAni::IsBlockDevice) }, - ani_native_function { "isCharacterDevice", ":Z", reinterpret_cast(StatAni::IsCharacterDevice) }, - ani_native_function { "isDirectory", ":Z", reinterpret_cast(StatAni::IsDirectory) }, - ani_native_function { "isFIFO", ":Z", reinterpret_cast(StatAni::IsFIFO) }, - ani_native_function { "isFile", ":Z", reinterpret_cast(StatAni::IsFile) }, - ani_native_function { "isSocket", ":Z", reinterpret_cast(StatAni::IsSocket) }, - ani_native_function { "isSymbolicLink", ":Z", reinterpret_cast(StatAni::IsSymbolicLink) }, + ani_native_function { "isBlockDevice", nullptr, reinterpret_cast(StatAni::IsBlockDevice) }, + ani_native_function { "isCharacterDevice", nullptr, reinterpret_cast(StatAni::IsCharacterDevice) }, + ani_native_function { "isDirectory", nullptr, reinterpret_cast(StatAni::IsDirectory) }, + ani_native_function { "isFIFO", nullptr, reinterpret_cast(StatAni::IsFIFO) }, + ani_native_function { "isFile", nullptr, reinterpret_cast(StatAni::IsFile) }, + ani_native_function { "isSocket", nullptr, reinterpret_cast(StatAni::IsSocket) }, + ani_native_function { "isSymbolicLink", nullptr, reinterpret_cast(StatAni::IsSymbolicLink) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindStreamMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/StreamInner;"; + auto classDesc = FS::StreamInner::classDesc.c_str(); std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(StreamAni::Close) }, @@ -146,24 +148,28 @@ static ani_status BindStreamMethods(ani_env *env) ani_native_function { "seek", nullptr, reinterpret_cast(StreamAni::Seek) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } static ani_status BindTaskSignalClassMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/fileIo/TaskSignal;"; + auto classDesc = FS::TaskSignal::classDesc.c_str(); std::array methods = { - ani_native_function { "cancel", ":V", reinterpret_cast(TaskSignalAni::Cancel) }, + ani_native_function { "cancel", nullptr, reinterpret_cast(TaskSignalAni::Cancel) }, ani_native_function { "onCancelNative", nullptr, reinterpret_cast(TaskSignalAni::OnCancel) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } +const static string mkdirCtorSig0 = Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType }); +const static string mkdirCtorSig1 = + Builder::BuildSignatureDescriptor({ BuiltInTypes::stringType, BasicTypes::booleanType }); + static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/fs/FileIoImpl;"; + auto classDesc = Impl::FileIoImpl::classDesc.c_str(); std::array methods = { ani_native_function { "closeSync", nullptr, reinterpret_cast(CloseAni::CloseSync) }, @@ -187,8 +193,8 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "listFileSync", nullptr, reinterpret_cast(ListFileAni::ListFileSync) }, ani_native_function { "lseekSync", nullptr, reinterpret_cast(LseekAni::LseekSync) }, ani_native_function { "lstatSync", nullptr, reinterpret_cast(LstatAni::LstatSync) }, - ani_native_function { "mkdirSync", "Lstd/core/String;:V", reinterpret_cast(MkdirkAni::MkdirSync0) }, - ani_native_function { "mkdirSync", "Lstd/core/String;Z:V", reinterpret_cast(MkdirkAni::MkdirSync1) }, + ani_native_function { "mkdirSync", mkdirCtorSig0.c_str(), reinterpret_cast(MkdirkAni::MkdirSync0) }, + ani_native_function { "mkdirSync", mkdirCtorSig1.c_str(), reinterpret_cast(MkdirkAni::MkdirSync1) }, ani_native_function { "mkdtempSync", nullptr, reinterpret_cast(MkdtempAni::MkdtempSync) }, ani_native_function { "movedirSync", nullptr, reinterpret_cast(MoveDirAni::MoveDirSync) }, ani_native_function { "moveFileSync", nullptr, reinterpret_cast(MoveAni::MoveFileSync) }, @@ -206,7 +212,7 @@ static ani_status BindStaticMethods(ani_env *env) ani_native_function { "utimes", nullptr, reinterpret_cast(UtimesAni::Utimes) }, ani_native_function { "writeSync", nullptr, reinterpret_cast(WriteAni::WriteSync) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) diff --git a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets index e8fa2c76b..a590ecb22 100644 --- a/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets +++ b/interfaces/kits/js/src/mod_fs/ani/ets/@ohos.file.fs.ets @@ -1301,21 +1301,6 @@ function lseek(fd: number, offset: number, whence?: WhenceType): number { return FileIoImpl.lseekSync(fd, offset, whence); } -export interface ConflictFiles { - srcFile: string; - destFile: string; -} - -class ConflictFilesInner implements ConflictFiles { - srcFile: string = ""; - destFile: string = ""; - - constructor(src: string, dest: string) { - this.srcFile = src; - this.destFile = dest; - } -} - export interface Filter { suffix?: Array; displayName?: Array; @@ -2043,8 +2028,6 @@ export enum WhenceType { SEEK_END = 2 } -} - export interface Watcher { start(): void; stop(): void; @@ -2064,6 +2047,23 @@ class WatcherInner implements Watcher { native stop(): void; } +} // namespace fileIo + +export interface ConflictFiles { + srcFile: string; + destFile: string; +} + +class ConflictFilesInner implements ConflictFiles { + srcFile: string = ""; + destFile: string = ""; + + constructor(src: string, dest: string) { + this.srcFile = src; + this.destFile = dest; + } +} + export interface WatchEvent { fileName: string; event: number; @@ -2180,7 +2180,7 @@ export class FileIoImpl { static native createStreamSync(path: string, mode: string): fileIo.Stream; - static native createWatcherSync(path: string, events: number, listener: WatchEventListener): Watcher; + static native createWatcherSync(path: string, events: number, listener: WatchEventListener): fileIo.Watcher; static native fdopenStreamSync(fd: number, mode: string): fileIo.Stream; 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 544d62f4c..fcbf8aab9 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 @@ -15,6 +15,7 @@ #include "file_wrapper.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "fs_file.h" @@ -24,8 +25,9 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsFile *FileWrapper::Unwrap(ani_env *env, ani_object object) { @@ -40,61 +42,79 @@ FsFile *FileWrapper::Unwrap(ani_env *env, ani_object object) return file; } -ani_object FileWrapper::Wrap(ani_env *env, const FsFile *file) +static bool SetFileProperties(ani_env *env, ani_class cls, ani_object obj, const FsFile *file) { - static const char *className = "L@ohos/file/fs/fileIo/FileInner;"; - ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); - return nullptr; - } - ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); - return nullptr; - } - ani_long ptr = static_cast(reinterpret_cast(file)); - ani_object obj; - if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); - return nullptr; - } - const auto &fdRet = file->GetFD(); if (!fdRet.IsSuccess()) { HILOGE("GetFD Failed!"); - return nullptr; + return false; } const auto &fd = fdRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast(fd))) { HILOGE("Set fd field value failed!"); - return nullptr; + return false; } const auto &pathRet = file->GetPath(); if (!pathRet.IsSuccess()) { HILOGE("GetPath Failed!"); - return nullptr; + return false; } const auto &path = pathRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "path", path)) { HILOGE("Set path field value failed!"); - return nullptr; + return false; } const auto &nameRet = file->GetName(); if (!pathRet.IsSuccess()) { HILOGE("GetPath Failed!"); - return nullptr; + return false; } const auto &name = nameRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "name", name)) { HILOGE("Set name field value failed!"); + return false; + } + return true; +} + +ani_object FileWrapper::Wrap(ani_env *env, const FsFile *file) +{ + if (file == nullptr) { + HILOGE("FsFile pointer is null!"); + return nullptr; + } + + auto classDesc = FS::FileInner::classDesc.c_str(); + ani_class cls; + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); + return nullptr; + } + + auto ctorDesc = FS::FileInner::ctorDesc.c_str(); + auto ctorSig = FS::FileInner::ctorSig.c_str(); + ani_method ctor; + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + + ani_long ptr = static_cast(reinterpret_cast(file)); + ani_object obj; + if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { + HILOGE("New %s obj Failed!", classDesc); + return nullptr; + } + + if (!SetFileProperties(env, cls, obj, file)) { + return nullptr; + } + return obj; } 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 51c130c07..335092ba8 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 @@ -16,6 +16,7 @@ #include "randomaccessfile_ani.h" #include "ani_helper.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "fs_randomaccessfile.h" @@ -25,8 +26,9 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; const int BUF_SIZE = 1024; const string READ_STREAM_CLASS = "ReadStream"; @@ -105,7 +107,8 @@ static tuple> ToWriteOptions(ani_env *env, ani_obje static tuple ParseStringBuffer(ani_env *env, const ani_object &buf) { ani_class cls; - env->FindClass("Lstd/core/String;", &cls); + auto classDesc = BuiltInTypes::String::classDesc.c_str(); + env->FindClass(classDesc, &cls); ani_boolean isString; env->Object_InstanceOf(buf, cls, &isString); @@ -119,7 +122,8 @@ static tuple ParseStringBuffer(ani_env *env, const ani_object static tuple ParseArrayBuffer(ani_env *env, const ani_object &buf) { ani_class cls; - env->FindClass("Lescompat/ArrayBuffer;", &cls); + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); + env->FindClass(classDesc, &cls); ani_boolean isArrayBuffer; env->Object_InstanceOf(buf, cls, &isArrayBuffer); diff --git a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp index f65189add..eb3f82c0e 100644 --- a/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_readeriterator/ani/reader_iterator_ani.cpp @@ -15,6 +15,7 @@ #include "reader_iterator_ani.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "reader_iterator_result_ani.h" @@ -24,27 +25,36 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; ani_object ReaderIteratorAni::Wrap(ani_env *env, const FsReaderIterator *it) { - static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorInner;"; + if (it == nullptr) { + HILOGE("FsReaderIterator pointer is null!"); + return nullptr; + } + auto classDesc = FS::ReaderIteratorInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::ReaderIteratorInner::ctorDesc.c_str(); + auto ctorSig = FS::ReaderIteratorInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_long ptr = static_cast(reinterpret_cast(it)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); + HILOGE("New %s obj Failed!", classDesc); return nullptr; } @@ -69,7 +79,7 @@ ani_object ReaderIteratorAni::Next(ani_env *env, [[maybe_unused]] ani_object obj auto fsReaderIterator = Unwrap(env, object); if (fsReaderIterator == nullptr) { ErrorHandler::Throw(env, UNKNOWN_ERR); - return ANI_FALSE; + return nullptr; } auto ret = fsReaderIterator->Next(); @@ -77,7 +87,7 @@ ani_object ReaderIteratorAni::Next(ani_env *env, [[maybe_unused]] ani_object obj HILOGE("Cannot get readeriterator next!"); const auto &err = ret.GetError(); ErrorHandler::Throw(env, err); - return {}; + return nullptr; } auto nextRet = ret.GetData().value(); auto result = ReaderIteratorResultAni::Wrap(env, &nextRet); 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 a94c62e22..b8efe806e 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 @@ -15,6 +15,7 @@ #include "reader_iterator_result_ani.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -23,27 +24,36 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; ani_object ReaderIteratorResultAni::Wrap(ani_env *env, const ReaderIteratorResult *result) { - static const char *className = "L@ohos/file/fs/fileIo/ReaderIteratorResultInner;"; + if (result == nullptr) { + HILOGE("ReaderIteratorResult pointer is null!"); + return nullptr; + } + auto classDesc = FS::ReaderIteratorResultInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::ReaderIteratorResultInner::ctorDesc.c_str(); + auto ctorSig = FS::ReaderIteratorResultInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_long ptr = static_cast(reinterpret_cast(result)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); + HILOGE("New %s obj Failed!", classDesc); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/lstat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/lstat_ani.cpp index f4379ac5f..deb3052c5 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/lstat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/lstat_ani.cpp @@ -49,15 +49,15 @@ ani_object LstatAni::LstatSync(ani_env *env, [[maybe_unused]] ani_class clazz, a } auto fsStat = ret.GetData().value(); - auto [status, statObject] = StatWrapper::Wrap(env, fsStat); - if (status != ANI_OK) { + auto statObject = StatWrapper::Wrap(env, fsStat); + if (statObject == nullptr) { delete fsStat; fsStat = nullptr; HILOGE("Wrap stat object failed!"); ErrorHandler::Throw(env, UNKNOWN_ERR); return {}; } - + return statObject; } } // namespace ANI diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp index 95b92f958..de57367da 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_ani.cpp @@ -34,7 +34,7 @@ ani_object StatAni::StatSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani if (!succPath) { HILOGE("The first argument requires filepath/fd"); ErrorHandler::Throw(env, EINVAL); - return {}; + return nullptr; } auto ret = StatCore::DoStat(fileInfo); @@ -42,17 +42,17 @@ ani_object StatAni::StatSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani HILOGE("DoStat failed!"); const FsError &err = ret.GetError(); ErrorHandler::Throw(env, err); - return {}; + return nullptr; } auto fsStat = ret.GetData().value(); - auto [status, statObject] = StatWrapper::Wrap(env, fsStat); - if (status != ANI_OK) { + auto statObject = StatWrapper::Wrap(env, fsStat); + if (statObject == nullptr) { delete fsStat; fsStat = nullptr; HILOGE("Wrap stat object failed!"); ErrorHandler::Throw(env, UNKNOWN_ERR); - return {}; + return nullptr; } return statObject; 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 ed6c02eb3..7545be645 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 @@ -17,7 +17,9 @@ #include #include +#include +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -28,6 +30,7 @@ namespace ModuleFileIO { namespace ANI { 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_double &value) @@ -46,17 +49,19 @@ static ani_status SetBigIntProperty( ani_env *env, const ani_class &statCls, ani_object &statObject, const char *name, ani_double &value) { ani_object object = {}; - static const char *className = "Lescompat/BigInt;"; + auto classDesc = BuiltInTypes::BigInt::classDesc.c_str(); ani_class cls; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); return ret; } + auto ctorDesc = BuiltInTypes::BigInt::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::BigInt::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "D:V", &ctor)) != ANI_OK) { + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { HILOGE("Not found ctor, err: %{private}d", ret); return ret; } @@ -98,45 +103,62 @@ static ani_status SetEnumLocation( return env->Object_CallMethod_Void(object, setter, GetLocationEnumIndex(value)); } +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) { ani_status ret; - vector> numProperties = { - { "mode", ani_double(static_cast(fsStat->GetMode())) }, - { "uid", ani_double(static_cast(fsStat->GetUid())) }, - { "gid", ani_double(static_cast(fsStat->GetGid())) }, - { "size", ani_double(static_cast(fsStat->GetSize())) }, - { "atime", ani_double(static_cast(fsStat->GetAtime())) }, - { "mtime", ani_double(static_cast(fsStat->GetMtime())) }, - { "ctime", ani_double(static_cast(fsStat->GetCtime())) }, + vector> numProperties = { + { MODE_SETTER, ani_double(static_cast(fsStat->GetMode())) }, + { UID_SETTER, ani_double(static_cast(fsStat->GetUid())) }, + { GID_SETTER, ani_double(static_cast(fsStat->GetGid())) }, + { SIZE_SETTER, ani_double(static_cast(fsStat->GetSize())) }, + { ATIME_SETTER, ani_double(static_cast(fsStat->GetAtime())) }, + { MTIME_SETTER, ani_double(static_cast(fsStat->GetMtime())) }, + { CTIME_SETTER, ani_double(static_cast(fsStat->GetCtime())) }, }; for (auto iter : numProperties) { - ret = SetNumProperty(env, cls, statObject, iter.first, iter.second); + auto key = iter.first.data(); + auto value = iter.second; + ret = SetNumProperty(env, cls, statObject, key, value); if (ret != ANI_OK) { - HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", iter.first, ret); + HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", key, ret); return ret; } } - vector> bigIntProperties = { - { "ino", ani_double(static_cast(fsStat->GetIno())) }, - { "atimeNs", ani_double(static_cast(fsStat->GetAtimeNs())) }, - { "mtimeNs", ani_double(static_cast(fsStat->GetMtimeNs())) }, - { "ctimeNs", ani_double(static_cast(fsStat->GetCtimeNs())) }, + 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())) }, }; for (auto iter : bigIntProperties) { - ret = SetBigIntProperty(env, cls, statObject, iter.first, iter.second); + auto key = iter.first.data(); + auto value = iter.second; + ret = SetBigIntProperty(env, cls, statObject, key, value); if (ret != ANI_OK) { - HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", iter.first, ret); + HILOGE("Object_CallMethod_Void Fail %{private}s, err: %{private}d", key, ret); return ret; } } #if !defined(WIN_PLATFORM) && !defined(IOS_PLATFORM) - if ((ret = SetEnumLocation(env, cls, statObject, "location", static_cast(fsStat->GetLocation()))) != - ANI_OK) { - HILOGE("Object_CallMethod_Void Fail location, err: %{private}d", ret); + 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); return ret; } #endif @@ -144,38 +166,44 @@ static ani_status SetProperties(ani_env *env, const ani_class &cls, ani_object & return ANI_OK; } -tuple StatWrapper::Wrap(ani_env *env, FsStat *fsStat) +ani_object StatWrapper::Wrap(ani_env *env, FsStat *fsStat) { + if (fsStat == nullptr) { + HILOGE("FsStat pointer is null!"); + return nullptr; + } + auto classDesc = FS::StatInner::classDesc.c_str(); ani_object statObject = {}; - static const char *className = "L@ohos/file/fs/fileIo/StatInner;"; ani_class cls; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); - return { ret, statObject }; + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); + return nullptr; } + auto ctorDesc = FS::StatInner::ctorDesc.c_str(); + auto ctorSig = FS::StatInner::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "J:V", &ctor)) != ANI_OK) { + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { HILOGE("Not found ctor, err: %{private}d", ret); - return { ret, statObject }; + return nullptr; } if ((ret = env->Object_New(cls, ctor, &statObject, reinterpret_cast(fsStat))) != ANI_OK) { HILOGE("New StatInner Fail, err: %{private}d", ret); - return { ret, statObject }; + return nullptr; } if ((ret = SetProperties(env, cls, statObject, fsStat)) != ANI_OK) { HILOGE("SetProperties Fail, err: %{private}d", ret); - return { ret, statObject }; + return nullptr; } - return { ANI_OK, statObject }; + return statObject; } -FsStat* StatWrapper::Unwrap(ani_env *env, ani_object object) +FsStat *StatWrapper::Unwrap(ani_env *env, ani_object object) { ani_long fsStat; auto ret = env->Object_GetFieldByName_Long(object, "nativeStat", &fsStat); diff --git a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.h b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.h index 77bbcbe6f..e84e1e5eb 100644 --- a/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.h +++ b/interfaces/kits/js/src/mod_fs/class_stat/ani/stat_wrapper.h @@ -28,8 +28,8 @@ using namespace std; class StatWrapper final { public: - static tuple Wrap(ani_env *env, FsStat *fsStat); - static FsStat* Unwrap(ani_env *env, ani_object object); + static ani_object Wrap(ani_env *env, FsStat *fsStat); + static FsStat *Unwrap(ani_env *env, ani_object object); }; } // namespace ANI } // namespace ModuleFileIO diff --git a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_ani.cpp b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_ani.cpp index 92cfc402c..a1d694187 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_ani.cpp @@ -19,6 +19,7 @@ #include #include "ani_helper.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "fs_stream.h" @@ -29,8 +30,9 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static tuple> ToReadOptions(ani_env *env, ani_object obj) { @@ -91,8 +93,9 @@ static tuple> ToWriteOptions(ani_env *env, ani_obje static std::tuple ParseStringBuffer(ani_env *env, const ani_object &buf) { + auto classDesc = BuiltInTypes::String::classDesc.c_str(); ani_class cls; - env->FindClass("Lstd/core/String;", &cls); + env->FindClass(classDesc, &cls); ani_boolean isString; env->Object_InstanceOf(buf, cls, &isString); @@ -105,8 +108,9 @@ static std::tuple ParseStringBuffer(ani_env *env, const ani_ob static std::tuple ParseArrayBuffer(ani_env *env, const ani_object &buf) { + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); ani_class cls; - env->FindClass("Lescompat/ArrayBuffer;", &cls); + env->FindClass(classDesc, &cls); ani_boolean isArrayBuffer; env->Object_InstanceOf(buf, cls, &isArrayBuffer); diff --git a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp index 2f03fb78e..c882f9dda 100644 --- a/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_stream/ani/stream_wrapper.cpp @@ -18,6 +18,7 @@ #include #include "ani_helper.h" +#include "ani_signature.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -25,8 +26,9 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsStream *StreamWrapper::Unwrap(ani_env *env, ani_object object) { @@ -43,21 +45,27 @@ FsStream *StreamWrapper::Unwrap(ani_env *env, ani_object object) ani_object StreamWrapper::Wrap(ani_env *env, const FsStream *stream) { - static const char *className = "L@ohos/file/fs/fileIo/StreamInner;"; + if (stream == nullptr) { + HILOGE("FsStream pointer is null!"); + return nullptr; + } + auto classDesc = FS::StreamInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + auto ctorDesc = FS::StreamInner::ctorDesc.c_str(); + auto ctorSig = FS::StreamInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } ani_long ptr = static_cast(reinterpret_cast(stream)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); + HILOGE("New %s obj Failed!", classDesc); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp index ca5212e4b..79d6fb1dc 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/fs_watcher_wrapper.cpp @@ -15,14 +15,16 @@ #include "fs_watcher_wrapper.h" +#include "ani_signature.h" #include "filemgmt_libhilog.h" namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; FsWatcher *FsWatcherWrapper::Unwrap(ani_env *env, ani_object object) { @@ -39,21 +41,27 @@ FsWatcher *FsWatcherWrapper::Unwrap(ani_env *env, ani_object object) ani_object FsWatcherWrapper::Wrap(ani_env *env, const FsWatcher *watcher) { - static const char *className = "L@ohos/file/fs/WatcherInner;"; + if (watcher == nullptr) { + HILOGE("FsWatcher pointer is null!"); + return nullptr; + } + auto classDesc = FS::WatcherInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + auto ctorDesc = FS::WatcherInner::ctorDesc.c_str(); + auto ctorSig = FS::WatcherInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } ani_long ptr = static_cast(reinterpret_cast(watcher)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); + HILOGE("New %s obj Failed!", classDesc); return nullptr; } return obj; diff --git a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp index 154ccd4cb..1712f7f1f 100644 --- a/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp +++ b/interfaces/kits/js/src/mod_fs/class_watcher/ani/watch_event_wrapper.cpp @@ -15,6 +15,7 @@ #include "watch_event_wrapper.h" +#include "ani_signature.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -22,20 +23,23 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { -using namespace OHOS::FileManagement::ModuleFileIO; using namespace std; +using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; ani_object WatchEventWrapper::Wrap(ani_env *env, const WatchEvent &evt) { - static const char *className = "L@ohos/file/fs/WatchEventInner;"; + auto classDesc = FS::WatchEventInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + auto ctorDesc = FS::WatchEventInner::ctorDesc.c_str(); + auto ctorSig = FS::WatchEventInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "Lstd/core/String;DD:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } @@ -50,7 +54,7 @@ ani_object WatchEventWrapper::Wrap(ani_env *env, const WatchEvent &evt) ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, fileName, event, cookie)) { - HILOGE("Create %s obj failed!", className); + HILOGE("Create %s obj failed!", classDesc); return nullptr; } return obj; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp index 7de532c82..7f8df824f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/close_ani.cpp @@ -17,6 +17,7 @@ #include +#include "ani_signature.h" #include "close_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" @@ -29,15 +30,17 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; tuple ParseFd(ani_env *env, ani_object obj) { int32_t result = -1; - ani_class IntClass; - env->FindClass("Lstd/core/Double;", &IntClass); - ani_boolean isInt; - env->Object_InstanceOf(obj, IntClass, &isInt); - if (isInt) { + auto doubleClassDesc = BoxedTypes::Double::classDesc.c_str(); + ani_class doubleClass; + env->FindClass(doubleClassDesc, &doubleClass); + ani_boolean isDouble; + env->Object_InstanceOf(obj, doubleClass, &isDouble); + if (isDouble) { ani_int fd; if (ANI_OK != env->Object_CallMethodByName_Int(obj, "intValue", nullptr, &fd)) { HILOGE("Get fd value failed"); @@ -47,10 +50,11 @@ tuple ParseFd(ani_env *env, ani_object obj) return { true, result }; } - ani_class FileClass; - env->FindClass("L@ohos/file/fs/fileIo/FileInner;", &FileClass); + auto fileClassDesc = FS::FileInner::classDesc.c_str(); + ani_class fileClass; + env->FindClass(fileClassDesc, &fileClass); ani_boolean isFile; - env->Object_InstanceOf(obj, FileClass, &isFile); + env->Object_InstanceOf(obj, fileClass, &isFile); if (isFile) { ani_double fd; if (ANI_OK != env->Object_GetPropertyByName_Double(obj, "fd", &fd)) { diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp index 003003d7d..e086c5ee1 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_ani.cpp @@ -16,6 +16,7 @@ #include "copy_ani.h" #include "ani_helper.h" +#include "ani_signature.h" #include "copy_core.h" #include "error_handler.h" #include "file_utils.h" @@ -28,6 +29,7 @@ namespace ModuleFileIO { namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static void SetProgressListenerCb(ani_env *env, ani_ref &callback, CopyOptions &opts) { @@ -37,7 +39,7 @@ static void SetProgressListenerCb(ani_env *env, ani_ref &callback, CopyOptions & opts.listenerCb = [vm, &callback](uint64_t progressSize, uint64_t totalSize) -> void { ani_status ret; ani_object progress = {}; - static const char *className = "L@ohos/file/fs/fileIo/ProgressInner;"; + auto classDesc = FS::ProgressInner::classDesc.c_str(); ani_class cls; auto env = AniHelper::GetThreadEnv(vm); @@ -51,19 +53,21 @@ static void SetProgressListenerCb(ani_env *env, ani_ref &callback, CopyOptions & totalSize); } - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Not found %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Not found %{private}s, err: %{private}d", classDesc, ret); return; } + auto ctorDesc = FS::ProgressInner::ctorDesc.c_str(); + auto ctorSig = FS::ProgressInner::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "DD:V", &ctor)) != ANI_OK) { + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { HILOGE("Not found ctor, err: %{private}d", ret); return; } if ((ret = env->Object_New(cls, ctor, &progress, ani_double(static_cast(progressSize)), - ani_double(static_cast(totalSize)))) != ANI_OK) { + ani_double(static_cast(totalSize)))) != ANI_OK) { HILOGE("New ProgressInner Fail, err: %{private}d", ret); return; } @@ -89,7 +93,7 @@ static bool SetTaskSignal(ani_env *env, ani_ref ©Signal, CopyOptions &opts) } ret = env->Object_SetFieldByName_Long(static_cast(copySignal), "nativeTaskSignal", - reinterpret_cast(taskSignalEntityCore.get())); + reinterpret_cast(taskSignalEntityCore.get())); if (ret != ANI_OK) { HILOGE("Object set nativeTaskSignal err: %{private}d", ret); return false; diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp index cfb210a51..13804c8ad 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/copy_dir_ani.cpp @@ -16,6 +16,7 @@ #include "copy_dir_ani.h" #include +#include "ani_signature.h" #include "copy_dir_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" @@ -27,20 +28,23 @@ namespace ModuleFileIO { namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static tuple ToConflictFiles(ani_env *env, const ConflictFiles &files) { - static const char *className = "L@ohos/file/fs/fileIo/ConflictFilesInner;"; + auto classDesc = FS::ConflictFilesInner::classDesc.c_str(); ani_class cls; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Cannot find class %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Cannot find class %{private}s, err: %{private}d", classDesc, ret); return { false, nullptr }; } + auto ctorDesc = FS::ConflictFilesInner::ctorDesc.c_str(); + auto ctorSig = FS::ConflictFilesInner::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "Lstd/core/String;Lstd/core/String;:V", &ctor)) != ANI_OK) { - HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", className, ret); + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { + HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", classDesc, ret); return { false, nullptr }; } @@ -71,18 +75,20 @@ static tuple> ToConflictFilesArray( if (!errFiles.has_value()) { return { true, nullopt }; } - static const char *className = "Lescompat/Array;"; + auto classDesc = BuiltInTypes::Array::classDesc.c_str(); ani_class cls = nullptr; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Cannot find class %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Cannot find class %{private}s, err: %{private}d", classDesc, ret); return { false, nullopt }; } + auto ctorDesc = BuiltInTypes::Array::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::Array::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { - HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", className, ret); + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { + HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", classDesc, ret); return { false, nullopt }; } @@ -93,6 +99,8 @@ static tuple> ToConflictFilesArray( return { false, nullopt }; } + auto setterDesc = BuiltInTypes::Array::setterDesc.c_str(); + auto setterSig = BuiltInTypes::Array::objectSetterSig.c_str(); ani_size index = 0; for (const auto &errFile : files) { auto [succ, fileObj] = ToConflictFiles(env, errFile); @@ -100,7 +108,7 @@ static tuple> ToConflictFilesArray( return { false, nullopt }; } - if ((ret = env->Object_CallMethodByName_Void(arr, "$_set", "ILstd/core/Object;:V", index, fileObj)) != ANI_OK) { + if ((ret = env->Object_CallMethodByName_Void(arr, setterDesc, setterSig, index, fileObj)) != ANI_OK) { HILOGE("Add element to Array failed, err: %{private}d", ret); return { false, nullopt }; } @@ -150,6 +158,7 @@ void CopyDirAni::CopyDirSync( return; } } + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement 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 677fde168..17fe296a3 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 @@ -16,6 +16,7 @@ #include "create_randomaccessfile_ani.h" #include "ani_helper.h" +#include "ani_signature.h" #include "create_randomaccessfile_core.h" #include "error_handler.h" #include "filemgmt_libhilog.h" @@ -25,44 +26,57 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { - using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) { - static const char *className = "L@ohos/file/fs/fileIo/RandomAccessFileInner;"; + if (rafFile == nullptr) { + HILOGE("FsRandomAccessFile pointer is null!"); + return nullptr; + } + + auto classDesc = FS::RandomAccessFileInner::classDesc.c_str(); ani_class cls; - if (ANI_OK != env->FindClass(className, &cls)) { - HILOGE("Cannot find class %s", className); + if (ANI_OK != env->FindClass(classDesc, &cls)) { + HILOGE("Cannot find class %s", classDesc); return nullptr; } + + auto ctorDesc = FS::RandomAccessFileInner::ctorDesc.c_str(); + auto ctorSig = FS::RandomAccessFileInner::ctorSig.c_str(); ani_method ctor; - if (ANI_OK != env->Class_FindMethod(cls, "", "J:V", &ctor)) { - HILOGE("Cannot find constructor method for class %s", className); + if (ANI_OK != env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) { + HILOGE("Cannot find constructor method for class %s", classDesc); return nullptr; } + ani_long ptr = static_cast(reinterpret_cast(rafFile)); ani_object obj; if (ANI_OK != env->Object_New(cls, ctor, &obj, ptr)) { - HILOGE("New %s obj Failed!", className); + HILOGE("New %s obj Failed!", classDesc); return nullptr; } + const auto &fdRet = rafFile->GetFD(); if (!fdRet.IsSuccess()) { HILOGE("GetFD Failed!"); return nullptr; } + const auto &fd = fdRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "fd", static_cast(fd))) { HILOGE("Set fd field value failed!"); return nullptr; } + const auto &fpRet = rafFile->GetFPointer(); if (!fpRet.IsSuccess()) { HILOGE("GetFPointer Failed!"); return nullptr; } + const auto &fp = fpRet.GetData().value(); if (ANI_OK != AniHelper::SetPropertyValue(env, cls, obj, "filePointer", static_cast(fp))) { HILOGE("Set fp field value failed!"); @@ -73,16 +87,18 @@ static ani_object Wrap(ani_env *env, const FsRandomAccessFile *rafFile) static tuple JudgeFile(ani_env *env, ani_object obj) { + auto stringTypeDesc = BuiltInTypes::String::classDesc.c_str(); ani_class stringClass; - env->FindClass("Lstd/core/String;", &stringClass); + env->FindClass(stringTypeDesc, &stringClass); ani_boolean isString = false; env->Object_InstanceOf(obj, stringClass, &isString); if (isString) { return { true, true }; } + auto fileClassDesc = FS::FileInner::classDesc.c_str(); ani_class fileClass; - env->FindClass("L@ohos/file/fs/fileIo/FileInner;", &fileClass); + env->FindClass(fileClassDesc, &fileClass); ani_boolean isFile = false; env->Object_InstanceOf(obj, fileClass, &isFile); if (isFile) { @@ -118,8 +134,8 @@ static tuple> ToRafOptions(ani_env *env, return { true, make_optional(move(options)) }; } -static ani_object CreateRandomAccessFileByString(ani_env *env, ani_object file, ani_object mode, - optional op) +static ani_object CreateRandomAccessFileByString( + ani_env *env, ani_object file, ani_object mode, optional op) { auto [succPath, path] = TypeConverter::ToUTF8String(env, static_cast(file)); if (!succPath) { @@ -142,14 +158,16 @@ static ani_object CreateRandomAccessFileByString(ani_env *env, ani_object file, const FsRandomAccessFile *refFile = ret.GetData().value(); auto result = Wrap(env, move(refFile)); if (result == nullptr) { + delete refFile; + refFile = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } return result; } -ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync(ani_env *env, [[maybe_unused]] ani_class clazz, - ani_object file, ani_object mode, ani_object options) +ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync( + ani_env *env, [[maybe_unused]] ani_class clazz, ani_object file, ani_object mode, ani_object options) { auto [succOp, op] = ToRafOptions(env, options); if (!succOp) { @@ -185,6 +203,8 @@ ani_object CreateRandomAccessFileAni::CreateRandomAccessFileSync(ani_env *env, [ const FsRandomAccessFile *refFile = ret.GetData().value(); auto result = Wrap(env, move(refFile)); if (result == nullptr) { + delete refFile; + refFile = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/create_stream_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/create_stream_ani.cpp index b8fd35e59..d81983404 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/create_stream_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/create_stream_ani.cpp @@ -57,6 +57,8 @@ ani_object CreateStreamAni::CreateStreamSync( const FsStream *stream = ret.GetData().value(); auto result = StreamWrapper::Wrap(env, move(stream)); if (result == nullptr) { + delete stream; + stream = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp index 8540848a3..2e8157190 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/listfile_ani.cpp @@ -15,6 +15,7 @@ #include "listfile_ani.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "listfile_core.h" @@ -27,6 +28,7 @@ namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; tuple ParseBooleanParam(ani_env *env, ani_object obj, string tag) { @@ -39,9 +41,11 @@ tuple ParseBooleanParam(ani_env *env, ani_object obj, string tag) if (isUndefined) { return { true, false }; } + auto unboxedDesc = BoxedTypes::Boolean::unboxedDesc.c_str(); + auto unboxedSig = BoxedTypes::Boolean::unboxedSig.c_str(); ani_boolean bool_ref_res; if (ANI_OK != env->Object_CallMethodByName_Boolean( - static_cast(bool_ref), "unboxed", ":Z", &bool_ref_res)) { + static_cast(bool_ref), unboxedDesc, unboxedSig, &bool_ref_res)) { return { false, false }; } return { true, static_cast(bool_ref_res) }; @@ -108,10 +112,12 @@ tuple>> ParseArrayString(ani_env *env, ani_object static_cast(result_ref), "length", &length) || length == 0) { return { false, nullopt }; } + auto getterDesc = BuiltInTypes::Array::getterDesc.c_str(); + auto getterSig = BuiltInTypes::Array::objectGetterSig.c_str(); for (int i = 0; i < int(length); i++) { ani_ref stringEntryRef; if (ANI_OK != env->Object_CallMethodByName_Ref( - static_cast(result_ref), "$_get", "I:Lstd/core/Object;", &stringEntryRef, (ani_int)i)) { + static_cast(result_ref), getterDesc, getterSig, &stringEntryRef, (ani_int)i)) { return { false, nullopt }; } auto [succ, tmp] = TypeConverter::ToUTF8String(env, static_cast(stringEntryRef)); diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp index beec317d3..33bfb77cf 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/movedir_ani.cpp @@ -16,6 +16,7 @@ #include "movedir_ani.h" #include +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "movedir_core.h" @@ -25,20 +26,23 @@ namespace OHOS { namespace FileManagement { namespace ModuleFileIO { namespace ANI { +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static tuple ToConflictFiles(ani_env *env, const ErrFiles &files) { - static const char *className = "L@ohos/file/fs/fileIo/ConflictFilesInner;"; + auto classDesc = FS::ConflictFilesInner::classDesc.c_str(); ani_class cls; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Cannot find class %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Cannot find class %{private}s, err: %{private}d", classDesc, ret); return { false, nullptr }; } + auto ctorDesc = FS::ConflictFilesInner::ctorDesc.c_str(); + auto ctorSig = FS::ConflictFilesInner::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "Lstd/core/String;Lstd/core/String;:V", &ctor)) != ANI_OK) { - HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", className, ret); + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { + HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", classDesc, ret); return { false, nullptr }; } @@ -69,18 +73,20 @@ static tuple> ToConflictFilesArray( if (!errFiles.has_value()) { return { true, nullopt }; } - static const char *className = "Lescompat/Array;"; + auto classDesc = BuiltInTypes::Array::classDesc.c_str(); ani_class cls = nullptr; ani_status ret; - if ((ret = env->FindClass(className, &cls)) != ANI_OK) { - HILOGE("Cannot find class %{private}s, err: %{private}d", className, ret); + if ((ret = env->FindClass(classDesc, &cls)) != ANI_OK) { + HILOGE("Cannot find class %{private}s, err: %{private}d", classDesc, ret); return { false, nullopt }; } + auto ctorDesc = BuiltInTypes::Array::ctorDesc.c_str(); + auto ctorSig = BuiltInTypes::Array::ctorSig.c_str(); ani_method ctor; - if ((ret = env->Class_FindMethod(cls, "", "I:V", &ctor)) != ANI_OK) { - HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", className, ret); + if ((ret = env->Class_FindMethod(cls, ctorDesc, ctorSig, &ctor)) != ANI_OK) { + HILOGE("Cannot find class %{private}s constructor method, err: %{private}d", classDesc, ret); return { false, nullopt }; } @@ -91,6 +97,8 @@ static tuple> ToConflictFilesArray( return { false, nullopt }; } + auto setterDesc = BuiltInTypes::Array::setterDesc.c_str(); + auto setterSig = BuiltInTypes::Array::objectSetterSig.c_str(); ani_size index = 0; for (const auto &errFile : files) { auto [succ, fileObj] = ToConflictFiles(env, errFile); @@ -98,7 +106,7 @@ static tuple> ToConflictFilesArray( return { false, nullopt }; } - if ((ret = env->Object_CallMethodByName_Void(arr, "$_set", "ILstd/core/Object;:V", index, fileObj)) != ANI_OK) { + if ((ret = env->Object_CallMethodByName_Void(arr, setterDesc, setterSig, index, fileObj)) != ANI_OK) { HILOGE("Add element to Array failed, err: %{private}d", ret); return { false, nullopt }; } @@ -140,6 +148,7 @@ void MoveDirAni::MoveDirSync( return; } } + } // namespace ANI } // namespace ModuleFileIO } // namespace FileManagement diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp index 68538e0be..eceb4b1bd 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/open_ani.cpp @@ -53,6 +53,8 @@ ani_object OpenAni::OpenSync(ani_env *env, [[maybe_unused]] ani_class clazz, ani const FsFile *file = ret.GetData().value(); auto result = FileWrapper::Wrap(env, move(file)); if (result == nullptr) { + delete file; + file = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/watcher_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/watcher_ani.cpp index 04e281ab0..f0641c93f 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/watcher_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/watcher_ani.cpp @@ -66,6 +66,8 @@ ani_object WatcherAni::CreateWatcherSync( const FsWatcher *watcher = ret.GetData().value(); auto result = FsWatcherWrapper::Wrap(env, move(watcher)); if (result == nullptr) { + delete watcher; + watcher = nullptr; ErrorHandler::Throw(env, UNKNOWN_ERR); return nullptr; } diff --git a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp index a87254108..f20a83917 100644 --- a/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp +++ b/interfaces/kits/js/src/mod_fs/properties/ani/write_ani.cpp @@ -17,6 +17,7 @@ #include #include "ani_helper.h" +#include "ani_signature.h" #include "error_handler.h" #include "filemgmt_libhilog.h" #include "type_converter.h" @@ -28,6 +29,7 @@ namespace ModuleFileIO { namespace ANI { using namespace std; using namespace OHOS::FileManagement::ModuleFileIO; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static tuple> ToWriteOptions(ani_env *env, ani_object obj) { @@ -63,8 +65,9 @@ static tuple> ToWriteOptions(ani_env *env, ani_obje static std::tuple ParseStringBuffer(ani_env *env, const ani_object &buf) { + auto classDesc = BuiltInTypes::String::classDesc.c_str(); ani_class cls; - env->FindClass("Lstd/core/String;", &cls); + env->FindClass(classDesc, &cls); ani_boolean isString; env->Object_InstanceOf(buf, cls, &isString); @@ -77,8 +80,9 @@ static std::tuple ParseStringBuffer(ani_env *env, const ani_ob static std::tuple ParseArrayBuffer(ani_env *env, const ani_object &buf) { + auto classDesc = BuiltInTypes::ArrayBuffer::classDesc.c_str(); ani_class cls; - env->FindClass("Lescompat/ArrayBuffer;", &cls); + env->FindClass(classDesc, &cls); ani_boolean isArrayBuffer; env->Object_InstanceOf(buf, cls, &isArrayBuffer); diff --git a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp index d93492ac7..9edb9abef 100644 --- a/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_hash/ani/bind_function_class.cpp @@ -14,19 +14,20 @@ */ #include +#include "ani_signature.h" #include "bind_function.h" #include "hash_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/hash/HashImpl;"; + auto classDesc = Impl::HashImpl::classDesc.c_str(); std::array methods = { - ani_native_function { "hashSync", "Lstd/core/String;Lstd/core/String;:Lstd/core/String;", - reinterpret_cast(HashAni::HashSync) }, + ani_native_function { "hashSync", nullptr, reinterpret_cast(HashAni::HashSync) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) diff --git a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp index 5bb0f2650..947e460b6 100644 --- a/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_securitylabel/ani/bind_function_class.cpp @@ -14,21 +14,23 @@ */ #include +#include "ani_signature.h" #include "bind_function.h" #include "securitylabel_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/securityLabel/SecurityLabelImpl;"; + auto classDesc = Impl::SecurityLabelImpl::classDesc.c_str(); std::array methods = { - ani_native_function { "setSecurityLabelSync", "Lstd/core/String;Lstd/core/String;:V", - reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, - ani_native_function { "getSecurityLabelSync", "Lstd/core/String;:Lstd/core/String;", - reinterpret_cast(SecurityLabelAni::GetSecurityLabelSync) }, + ani_native_function { + "setSecurityLabelSync", nullptr, reinterpret_cast(SecurityLabelAni::SetSecurityLabelSync) }, + ani_native_function { + "getSecurityLabelSync", nullptr, reinterpret_cast(SecurityLabelAni::GetSecurityLabelSync) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) diff --git a/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp b/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp index aed652923..33782a3d1 100644 --- a/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp +++ b/interfaces/kits/js/src/mod_statvfs/ani/bind_function_class.cpp @@ -15,20 +15,22 @@ #include #include +#include "ani_signature.h" #include "bind_function.h" #include "statvfs_ani.h" using namespace OHOS::FileManagement::ModuleFileIO::ANI; +using namespace OHOS::FileManagement::ModuleFileIO::ANI::AniSignature; static ani_status BindStaticMethods(ani_env *env) { - static const char *className = "L@ohos/file/statvfs/StatvfsImpl;"; + auto classDesc = Impl::StatvfsImpl::classDesc.c_str(); std::array methods = { ani_native_function { "getFreeSizeSync", nullptr, reinterpret_cast(StatvfsAni::GetFreeSizeSync) }, ani_native_function { "getTotalSizeSync", nullptr, reinterpret_cast(StatvfsAni::GetTotalSizeSync) }, }; - return BindClass(env, className, methods); + return BindClass(env, classDesc, methods); } ANI_EXPORT ani_status ANI_Constructor(ani_vm *vm, uint32_t *result) -- Gitee