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 enum class ReferenceOwnerShip : uint8_t { 38 RUNTIME, 39 USER, 40 }; 41 42 struct ArkNativeReferenceConfig { 43 uint32_t initialRefcount; 44 bool deleteSelf; 45 NapiNativeFinalize napiCallback; 46 void* data; 47 48 explicit ArkNativeReferenceConfig(uint32_t initialRefcount, 49 bool deleteSelf = false, 50 NapiNativeFinalize napiCallback = nullptr, 51 void* data = nullptr) initialRefcountArkNativeReferenceConfig52 : initialRefcount(initialRefcount), 53 deleteSelf(deleteSelf), 54 napiCallback(napiCallback), 55 data(data) 56 {} 57 }; 58 59 class ArkNativeReference : public NativeReference { 60 public: 61 ArkNativeReference(ArkNativeEngine* engine, 62 napi_value value, 63 uint32_t initialRefcount, 64 bool deleteSelf = false, 65 NapiNativeFinalize napiCallback = nullptr, 66 void* data = nullptr, 67 void* hint = nullptr, 68 bool isAsyncCall = false, 69 size_t nativeBindingSize = 0); 70 ArkNativeReference(ArkNativeEngine* engine, 71 Local<JSValueRef> value, 72 uint32_t initialRefcount, 73 bool deleteSelf, 74 NapiNativeFinalize napiCallback, 75 void* data, 76 void* hint, 77 bool isAsyncCall = false, 78 size_t nativeBindingSize = 0); 79 ~ArkNativeReference() override; 80 81 uint32_t Ref() override; 82 uint32_t Unref() override; 83 napi_value Get() override; 84 napi_value Get(NativeEngine* engine) override; 85 void* GetData() override; 86 operator napi_value() override; 87 void SetDeleteSelf() override; 88 bool GetDeleteSelf() const override; 89 uint32_t GetRefCount() override; 90 bool GetFinalRun() override; 91 napi_value GetNapiValue() override; 92 void ResetFinalizer() override; 93 #ifdef PANDA_JS_ETS_HYBRID_MODE 94 void MarkFromObject(); 95 bool IsObjectAlive(); 96 bool IsValidHeapObject(); 97 #endif // PANDA_JS_ETS_HYBRID_MODE 98 99 protected: ArkNativeReference(ArkNativeEngine * engine,const ArkNativeReferenceConfig & config)100 ArkNativeReference(ArkNativeEngine* engine, 101 const ArkNativeReferenceConfig &config) 102 : engine_(engine), 103 value_(), 104 refCount_(config.initialRefcount), 105 ownership_(config.deleteSelf ? ReferenceOwnerShip::RUNTIME : ReferenceOwnerShip::USER), 106 napiCallback_(config.napiCallback), 107 data_(config.data) 108 {} 109 110 virtual void ArkNativeReferenceConstructor(); 111 112 enum ReferencePropertiesMask : uint8_t { 113 DELETE_SELF_MASK = 1, 114 IS_ASYNC_CALL_MASK = DELETE_SELF_MASK << 1, 115 HAS_DELETE_MASK = IS_ASYNC_CALL_MASK << 1, 116 FINAL_RAN_MASK = HAS_DELETE_MASK << 1, 117 }; 118 119 void InitProperties(bool deleteSelf = false, bool isAsyncCall = false); 120 121 ArkNativeEngine* engine_; 122 uint64_t engineId_ {0}; 123 124 Global<JSValueRef> value_; 125 uint32_t refCount_ {0}; 126 127 const ReferenceOwnerShip ownership_; 128 // Bit-packed flags: saves memory and speeds up object creation vs. multiple bools. 129 // std::bitset will use more memory than uint8_t number. 130 uint8_t properties_ {0}; 131 132 NapiNativeFinalize napiCallback_ {nullptr}; 133 void* data_ {nullptr}; 134 void* hint_ {nullptr}; 135 size_t nativeBindingSize_ {0}; 136 137 NativeReference* prev_ {nullptr}; 138 NativeReference* next_ {nullptr}; 139 140 bool IsAsyncCall() const; 141 bool HasDelete() const; 142 void SetHasDelete(); 143 void SetFinalRan(); 144 145 void FinalizeCallback(FinalizerState state); 146 void DispatchFinalizeCallback(); 147 void EnqueueAsyncTask(); 148 void EnqueueDeferredTask(); 149 150 void IncreaseCounter(); 151 void DecreaseCounter(); 152 153 static void NativeFinalizeCallBack(void* ref); 154 private: 155 static void FreeGlobalCallBack(void* ref); 156 friend class NativeReferenceManager; 157 }; 158 159 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H */ 160