1 /*
2 * Copyright (c) 2025 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bridge/arkts_frontend/arkts_ani_utils.h"
17
18 #include <ani.h>
19 #include <sstream>
20 #include <iostream>
21
22 #include "base/log/log.h"
23
24 namespace OHOS::Ace {
CreateAniBoolean(ani_env * env,bool value,ani_object & result)25 int32_t ArktsAniUtils::CreateAniBoolean(ani_env* env, bool value, ani_object& result)
26 {
27 ani_status state;
28 ani_class booleanClass;
29 if ((state = env->FindClass("Lstd/core/Boolean;", &booleanClass)) != ANI_OK) {
30 LOGE("FindClass std/core/Boolean failed, %{public}d", state);
31 return static_cast<int32_t>(state);
32 }
33 ani_method booleanClassCtor;
34 if ((state = env->Class_FindMethod(booleanClass, "<ctor>", "Z:V", &booleanClassCtor)) != ANI_OK) {
35 LOGE("Class_FindMethod Boolean ctor failed, %{public}d", state);
36 return static_cast<int32_t>(state);
37 }
38 ani_boolean aniValue = value;
39 if ((state = env->Object_New(booleanClass, booleanClassCtor, &result, aniValue)) != ANI_OK) {
40 LOGE("New Boolean object failed, %{public}d", state);
41 return static_cast<int32_t>(state);
42 }
43 return static_cast<int32_t>(state);
44 }
45
GetNearestNonBootRuntimeLinker(ani_env * env,ani_ref & result)46 int32_t ArktsAniUtils::GetNearestNonBootRuntimeLinker(ani_env* env, ani_ref& result)
47 {
48 ani_module stdCoreModule;
49 auto state = env->FindModule("Lstd/core;", &stdCoreModule);
50 if (state != ANI_OK) {
51 LOGE("Find module core failed, %{public}d", state);
52 return static_cast<int32_t>(state);
53 }
54 ani_function fn;
55 state = env->Module_FindFunction(stdCoreModule, "getNearestNonBootRuntimeLinker", ":Lstd/core/RuntimeLinker;", &fn);
56 if (state != ANI_OK) {
57 LOGE("Find function getNearestNonBootRuntimeLinker failed, %{public}d", state);
58 return static_cast<int32_t>(state);
59 }
60 state = env->Function_Call_Ref(fn, &result);
61 if (state != ANI_OK) {
62 LOGE("Call getNearestNonBootRuntimeLinker failed, %{public}d", state);
63 return static_cast<int32_t>(state);
64 }
65 ani_boolean isUndef = ANI_FALSE;
66 state = env->Reference_IsUndefined(result, &isUndef);
67 if (isUndef == ANI_TRUE) {
68 LOGE("Result of getNearestNonBootRuntimeLinker is undefined, %{public}d", state);
69 return static_cast<int32_t>(state);
70 }
71 return static_cast<int32_t>(state);
72 }
73
ClearAniPendingError(ani_env * env)74 void ArktsAniUtils::ClearAniPendingError(ani_env* env)
75 {
76 ani_boolean result = ANI_FALSE;
77 env->ExistUnhandledError(&result);
78 if (result != ANI_FALSE) {
79 std::ostringstream oss;
80 auto rdbufBak = std::cerr.rdbuf(oss.rdbuf());
81 env->DescribeError();
82 std::cerr.rdbuf(rdbufBak);
83 LOGE("ani pending error: %{public}s", oss.str().c_str());
84 env->ResetError();
85 }
86 }
87
GetAniEnv(ani_vm * vm)88 ani_env* ArktsAniUtils::GetAniEnv(ani_vm* vm)
89 {
90 if (!vm) {
91 LOGW("GetAniEnv from null vm");
92 return nullptr;
93 }
94 ani_env* env = nullptr;
95 ani_status status;
96 if ((status = vm->GetEnv(ANI_VERSION_1, &env)) != ANI_OK) {
97 LOGW("GetAniEnv from vm failed, status: %{public}d", status);
98 return nullptr;
99 }
100 return env;
101 }
102 } // namespace OHOS::Ace
103