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