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 #ifndef FOUNDATION_ACE_JSVM_INTERFACES_KITS_JSVM_NATIVE_COMMON_H 16 #define FOUNDATION_ACE_JSVM_INTERFACES_KITS_JSVM_NATIVE_COMMON_H 17 18 #define DEPRECATED __attribute__((__deprecated__)) 19 20 #define JSVM_VERSION 8 21 22 #define JSVM_RETVAL_NOTHING 23 24 #define JSVM_GET_AND_THROW_LAST_ERROR(env) \ 25 do { \ 26 const JSVM_ExtendedErrorInfo* errorInfo = nullptr; \ 27 OH_JSVM_GetLastErrorInfo((env), &errorInfo); \ 28 bool isPending = false; \ 29 OH_JSVM_IsExceptionPending((env), &isPending); \ 30 if (!isPending && errorInfo != nullptr) { \ 31 const char* errorMessage = \ 32 errorInfo->errorMessage != nullptr ? errorInfo->errorMessage : "empty error message"; \ 33 OH_JSVM_ThrowError((env), nullptr, errorMessage); \ 34 } \ 35 } while (0) 36 37 #define JSVM_ASSERT_BASE(env, assertion, message, retVal) \ 38 do { \ 39 if (!(assertion)) { \ 40 OH_JSVM_ThrowError((env), nullptr, "assertion (" #assertion ") failed: " message); \ 41 return retVal; \ 42 } \ 43 } while (0) 44 45 #define JSVM_ASSERT(env, assertion, message) JSVM_ASSERT_BASE(env, assertion, message, nullptr) 46 47 #define JSVM_ASSERT_RETURN_VOID(env, assertion, message) JSVM_ASSERT_BASE(env, assertion, message, JSVM_RETVAL_NOTHING) 48 49 #define JSVM_CALL_BASE(env, theCall, retVal) \ 50 do { \ 51 if ((theCall) != JSVM_OK) { \ 52 JSVM_GET_AND_THROW_LAST_ERROR((env)); \ 53 return retVal; \ 54 } \ 55 } while (0) 56 57 #define JSVM_CALL(env, theCall) JSVM_CALL_BASE(env, theCall, nullptr) 58 59 #define NODE_API_CALL(env, the_call) \ 60 NODE_API_CALL_BASE(env, the_call, NULL) 61 62 #define JSVM_CALL_RETURN_VOID(env, theCall) JSVM_CALL_BASE(env, theCall, JSVM_RETVAL_NOTHING) 63 64 #define DECLARE_JSVM_PROPERTY(name, val) \ 65 { \ 66 (name), nullptr, nullptr, nullptr, nullptr, val, napi_default, nullptr \ 67 } 68 69 #define DECLARE_JSVM_STATIC_PROPERTY(name, val) \ 70 { \ 71 (name), nullptr, nullptr, nullptr, nullptr, val, napi_static, nullptr \ 72 } 73 74 #define DECLARE_JSVM_FUNCTION(name, func) \ 75 { \ 76 (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, nullptr \ 77 } 78 79 #define DECLARE_JSVM_FUNCTION_WITH_DATA(name, func, data) \ 80 { \ 81 (name), nullptr, (func), nullptr, nullptr, nullptr, napi_default, data \ 82 } 83 84 #define DECLARE_JSVM_STATIC_FUNCTION(name, func) \ 85 { \ 86 (name), nullptr, (func), nullptr, nullptr, nullptr, napi_static, nullptr \ 87 } 88 89 #define DECLARE_JSVM_GETTER(name, getter) \ 90 { \ 91 (name), nullptr, nullptr, (getter), nullptr, nullptr, napi_default, nullptr \ 92 } 93 94 #define DECLARE_JSVM_SETTER(name, setter) \ 95 { \ 96 (name), nullptr, nullptr, nullptr, (setter), nullptr, napi_default, nullptr \ 97 } 98 99 #define DECLARE_JSVM_GETTER_SETTER(name, getter, setter) \ 100 { \ 101 (name), nullptr, nullptr, (getter), (setter), nullptr, napi_default, nullptr \ 102 } 103 104 #endif /* FOUNDATION_ACE_JSVM_INTERFACES_KITS_JSVM_NATIVE_COMMON_H */ 105 106