• 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 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_field_wrapper.h"
17 
18 #include "libpandafile/file.h"
19 #include "plugins/ets/runtime/ets_class_root.h"
20 #include "plugins/ets/runtime/ets_handle.h"
21 #include "plugins/ets/runtime/ets_handle_scope.h"
22 #include "plugins/ets/runtime/interop_js/js_convert.h"
23 #include "plugins/ets/runtime/interop_js/ets_proxy/shared_reference.h"
24 #include "plugins/ets/runtime/interop_js/code_scopes.h"
25 #include "plugins/ets/runtime/types/ets_object.h"
26 #include "runtime/mem/local_object_handle.h"
27 
28 #include "runtime/mem/vm_handle-inl.h"
29 
30 namespace ark::ets::interop::js::ets_proxy {
31 
32 template <bool IS_STATIC>
EtsAccessorsHandleThis(EtsFieldWrapper * fieldWrapper,EtsCoroutine * coro,InteropCtx * ctx,napi_env env,napi_value jsThis)33 static EtsObject *EtsAccessorsHandleThis(EtsFieldWrapper *fieldWrapper, EtsCoroutine *coro, InteropCtx *ctx,
34                                          napi_env env, napi_value jsThis)
35 {
36     if constexpr (IS_STATIC) {
37         EtsClass *etsClass = fieldWrapper->GetOwner()->GetEtsClass();
38         if (UNLIKELY(!coro->GetPandaVM()->GetClassLinker()->InitializeClass(coro, etsClass))) {
39             ctx->ForwardEtsException(coro);
40             return nullptr;
41         }
42         return etsClass->AsObject();
43     }
44 
45     if (UNLIKELY(IsNullOrUndefined(env, jsThis))) {
46         ctx->ThrowJSTypeError(env, "ets this in set accessor cannot be null or undefined");
47         return nullptr;
48     }
49 
50     EtsObject *etsThis = fieldWrapper->GetOwner()->UnwrapEtsProxy(ctx, jsThis);
51     if (UNLIKELY(etsThis == nullptr)) {
52         if (coro->HasPendingException()) {
53             ctx->ForwardEtsException(coro);
54         }
55         return nullptr;
56     }
57     return etsThis;
58 }
59 
60 template <typename FieldAccessor, bool IS_STATIC>
EtsFieldGetter(napi_env env,napi_callback_info cinfo)61 static napi_value EtsFieldGetter(napi_env env, napi_callback_info cinfo)
62 {
63     size_t argc = 0;
64     napi_value jsThis;
65     void *data;
66     NAPI_CHECK_FATAL(napi_get_cb_info(env, cinfo, &argc, nullptr, &jsThis, &data));
67     if (UNLIKELY(argc != 0)) {
68         InteropCtx::ThrowJSError(env, "getter called in wrong context");
69         return napi_value {};
70     }
71 
72     auto etsFieldWrapper = reinterpret_cast<EtsFieldWrapper *>(data);
73     EtsCoroutine *coro = EtsCoroutine::GetCurrent();
74     InteropCtx *ctx = InteropCtx::Current(coro);
75     INTEROP_CODE_SCOPE_JS(coro, env);
76 
77     EtsObject *etsThis = EtsAccessorsHandleThis<IS_STATIC>(etsFieldWrapper, coro, ctx, env, jsThis);
78     if (UNLIKELY(etsThis == nullptr)) {
79         ASSERT(ctx->SanityJSExceptionPending());
80         return nullptr;
81     }
82 
83     napi_value res = FieldAccessor::Getter(ctx, env, etsThis, etsFieldWrapper);
84     ASSERT(res != nullptr || ctx->SanityJSExceptionPending());
85     return res;
86 }
87 
88 template <typename FieldAccessor, bool IS_STATIC>
EtsFieldSetter(napi_env env,napi_callback_info cinfo)89 static napi_value EtsFieldSetter(napi_env env, napi_callback_info cinfo)
90 {
91     size_t argc = 1;
92     napi_value jsValue;
93     napi_value jsThis;
94     void *data;
95     NAPI_CHECK_FATAL(napi_get_cb_info(env, cinfo, &argc, &jsValue, &jsThis, &data));
96     if (UNLIKELY(argc != 1)) {
97         InteropCtx::ThrowJSError(env, "setter called in wrong context");
98         return napi_value {};
99     }
100 
101     auto etsFieldWrapper = reinterpret_cast<EtsFieldWrapper *>(data);
102     EtsCoroutine *coro = EtsCoroutine::GetCurrent();
103     InteropCtx *ctx = InteropCtx::Current(coro);
104     INTEROP_CODE_SCOPE_JS(coro, env);
105 
106     EtsObject *etsThis = EtsAccessorsHandleThis<IS_STATIC>(etsFieldWrapper, coro, ctx, env, jsThis);
107     if (UNLIKELY(etsThis == nullptr)) {
108         ASSERT(ctx->SanityJSExceptionPending());
109         return nullptr;
110     }
111 
112     LocalObjectHandle<EtsObject> etsThisHandle(coro, etsThis);
113     auto res = FieldAccessor::Setter(ctx, env, EtsHandle<EtsObject>(VMHandle<EtsObject>(etsThisHandle)),
114                                      etsFieldWrapper, jsValue);
115     if (UNLIKELY(!res)) {
116         if (coro->HasPendingException()) {
117             ctx->ForwardEtsException(coro);
118         }
119         ASSERT(ctx->SanityJSExceptionPending());
120     }
121     return nullptr;
122 }
123 
124 struct EtsFieldAccessorREFERENCE {
Getterark::ets::interop::js::ets_proxy::EtsFieldAccessorREFERENCE125     static napi_value Getter(InteropCtx *ctx, napi_env env, EtsObject *etsObject, EtsFieldWrapper *etsFieldWrapper)
126     {
127         EtsObject *etsValue = etsObject->GetFieldObject(etsFieldWrapper->GetObjOffset());
128         if (etsValue == nullptr) {
129             return GetNull(env);
130         }
131         auto refconv = JSRefConvertResolve(ctx, etsValue->GetClass()->GetRuntimeClass());
132         ASSERT(refconv != nullptr);
133         return refconv->Wrap(ctx, etsValue);
134     }
135 
Setterark::ets::interop::js::ets_proxy::EtsFieldAccessorREFERENCE136     static bool Setter(InteropCtx *ctx, napi_env env, EtsHandle<EtsObject> etsObject, EtsFieldWrapper *etsFieldWrapper,
137                        napi_value jsValue)
138     {
139         EtsObject *etsValue;
140         if (IsNull(env, jsValue)) {
141             etsValue = nullptr;
142         } else if (IsUndefined(env, jsValue)) {
143             etsValue = ctx->GetUndefinedObject();
144         } else {
145             JSRefConvert *refconv = etsFieldWrapper->GetRefConvert<true>(ctx);
146             if (UNLIKELY(refconv == nullptr)) {
147                 return false;
148             }
149             etsValue = refconv->Unwrap(ctx, jsValue);
150             if (UNLIKELY(etsValue == nullptr)) {
151                 return false;
152             }
153         }
154         etsObject->SetFieldObject(etsFieldWrapper->GetObjOffset(), etsValue);
155         return true;
156     }
157 };
158 
159 template <typename Convertor>
160 struct EtsFieldAccessorPRIMITIVE {
161     using PrimitiveType = typename Convertor::cpptype;
162 
Getterark::ets::interop::js::ets_proxy::EtsFieldAccessorPRIMITIVE163     static napi_value Getter(InteropCtx * /*ctx*/, napi_env env, EtsObject *etsObject, EtsFieldWrapper *etsFieldWrapper)
164     {
165         auto etsValue = etsObject->GetFieldPrimitive<PrimitiveType>(etsFieldWrapper->GetObjOffset());
166         return Convertor::Wrap(env, etsValue);
167     }
168 
169     // NOTE(vpukhov): elide etsObject handle
Setterark::ets::interop::js::ets_proxy::EtsFieldAccessorPRIMITIVE170     static bool Setter(InteropCtx *ctx, napi_env env, EtsHandle<EtsObject> etsObject, EtsFieldWrapper *etsFieldWrapper,
171                        napi_value jsValue)
172     {
173         std::optional<PrimitiveType> etsValue = Convertor::Unwrap(ctx, env, jsValue);
174         if (LIKELY(etsValue.has_value())) {
175             etsObject->SetFieldPrimitive<PrimitiveType>(etsFieldWrapper->GetObjOffset(), etsValue.value());
176         }
177         return etsValue.has_value();
178     }
179 };
180 
181 template <bool ALLOW_INIT>
GetRefConvert(InteropCtx * ctx)182 JSRefConvert *EtsFieldWrapper::GetRefConvert(InteropCtx *ctx)
183 {
184     if (LIKELY(lazyRefconvertLink_.IsResolved())) {
185         return lazyRefconvertLink_.GetResolved();
186     }
187 
188     const Field *field = lazyRefconvertLink_.GetUnresolved();
189     ASSERT(field->GetTypeId() == panda_file::Type::TypeId::REFERENCE);
190 
191     const auto *pandaFile = field->GetPandaFile();
192     auto *classLinker = Runtime::GetCurrent()->GetClassLinker();
193     Class *fieldClass =
194         classLinker->GetClass(*pandaFile, panda_file::FieldDataAccessor::GetTypeId(*pandaFile, field->GetFileId()),
195                               ctx->LinkerCtx(), nullptr);
196 
197     JSRefConvert *refconv = JSRefConvertResolve<ALLOW_INIT>(ctx, fieldClass);
198     if (UNLIKELY(refconv == nullptr)) {
199         return nullptr;
200     }
201     lazyRefconvertLink_.Set(refconv);  // Update link
202     return refconv;
203 }
204 
205 // Explicit instantiation
206 template JSRefConvert *EtsFieldWrapper::GetRefConvert<false>(InteropCtx *ctx);
207 template JSRefConvert *EtsFieldWrapper::GetRefConvert<true>(InteropCtx *ctx);
208 
209 template <bool IS_STATIC>
DoMakeNapiProperty(EtsFieldWrapper * wrapper)210 static napi_property_descriptor DoMakeNapiProperty(EtsFieldWrapper *wrapper)
211 {
212     Field *field = wrapper->GetField();
213     napi_property_descriptor prop {};
214     prop.utf8name = utf::Mutf8AsCString(field->GetName().data);
215     prop.attributes = IS_STATIC ? EtsClassWrapper::STATIC_FIELD_ATTR : EtsClassWrapper::FIELD_ATTR;
216     prop.data = wrapper;
217 
218     // NOTE(vpukhov): apply the same rule to instance fields?
219     ASSERT(!IS_STATIC || wrapper->GetOwner()->GetEtsClass()->GetRuntimeClass() == field->GetClass());
220 
221     auto setupAccessors = [&prop](auto accessorTag) {
222         using Accessor = typename decltype(accessorTag)::type;
223         prop.getter = EtsFieldGetter<Accessor, IS_STATIC>;
224         prop.setter = EtsFieldSetter<Accessor, IS_STATIC>;
225         return prop;
226     };
227 
228     panda_file::Type type = field->GetType();
229     switch (type.GetId()) {
230         case panda_file::Type::TypeId::U1:
231             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertU1>>());
232         case panda_file::Type::TypeId::I8:
233             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertI8>>());
234         case panda_file::Type::TypeId::U8:
235             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertU8>>());
236         case panda_file::Type::TypeId::I16:
237             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertI16>>());
238         case panda_file::Type::TypeId::U16:
239             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertU16>>());
240         case panda_file::Type::TypeId::I32:
241             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertI32>>());
242         case panda_file::Type::TypeId::U32:
243             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertU32>>());
244         case panda_file::Type::TypeId::I64:
245             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertI64>>());
246         case panda_file::Type::TypeId::U64:
247             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertU64>>());
248         case panda_file::Type::TypeId::F32:
249             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertF32>>());
250         case panda_file::Type::TypeId::F64:
251             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorPRIMITIVE<JSConvertF64>>());
252         case panda_file::Type::TypeId::REFERENCE:
253             return setupAccessors(helpers::TypeIdentity<EtsFieldAccessorREFERENCE>());
254         default:
255             InteropCtx::Fatal(std::string("DoMakeNapiProperty: unsupported typeid ") +
256                               panda_file::Type::GetSignatureByTypeId(type));
257     }
258     UNREACHABLE();
259 }
260 
MakeInstanceProperty(EtsClassWrapper * owner,Field * field)261 napi_property_descriptor EtsFieldWrapper::MakeInstanceProperty(EtsClassWrapper *owner, Field *field)
262 {
263     new (this) EtsFieldWrapper(owner, field);
264     /*IS_STATIC=false*/
265     return DoMakeNapiProperty<false>(this);
266 }
267 
MakeStaticProperty(EtsClassWrapper * owner,Field * field)268 napi_property_descriptor EtsFieldWrapper::MakeStaticProperty(EtsClassWrapper *owner, Field *field)
269 {
270     new (this) EtsFieldWrapper(owner, field);
271     /*IS_STATIC=true*/
272     return DoMakeNapiProperty<true>(this);
273 }
274 
275 }  // namespace ark::ets::interop::js::ets_proxy
276