diff --git a/native_engine/native_api.cpp b/native_engine/native_api.cpp index e0323df624782f6a0e18e117a6402a0dad2a3c24..ca116ebb945266ae16338735600ac877ac501b55 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 7d716b46a5a5d037ab0c4cd4a14dd01d7a87f3d6..e664a3de581ce7838dfecd248eedb1a0569e08e5 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;