diff --git a/interfaces/napi/kits/inspector/js_inspector.cpp b/interfaces/napi/kits/inspector/js_inspector.cpp index 021b747b14ce97f67234e75edfa1e0bd12d5aa3a..7548674e7dced48baf84b67472e9a5c1391a1093 100644 --- a/interfaces/napi/kits/inspector/js_inspector.cpp +++ b/interfaces/napi/kits/inspector/js_inspector.cpp @@ -31,9 +31,8 @@ static ComponentObserver* GetObserver(napi_env env, napi_value thisVar) { ComponentObserver* observer = nullptr; napi_unwrap(env, thisVar, (void**)&observer); - if (observer->thisVarRef_ == nullptr) { - observer->Initialize(env, thisVar); - } + observer->Initialize(env, thisVar); + return observer; } @@ -81,8 +80,6 @@ void ComponentObserver::callUserFunction(std::list& cbList) if (scope == nullptr) { return; } - napi_value thisVal = nullptr; - napi_get_reference_value(env_, thisVarRef_, &thisVal); std::list cbs; for (auto& cbRef : cbList) { @@ -93,7 +90,7 @@ void ComponentObserver::callUserFunction(std::list& cbList) for (auto& cb : cbs) { napi_value resultArg = nullptr; napi_value result = nullptr; - napi_call_function(env_, thisVal, cb, 1, &resultArg, &result); + napi_call_function(env_, nullptr, cb, 1, &resultArg, &result); } napi_close_handle_scope(env_, scope); } @@ -162,9 +159,7 @@ void ComponentObserver::FunctionOn(napi_env& env, napi_value result, const char* napi_handle_scope scope = nullptr; napi_open_handle_scope(env, &scope); - if (scope == nullptr) { - return nullptr; - } + CHECK_NULL_RETURN(scope, nullptr); napi_value thisVar = nullptr; napi_value cb = nullptr; CalloutType calloutType = CalloutType::UNKNOW; @@ -195,9 +190,13 @@ void ComponentObserver::FunctionOff(napi_env& env, napi_value result, const char napi_value thisVar = nullptr; napi_value cb = nullptr; CalloutType calloutType = CalloutType::UNKNOW; + napi_handle_scope scope = nullptr; + napi_open_handle_scope(env, &scope); + CHECK_NULL_RETURN(scope, nullptr); size_t argc = ParseArgs(env, info, thisVar, cb, calloutType); ComponentObserver* observer = GetObserver(env, thisVar); if (!observer) { + napi_close_handle_scope(env, scope); return nullptr; } if (calloutType == CalloutType::LAYOUTCALLOUT) { @@ -205,6 +204,7 @@ void ComponentObserver::FunctionOff(napi_env& env, napi_value result, const char } else if (calloutType == CalloutType::DRAWCALLOUT) { observer->DeleteCallbackFromList(argc, observer->cbDrawList_, calloutType, cb, env); } + napi_close_handle_scope(env, scope); return nullptr; }; @@ -224,7 +224,6 @@ void ComponentObserver::NapiSerializer(napi_env& env, napi_value& result) napi_value componentIdVal = nullptr; napi_create_string_utf8(env, componentId_.c_str(), componentId_.size(), &componentIdVal); napi_set_named_property(env, result, "componentId", componentIdVal); - napi_close_handle_scope(env, scope); napi_wrap( env, result, this, @@ -238,6 +237,7 @@ void ComponentObserver::NapiSerializer(napi_env& env, napi_value& result) FunctionOn(env, result, "on"); FunctionOff(env, result, "off"); + napi_close_handle_scope(env, scope); } void ComponentObserver::Initialize(napi_env env, napi_value thisVar) @@ -250,7 +250,6 @@ void ComponentObserver::Initialize(napi_env env, napi_value thisVar) if (env_ == nullptr) { env_ = env; } - napi_create_reference(env, thisVar, 1, &thisVarRef_); napi_close_handle_scope(env, scope); } diff --git a/interfaces/napi/kits/inspector/js_inspector.h b/interfaces/napi/kits/inspector/js_inspector.h index e1c2a40a5fc210aadea7b98d7921ffb5870d3494..0cd449a08198e7dc7f861eacdadc8f1b7d465408 100644 --- a/interfaces/napi/kits/inspector/js_inspector.h +++ b/interfaces/napi/kits/inspector/js_inspector.h @@ -45,10 +45,6 @@ public: for (auto& drawitem : cbDrawList_) { napi_delete_reference(env_, drawitem); } - if (thisVarRef_ != nullptr) { - napi_delete_reference(env_, thisVarRef_); - } - auto jsEngine = EngineHelper::GetCurrentEngineSafely(); if (!jsEngine) { return; @@ -71,7 +67,6 @@ public: std::string componentId_; std::list cbLayoutList_; std::list cbDrawList_; - napi_ref thisVarRef_ = nullptr; private: void FunctionOn(napi_env& env, napi_value result, const char* funName);