1 /*
2  * Copyright (c) 2024 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 
17 #ifndef FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H
18 #define FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H
19 
20 #define DEPRECATED __attribute__((__deprecated__))
21 
22 #define NAPI_VERSION 8
23 
24 #define NAPI_RETVAL_NOTHING
25 
26 #define GET_AND_THROW_LAST_ERROR(env)                                                                   \
27     do {                                                                                                \
28         const napi_extended_error_info* errorInfo = nullptr;                                            \
29         napi_get_last_error_info((env), &errorInfo);                                                    \
30         bool isPending = false;                                                                         \
31         napi_is_exception_pending((env), &isPending);                                                   \
32         if (!isPending && errorInfo != nullptr) {                                                       \
33             const char* errorMessage =                                                                  \
34                 errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message"; \
35             napi_throw_error((env), nullptr, errorMessage);                                             \
36         }                                                                                               \
37     } while (0)
38 
39 #define NAPI_ASSERT_BASE(env, assertion, message, retVal)                                    \
40     do {                                                                                     \
41         if (!(assertion)) {                                                                  \
42             napi_throw_error((env), nullptr, "assertion (" #assertion ") failed: " message); \
43             return retVal;                                                                   \
44         }                                                                                    \
45     } while (0)
46 
47 #define NAPI_ASSERT(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, nullptr)
48 
49 #define NAPI_ASSERT_RETURN_VOID(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
50 
51 #define NAPI_CALL_BASE(env, theCall, retVal) \
52     do {                                     \
53         if ((theCall) != napi_ok) {          \
54             GET_AND_THROW_LAST_ERROR((env)); \
55             return retVal;                   \
56         }                                    \
57     } while (0)
58 
59 #define NAPI_CALL(env, theCall) NAPI_CALL_BASE(env, theCall, nullptr)
60 
61 #define NAPI_CALL_RETURN_VOID(env, theCall) NAPI_CALL_BASE(env, theCall, NAPI_RETVAL_NOTHING)
62 
63 #define DECLARE_NAPI_PROPERTY(name, val)                                       \
64     {                                                                          \
65         (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr \
66     }
67 
68 #define DECLARE_NAPI_STATIC_PROPERTY(name, val)                               \
69     {                                                                         \
70         (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr \
71     }
72 
73 #define DECLARE_NAPI_FUNCTION(name, func)                                         \
74     {                                                                             \
75         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \
76     }
77 
78 #define DECLARE_NAPI_FUNCTION_WITH_DATA(name, func, data)                         \
79     {                                                                             \
80         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data    \
81     }
82 
83 #define DECLARE_NAPI_STATIC_FUNCTION(name, func)                                 \
84     {                                                                            \
85         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr \
86     }
87 
88 #define DECLARE_NAPI_GETTER(name, getter)                                           \
89     {                                                                               \
90         (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr \
91     }
92 
93 #define DECLARE_NAPI_SETTER(name, setter)                                           \
94     {                                                                               \
95         (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr \
96     }
97 
98 #define DECLARE_NAPI_GETTER_SETTER(name, getter, setter)                             \
99     {                                                                                \
100         (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr \
101     }
102 
103 #endif /* FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H */