1f6603c60Sopenharmony_ci/* 2f6603c60Sopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 3f6603c60Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4f6603c60Sopenharmony_ci * you may not use this file except in compliance with the License. 5f6603c60Sopenharmony_ci * You may obtain a copy of the License at 6f6603c60Sopenharmony_ci * 7f6603c60Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8f6603c60Sopenharmony_ci * 9f6603c60Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10f6603c60Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11f6603c60Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12f6603c60Sopenharmony_ci * See the License for the specific language governing permissions and 13f6603c60Sopenharmony_ci * limitations under the License. 14f6603c60Sopenharmony_ci */ 15f6603c60Sopenharmony_ci 16f6603c60Sopenharmony_ci 17f6603c60Sopenharmony_ci#ifndef FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H 18f6603c60Sopenharmony_ci#define FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H 19f6603c60Sopenharmony_ci 20f6603c60Sopenharmony_ci#define DEPRECATED __attribute__((__deprecated__)) 21f6603c60Sopenharmony_ci 22f6603c60Sopenharmony_ci#define NAPI_VERSION 8 23f6603c60Sopenharmony_ci 24f6603c60Sopenharmony_ci#define NAPI_RETVAL_NOTHING 25f6603c60Sopenharmony_ci 26f6603c60Sopenharmony_ci#define GET_AND_THROW_LAST_ERROR(env) \ 27f6603c60Sopenharmony_ci do { \ 28f6603c60Sopenharmony_ci const napi_extended_error_info* errorInfo = nullptr; \ 29f6603c60Sopenharmony_ci napi_get_last_error_info((env), &errorInfo); \ 30f6603c60Sopenharmony_ci bool isPending = false; \ 31f6603c60Sopenharmony_ci napi_is_exception_pending((env), &isPending); \ 32f6603c60Sopenharmony_ci if (!isPending && errorInfo != nullptr) { \ 33f6603c60Sopenharmony_ci const char* errorMessage = \ 34f6603c60Sopenharmony_ci errorInfo->error_message != nullptr ? errorInfo->error_message : "empty error message"; \ 35f6603c60Sopenharmony_ci napi_throw_error((env), nullptr, errorMessage); \ 36f6603c60Sopenharmony_ci } \ 37f6603c60Sopenharmony_ci } while (0) 38f6603c60Sopenharmony_ci 39f6603c60Sopenharmony_ci#define NAPI_ASSERT_BASE(env, assertion, message, retVal) \ 40f6603c60Sopenharmony_ci do { \ 41f6603c60Sopenharmony_ci if (!(assertion)) { \ 42f6603c60Sopenharmony_ci napi_throw_error((env), nullptr, "assertion (" #assertion ") failed: " message); \ 43f6603c60Sopenharmony_ci return retVal; \ 44f6603c60Sopenharmony_ci } \ 45f6603c60Sopenharmony_ci } while (0) 46f6603c60Sopenharmony_ci 47f6603c60Sopenharmony_ci#define NAPI_ASSERT(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, nullptr) 48f6603c60Sopenharmony_ci 49f6603c60Sopenharmony_ci#define NAPI_ASSERT_RETURN_VOID(env, assertion, message) NAPI_ASSERT_BASE(env, assertion, message, NAPI_RETVAL_NOTHING) 50f6603c60Sopenharmony_ci 51f6603c60Sopenharmony_ci#define NAPI_CALL_BASE(env, theCall, retVal) \ 52f6603c60Sopenharmony_ci do { \ 53f6603c60Sopenharmony_ci if ((theCall) != napi_ok) { \ 54f6603c60Sopenharmony_ci GET_AND_THROW_LAST_ERROR((env)); \ 55f6603c60Sopenharmony_ci return retVal; \ 56f6603c60Sopenharmony_ci } \ 57f6603c60Sopenharmony_ci } while (0) 58f6603c60Sopenharmony_ci 59f6603c60Sopenharmony_ci#define NAPI_CALL(env, theCall) NAPI_CALL_BASE(env, theCall, nullptr) 60f6603c60Sopenharmony_ci 61f6603c60Sopenharmony_ci#define NAPI_CALL_RETURN_VOID(env, theCall) NAPI_CALL_BASE(env, theCall, NAPI_RETVAL_NOTHING) 62f6603c60Sopenharmony_ci 63f6603c60Sopenharmony_ci#define DECLARE_NAPI_PROPERTY(name, val) \ 64f6603c60Sopenharmony_ci { \ 65f6603c60Sopenharmony_ci (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr \ 66f6603c60Sopenharmony_ci } 67f6603c60Sopenharmony_ci 68f6603c60Sopenharmony_ci#define DECLARE_NAPI_STATIC_PROPERTY(name, val) \ 69f6603c60Sopenharmony_ci { \ 70f6603c60Sopenharmony_ci (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr \ 71f6603c60Sopenharmony_ci } 72f6603c60Sopenharmony_ci 73f6603c60Sopenharmony_ci#define DECLARE_NAPI_FUNCTION(name, func) \ 74f6603c60Sopenharmony_ci { \ 75f6603c60Sopenharmony_ci (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \ 76f6603c60Sopenharmony_ci } 77f6603c60Sopenharmony_ci 78f6603c60Sopenharmony_ci#define DECLARE_NAPI_FUNCTION_WITH_DATA(name, func, data) \ 79f6603c60Sopenharmony_ci { \ 80f6603c60Sopenharmony_ci (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data \ 81f6603c60Sopenharmony_ci } 82f6603c60Sopenharmony_ci 83f6603c60Sopenharmony_ci#define DECLARE_NAPI_STATIC_FUNCTION(name, func) \ 84f6603c60Sopenharmony_ci { \ 85f6603c60Sopenharmony_ci (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr \ 86f6603c60Sopenharmony_ci } 87f6603c60Sopenharmony_ci 88f6603c60Sopenharmony_ci#define DECLARE_NAPI_GETTER(name, getter) \ 89f6603c60Sopenharmony_ci { \ 90f6603c60Sopenharmony_ci (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr \ 91f6603c60Sopenharmony_ci } 92f6603c60Sopenharmony_ci 93f6603c60Sopenharmony_ci#define DECLARE_NAPI_SETTER(name, setter) \ 94f6603c60Sopenharmony_ci { \ 95f6603c60Sopenharmony_ci (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr \ 96f6603c60Sopenharmony_ci } 97f6603c60Sopenharmony_ci 98f6603c60Sopenharmony_ci#define DECLARE_NAPI_GETTER_SETTER(name, getter, setter) \ 99f6603c60Sopenharmony_ci { \ 100f6603c60Sopenharmony_ci (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr \ 101f6603c60Sopenharmony_ci } 102f6603c60Sopenharmony_ci 103f6603c60Sopenharmony_ci#endif /* FOUNDATION_ACE_NAPI_INTERFACES_KITS_NAPI_NATIVE_COMMON_H */