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 23namespace OHOS { 24namespace 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 CHECK_SCOPE_BEFORE_BREAK(env, state, desc, scope, pointerEvent) \ 84 do { \ 85 if ((state) != napi_ok) { \ 86 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 87 (pointerEvent)->MarkProcessed(); \ 88 napi_close_handle_scope(env, scope); \ 89 break; \ 90 } \ 91 } while (0) 92 93#define CHKNRV_SCOPE(env, cond, desc, scope) \ 94 do { \ 95 if ((cond) == nullptr) { \ 96 MMI_HILOGE("%{public}s is nullptr, failed", std::string(desc).c_str()); \ 97 napi_close_handle_scope(env, scope); \ 98 return; \ 99 } \ 100 } while (0) 101 102#define THROWERR(env, desc) \ 103 do { \ 104 MMI_HILOGE("%{public}s", (#desc)); \ 105 auto infoTemp = std::string(__FUNCTION__)+ ": " + #desc; \ 106 napi_throw_error(env, nullptr, infoTemp.c_str()); \ 107 } while (0) 108 109namespace UtilNapi { 110bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 111} // namespace UtilNapi 112} // namespace MMI 113} // namespace OHOS 114#endif // UTIL_NAPI_H 115