• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2023-2024 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/types/ets_class.h"
24 #include "runtime/include/field.h"
25 
26 namespace ark {
27 
28 namespace ets::interop::js {
29 class InteropCtx;
30 class JSRefConvert;
31 }  // namespace ets::interop::js
32 
33 class Field;
34 }  // namespace ark
35 
36 namespace ark::ets::interop::js::ets_proxy {
37 
38 class EtsClassWrapper;
39 
40 class EtsFieldWrapper {
41 public:
GetOwner()42     EtsClassWrapper *GetOwner() const
43     {
44         return owner_;
45     }
46 
GetField()47     Field *GetField() const
48     {
49         return field_;
50     }
51 
GetObjOffset()52     uint32_t GetObjOffset() const
53     {
54         return objOffset_;
55     }
56 
57     template <bool ALLOW_INIT>
58     JSRefConvert *GetRefConvert(InteropCtx *ctx);
59 
60     napi_property_descriptor MakeInstanceProperty(EtsClassWrapper *owner, Field *field);
61     napi_property_descriptor MakeStaticProperty(EtsClassWrapper *owner, Field *field);
62 
63     EtsFieldWrapper() = default;
64     ~EtsFieldWrapper() = default;
65     NO_COPY_SEMANTIC(EtsFieldWrapper);
66     NO_MOVE_SEMANTIC(EtsFieldWrapper);
67 
68 private:
EtsFieldWrapper(EtsClassWrapper * owner,Field * field)69     EtsFieldWrapper(EtsClassWrapper *owner, Field *field) : owner_(owner), field_(field), lazyRefconvertLink_(field)
70     {
71         static_assert(std::is_trivially_destructible_v<EtsFieldWrapper>);
72 
73         if (field->IsStatic()) {
74             objOffset_ = field_->GetOffset() + EtsClass::GetRuntimeClassOffset();
75         } else {
76             objOffset_ = field_->GetOffset();
77         }
78     }
79 
80     EtsClassWrapper *owner_ {};
81     Field *field_ {};
82     TypedPointer<const Field, JSRefConvert> lazyRefconvertLink_ {};
83 
84     uint32_t objOffset_ {};
85 };
86 
87 }  // namespace ark::ets::interop::js::ets_proxy
88 
89 #endif  // !PANDA_PLUGINS_ETS_RUNTIME_INTEROP_JS_ETS_FIELD_WRAPPER_H_
90