• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022-2025 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 PANDA_PLUGINS_ETS_RUNTIME_MEM_ETS_REFERENCE_H
17 #define PANDA_PLUGINS_ETS_RUNTIME_MEM_ETS_REFERENCE_H
18 
19 #include "libpandabase/macros.h"
20 #include "runtime/mem/refstorage/reference.h"
21 #include "runtime/mem/refstorage/reference_storage.h"
22 #include "plugins/ets/runtime/napi/ets_napi.h"
23 #include "plugins/ets/runtime/types/ets_class.h"
24 #include "plugins/ets/runtime/types/ets_object.h"
25 
26 namespace ark::ets {
27 class EtsReference final {
28 public:
29     DEFAULT_COPY_SEMANTIC(EtsReference);
30     DEFAULT_MOVE_SEMANTIC(EtsReference);
31     EtsReference() = default;
32     ~EtsReference() = default;
33 
34     using EtsObjectType = mem::Reference::ObjectType;
35 
IsStack()36     bool IsStack() const
37     {
38         return GetReference()->IsStack();
39     }
40 
IsLocal()41     bool IsLocal() const
42     {
43         return GetReference()->IsLocal();
44     }
45 
IsGlobal()46     bool IsGlobal() const
47     {
48         return GetReference()->IsGlobal();
49     }
50 
IsWeak()51     bool IsWeak() const
52     {
53         return GetReference()->IsWeak();
54     }
55 
CastToReference(EtsReference * etsRef)56     static mem::Reference *CastToReference(EtsReference *etsRef)
57     {
58         return reinterpret_cast<mem::Reference *>(etsRef);
59     }
60 
CastFromReference(mem::Reference * ref)61     static EtsReference *CastFromReference(mem::Reference *ref)
62     {
63         return reinterpret_cast<EtsReference *>(ref);
64     }
65 
66 private:
GetReference()67     const mem::Reference *GetReference() const
68     {
69         return reinterpret_cast<const mem::Reference *>(this);
70     }
71 };
72 
EtsObjectToEtsRef(ets_object obj)73 [[maybe_unused]] static inline EtsReference *EtsObjectToEtsRef(ets_object obj)
74 {
75     return reinterpret_cast<EtsReference *>(obj);
76 }
77 
EtsRefToEtsObject(EtsReference * ref)78 [[maybe_unused]] static inline ets_object EtsRefToEtsObject(EtsReference *ref)
79 {
80     return reinterpret_cast<ets_object>(ref);
81 }
82 
EtsNapiWeakToEtsRef(ets_object obj)83 [[maybe_unused]] static inline EtsReference *EtsNapiWeakToEtsRef(ets_object obj)
84 {
85     auto ref = reinterpret_cast<EtsReference *>(obj);
86     ASSERT(ref->IsWeak());
87     return ref;
88 }
89 
EtsRefToEtsNapiWeak(EtsReference * ref)90 [[maybe_unused]] static inline ets_object EtsRefToEtsNapiWeak(EtsReference *ref)
91 {
92     ASSERT(ref->IsWeak());
93     return reinterpret_cast<ets_object>(ref);
94 }
95 
96 class EtsReferenceStorage final : private mem::ReferenceStorage {
97 public:
EtsReferenceStorage(mem::GlobalObjectStorage * globalStorage,mem::InternalAllocatorPtr allocator,bool refCheckValidate)98     EtsReferenceStorage(mem::GlobalObjectStorage *globalStorage, mem::InternalAllocatorPtr allocator,
99                         bool refCheckValidate)
100         : mem::ReferenceStorage(globalStorage, allocator, refCheckValidate)
101     {
102     }
103     ~EtsReferenceStorage() = default;
104 
105     NO_COPY_SEMANTIC(EtsReferenceStorage);
106     NO_MOVE_SEMANTIC(EtsReferenceStorage);
107 
NewEtsStackRef(EtsObject ** obj)108     static EtsReference *NewEtsStackRef(EtsObject **obj)
109     {
110         mem::Reference *ref = mem::ReferenceStorage::NewStackRef(reinterpret_cast<ObjectHeader **>(obj));
111         return EtsReference::CastFromReference(ref);
112     }
113 
NewEtsRef(EtsObject * obj,EtsReference::EtsObjectType objType)114     EtsReference *NewEtsRef(EtsObject *obj, EtsReference::EtsObjectType objType)
115     {
116         ASSERT(obj != nullptr);
117         mem::Reference *ref = NewRef(obj->GetCoreType(), objType);
118         return EtsReference::CastFromReference(ref);
119     }
120 
RemoveEtsRef(EtsReference * etsRef)121     void RemoveEtsRef(EtsReference *etsRef)
122     {
123         RemoveRef(EtsReference::CastToReference(etsRef));
124     }
125 
GetEtsObject(EtsReference * etsRef)126     [[nodiscard]] EtsObject *GetEtsObject(EtsReference *etsRef)
127     {
128         return EtsObject::FromCoreType(GetObject(EtsReference::CastToReference(etsRef)));
129     }
130 
IsValidEtsRef(EtsReference * etsRef)131     [[nodiscard]] bool IsValidEtsRef(EtsReference *etsRef)
132     {
133         return IsValidRef(EtsReference::CastToReference(etsRef));
134     }
135 
PushLocalEtsFrame(uint32_t capacity)136     bool PushLocalEtsFrame(uint32_t capacity)
137     {
138         return PushLocalFrame(capacity);
139     }
140 
PopLocalEtsFrame(EtsReference * result)141     EtsReference *PopLocalEtsFrame(EtsReference *result)
142     {
143         return EtsReference::CastFromReference(PopLocalFrame(EtsReference::CastToReference(result)));
144     }
145 
EnsureLocalEtsCapacity(uint32_t capacity)146     bool EnsureLocalEtsCapacity(uint32_t capacity)
147     {
148         return EnsureLocalCapacity(capacity);
149     }
150 
GetAsReferenceStorage()151     mem::ReferenceStorage *GetAsReferenceStorage()
152     {
153         return this;
154     }
155 };
156 }  // namespace ark::ets
157 
158 #endif  // PANDA_PLUGINS_ETS_RUNTIME_MEM_ETS_REFERENCE_H
159