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 #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 { (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr } 63 64 #define DECLARE_NAPI_STATIC_PROPERTY(name, val) \ 65 { (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr } 66 67 #define DECLARE_NAPI_FUNCTION(name, func) \ 68 { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr } 69 70 #define DECLARE_NAPI_FUNCTION_WITH_DATA(name, func, data) \ 71 { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data } 72 73 #define DECLARE_NAPI_STATIC_FUNCTION(name, func) \ 74 { (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr } 75 76 #define DECLARE_NAPI_GETTER(name, getter) \ 77 { (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr } 78 79 #define DECLARE_NAPI_SETTER(name, setter) \ 80 { (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr } 81 82 #define DECLARE_NAPI_GETTER_SETTER(name, getter, setter) \ 83 { (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr } 84 85 #endif /* FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H */ 86 87