• 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 #include "plugins/ets/runtime/interop_js/ets_proxy/ets_method_wrapper.h"
17 
18 #include "plugins/ets/runtime/interop_js/interop_context.h"
19 #include "plugins/ets/runtime/interop_js/js_refconvert.h"
20 #include "plugins/ets/runtime/interop_js/ets_proxy/shared_reference.h"
21 #include "plugins/ets/runtime/interop_js/call/call.h"
22 #include "plugins/ets/runtime/interop_js/code_scopes.h"
23 
24 #include "runtime/mem/vm_handle-inl.h"
25 
26 namespace ark::ets::interop::js::ets_proxy {
27 
28 /*static*/
CreateMethod(EtsMethodSet * etsMethodSet,EtsClassWrapper * owner)29 std::unique_ptr<EtsMethodWrapper> EtsMethodWrapper::CreateMethod(EtsMethodSet *etsMethodSet, EtsClassWrapper *owner)
30 {
31     auto wrapper = new EtsMethodWrapper(etsMethodSet, owner);
32     if (UNLIKELY(nullptr == wrapper)) {
33         return nullptr;
34     }
35     return std::unique_ptr<EtsMethodWrapper>(wrapper);
36 }
37 
38 /*static*/
GetMethod(InteropCtx * ctx,EtsMethodSet * etsMethodSet)39 EtsMethodWrapper *EtsMethodWrapper::GetMethod(InteropCtx *ctx, EtsMethodSet *etsMethodSet)
40 {
41     EtsMethodWrappersCache *cache = ctx->GetEtsMethodWrappersCache();
42     EtsMethodWrapper *wrapper = cache->Lookup(etsMethodSet);
43     if (LIKELY(wrapper != nullptr)) {
44         return wrapper;
45     }
46 
47     auto owner = ctx->GetEtsClassWrappersCache()->Lookup(etsMethodSet->GetEnclosingClass());
48     ASSERT(owner != nullptr);
49 
50     std::unique_ptr<EtsMethodWrapper> etsMethodWrapper = EtsMethodWrapper::CreateMethod(etsMethodSet, owner);
51     return cache->Insert(etsMethodSet, std::move(etsMethodWrapper));
52 }
53 
54 /* static */
MakeNapiIteratorProperty(napi_value & iterator,EtsMethodSet * method,LazyEtsMethodWrapperLink * lazyLink)55 napi_property_descriptor EtsMethodWrapper::MakeNapiIteratorProperty(napi_value &iterator, EtsMethodSet *method,
56                                                                     LazyEtsMethodWrapperLink *lazyLink)
57 {
58     napi_property_descriptor prop = MakeNapiProperty(method, lazyLink);
59     prop.utf8name = nullptr;
60     prop.name = iterator;
61     return prop;
62 }
63 
64 /* static */
MakeNapiProperty(EtsMethodSet * method,LazyEtsMethodWrapperLink * lazyLink)65 napi_property_descriptor EtsMethodWrapper::MakeNapiProperty(EtsMethodSet *method, LazyEtsMethodWrapperLink *lazyLink)
66 {
67     napi_callback callback {};
68     if (method->IsStatic()) {
69         callback = EtsMethodWrapper::EtsMethodCallHandler<true>;
70     } else {
71         callback = EtsMethodWrapper::EtsMethodCallHandler<false>;
72     }
73 
74     napi_property_descriptor prop {};
75     prop.utf8name = method->GetName();
76     prop.method = callback;
77     prop.attributes = method->IsStatic() ? EtsClassWrapper::STATIC_METHOD_ATTR : EtsClassWrapper::METHOD_ATTR;
78     prop.data = lazyLink;
79 
80     return prop;
81 }
82 
AttachGetterSetterToProperty(EtsMethodSet * method,napi_property_descriptor & property)83 void EtsMethodWrapper::AttachGetterSetterToProperty(EtsMethodSet *method, napi_property_descriptor &property)
84 {
85     napi_callback callback {};
86     if (method->IsStatic()) {
87         callback = EtsMethodWrapper::GetterSetterCallHandler<true>;
88     } else {
89         callback = EtsMethodWrapper::GetterSetterCallHandler<false>;
90     }
91     auto fieldWrapper = reinterpret_cast<EtsFieldWrapper *>(property.data);
92     if (method->IsGetter()) {
93         property.getter = callback;
94         fieldWrapper->SetGetterMethod(method);
95     } else {
96         property.setter = callback;
97         fieldWrapper->SetSetterMethod(method);
98     }
99 }
100 
101 template <bool IS_STATIC>
102 /*static*/
GetterSetterCallHandler(napi_env env,napi_callback_info cinfo)103 napi_value EtsMethodWrapper::GetterSetterCallHandler(napi_env env, napi_callback_info cinfo)
104 {
105     FindMethodFunc findMethodFunc = [](void *data, size_t argc) {
106         auto fieldWrapper = reinterpret_cast<EtsFieldWrapper *>(data);
107         if (fieldWrapper == nullptr) {
108             return EtsMethodAndClassWrapper(nullptr, "method didn't bind a EtsFieldWrapper", nullptr);
109         }
110         auto etsMethodSet = fieldWrapper->GetGetterSetterMethod(argc);
111         return EtsMethodAndClassWrapper(etsMethodSet->GetMethod(argc), nullptr, fieldWrapper->GetOwner());
112     };
113 
114     return EtsMethodWrapper::DoEtsMethodCall<IS_STATIC>(env, cinfo, findMethodFunc);
115 }
116 
117 template <bool IS_STATIC>
118 /*static*/
DoEtsMethodCall(napi_env env,napi_callback_info cinfo,FindMethodFunc & findMethodFunc)119 napi_value EtsMethodWrapper::DoEtsMethodCall(napi_env env, napi_callback_info cinfo, FindMethodFunc &findMethodFunc)
120 {
121     EtsCoroutine *coro = EtsCoroutine::GetCurrent();
122     InteropCtx *ctx = InteropCtx::Current(coro);
123 
124     INTEROP_CODE_SCOPE_JS(coro);
125     size_t argc;
126     napi_value jsThis;
127     void *data;
128     NAPI_CHECK_FATAL(napi_get_cb_info(env, cinfo, &argc, nullptr, nullptr, nullptr));
129     auto jsArgs = ctx->GetTempArgs<napi_value>(argc);
130     NAPI_CHECK_FATAL(napi_get_cb_info(env, cinfo, &argc, jsArgs->data(), &jsThis, &data));
131 
132     ScopedManagedCodeThread managedScope(coro);
133     auto [etsMethod, errorMessage, etsClassWrapper] = findMethodFunc(data, argc);
134 
135     ASSERT(nullptr != etsMethod || nullptr != errorMessage || etsClassWrapper != nullptr);
136     if (UNLIKELY(nullptr == etsMethod || nullptr == etsClassWrapper)) {
137         ctx->ThrowJSTypeError(env, errorMessage);
138         return nullptr;
139     }
140 
141     Method *method = etsMethod->GetPandaMethod();
142 
143     if constexpr (IS_STATIC) {
144         EtsClass *etsClass = etsMethod->GetClass();
145         ASSERT(method->GetClass() == etsClass->GetRuntimeClass());
146         if (UNLIKELY(!coro->GetPandaVM()->GetClassLinker()->InitializeClass(coro, etsClass))) {
147             ctx->ForwardEtsException(coro);
148             return nullptr;
149         }
150         return CallETSStatic(coro, ctx, method, *jsArgs);
151     }
152 
153     if (UNLIKELY(IsNullOrUndefined(env, jsThis))) {
154         ctx->ThrowJSTypeError(env, "ets this in instance method cannot be null or undefined");
155         return nullptr;
156     }
157 
158     EtsObject *etsThis = etsClassWrapper->UnwrapEtsProxy(ctx, jsThis);
159     if (UNLIKELY(etsThis == nullptr)) {
160         if (coro->HasPendingException()) {
161             ctx->ForwardEtsException(coro);
162         }
163         ASSERT(ctx->SanityJSExceptionPending());
164         return nullptr;
165     }
166     uint32_t actualArgNum = etsMethod->GetParametersNum();
167     if (actualArgNum > argc && !method->HasVarArgs()) {
168         auto newJsArgs = ctx->GetTempArgs<napi_value>(actualArgNum);
169         std::copy(jsArgs->begin(), jsArgs->end(), newJsArgs->begin());
170         napi_value result;
171         napi_get_undefined(env, &result);
172         std::fill(newJsArgs->begin() + argc, newJsArgs->end(), result);
173         return CallETSInstance(coro, ctx, method, *newJsArgs, etsThis);
174     }
175     return CallETSInstance(coro, ctx, method, *jsArgs, etsThis);
176 }
177 
FindSuitableMethod(const EtsMethodSet * methodSet,uint32_t parametersNum)178 static std::tuple<EtsMethod *, const char *> FindSuitableMethod(const EtsMethodSet *methodSet, uint32_t parametersNum)
179 {
180     if (nullptr == methodSet) {
181         return {nullptr, "no method found"};
182     }
183 
184     EtsMethod *etsMethod = methodSet->GetMethod(parametersNum);
185 
186     // Methods with matched number of arguments might present in base classes
187     while (nullptr == etsMethod && nullptr != methodSet) {
188         methodSet = methodSet->GetBaseMethodSet();
189         if (nullptr != methodSet) {
190             etsMethod = methodSet->GetMethod(parametersNum);
191         }
192     }
193 
194     if (UNLIKELY(nullptr != methodSet && !methodSet->IsValid())) {
195         return {nullptr, "method has unsupported overloads"};
196     }
197 
198     if (UNLIKELY(nullptr == etsMethod)) {
199         return {nullptr, "no suitable method found for this number of arguments"};
200     }
201 
202     return {etsMethod, nullptr};
203 }
204 
205 template <bool IS_STATIC>
206 /*static*/
EtsMethodCallHandler(napi_env env,napi_callback_info cinfo)207 napi_value EtsMethodWrapper::EtsMethodCallHandler(napi_env env, napi_callback_info cinfo)
208 {
209     EtsCoroutine *coro = EtsCoroutine::GetCurrent();
210     InteropCtx *ctx = InteropCtx::Current(coro);
211 
212     FindMethodFunc findMethodFunc = [&](void *data, size_t argc) {
213         auto lazyLink = reinterpret_cast<LazyEtsMethodWrapperLink *>(data);
214         auto methodWrapper = EtsMethodWrapper::ResolveLazyLink(ctx, *lazyLink);
215         if (methodWrapper == nullptr) {
216             return EtsMethodAndClassWrapper(nullptr, "method didn't bind a LazyEtsMethodWrapperLink", nullptr);
217         }
218         auto result = FindSuitableMethod(methodWrapper->etsMethodSet_, argc);
219         auto etsMethod = std::get<0>(result);
220         auto errorMessage = std::get<1>(result);
221         auto classWrapper = methodWrapper->owner_;
222         if (etsMethod == nullptr) {
223             return EtsMethodAndClassWrapper(nullptr, errorMessage, nullptr);
224         }
225         return EtsMethodAndClassWrapper(etsMethod, errorMessage, classWrapper);
226     };
227 
228     return EtsMethodWrapper::DoEtsMethodCall<IS_STATIC>(env, cinfo, findMethodFunc);
229 }
230 
231 // Explicit instantiation
232 template napi_value EtsMethodWrapper::EtsMethodCallHandler<false>(napi_env, napi_callback_info);
233 template napi_value EtsMethodWrapper::EtsMethodCallHandler<true>(napi_env, napi_callback_info);
234 
235 // Explicit instantiation
236 template napi_value EtsMethodWrapper::GetterSetterCallHandler<false>(napi_env, napi_callback_info);
237 template napi_value EtsMethodWrapper::GetterSetterCallHandler<true>(napi_env, napi_callback_info);
238 
239 }  // namespace ark::ets::interop::js::ets_proxy
240