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 #ifndef PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_STDLIB_ANI_HELPERS_H 17 #define PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_STDLIB_ANI_HELPERS_H 18 19 #include <ani.h> 20 #include "libpandabase/macros.h" 21 22 // This functions must be moved from stdlib. #23681 23 namespace ark::ets::stdlib { 24 25 constexpr const char *ERR_CLS_RUNTIME_EXCEPTION = "Lstd/core/RuntimeException;"; 26 constexpr const char *CTOR_SIGNATURE_STR = "Lstd/core/String;:V"; 27 28 void StdlibLogFatal(const char *msg); 29 void StdlibLogFatal(const char *msg, ani_status status); 30 31 ANI_EXPORT std::string StatusToString(ani_status status); 32 33 ANI_EXPORT void ThrowNewError(ani_env *env, ani_class errorClass, std::string_view msg, 34 const char *ctorSignature = nullptr); 35 ANI_EXPORT void ThrowNewError(ani_env *env, std::string_view classDescriptor, std::string_view msg, 36 const char *ctorSignature = nullptr); 37 38 ANI_EXPORT std::string ConvertFromAniString(ani_env *env, ani_string aniStr); 39 40 ANI_EXPORT ani_string CreateUtf8String(ani_env *env, const char *data, ani_size size); 41 ANI_EXPORT ani_string CreateUtf16String(ani_env *env, const uint16_t *data, ani_size size); 42 43 ANI_EXPORT std::string GetFieldStrUndefined(ani_env *env, ani_object obj, const char *name); 44 ANI_EXPORT std::string GetFieldStr(ani_env *env, ani_object obj, const char *name); 45 46 // NOLINTBEGIN(cppcoreguidelines-macro-usage) 47 48 // CC-OFFNXT(G.PRE.02) should be with define 49 #define ANI_FATAL_IF_ERROR(status) \ 50 do { \ 51 ani_status _status = (status); \ 52 if (UNLIKELY(_status != ANI_OK)) { \ 53 ark::ets::stdlib::StdlibLogFatal("ANI_FATAL_IF_ERROR: " #status, _status); \ 54 UNREACHABLE(); \ 55 } \ 56 } while (0) 57 58 // CC-OFFNXT(G.PRE.02) should be with define 59 #define ANI_FATAL_IF(cond) \ 60 do { \ 61 bool _cond = (cond); \ 62 if (UNLIKELY(_cond)) { \ 63 ark::ets::stdlib::StdlibLogFatal("ANI FATAL: " #cond); \ 64 UNREACHABLE(); \ 65 } \ 66 } while (0) 67 68 // NOLINTEND(cppcoreguidelines-macro-usage) 69 70 } // namespace ark::ets::stdlib 71 72 #endif // PANDA_PLUGINS_ETS_STDLIB_NATIVE_CORE_STDLIB_ANI_HELPERS_H 73