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 "plugins/ets/runtime/ets_itable_builder.h"
17 #include "plugins/ets/runtime/ets_language_context.h"
18 #include "plugins/ets/runtime/ets_vtable_builder.h"
19 #include "plugins/ets/runtime/types/ets_method.h"
20 #include "plugins/ets/runtime/types/ets_string.h"
21 #include "plugins/ets/runtime/ets_exceptions.h"
22 #include "plugins/ets/runtime/ets_handle_scope.h"
23 #include "plugins/ets/runtime/ets_handle.h"
24
25 #include <array>
26
27 namespace ark::ets {
28
ThrowException(ManagedThread * thread,const uint8_t * mutf8Name,const uint8_t * mutf8Msg) const29 void EtsLanguageContext::ThrowException(ManagedThread *thread, const uint8_t *mutf8Name, const uint8_t *mutf8Msg) const
30 {
31 EtsCoroutine *coroutine = EtsCoroutine::CastFromThread(thread);
32 ThrowEtsException(coroutine, utf::Mutf8AsCString(mutf8Name), utf::Mutf8AsCString(mutf8Msg));
33 }
34
CreateITableBuilder(ClassLinkerErrorHandler * errHandler) const35 PandaUniquePtr<ITableBuilder> EtsLanguageContext::CreateITableBuilder(ClassLinkerErrorHandler *errHandler) const
36 {
37 return MakePandaUnique<EtsITableBuilder>(errHandler);
38 }
39
CreateVTableBuilder(ClassLinkerErrorHandler * errHandler) const40 PandaUniquePtr<VTableBuilder> EtsLanguageContext::CreateVTableBuilder(ClassLinkerErrorHandler *errHandler) const
41 {
42 return MakePandaUnique<EtsVTableBuilder>(errHandler);
43 }
44
CreateVM(Runtime * runtime,const RuntimeOptions & options) const45 ets::PandaEtsVM *EtsLanguageContext::CreateVM(Runtime *runtime, const RuntimeOptions &options) const
46 {
47 ASSERT(runtime != nullptr);
48
49 auto vm = ets::PandaEtsVM::Create(runtime, options);
50 if (!vm) {
51 LOG(ERROR, RUNTIME) << vm.Error();
52 return nullptr;
53 }
54
55 return vm.Value();
56 }
57
CreateGC(mem::GCType gcType,mem::ObjectAllocatorBase * objectAllocator,const mem::GCSettings & settings) const58 mem::GC *EtsLanguageContext::CreateGC(mem::GCType gcType, mem::ObjectAllocatorBase *objectAllocator,
59 const mem::GCSettings &settings) const
60 {
61 return mem::CreateGC<EtsLanguageConfig>(gcType, objectAllocator, settings);
62 }
63
ThrowStackOverflowException(ManagedThread * thread) const64 void EtsLanguageContext::ThrowStackOverflowException(ManagedThread *thread) const
65 {
66 ASSERT(thread != nullptr);
67 EtsCoroutine *coroutine = EtsCoroutine::CastFromThread(thread);
68 EtsClassLinker *classLinker = coroutine->GetPandaVM()->GetClassLinker();
69 const char *classDescriptor = utf::Mutf8AsCString(GetStackOverflowErrorClassDescriptor());
70 EtsClass *cls = classLinker->GetClass(classDescriptor, true);
71 ASSERT(cls != nullptr);
72
73 EtsHandleScope scope(coroutine);
74 EtsHandle<EtsObject> exc(coroutine, EtsObject::Create(cls));
75 coroutine->SetException(exc.GetPtr()->GetCoreType());
76 }
77
GetVerificationInitAPI() const78 VerificationInitAPI EtsLanguageContext::GetVerificationInitAPI() const
79 {
80 VerificationInitAPI vApi;
81 vApi.primitiveRootsForVerification = {
82 panda_file::Type::TypeId::TAGGED, panda_file::Type::TypeId::VOID, panda_file::Type::TypeId::U1,
83 panda_file::Type::TypeId::U8, panda_file::Type::TypeId::U16, panda_file::Type::TypeId::U32,
84 panda_file::Type::TypeId::U64, panda_file::Type::TypeId::I8, panda_file::Type::TypeId::I16,
85 panda_file::Type::TypeId::I32, panda_file::Type::TypeId::I64, panda_file::Type::TypeId::F32,
86 panda_file::Type::TypeId::F64};
87
88 vApi.arrayElementsForVerification = {reinterpret_cast<const uint8_t *>("[Z"),
89 reinterpret_cast<const uint8_t *>("[B"),
90 reinterpret_cast<const uint8_t *>("[S"),
91 reinterpret_cast<const uint8_t *>("[C"),
92 reinterpret_cast<const uint8_t *>("[I"),
93 reinterpret_cast<const uint8_t *>("[J"),
94 reinterpret_cast<const uint8_t *>("[F"),
95 reinterpret_cast<const uint8_t *>("[D")
96
97 };
98
99 vApi.isNeedClassSyntheticClass = true;
100 vApi.isNeedObjectSyntheticClass = false;
101 vApi.isNeedStringSyntheticClass = false;
102
103 return vApi;
104 }
105
106 } // namespace ark::ets
107