• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(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 CHECK_SCOPE_BEFORE_BREAK(env, state, desc, scope, pointerEvent) \
74     do { \
75         if ((state) != napi_ok) { \
76             MMI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
77             (pointerEvent)->MarkProcessed(); \
78             napi_close_handle_scope(env, scope); \
79             break; \
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 
105 #endif // UTIL_NAPI_H