1 /* 2 * Copyright (c) 2022-2023 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 UTIL_NAPI_H 17 #define UTIL_NAPI_H 18 19 #include "napi/native_api.h" 20 #include "napi/native_node_api.h" 21 #include "utils/log.h" 22 23 namespace OHOS { 24 namespace MMI { 25 #define CHKRV(state, desc) \ 26 do { \ 27 if ((state) != napi_ok) { \ 28 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 29 return; \ 30 } \ 31 } while (0) 32 33 #define CHKRP(state, desc) \ 34 do { \ 35 if ((state) != napi_ok) { \ 36 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 37 return nullptr; \ 38 } \ 39 } while (0) 40 41 #define CHKRF(state, desc) \ 42 do { \ 43 if ((state) != napi_ok) { \ 44 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 45 return false; \ 46 } \ 47 } while (0) 48 49 #define CHKRR(state, desc, ret) \ 50 do { \ 51 if ((state) != napi_ok) { \ 52 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 53 return ret; \ 54 } \ 55 } while (0) 56 57 #define CHECK_RETURN(condition, desc, retVal) \ 58 do { \ 59 if (!(condition)) { \ 60 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 61 return retVal; \ 62 } \ 63 } while (0) 64 65 #define CHKRV_SCOPE(env, state, desc, scope) \ 66 do { \ 67 if ((state) != napi_ok) { \ 68 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 69 napi_close_handle_scope(env, scope); \ 70 return; \ 71 } \ 72 } while (0) 73 74 #define CHKRV_SCOPE_DEL(env, state, desc, scope) \ 75 do { \ 76 if ((state) != napi_ok) { \ 77 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 78 napi_close_handle_scope(env, scope); \ 79 return; \ 80 } \ 81 } while (0) 82 83 #define CHKNRV_SCOPE(env, cond, desc, scope) \ 84 do { \ 85 if ((cond) == nullptr) { \ 86 MMI_HILOGE("%{public}s is nullptr, failed", std::string(desc).c_str()); \ 87 napi_close_handle_scope(env, scope); \ 88 return; \ 89 } \ 90 } while (0) 91 92 #define THROWERR(env, desc) \ 93 do { \ 94 MMI_HILOGE("%{public}s", (#desc)); \ 95 auto infoTemp = std::string(__FUNCTION__)+ ": " + #desc; \ 96 napi_throw_error(env, nullptr, infoTemp.c_str()); \ 97 } while (0) 98 99 namespace UtilNapi { 100 bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 101 } // namespace UtilNapi 102 } // namespace MMI 103 } // namespace OHOS 104 #endif // UTIL_NAPI_H 105