1f857971dSopenharmony_ci/*
2f857971dSopenharmony_ci * Copyright (c) 2022-2023 Huawei Device Co., Ltd.
3f857971dSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4f857971dSopenharmony_ci * you may not use this file except in compliance with the License.
5f857971dSopenharmony_ci * You may obtain a copy of the License at
6f857971dSopenharmony_ci *
7f857971dSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8f857971dSopenharmony_ci *
9f857971dSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10f857971dSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11f857971dSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12f857971dSopenharmony_ci * See the License for the specific language governing permissions and
13f857971dSopenharmony_ci * limitations under the License.
14f857971dSopenharmony_ci */
15f857971dSopenharmony_ci
16f857971dSopenharmony_ci#ifndef UTIL_NAPI_H
17f857971dSopenharmony_ci#define UTIL_NAPI_H
18f857971dSopenharmony_ci
19f857971dSopenharmony_ci#include "napi/native_api.h"
20f857971dSopenharmony_ci#include "napi/native_node_api.h"
21f857971dSopenharmony_ci#include "fi_log.h"
22f857971dSopenharmony_ci
23f857971dSopenharmony_cinamespace OHOS {
24f857971dSopenharmony_cinamespace Msdp {
25f857971dSopenharmony_ci#define RELEASE_CALLBACKINFO(env, ref) \
26f857971dSopenharmony_ci    do { \
27f857971dSopenharmony_ci        if (ref != nullptr && env != nullptr) { \
28f857971dSopenharmony_ci            napi_delete_reference(env, ref); \
29f857971dSopenharmony_ci            env = nullptr; \
30f857971dSopenharmony_ci        } \
31f857971dSopenharmony_ci    } while (0)
32f857971dSopenharmony_ci
33f857971dSopenharmony_ci#define CHKRV(state, desc) \
34f857971dSopenharmony_ci    do { \
35f857971dSopenharmony_ci        if ((state) != napi_ok) { \
36f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
37f857971dSopenharmony_ci            return; \
38f857971dSopenharmony_ci        } \
39f857971dSopenharmony_ci    } while (0)
40f857971dSopenharmony_ci
41f857971dSopenharmony_ci#define CHKRP(state, desc) \
42f857971dSopenharmony_ci    do { \
43f857971dSopenharmony_ci        if ((state) != napi_ok) { \
44f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
45f857971dSopenharmony_ci            return nullptr; \
46f857971dSopenharmony_ci        } \
47f857971dSopenharmony_ci    } while (0)
48f857971dSopenharmony_ci
49f857971dSopenharmony_ci#define CHKRF(state, desc) \
50f857971dSopenharmony_ci    do { \
51f857971dSopenharmony_ci        if ((state) != napi_ok) { \
52f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
53f857971dSopenharmony_ci            return false; \
54f857971dSopenharmony_ci        } \
55f857971dSopenharmony_ci    } while (0)
56f857971dSopenharmony_ci
57f857971dSopenharmony_ci#define CHKRV_SCOPE(env, state, desc, scope) \
58f857971dSopenharmony_ci    do { \
59f857971dSopenharmony_ci        if ((state) != napi_ok) { \
60f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
61f857971dSopenharmony_ci            napi_close_handle_scope(env, scope); \
62f857971dSopenharmony_ci            return; \
63f857971dSopenharmony_ci        } \
64f857971dSopenharmony_ci    } while (0)
65f857971dSopenharmony_ci
66f857971dSopenharmony_ci#define CHKRF_SCOPE(env, state, desc, scope) \
67f857971dSopenharmony_ci    do { \
68f857971dSopenharmony_ci        if ((state) != napi_ok) { \
69f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
70f857971dSopenharmony_ci            napi_close_handle_scope(env, scope); \
71f857971dSopenharmony_ci            return false; \
72f857971dSopenharmony_ci        } \
73f857971dSopenharmony_ci    } while (0)
74f857971dSopenharmony_ci
75f857971dSopenharmony_ci#define CHKRP_SCOPE(env, state, desc, scope) \
76f857971dSopenharmony_ci    do { \
77f857971dSopenharmony_ci        if ((state) != napi_ok) { \
78f857971dSopenharmony_ci            FI_HILOGE("%{public}s failed", std::string(desc).c_str()); \
79f857971dSopenharmony_ci            napi_close_handle_scope(env, scope); \
80f857971dSopenharmony_ci            return nullptr; \
81f857971dSopenharmony_ci        } \
82f857971dSopenharmony_ci    } while (0)
83f857971dSopenharmony_ci
84f857971dSopenharmony_cinamespace UtilNapi {
85f857971dSopenharmony_cibool TypeOf(napi_env env, napi_value value, napi_valuetype type);
86f857971dSopenharmony_ci} // namespace UtilNapi
87f857971dSopenharmony_ci} // namespace Msdp
88f857971dSopenharmony_ci} // namespace OHOS
89f857971dSopenharmony_ci
90f857971dSopenharmony_ci#endif // UTIL_NAPI_H