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 ECMASCRIPT_NAPI_JSNAPI_HELPER_H 17 #define ECMASCRIPT_NAPI_JSNAPI_HELPER_H 18 19 #include "ecmascript/ecma_runtime_call_info.h" 20 #include "ecmascript/js_handle.h" 21 #include "ecmascript/js_tagged_value.h" 22 #include "ecmascript/napi/include/jsnapi.h" 23 24 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 25 #define RETURN_VALUE_IF_ABRUPT(thread, value) \ 26 do { \ 27 if (thread->HasPendingException()) { \ 28 thread->ClearException(); \ 29 return value; \ 30 } \ 31 } while (false) 32 33 #define RETURN_VALUE_IF_ABRUPT_NOT_CLEAR_EXCEPTION(thread, value) \ 34 do { \ 35 if (thread->HasPendingException()) { \ 36 return value; \ 37 } \ 38 } while (false) 39 40 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 41 #define TYPED_ARRAY_ALL(V) \ 42 V(Int8Array) \ 43 V(Uint8Array) \ 44 V(Uint8ClampedArray) \ 45 V(Int16Array) \ 46 V(Uint16Array) \ 47 V(Int32Array) \ 48 V(Uint32Array) \ 49 V(Float32Array) \ 50 V(Float64Array) 51 52 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 53 #define EXCEPTION_ERROR_ALL(V) \ 54 V(Error, ERROR) \ 55 V(RangeError, RANGE_ERROR) \ 56 V(SyntaxError, SYNTAX_ERROR) \ 57 V(ReferenceError, REFERENCE_ERROR) \ 58 V(TypeError, TYPE_ERROR) \ 59 V(EvalError, EVAL_ERROR) 60 61 namespace panda { 62 class JSNApiHelper { 63 public: 64 template<typename T> 65 static inline Local<T> ToLocal(ecmascript::JSHandle<ecmascript::JSTaggedValue> from); 66 67 static inline ecmascript::JSTaggedValue ToJSTaggedValue(JSValueRef *from); 68 69 static inline ecmascript::JSHandle<ecmascript::JSTaggedValue> ToJSHandle(Local<JSValueRef> from); 70 71 static inline ecmascript::JSHandle<ecmascript::JSTaggedValue> ToJSHandle(JSValueRef *from); 72 }; 73 74 class Callback { 75 public: 76 static ecmascript::JSTaggedValue RegisterCallback(ecmascript::EcmaRuntimeCallInfo *info); 77 static ecmascript::JSTaggedValue RegisterCallbackWithProperty(ecmascript::EcmaRuntimeCallInfo *info); 78 static ecmascript::JSTaggedValue RegisterCallbackWithNewTarget(ecmascript::EcmaRuntimeCallInfo *info); 79 }; 80 } // namespace panda 81 #endif // ECMASCRIPT_NAPI_JSNAPI_HELPER_H 82