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 "fi_log.h"
22
23namespace OHOS {
24namespace Msdp {
25#define RELEASE_CALLBACKINFO(env, ref) \
26    do { \
27        if (ref != nullptr && env != nullptr) { \
28            napi_delete_reference(env, ref); \
29            env = nullptr; \
30        } \
31    } while (0)
32
33#define CHKRV(state, desc) \
34    do { \
35        if ((state) != napi_ok) { \
36            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
37            return; \
38        } \
39    } while (0)
40
41#define CHKRP(state, desc) \
42    do { \
43        if ((state) != napi_ok) { \
44            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
45            return nullptr; \
46        } \
47    } while (0)
48
49#define CHKRF(state, desc) \
50    do { \
51        if ((state) != napi_ok) { \
52            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
53            return false; \
54        } \
55    } while (0)
56
57#define CHKRV_SCOPE(env, state, desc, scope) \
58    do { \
59        if ((state) != napi_ok) { \
60            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
61            napi_close_handle_scope(env, scope); \
62            return; \
63        } \
64    } while (0)
65
66#define CHKRF_SCOPE(env, state, desc, scope) \
67    do { \
68        if ((state) != napi_ok) { \
69            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
70            napi_close_handle_scope(env, scope); \
71            return false; \
72        } \
73    } while (0)
74
75#define CHKRP_SCOPE(env, state, desc, scope) \
76    do { \
77        if ((state) != napi_ok) { \
78            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
79            napi_close_handle_scope(env, scope); \
80            return nullptr; \
81        } \
82    } while (0)
83
84namespace UtilNapi {
85bool TypeOf(napi_env env, napi_value value, napi_valuetype type);
86} // namespace UtilNapi
87} // namespace Msdp
88} // namespace OHOS
89
90#endif // UTIL_NAPI_H