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 LOG_FULL(ERROR) << "occur exception need return"; \ 29 return value; \ 30 } \ 31 } while (false) 32 33 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 34 #define TYPED_ARRAY_ALL(V) \ 35 V(Int8Array) \ 36 V(Uint8Array) \ 37 V(Uint8ClampedArray) \ 38 V(Int16Array) \ 39 V(Uint16Array) \ 40 V(Int32Array) \ 41 V(Uint32Array) \ 42 V(Float32Array) \ 43 V(Float64Array) \ 44 V(BigInt64Array) \ 45 V(BigUint64Array) 46 47 // NOLINTNEXTLINE(cppcoreguidelines-macro-usage) 48 #define EXCEPTION_ERROR_ALL(V) \ 49 V(Error, ERROR) \ 50 V(RangeError, RANGE_ERROR) \ 51 V(SyntaxError, SYNTAX_ERROR) \ 52 V(ReferenceError, REFERENCE_ERROR) \ 53 V(TypeError, TYPE_ERROR) \ 54 V(AggregateError, AGGREGATE_ERROR) \ 55 V(EvalError, EVAL_ERROR) \ 56 V(OOMError, OOM_ERROR) 57 58 namespace panda { 59 using NativeFinalize = void (*)(EcmaVM *); 60 class JSNApiHelper { 61 public: 62 template<typename T> ToLocal(ecmascript::JSHandle<ecmascript::JSTaggedValue> from)63 static inline Local<T> ToLocal(ecmascript::JSHandle<ecmascript::JSTaggedValue> from) 64 { 65 return Local<T>(from.GetAddress()); 66 } 67 ToJSTaggedValue(JSValueRef * from)68 static inline ecmascript::JSTaggedValue ToJSTaggedValue(JSValueRef *from) 69 { 70 ASSERT(from != nullptr); 71 return *reinterpret_cast<ecmascript::JSTaggedValue *>(from); 72 } 73 ToJSHandle(Local<JSValueRef> from)74 static inline ecmascript::JSHandle<ecmascript::JSTaggedValue> ToJSHandle(Local<JSValueRef> from) 75 { 76 ASSERT(!from.IsEmpty()); 77 return ecmascript::JSHandle<ecmascript::JSTaggedValue>(reinterpret_cast<uintptr_t>(*from)); 78 } 79 ToJSHandle(JSValueRef * from)80 static inline ecmascript::JSHandle<ecmascript::JSTaggedValue> ToJSHandle(JSValueRef *from) 81 { 82 ASSERT(from != nullptr); 83 return ecmascript::JSHandle<ecmascript::JSTaggedValue>(reinterpret_cast<uintptr_t>(from)); 84 } 85 }; 86 87 class NativeReferenceHelper { 88 public: NativeReferenceHelper(EcmaVM * vm,Global<ObjectRef> obj,NativeFinalize callback)89 NativeReferenceHelper(EcmaVM *vm, Global<ObjectRef> obj, NativeFinalize callback) 90 : vm_(vm), obj_(obj), callback_(callback) {} ~NativeReferenceHelper()91 ~NativeReferenceHelper() 92 { 93 obj_.FreeGlobalHandleAddr(); 94 } FirstPassCallBack(void * ref)95 static void FirstPassCallBack(void* ref) 96 { 97 auto that = reinterpret_cast<NativeReferenceHelper*>(ref); 98 that->obj_.FreeGlobalHandleAddr(); 99 } 100 SecondPassCallBack(void * ref)101 static void SecondPassCallBack(void* ref) 102 { 103 auto that = reinterpret_cast<NativeReferenceHelper*>(ref); 104 if (that->callback_ != nullptr) { 105 that->callback_(that->vm_); 106 } 107 that->callback_ = nullptr; 108 } 109 SetWeakCallback()110 void SetWeakCallback() 111 { 112 obj_.SetWeakCallback(this, FirstPassCallBack, SecondPassCallBack); 113 } 114 115 private: 116 EcmaVM *vm_; 117 Global<ObjectRef> obj_; 118 NativeFinalize callback_ = nullptr; 119 }; 120 121 class Callback { 122 public: 123 static ecmascript::JSTaggedValue RegisterCallback(ecmascript::EcmaRuntimeCallInfo *ecmaRuntimeCallInfo); 124 }; 125 } // namespace panda 126 #endif // ECMASCRIPT_NAPI_JSNAPI_HELPER_H 127