diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp index 7e4022d402b2c413935522e639e3c1a24806ca4e..93ce9ace08e0476da78cc2caf379790fcd5c051f 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator.cpp @@ -172,28 +172,27 @@ ani_object EtsAbilityDelegator::GetAppContext(ani_env *env, [[maybe_unused]]ani_ TAG_LOGD(AAFwkTag::DELEGATOR, "GetAppContext call"); if (env == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "env is nullptr"); - return {}; + return nullptr; } ani_class cls = nullptr; - ani_object nullobj = nullptr; if (ANI_OK != env->FindClass(CONTEXT_CLASS_NAME, &cls)) { TAG_LOGE(AAFwkTag::DELEGATOR, "FindClass Context Failed"); - return nullobj; + return AppExecFwk::CreateEtsNull(env); } auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS); if (!delegator) { TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator"); - return nullobj; + return AppExecFwk::CreateEtsNull(env); } std::shared_ptr context = delegator->GetAppContext(); if (!context) { TAG_LOGE(AAFwkTag::DELEGATOR, "null context"); - return nullobj; + return AppExecFwk::CreateEtsNull(env); } ani_object contextObj = SetAppContext(env, context); if (contextObj == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null contextObj"); - return nullobj; + return AppExecFwk::CreateEtsNull(env); } ContextUtil::CreateEtsBaseContext(env, cls, contextObj, context); TAG_LOGD(AAFwkTag::DELEGATOR, "GetAppContext end"); diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp index 76673d5ff46810a141f12b987de2b6c279614426..05c5bdd71891789bb778efa41926064e3086abb3 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_registry.cpp @@ -18,6 +18,7 @@ #include #include "ability_delegator.h" #include "ability_delegator_registry.h" +#include "ani_common_util.h" #include "ets_ability_delegator.h" #include "ets_ability_delegator_utils.h" #include "ets_native_reference.h" @@ -39,34 +40,34 @@ static ani_object GetAbilityDelegator(ani_env *env) { if (env == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null env"); - return {}; + return nullptr; } std::lock_guard lock(etsReferenceMutex); auto delegator = AppExecFwk::AbilityDelegatorRegistry::GetAbilityDelegator(AbilityRuntime::Runtime::Language::ETS); if (delegator == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null delegator"); - return {}; + return AppExecFwk::CreateEtsNull(env); } if (etsReference == nullptr) { ani_object value = CreateEtsAbilityDelegator(env); if (value == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "value is nullptr"); - return {}; + return AppExecFwk::CreateEtsNull(env); } ani_boolean isValue = false; env->Reference_IsNullishValue(value, &isValue); if (isValue) { TAG_LOGE(AAFwkTag::DELEGATOR, "Reference_IsNullishValue"); - return {}; + return AppExecFwk::CreateEtsNull(env); } etsReference = std::make_unique(); ani_ref result = nullptr; auto status = env->GlobalReference_Create(value, &(result)); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Create Gloabl ref for delegator failed %{public}d", status); - return {}; + return AppExecFwk::CreateEtsNull(env); } etsReference->aniObj = static_cast(result); return etsReference->aniObj; @@ -79,13 +80,13 @@ static ani_object GetArguments(ani_env *env) { if (env == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null env"); - return {}; + return nullptr; } auto abilityDelegatorArgs = AppExecFwk::AbilityDelegatorRegistry::GetArguments(); if (abilityDelegatorArgs == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "get argument failed"); - return {}; + return AppExecFwk::CreateEtsNull(env); } return CreateEtsAbilityDelegatorArguments(env, abilityDelegatorArgs); diff --git a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp index e5ad56d26808529e9f2e01dde1972bef71d779e1..026511a74338ce2a7857c42425090e7de2455c63 100644 --- a/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp +++ b/frameworks/ets/ani/ability_delegator/src/ets_ability_delegator_utils.cpp @@ -16,6 +16,7 @@ #include "ets_ability_delegator_utils.h" #include +#include "ani_common_util.h" #include "ets_ability_delegator.h" #include "hilog_tag_wrapper.h" @@ -312,14 +313,14 @@ ani_object CreateEtsAbilityDelegatorArguments( TAG_LOGD(AAFwkTag::DELEGATOR, "CreateJsAbilityDelegatorArguments"); if (aniEnv == nullptr || abilityDelegatorArgs == nullptr) { TAG_LOGE(AAFwkTag::DELEGATOR, "null aniEnv or abilityDelegatorArgs"); - return {}; + return nullptr; } ani_class arguments = nullptr; ani_status status = ANI_ERROR; status = aniEnv->FindClass(ARGS_ABILITY_DELEGATOR_CLASS_NAME, &arguments); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "find abilityDelegatorArgs failed status: %{public}d", status); - return {}; + return AppExecFwk::CreateEtsNull(aniEnv); } TAG_LOGD(AAFwkTag::DELEGATOR, "find AbilityDelegatorArgs success"); @@ -327,7 +328,7 @@ ani_object CreateEtsAbilityDelegatorArguments( status = aniEnv->Class_FindMethod(arguments, "", ":V", &method); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Class_FindMethod ctor failed status: %{public}d", status); - return {}; + return AppExecFwk::CreateEtsNull(aniEnv); } TAG_LOGD(AAFwkTag::DELEGATOR, "Class_FindMethod ctor success"); @@ -335,7 +336,7 @@ ani_object CreateEtsAbilityDelegatorArguments( status = aniEnv->Object_New(arguments, method, &argumentObject); if (status != ANI_OK) { TAG_LOGE(AAFwkTag::DELEGATOR, "Object_New failed status: %{public}d", status); - return {}; + return AppExecFwk::CreateEtsNull(aniEnv); } TAG_LOGD(AAFwkTag::DELEGATOR, "Object_New success"); diff --git a/frameworks/ets/ani/ani_common/include/ani_common_util.h b/frameworks/ets/ani/ani_common/include/ani_common_util.h index 46cbac98b79a885252b7286cdc3ae8594e8b6dc8..d92c63419fa31e21d8a69919caecbda6d28fa6f1 100644 --- a/frameworks/ets/ani/ani_common/include/ani_common_util.h +++ b/frameworks/ets/ani/ani_common/include/ani_common_util.h @@ -56,6 +56,8 @@ ani_object CreateDouble(ani_env *env, ani_double value); ani_object CreateBoolean(ani_env *env, ani_boolean value); ani_object CreateLong(ani_env *env, ani_long value); ani_object CreateInt(ani_env *env, ani_int value); +ani_object CreateEtsNull(ani_env *env); +ani_object CreateEtsUndefine(ani_env *env); bool AsyncCallback(ani_env *env, ani_object call, ani_object error, ani_object result); bool GetPropertyRef(ani_env *env, ani_object obj, const char *name, ani_ref &ref, ani_boolean &isUndefined); diff --git a/frameworks/ets/ani/ani_common/src/ani_common_util.cpp b/frameworks/ets/ani/ani_common/src/ani_common_util.cpp index 9f6c032d3ff7f4ba075c966bfed7536743df6e99..315efbeb8c7872392a0bdbe4493ecf5d93f99b2a 100644 --- a/frameworks/ets/ani/ani_common/src/ani_common_util.cpp +++ b/frameworks/ets/ani/ani_common/src/ani_common_util.cpp @@ -526,6 +526,27 @@ ani_object CreateBoolean(ani_env *env, ani_boolean value) } return obj; } +ani_object CreateEtsNull(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + ani_ref ref = nullptr; + env->GetNull(&ref); + return reinterpret_cast(ref); +} + +ani_object CreateEtsUndefine(ani_env *env) +{ + if (env == nullptr) { + TAG_LOGE(AAFwkTag::ANI, "null env"); + return nullptr; + } + ani_ref ref = nullptr; + env->GetUndefined(&ref); + return reinterpret_cast(ref); +} ani_object CreateLong(ani_env *env, ani_long value) { diff --git a/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp b/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp index 858c33c7c3860175d3ef4cc4f409a3764ead2ca1..1b9c1904d9866617b0ef7f503da0e0f1509926ec 100644 --- a/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp +++ b/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp @@ -455,45 +455,39 @@ ani_object NativeCreateDisplayContext(ani_env *env, ani_object aniObj, ani_long TAG_LOGE(AAFwkTag::APPKIT, "null env"); return nullptr; } - ani_ref undefRef = nullptr; - ani_status status = env->GetUndefined(&undefRef); - if (status != ANI_OK) { - TAG_LOGE(AAFwkTag::ABILITY, "GetUndefined failed %{public}d", status); - return nullptr; - } #ifdef SUPPORT_GRAPHICS auto context = GetBaseContext(env, aniObj); if (context == nullptr) { TAG_LOGE(AAFwkTag::APPKIT, "null context"); EtsErrorUtil::ThrowRuntimeError(env, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); } if (displayId < 0) { TAG_LOGE(AAFwkTag::APPKIT, "displayId is invalid, less than 0"); EtsErrorUtil::ThrowRuntimeError(env, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); } uint64_t validDisplayId = static_cast(displayId); auto displayContext = context->CreateDisplayContext(validDisplayId); if (displayContext == nullptr) { TAG_LOGE(AAFwkTag::APPKIT, "Failed to create displayContext"); - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); } ani_class contextClass = nullptr; - status = env->FindClass(CONTEXT_CLASS_NAME, &contextClass); + ani_status status = env->FindClass(CONTEXT_CLASS_NAME, &contextClass); if (status != ANI_OK || contextClass == nullptr) { TAG_LOGE(AAFwkTag::APPKIT, "FindClass Context failed, status: %{public}d", status); EtsErrorUtil::ThrowInvalidParamError(env, "FindClass failed"); - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); } ani_object displayContextObj = CreateContextObject(env, contextClass, displayContext); if (displayContextObj == nullptr) { TAG_LOGE(AAFwkTag::APPKIT, "null contextObj"); - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); } return displayContextObj; #else - return reinterpret_cast(undefRef); + return AppExecFwk::CreateEtsUndefine(env); #endif }