From af650d43eb1bc6f22b6422130efc3cf7d74aeacc Mon Sep 17 00:00:00 2001 From: gaoshunli Date: Tue, 1 Nov 2022 23:25:23 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BF=AE=E6=94=B9Entity?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: gaoshunli --- interfaces/kits/js/src/common/napi/n_class.h | 14 ++++++++++++++ .../mod_fileio/class_dir/dir_n_exporter.cpp | 11 ++++++++++- .../randomaccessfile_n_exporter.cpp | 2 ++ .../class_stream/stream_n_exporter.cpp | 5 ++++- .../js/src/mod_fileio/properties/close.cpp | 19 ++++++++++++++++++- 5 files changed, 48 insertions(+), 3 deletions(-) diff --git a/interfaces/kits/js/src/common/napi/n_class.h b/interfaces/kits/js/src/common/napi/n_class.h index 13a194ade..b44dd2933 100644 --- a/interfaces/kits/js/src/common/napi/n_class.h +++ b/interfaces/kits/js/src/common/napi/n_class.h @@ -68,6 +68,20 @@ public: return status == napi_ok; } + template static T *RemoveEntityOfFinal(napi_env env, napi_value objStat) { + if(!env || !objStat) { + HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr); + return nullptr; + } + T *t = nullptr; + napi_status status = napi_remove_wrap(env, objStat, (void **)&t); + if(status != napi_ok) { + HILOGD("Cannot umwrap for pointer: %d", status); + return nullptr; + } + return t; + } + private: NClass() = default; ~NClass() = default; diff --git a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp index 43bae0a64..0ec795d8a 100644 --- a/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_dir/dir_n_exporter.cpp @@ -54,6 +54,12 @@ static DirEntity *GetDirEntity(napi_env env, napi_callback_info info) napi_value DirNExporter::CloseSync(napi_env env, napi_callback_info info) { + NFuncArg funcArg(env, info); + if (!funcArg.InitArgs(NARG_CNT::ZERO, NARG_CNT::ONE)) { + UniError(EINVAL).ThrowErr(env, "Number of arguments unmatched"); + return nullptr; + } + DirEntity *dirEntity = GetDirEntity(env, info); if (!dirEntity || !dirEntity->dir_) { UniError(EBADF).ThrowErr(env, "Dir has been closed yet"); @@ -61,6 +67,8 @@ napi_value DirNExporter::CloseSync(napi_env env, napi_callback_info info) } dirEntity->dir_.reset(); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + return nullptr; } @@ -92,7 +100,8 @@ napi_value DirNExporter::Close(napi_env env, napi_callback_info info) return UniError(ERRNO_NOERR); } }; - auto cbCompl = [](napi_env env, UniError err) -> NVal { + auto cbCompl = [arg = funcArg.GetThisVar()](napi_env env, UniError err) -> NVal { + (void)NClass::RemoveEntityOfFinal(env, arg); if (err) { return { env, err.GetNapiErr(env) }; } else { diff --git a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp index f3f513c05..46e73c366 100644 --- a/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_randomaccessfile/randomaccessfile_n_exporter.cpp @@ -353,6 +353,8 @@ napi_value RandomAccessFileNExporter::CloseSync(napi_env env, napi_callback_info return nullptr; } rafEntity->fd_.reset(); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + return NVal::CreateUndefined(env).val_; } diff --git a/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp b/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp index f9d6960b8..3398e5ca1 100644 --- a/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp +++ b/interfaces/kits/js/src/mod_fileio/class_stream/stream_n_exporter.cpp @@ -96,6 +96,8 @@ napi_value StreamNExporter::CloseSync(napi_env env, napi_callback_info info) return nullptr; } streamEntity->fp.reset(); + (void)NClass::RemoveEntityOfFinal(env, funcArg.GetThisVar()); + return NVal::CreateUndefined(env).val_; } @@ -319,7 +321,8 @@ napi_value StreamNExporter::Close(napi_env env, napi_callback_info info) } }; - auto cbCompl = [](napi_env env, UniError err) -> NVal { + auto cbCompl = [arg = funcArg.GetThisVar()](napi_env env, UniError err) -> NVal { + (void)NClass::RemoveEntityOfFinal(env, arg); if (err) { return { env, err.GetNapiErr(env) }; } else { diff --git a/interfaces/kits/js/src/mod_fileio/properties/close.cpp b/interfaces/kits/js/src/mod_fileio/properties/close.cpp index 202ad04bc..ef6dcb346 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/close.cpp @@ -19,6 +19,7 @@ #include #include +#include "../../common/log.h" #include "../../common/napi/n_async/n_async_work_callback.h" #include "../../common/napi/n_async/n_async_work_promise.h" #include "../../common/napi/n_func_arg.h" @@ -27,6 +28,19 @@ namespace DistributedFS { namespace ModuleFileIO { using namespace std; +static void RemoveWrapFinal(napi_env env, napi_value objStat) { + if(!env || !objStat) { + HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr); + return; + } + void *t = nullptr; + napi_status status = napi_remove_wrap(env, objStat, &t); + if(status != napi_ok) { + HILOGD("Cannot umwrap for pointer: %d", status); + return; + } + } + napi_value Close::Sync(napi_env env, napi_callback_info info) { NFuncArg funcArg(env, info); @@ -46,6 +60,8 @@ napi_value Close::Sync(napi_env env, napi_callback_info info) return nullptr; } + RemoveWrapFinal(env, funcArg.GetThisVar()); + return NVal::CreateUndefined(env).val_; } @@ -72,7 +88,8 @@ napi_value Close::Async(napi_env env, napi_callback_info info) } }; - auto cbComplete = [](napi_env env, UniError err) -> NVal { + auto cbComplete = [arg = funcArg.GetThisVar()](napi_env env, UniError err) -> NVal { + RemoveWrapFinal(env, arg); if (err) { return { env, err.GetNapiErr(env) }; } else { -- Gitee From 3a9e8505e0ba29f780713090b79644669043a4d4 Mon Sep 17 00:00:00 2001 From: gsl_1234 Date: Tue, 1 Nov 2022 16:18:46 +0000 Subject: [PATCH 2/2] update interfaces/kits/js/src/mod_fileio/properties/close.cpp. Signed-off-by: gsl_1234 --- .../js/src/mod_fileio/properties/close.cpp | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/interfaces/kits/js/src/mod_fileio/properties/close.cpp b/interfaces/kits/js/src/mod_fileio/properties/close.cpp index ef6dcb346..f54a42a26 100644 --- a/interfaces/kits/js/src/mod_fileio/properties/close.cpp +++ b/interfaces/kits/js/src/mod_fileio/properties/close.cpp @@ -29,17 +29,17 @@ namespace ModuleFileIO { using namespace std; static void RemoveWrapFinal(napi_env env, napi_value objStat) { - if(!env || !objStat) { - HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr); - return; - } - void *t = nullptr; - napi_status status = napi_remove_wrap(env, objStat, &t); - if(status != napi_ok) { - HILOGD("Cannot umwrap for pointer: %d", status); - return; - } + if(!env || !objStat) { + HILOGD("Empty input: env %d,obj %d", env == nullptr, objStat == nullptr); + return; } + void *t = nullptr; + napi_status status = napi_remove_wrap(env, objStat, &t); + if(status != napi_ok) { + HILOGD("Cannot umwrap for pointer: %d", status); + return; + } +} napi_value Close::Sync(napi_env env, napi_callback_info info) { -- Gitee