• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "types/ets_void.h"
35 #include "runtime/include/value-inl.h"
36 
37 namespace panda::ets::intrinsics {
38 
39 namespace {
40 // NOTE: requires parent handle scope
TypeAPIMethodInvokeImplementation(EtsCoroutine * coro,EtsMethod * meth,EtsObject * recv,EtsArray * args)41 EtsObject *TypeAPIMethodInvokeImplementation(EtsCoroutine *coro, EtsMethod *meth, EtsObject *recv, EtsArray *args)
42 {
43     if (meth->IsAbstract()) {
44         ASSERT(recv != nullptr);
45         meth = recv->GetClass()->ResolveVirtualMethod(meth);
46     }
47     size_t methArgsCount = meth->GetNumArgs();
48     PandaVector<Value> realArgs {methArgsCount};
49     size_t firstRealArg = 0;
50     if (!meth->IsStatic()) {
51         realArgs[0] = Value(recv->GetCoreType());
52         firstRealArg = 1;
53     }
54 
55     size_t argsLength = args->GetLength();
56     if (methArgsCount - firstRealArg != argsLength) {
57         UNREACHABLE();
58     }
59 
60     for (size_t i = 0; i < argsLength; i++) {
61         // issue #14003
62         // SUPPRESS_CSA_NEXTLINE(alpha.core.WasteObjHeader)
63         auto arg = args->GetCoreType()->Get<ObjectHeader *>(i);
64         auto argType = meth->GetArgType(firstRealArg + i);
65         if (argType == EtsType::OBJECT) {
66             realArgs[firstRealArg + i] = Value(arg);
67             continue;
68         }
69         EtsPrimitiveTypeEnumToComptimeConstant(argType, [&](auto type) -> void {
70             using T = EtsTypeEnumToCppType<decltype(type)::value>;
71             realArgs[firstRealArg + i] =
72                 Value(EtsBoxPrimitive<T>::FromCoreType(EtsObject::FromCoreType(arg))->GetValue());
73         });
74     }
75 
76     ASSERT(meth->GetPandaMethod()->GetNumArgs() == realArgs.size());
77     auto res = meth->GetPandaMethod()->Invoke(coro, realArgs.data());
78     if (res.IsReference()) {
79         return EtsObject::FromCoreType(res.GetAs<ObjectHeader *>());
80     }
81     if (meth->GetReturnValueType() == EtsType::VOID) {
82         // NOTE(kprokopenko): remove reinterpret_cast when void is synced with runtime
83         return reinterpret_cast<EtsObject *>(EtsVoid::GetInstance());
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 panda::ets::intrinsics
126