From a35b61792cbf6705dbd69b41d4a4167a4c5c765d Mon Sep 17 00:00:00 2001 From: Far Date: Mon, 25 Dec 2023 22:50:18 +0800 Subject: [PATCH] ContainerScope opitimize Signed-off-by: Far --- native_engine/native_api.cpp | 30 +++++++++++++++++++++++++++--- native_engine/native_engine.h | 11 +++++++++++ 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/native_engine/native_api.cpp b/native_engine/native_api.cpp index e0323df62..ca116ebb9 100644 --- a/native_engine/native_api.cpp +++ b/native_engine/native_api.cpp @@ -22,6 +22,7 @@ #ifdef ENABLE_CONTAINER_SCOPE #include "core/common/container_scope.h" +#include "core/common/container_consts.h" #endif #include "ecmascript/napi/include/jsnapi.h" #include "native_api_internal.h" @@ -1111,16 +1112,29 @@ NAPI_EXTERN napi_status napi_call_function(napi_env env, } RETURN_STATUS_IF_FALSE(env, reinterpret_cast(func)->IsFunction(), napi_function_expected); - auto vm = reinterpret_cast(env)->GetEcmaVm(); + auto engine = reinterpret_cast(env); + auto vm = engine->GetEcmaVm(); panda::JSValueRef* thisObj = reinterpret_cast(recv); panda::FunctionRef* function = reinterpret_cast(func); #ifdef ENABLE_CONTAINER_SCOPE - int32_t scopeId = OHOS::Ace::ContainerScope::CurrentId(); + int32_t *ptr = engine->GetInstanceIdPtr(); +#if !defined(PREVIEW) + int32_t scopeId = OHOS::Ace::INSTANCE_ID_UNDEFINED; +#else + int32_t scopeId = 0; +#endif + int32_t restoreId = scopeId; + if (ptr != nullptr) { + restoreId = *ptr; + scopeId = restoreId; + } auto funcInfo = reinterpret_cast(function->GetData(vm)); if (funcInfo != nullptr) { scopeId = funcInfo->scopeId; } - OHOS::Ace::ContainerScope containerScope(scopeId); + if (ptr != nullptr) { + *ptr = scopeId; + } #endif panda::JSValueRef* value = function->CallForNapi(vm, thisObj, reinterpret_cast(argv), argc); @@ -1128,11 +1142,21 @@ NAPI_EXTERN napi_status napi_call_function(napi_env env, HILOG_ERROR("pending exception when js function called"); HILOG_ERROR("print exception info: "); panda::JSNApi::PrintExceptionInfo(vm); +#ifdef ENABLE_CONTAINER_SCOPE + if (ptr != nullptr) { + *ptr = restoreId; + } +#endif return napi_set_last_error(env, napi_pending_exception); } if (result) { *result = reinterpret_cast(value); } +#ifdef ENABLE_CONTAINER_SCOPE + if (ptr != nullptr) { + *ptr = restoreId; + } +#endif return napi_clear_last_error(env); } diff --git a/native_engine/native_engine.h b/native_engine/native_engine.h index 7d716b46a..e664a3de5 100644 --- a/native_engine/native_engine.h +++ b/native_engine/native_engine.h @@ -441,6 +441,16 @@ public: uint64_t openHandleScopes_ = 0; panda::Local lastException_; + void SetInstanceIdPtr(int32_t *id) + { + instanceId_ = id; + } + + int32_t *GetInstanceIdPtr() + { + return instanceId_; + } + private: std::string moduleName_; std::string moduleFileName_; @@ -464,6 +474,7 @@ private: bool checkUVLoop_ = false; uv_thread_t uvThread_; #endif + int32_t *instanceId_ = nullptr; PostTask postTask_ = nullptr; CleanEnv cleanEnv_ = nullptr; -- Gitee