• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 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 FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H
17 #define FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H
18 
19 #define NAPI_RETVAL_NOTHING
20 
21 #define GET_AND_THROW_LAST_ERROR(env)                                                                   \
22     do {                                                                                                \
23         const napi_extended_error_info* errorInfo = nullptr;                                            \
24         napi_get_last_error_info((env), &errorInfo);                                                    \
25         bool isPending = false;                                                                         \
26         napi_is_exception_pending((env), &isPending);                                                   \
27         if (!isPending && errorInfo != nullptr) {                                                       \
28             const char* errorMessage =                                                                  \
29                 errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message"; \
30             napi_throw_error((env), nullptr, errorMessage);                                             \
31         }                                                                                               \
32     } while (0)
33 
34 #define NAPI_ASSERT_BASE(env, assertion, message, retVal)                                    \
35     do {                                                                                     \
36         if (!(assertion)) {                                                                  \
37             napi_throw_error((env), nullptr, "assertion (" #assertion ") failed: " message); \
38             return retVal;                                                                   \
39         }                                                                                    \
40     } while (0)
41 
42 #define NAPI_ASSERT(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, nullptr)
43 
44 #define NAPI_ASSERT_RETURN_VOID(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING)
45 
46 #define NAPI_CALL_BASE(env, theCall, retVal) \
47     do {                                     \
48         if ((theCall) != napi_ok) {          \
49             GET_AND_THROW_LAST_ERROR((env)); \
50             return retVal;                   \
51         }                                    \
52     } while (0)
53 
54 #define NAPI_CALL(env, theCall) NAPI_CALL_BASE(env, theCall, nullptr)
55 
56 #define NAPI_CALL_RETURN_VOID(env, theCall) NAPI_CALL_BASE(env, theCall, NAPI_RETVAL_NOTHING)
57 
58 #define DECLARE_NAPI_PROPERTY(name, val)                                       \
59     {                                                                          \
60         (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr \
61     }
62 
63 #define DECLARE_NAPI_STATIC_PROPERTY(name, val)                               \
64     {                                                                         \
65         (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr \
66     }
67 
68 #define DECLARE_NAPI_FUNCTION(name, func)                                         \
69     {                                                                             \
70         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \
71     }
72 
73 #define DECLARE_NAPI_DEFAULT_PROPERTY_FUNCTION(name, func)                                   \
74     {                                                                                        \
75         (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default_jsproperty, 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 */
104