• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "ani_util.h"
17 
18 namespace OHOS {
19 namespace HiviewDFX {
ThrowErrorMessage(ani_env * env,const std::string & msg,int32_t errCode)20 void AniUtil::ThrowErrorMessage(ani_env *env, const std::string &msg, int32_t errCode)
21 {
22     ani_module hiDebugModule = nullptr;
23     if (env->FindModule("L@ohos/hidebug;", &hiDebugModule) != ANI_OK) {
24         return;
25     }
26     ani_function createErrorFn = nullptr;
27     if (env->Module_FindFunction(hiDebugModule, "createVoidBusinessError",
28         "DLstd/core/String;:L@ohos/base/BusinessError;", &createErrorFn) != ANI_OK) {
29         return;
30     }
31     ani_double errCodeAni = static_cast<ani_double>(errCode);
32     ani_string msgAni = nullptr;
33     if (ANI_OK != env->String_NewUTF8(msg.c_str(), msg.size(), &msgAni)) {
34         return;
35     }
36     ani_ref errRef = nullptr;
37     if (env->Function_Call_Ref(createErrorFn, &errRef, errCodeAni, msgAni) != ANI_OK) {
38         return;
39     }
40     env->ThrowError(static_cast<ani_error>(errRef));
41 }
42 
CreateUndefined(ani_env * env)43 ani_object AniUtil::CreateUndefined(ani_env *env)
44 {
45     ani_ref undefinedRef = nullptr;
46     if (env->GetUndefined(&undefinedRef) != ANI_OK) {
47         return nullptr;
48     }
49     return static_cast<ani_object>(undefinedRef);
50 }
51 
ToAniBigInt(ani_env * env,uint64_t uint64Val,ani_object & aniBigInt)52 ani_status AniUtil::ToAniBigInt(ani_env *env, uint64_t uint64Val, ani_object &aniBigInt)
53 {
54     ani_class bigIntCls = nullptr;
55     ani_status status = env->FindClass("Lescompat/BigInt;", &bigIntCls);
56     if (status != ANI_OK) {
57         return status;
58     }
59     ani_method ctorMethod = nullptr;
60     status = env->Class_FindMethod(bigIntCls, "<ctor>", "Lstd/core/String;:V", &ctorMethod);
61     if (status != ANI_OK) {
62         return status;
63     }
64     ani_string aniBigIntStr = nullptr;
65     status = env->String_NewUTF8(std::to_string(uint64Val).c_str(), std::to_string(uint64Val).size(), &aniBigIntStr);
66     if (status != ANI_OK) {
67         return status;
68     }
69     return env->Object_New(bigIntCls, ctorMethod, &aniBigInt, aniBigIntStr);
70 }
71 
ParseAniString(ani_env * env,ani_string aniStr,std::string & str)72 ani_status AniUtil::ParseAniString(ani_env *env, ani_string aniStr, std::string &str)
73 {
74     ani_size srcSize = 0;
75     ani_status status = env->String_GetUTF8Size(aniStr, &srcSize);
76     if (status != ANI_OK) {
77         return status;
78     }
79     std::vector<char> buffer(srcSize + 1);
80     ani_size dstSize = 0;
81     status = env->String_GetUTF8SubString(aniStr, 0, srcSize, buffer.data(), buffer.size(), &dstSize);
82     if (status != ANI_OK || srcSize != dstSize) {
83         return status;
84     }
85     str.assign(buffer.data(), dstSize);
86     return ANI_OK;
87 }
88 
ParseAniEnum(ani_env * env,ani_enum_item enumItem,uint32_t & value)89 ani_status AniUtil::ParseAniEnum(ani_env *env, ani_enum_item enumItem, uint32_t &value)
90 {
91     ani_int aniInt = 0;
92     ani_status status = env->EnumItem_GetValue_Int(enumItem, &aniInt);
93     if (status != ANI_OK) {
94         return status;
95     }
96     value = static_cast<uint32_t>(aniInt);
97     return ANI_OK;
98 }
99 
SetNamedPropertyNumber(ani_env * env,ani_object object,const std::string & name,double value)100 ani_status AniUtil::SetNamedPropertyNumber(ani_env *env, ani_object object, const std::string& name, double value)
101 {
102     ani_double aniNumber = static_cast<ani_double>(value);
103     ani_status status = env->Object_SetPropertyByName_Double(object, name.c_str(), aniNumber);
104     return status;
105 }
106 
SetNamedPropertyBigInt(ani_env * env,ani_object object,const std::string & name,uint64_t value)107 ani_status AniUtil::SetNamedPropertyBigInt(ani_env *env, ani_object object, const std::string& name, uint64_t value)
108 {
109     ani_object aniBigInt = nullptr;
110     ani_status status = AniUtil::ToAniBigInt(env, value, aniBigInt);
111     if (status != ANI_OK) {
112         return status;
113     }
114     status = env->Object_SetPropertyByName_Ref(object, name.c_str(), static_cast<ani_ref>(aniBigInt));
115     return status;
116 }
117 } // namespace HiviewDFX
118 } // namespace OHOS
119