• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-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_INTEROP_JS_ETS_FIELD_WRAPPER_H_
17 #define PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_FIELD_WRAPPER_H_
18 
19 #include <memory>
20 #include <node_api.h>
21 
22 #include "plugins/ets/runtime/interop_js/ets_proxy/typed_pointer.h"
23 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_method_set.h"
24 #include "plugins/ets/runtime/types/ets_class.h"
25 #include "runtime/include/field.h"
26 
27 namespace ark {
28 
29 namespace ets::interop::js {
30 class InteropCtx;
31 class JSRefConvert;
32 }  // namespace ets::interop::js
33 
34 class Field;
35 }  // namespace ark
36 
37 namespace ark::ets::interop::js::ets_proxy {
38 
39 class EtsClassWrapper;
40 
41 class EtsFieldWrapper {
42 public:
GetOwner()43     EtsClassWrapper *GetOwner() const
44     {
45         return owner_;
46     }
47 
GetField()48     Field *GetField() const
49     {
50         return field_;
51     }
52 
GetGetterSetterMethod(int argc)53     EtsMethodSet *GetGetterSetterMethod(int argc) const
54     {
55         return argc == 0 ? getter_ : setter_;
56     }
57 
SetSetterMethod(EtsMethodSet * setter)58     void SetSetterMethod(EtsMethodSet *setter)
59     {
60         this->setter_ = setter;
61     }
62 
SetGetterMethod(EtsMethodSet * getter)63     void SetGetterMethod(EtsMethodSet *getter)
64     {
65         this->getter_ = getter;
66     }
67 
GetObjOffset()68     uint32_t GetObjOffset() const
69     {
70         return objOffset_;
71     }
72 
73     template <bool ALLOW_INIT>
74     JSRefConvert *GetRefConvert(InteropCtx *ctx);
75 
76     napi_property_descriptor MakeInstanceProperty(EtsClassWrapper *owner, Field *field);
77     napi_property_descriptor MakeStaticProperty(EtsClassWrapper *owner, Field *field);
78 
79     EtsFieldWrapper() = default;
EtsFieldWrapper(EtsClassWrapper * owner)80     explicit EtsFieldWrapper(EtsClassWrapper *owner) : owner_(owner) {};
81     ~EtsFieldWrapper() = default;
82     NO_COPY_SEMANTIC(EtsFieldWrapper);
83     NO_MOVE_SEMANTIC(EtsFieldWrapper);
84 
85 private:
EtsFieldWrapper(EtsClassWrapper * owner,Field * field)86     EtsFieldWrapper(EtsClassWrapper *owner, Field *field) : owner_(owner), field_(field), lazyRefconvertLink_(field)
87     {
88         static_assert(std::is_trivially_destructible_v<EtsFieldWrapper>);
89 
90         if (field->IsStatic()) {
91             objOffset_ = field_->GetOffset() + EtsClass::GetRuntimeClassOffset();
92         } else {
93             objOffset_ = field_->GetOffset();
94         }
95     }
96 
97     EtsClassWrapper *owner_ {};
98     Field *field_ {};
99     EtsMethodSet *getter_ {};
100     EtsMethodSet *setter_ {};
101     TypedPointer<const Field, JSRefConvert> lazyRefconvertLink_ {};
102 
103     uint32_t objOffset_ {};
104 };
105 
106 }  // namespace ark::ets::interop::js::ets_proxy
107 
108 #endif  // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_FIELD_WRAPPER_H_
109