• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 panda::EcmaVM;
27 using panda::Global;
28 using panda::JSValueRef;
29 using panda::Local;
30 using panda::LocalScope;
31 
32 class ArkNativeReference : public NativeReference {
33 public:
34     ArkNativeReference(ArkNativeEngine* engine,
35                        const EcmaVM* vm,
36                        napi_value value,
37                        uint32_t initialRefcount,
38                        bool deleteSelf = false,
39                        NapiNativeFinalize napiCallback = nullptr,
40                        void* data = nullptr,
41                        void* hint = nullptr);
42     ArkNativeReference(ArkNativeEngine* engine,
43                        const EcmaVM* vm,
44                        Local<JSValueRef> value,
45                        uint32_t initialRefcount,
46                        bool deleteSelf,
47                        NapiNativeFinalize napiCallback,
48                        void* data,
49                        void* hint);
50     ~ArkNativeReference() override;
51 
52     uint32_t Ref() override;
53     uint32_t Unref() override;
54     napi_value Get() override;
55     void* GetData() override;
56     operator napi_value() override;
57     void SetDeleteSelf() override;
58     uint32_t GetRefCount() override;
59     bool GetFinalRun() override;
60     napi_value GetNapiValue() override;
61 
62 private:
ArkNativeReferenceConstructor(uint32_t initialRefCount,bool deleteSelf)63     inline void ArkNativeReferenceConstructor(uint32_t initialRefCount, bool deleteSelf)
64     {
65         if (initialRefCount == 0) {
66             value_.SetWeakCallback(reinterpret_cast<void*>(this), FreeGlobalCallBack, NativeFinalizeCallBack);
67         }
68 
69         if (deleteSelf) {
70             NativeReferenceManager* referenceManager = engine_->GetReferenceManager();
71             if (referenceManager != nullptr) {
72                 referenceManager->CreateHandler(this);
73             }
74         }
75     }
76 
77     ArkNativeEngine* engine_;
78     const EcmaVM* vm_;
79     Global<JSValueRef> value_;
80     uint32_t refCount_ {0};
81     bool deleteSelf_ {false};
82     NapiNativeFinalize napiCallback_ {nullptr};
83     void* data_ = nullptr;
84     void* hint_ = nullptr;
85 
86     bool hasDelete_ {false};
87     bool finalRun_ {false};
88 
89     NativeReference* prev_ {nullptr};
90     NativeReference* next_ {nullptr};
91 
92     void FinalizeCallback();
93 
94     static void FreeGlobalCallBack(void* ref);
95     static void NativeFinalizeCallBack(void* ref);
96     friend class NativeReferenceManager;
97 };
98 
99 #endif /* FOUNDATION_ACE_NAPI_NATIVE_ENGINE_ARK_NATIVE_REFERENCE_H */
100