• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 <cctype>
17 #include "assembly-type.h"
18 #include "ets_coroutine.h"
19 #include "handle_scope.h"
20 #include "include/mem/panda_containers.h"
21 #include "macros.h"
22 #include "mem/mem.h"
23 #include "mem/vm_handle.h"
24 #include "types/ets_array.h"
25 #include "types/ets_box_primitive.h"
26 #include "types/ets_class.h"
27 #include "types/ets_method.h"
28 #include "types/ets_object.h"
29 #include "types/ets_type.h"
30 #include "types/ets_box_primitive-inl.h"
31 #include "types/ets_type_comptime_traits.h"
32 #include "types/ets_typeapi_create_panda_constants.h"
33 #include "types/ets_typeapi_method.h"
34 #include "runtime/include/value-inl.h"
35 
36 namespace ark::ets::intrinsics {
37 
38 namespace {
39 // NOTE: requires parent handle scope
TypeAPIMethodInvokeImplementation(EtsCoroutine * coro,EtsMethod * meth,EtsObject * recv,EtsArray * args)40 EtsObject *TypeAPIMethodInvokeImplementation(EtsCoroutine *coro, EtsMethod *meth, EtsObject *recv, EtsArray *args)
41 {
42     if (meth->IsAbstract()) {
43         ASSERT(recv != nullptr);
44         meth = recv->GetClass()->ResolveVirtualMethod(meth);
45     }
46     size_t methArgsCount = meth->GetNumArgs();
47     PandaVector<Value> realArgs {methArgsCount};
48     size_t firstRealArg = 0;
49     if (!meth->IsStatic()) {
50         realArgs[0] = Value(recv->GetCoreType());
51         firstRealArg = 1;
52     }
53 
54     size_t argsLength = args->GetLength();
55     if (methArgsCount - firstRealArg != argsLength) {
56         UNREACHABLE();
57     }
58 
59     for (size_t i = 0; i < argsLength; i++) {
60         // issue #14003
61         // SUPPRESS_CSA_NEXTLINE(alpha.core.WasteObjHeader)
62         auto arg = args->GetCoreType()->Get<ObjectHeader *>(i);
63         auto argType = meth->GetArgType(firstRealArg + i);
64         if (argType == EtsType::OBJECT) {
65             realArgs[firstRealArg + i] = Value(arg);
66             continue;
67         }
68         EtsPrimitiveTypeEnumToComptimeConstant(argType, [&](auto type) -> void {
69             using T = EtsTypeEnumToCppType<decltype(type)::value>;
70             realArgs[firstRealArg + i] =
71                 Value(EtsBoxPrimitive<T>::FromCoreType(EtsObject::FromCoreType(arg))->GetValue());
72         });
73     }
74 
75     ASSERT(meth->GetPandaMethod()->GetNumArgs() == realArgs.size());
76     auto res = meth->GetPandaMethod()->Invoke(coro, realArgs.data());
77     if (res.IsReference()) {
78         return EtsObject::FromCoreType(res.GetAs<ObjectHeader *>());
79     }
80 
81     if (meth->GetReturnValueType() == EtsType::VOID) {
82         // NOTE(kprokopenko): remove reinterpret_cast when void is synced with runtime
83         return reinterpret_cast<EtsObject *>(EtsCoroutine::GetCurrent()->GetUndefinedObject());
84     }
85 
86     ASSERT(res.IsPrimitive());
87     return EtsPrimitiveTypeEnumToComptimeConstant(meth->GetReturnValueType(), [&](auto type) -> EtsObject * {
88         using T = EtsTypeEnumToCppType<decltype(type)::value>;
89         return EtsBoxPrimitive<T>::Create(coro, res.GetAs<T>());
90     });
91 }
92 }  // namespace
93 
94 extern "C" {
TypeAPIMethodInvoke(EtsString * desc,EtsObject * recv,EtsArray * args)95 EtsObject *TypeAPIMethodInvoke(EtsString *desc, EtsObject *recv, EtsArray *args)
96 {
97     auto coro = EtsCoroutine::GetCurrent();
98     [[maybe_unused]] HandleScope<ObjectHeader *> scope {coro};
99     VMHandle<EtsObject> recvHandle {coro, recv->GetCoreType()};
100     VMHandle<EtsArray> argsHandle {coro, args->GetCoreType()};
101     // this method shouldn't trigger gc, because class is loaded,
102     // however static analyzer blames this line
103     auto meth = EtsMethod::FromTypeDescriptor(desc->GetMutf8());
104     return TypeAPIMethodInvokeImplementation(coro, meth, recvHandle.GetPtr(), argsHandle.GetPtr());
105 }
106 
TypeAPIMethodInvokeConstructor(EtsString * desc,EtsArray * args)107 EtsObject *TypeAPIMethodInvokeConstructor(EtsString *desc, EtsArray *args)
108 {
109     auto coro = EtsCoroutine::GetCurrent();
110     [[maybe_unused]] HandleScope<ObjectHeader *> scope {coro};
111     VMHandle<EtsArray> argsHandle {coro, args->GetCoreType()};
112     VMHandle<EtsString> descHandle {coro, desc->GetCoreType()};
113 
114     auto meth = EtsMethod::FromTypeDescriptor(desc->GetMutf8());
115     ASSERT(meth->IsConstructor());
116     auto klass = meth->GetClass()->GetRuntimeClass();
117     auto initedObj = ObjectHeader::Create(coro, klass);
118     ASSERT(initedObj->ClassAddr<Class>() == klass);
119     VMHandle<EtsObject> ret {coro, initedObj};
120     TypeAPIMethodInvokeImplementation(coro, meth, ret.GetPtr(), argsHandle.GetPtr());
121     return ret.GetPtr();
122 }
123 }
124 
125 }  // namespace ark::ets::intrinsics
126