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 FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H 17 #define FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H 18 19 #include "ark_native_engine.h" 20 #include "ecmascript/napi/include/jsnapi.h" 21 #include "native_engine/native_reference.h" 22 #include "native_engine/native_value.h" 23 24 class ArkNativeEngine; 25 26 using EcmaVM = panda::EcmaVM; 27 using panda::Global; 28 using JSValueRef = panda::JSValueRef; 29 using panda::Local; 30 using LocalScope = panda::LocalScope; 31 32 enum class FinalizerState { 33 DESTRUCTION, 34 COLLECTION, 35 }; 36 37 class ArkNativeReference : public NativeReference { 38 public: 39 ArkNativeReference(ArkNativeEngine* engine, 40 napi_value value, 41 uint32_t initialRefcount, 42 bool deleteSelf = false, 43 NapiNativeFinalize napiCallback = nullptr, 44 void* data = nullptr, 45 void* hint = nullptr, 46 bool isAsyncCall = false, 47 size_t nativeBindingSize = 0); 48 ArkNativeReference(ArkNativeEngine* engine, 49 Local<JSValueRef> value, 50 uint32_t initialRefcount, 51 bool deleteSelf, 52 NapiNativeFinalize napiCallback, 53 void* data, 54 void* hint, 55 bool isAsyncCall = false, 56 size_t nativeBindingSize = 0); 57 ~ArkNativeReference() override; 58 59 uint32_t Ref() override; 60 uint32_t Unref() override; 61 napi_value Get() override; 62 napi_value Get(NativeEngine* engine) override; 63 void* GetData() override; 64 operator napi_value() override; 65 void SetDeleteSelf() override; 66 bool GetDeleteSelf() const override; 67 uint32_t GetRefCount() override; 68 bool GetFinalRun() override; 69 napi_value GetNapiValue() override; 70 void ResetFinalizer() override; 71 72 private: ArkNativeReferenceConstructor(uint32_t initialRefCount,bool deleteSelf)73 inline void ArkNativeReferenceConstructor(uint32_t initialRefCount, bool deleteSelf) 74 { 75 if (initialRefCount == 0) { 76 value_.SetWeakCallback(reinterpret_cast<void*>(this), FreeGlobalCallBack, NativeFinalizeCallBack); 77 } 78 79 if (deleteSelf) { 80 NativeReferenceManager* referenceManager = engine_->GetReferenceManager(); 81 if (referenceManager != nullptr) { 82 referenceManager->CreateHandler(this); 83 } 84 } 85 86 engineId_ = engine_->GetId(); 87 } 88 89 ArkNativeEngine* engine_; 90 uint64_t engineId_ {0}; 91 92 Global<JSValueRef> value_; 93 uint32_t refCount_ {0}; 94 bool deleteSelf_ {false}; 95 bool isAsyncCall_ {false}; 96 97 bool hasDelete_ {false}; 98 bool finalRun_ {false}; 99 NapiNativeFinalize napiCallback_ {nullptr}; 100 void* data_ {nullptr}; 101 void* hint_ {nullptr}; 102 size_t nativeBindingSize_ {0}; 103 104 NativeReference* prev_ {nullptr}; 105 NativeReference* next_ {nullptr}; 106 107 void FinalizeCallback(FinalizerState state); 108 109 static void FreeGlobalCallBack(void* ref); 110 static void NativeFinalizeCallBack(void* ref); 111 friend class NativeReferenceManager; 112 }; 113 114 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H */ 115