• 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_NATIVE_ENGINE_NATIVE_API_INTERNAL_H
17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_API_INTERNAL_H
18 
19 #include "napi/native_api.h"
20 #include "native_engine.h"
21 
napi_clear_last_error(napi_env env)22 static inline napi_status napi_clear_last_error(napi_env env)
23 {
24     ((NativeEngine*)env)->ClearLastError();
25     return napi_ok;
26 }
27 
28 static inline napi_status napi_set_last_error(napi_env env,
29                                               napi_status error_code,
30                                               uint32_t engine_error_code = 0,
31                                               void* engine_reserved = nullptr)
32 {
33     ((NativeEngine*)env)->SetLastError(error_code, engine_error_code, engine_reserved);
34     return error_code;
35 }
36 
37 #define RETURN_STATUS_IF_FALSE(env, condition, status) \
38     if (!(condition)) {                                \
39         return napi_set_last_error((env), (status));   \
40     }
41 
42 #define CHECK_ENV(env)           \
43     if ((env) == nullptr) {      \
44         return napi_invalid_arg; \
45     }
46 
47 #define CHECK_ARG(env, arg) RETURN_STATUS_IF_FALSE((env), ((arg) != nullptr), napi_invalid_arg)
48 
49 #define NAPI_PREAMBLE(env)                                                     \
50     CHECK_ENV((env));                                                          \
51     RETURN_STATUS_IF_FALSE(                                                    \
52         (env),                                                                 \
53         (reinterpret_cast<NativeEngine*>(env))->lastException_.IsEmpty(),      \
54         napi_pending_exception);                                               \
55         napi_clear_last_error((env));                                          \
56     TryCatch tryCatch(env)
57 
58 #define GET_RETURN_STATUS(env)                                                 \
59     (!tryCatch.HasCaught()                                                     \
60         ? napi_ok                                                              \
61         : napi_set_last_error((env), napi_pending_exception))
62 
63 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_NATIVE_API_INTERNAL_H */
64