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