1 /* 2 * Copyright (c) 2022 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 #ifndef UTIL_NAPI_H 16 #define UTIL_NAPI_H 17 18 #include "napi/native_api.h" 19 #include "napi/native_node_api.h" 20 #include "utils/log.h" 21 22 namespace OHOS { 23 namespace MMI { 24 #define CHKRV(env, state, desc) \ 25 do { \ 26 if ((state) != napi_ok) { \ 27 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 28 auto infoTemp = std::string(__FUNCTION__)+ ": " + std::string(desc) + " failed"; \ 29 return; \ 30 } \ 31 } while (0) 32 33 #define CHKRV_SCOPE(env, state, desc, scope) \ 34 do { \ 35 if ((state) != napi_ok) { \ 36 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 37 auto infoTemp = std::string(__FUNCTION__)+ ":" + std::string(desc) + " failed"; \ 38 napi_close_handle_scope(env, scope); \ 39 return; \ 40 } \ 41 } while (0) 42 43 #define CHKRP(env, state, desc) \ 44 do { \ 45 if ((state) != napi_ok) { \ 46 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 47 auto infoTemp = std::string(__FUNCTION__)+ ": " + std::string(desc) + " failed"; \ 48 return nullptr; \ 49 } \ 50 } while (0) 51 52 #define CHKRF(env, state, desc) \ 53 do { \ 54 if ((state) != napi_ok) { \ 55 MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \ 56 auto infoTemp = std::string(__FUNCTION__)+ ": " + std::string(desc) + " failed"; \ 57 return false; \ 58 } \ 59 } while (0) 60 61 #define THROWERR(env, desc) \ 62 do { \ 63 MMI_HILOGE("%{public}s", (#desc)); \ 64 auto infoTemp = std::string(__FUNCTION__)+ ": " + #desc; \ 65 napi_throw_error(env, nullptr, infoTemp.c_str()); \ 66 } while (0) 67 68 namespace UtilNapi { 69 bool TypeOf(napi_env env, napi_value value, napi_valuetype type); 70 } // namespace UtilNapi 71 } // namespace MMI 72 } // namespace OHOS 73 74 #endif // UTIL_NAPI_H