From ede11ac157be0c7e6952bac46296ac13271815d9 Mon Sep 17 00:00:00 2001 From: zhangzezhong Date: Wed, 20 Aug 2025 10:13:46 +0800 Subject: [PATCH] fix ret errorcode 0820 Signed-off-by: zhangzezhong --- .../src/ets_ability_delegator.cpp | 11 +++++----- .../src/ets_ability_delegator_registry.cpp | 19 +++++++++-------- .../src/ets_ability_delegator_utils.cpp | 11 +++++----- .../ani/ani_common/include/ani_common_util.h | 2 ++ .../ani/ani_common/src/ani_common_util.cpp | 21 +++++++++++++++++++ .../ani/ani_common/src/ets_context_utils.cpp | 1 + .../dialog_session/src/ets_dialog_session.cpp | 8 +++++-- .../src/ets_dialog_session_utils.cpp | 6 +++--- ...s.app.ability.abilityDelegatorRegistry.ets | 4 ++-- .../ets/@ohos.app.ability.dialogSession.ets | 2 +- .../ets/ets/application/AbilityDelegator.ets | 2 +- 11 files changed, 58 insertions(+), 29 deletions(-) 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 7e4022d402b..93ce9ace08e 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 76673d5ff46..59e1ce0a8ee 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" @@ -31,42 +32,42 @@ std::mutex etsReferenceMutex; namespace { constexpr const char* ETS_DELEGATOR_REGISTRY_NAMESPACE = "L@ohos/app/ability/abilityDelegatorRegistry/abilityDelegatorRegistry;"; -constexpr const char* ETS_DELEGATOR_REGISTRY_SIGNATURE_DELEAGTOR = ":Lapplication/AbilityDelegator/AbilityDelegator;"; +constexpr const char* ETS_DELEGATOR_REGISTRY_SIGNATURE_DELEAGTOR = ":Lstd/core/Object;"; constexpr const char* ETS_DELEGATOR_REGISTRY_SIGNATURE_ATGS = - ":Lapplication/abilityDelegatorArgs/AbilityDelegatorArgs;";; + ":Lstd/core/Object;"; } 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 e5ad56d2680..efae264b163 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" @@ -24,7 +25,7 @@ namespace AbilityDelegatorEts { namespace { constexpr const char* ABILITY_DELEGATOR_CLASS_NAME = "Lapplication/AbilityDelegator/AbilityDelegatorInner;"; constexpr const char* RECORD_CLASS_NAME = "Lescompat/Record;"; -constexpr const char* VOID_CONTEXT = ":Lapplication/Context/Context;"; +constexpr const char* VOID_CONTEXT = ":Lstd/core/Object"; constexpr const char* STRING_NUMBER_ASYNCCALLBACK = "Lstd/core/String;JLutils/AbilityUtils/AsyncCallbackWrapper;:V"; constexpr const char* STRING_VOID = "Lstd/core/String;:V"; constexpr const char* MONITOR_ASYNCCALLBACK_VOID = @@ -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 46cbac98b79..05e058e3d2e 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 CreateEtsUndefined(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 c068999669d..ecbcee8b825 100644 --- a/frameworks/ets/ani/ani_common/src/ani_common_util.cpp +++ b/frameworks/ets/ani/ani_common/src/ani_common_util.cpp @@ -539,6 +539,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 CreateEtsUndefined(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 858c33c7c38..d02450b9cbd 100644 --- a/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp +++ b/frameworks/ets/ani/ani_common/src/ets_context_utils.cpp @@ -493,6 +493,7 @@ ani_object NativeCreateDisplayContext(ani_env *env, ani_object aniObj, ani_long } return displayContextObj; #else + EtsErrorUtil::ThrowRuntimeError(env, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); return reinterpret_cast(undefRef); #endif } diff --git a/frameworks/ets/ani/dialog_session/src/ets_dialog_session.cpp b/frameworks/ets/ani/dialog_session/src/ets_dialog_session.cpp index e645f96ebb7..b6dd2482346 100644 --- a/frameworks/ets/ani/dialog_session/src/ets_dialog_session.cpp +++ b/frameworks/ets/ani/dialog_session/src/ets_dialog_session.cpp @@ -67,11 +67,15 @@ static void SendDialogResult( static ani_object GetDialogSessionInfo(ani_env *env, ani_string etsDialogSessionId) { TAG_LOGD(AAFwkTag::DIALOG, "call GetDialogSessionInfo"); + if (env == nullptr) { + TAG_LOGE(AAFwkTag::DELEGATOR, "null env"); + return nullptr; + } std::string dialogSessionId = ""; if (!AppExecFwk::GetStdString(env, etsDialogSessionId, dialogSessionId)) { TAG_LOGE(AAFwkTag::DIALOG, "Failed unwrap dialogSessionId"); EtsErrorUtil::ThrowInvalidParamError(env, "Parameter error: dialogSessionId must be a valid string."); - return nullptr; + return AppExecFwk::CreateEtsUndefined(env); } sptr dialogSessionInfo; @@ -81,7 +85,7 @@ static ani_object GetDialogSessionInfo(ani_env *env, ani_string etsDialogSession if (errCode != ERR_OK || dialogSessionInfo == nullptr) { TAG_LOGE(AAFwkTag::DIALOG, "GetDialogSessionInfo failed with incorrect return value or empty dialogSessionInfo"); - return nullptr; + return AppExecFwk::CreateEtsUndefined(env); } #endif // SUPPORT_SCREEN return AppExecFwk::WrapDialogSessionInfo(env, *dialogSessionInfo); diff --git a/frameworks/ets/ani/dialog_session/src/ets_dialog_session_utils.cpp b/frameworks/ets/ani/dialog_session/src/ets_dialog_session_utils.cpp index 587d5e89ff1..44b68b87825 100644 --- a/frameworks/ets/ani/dialog_session/src/ets_dialog_session_utils.cpp +++ b/frameworks/ets/ani/dialog_session/src/ets_dialog_session_utils.cpp @@ -190,15 +190,15 @@ ani_object WrapDialogSessionInfo(ani_env *env, const AAFwk::DialogSessionInfo &d } if ((status = env->FindClass(CLASSNAME_DIALOG_SESSION_INFO, &cls)) != ANI_OK || cls == nullptr) { TAG_LOGE(AAFwkTag::DIALOG, "FindClass status : %{public}d or null cls", status); - return nullptr; + return AppExecFwk::CreateEtsUndefined(env); } if ((status = env->Class_FindMethod(cls, "", ":V", &method)) != ANI_OK || method == nullptr) { TAG_LOGE(AAFwkTag::DIALOG, "Class_FindMethod status : %{public}d or null method", status); - return nullptr; + return AppExecFwk::CreateEtsUndefined(env); } if ((status = env->Object_New(cls, method, &etsObject)) != ANI_OK || etsObject == nullptr) { TAG_LOGE(AAFwkTag::DIALOG, "Object_New status : %{public}d or null etsObject", status); - return nullptr; + return AppExecFwk::CreateEtsUndefined(env); } status = env->Object_SetPropertyByName_Ref( diff --git a/frameworks/ets/ets/@ohos.app.ability.abilityDelegatorRegistry.ets b/frameworks/ets/ets/@ohos.app.ability.abilityDelegatorRegistry.ets index 687ae42a14f..115e293f217 100644 --- a/frameworks/ets/ets/@ohos.app.ability.abilityDelegatorRegistry.ets +++ b/frameworks/ets/ets/@ohos.app.ability.abilityDelegatorRegistry.ets @@ -22,9 +22,9 @@ import { ShellCmdResult as _ShellCmdResult } from 'application.shellCmdResult'; export default namespace abilityDelegatorRegistry { loadLibrary("ability_delegator_registry_ani_kit.z") - export native function getAbilityDelegator(): AbilityDelegator; + export native function getAbilityDelegator(): AbilityDelegator | null; - export native function getArguments(): AbilityDelegatorArgs; + export native function getArguments(): AbilityDelegatorArgs | null; export enum AbilityLifecycleState { UNINITIALIZED, diff --git a/frameworks/ets/ets/@ohos.app.ability.dialogSession.ets b/frameworks/ets/ets/@ohos.app.ability.dialogSession.ets index 7a47ea7d8e3..79e6408e925 100644 --- a/frameworks/ets/ets/@ohos.app.ability.dialogSession.ets +++ b/frameworks/ets/ets/@ohos.app.ability.dialogSession.ets @@ -22,7 +22,7 @@ import Want from '@ohos.app.ability.Want'; export default namespace dialogSession { loadLibrary("dialog_session_ani_kit.z") - export native function getDialogSessionInfo(dialogSessionId: string): DialogSessionInfo; + export native function getDialogSessionInfo(dialogSessionId: string): DialogSessionInfo | undefined; export native function nativeSendDialogResult(dialogSessionId: string, targetWant: Want, isAllowed: boolean, callback: AsyncCallbackWrapper): void; export interface DialogAbilityInfo { diff --git a/frameworks/ets/ets/application/AbilityDelegator.ets b/frameworks/ets/ets/application/AbilityDelegator.ets index fec39c5bcea..d56ca3dd337 100644 --- a/frameworks/ets/ets/application/AbilityDelegator.ets +++ b/frameworks/ets/ets/application/AbilityDelegator.ets @@ -26,7 +26,7 @@ import AbilityStage from '@ohos.app.ability.AbilityStage'; export interface AbilityDelegator { printSync(msg: string): void; - getAppContext(): Context; + getAppContext(): Context | null; finishTest(msg: string, code: long, callback: AsyncCallback): void; -- Gitee