diff --git a/interfaces/kits/js/src/common/napi/n_class.h b/interfaces/kits/js/src/common/napi/n_class.h index 13a194ade3728777444816f0c451c1a1a26f7e8f..b44dd29332f6e125021108386f940f5d82106a24 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 43bae0a641fe559f119748a490977681cc3a716a..0ec795d8af69c5afeff193610adfe5839a589243 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 f3f513c05eda5982855310a4ca753f9537e9cbb3..46e73c3667ff9324f24acfd9c991615c6be21dad 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 f9d6960b858f73d554ce6bfcd907950f535b1172..3398e5ca184316e5b3534a31901597b93f51c1fc 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 202ad04bc0780ab1eda99d2bc6aa314667b7fe3f..f54a42a264222f1153febfc3ae1764f4f1640342 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 {