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 "runtime/runtime_helpers.h"
17 #include "plugins/ets/runtime/ets_coroutine.h"
18 #include "plugins/ets/runtime/ets_exceptions.h"
19 #include "plugins/ets/runtime/ets_panda_file_items.h"
20 #include "plugins/ets/runtime/ets_vm.h"
21 #include "plugins/ets/runtime/types/ets_string.h"
22 #include "runtime/include/thread_scopes.h"
23
24 #include "runtime/include/stack_walker.h"
25 #include "runtime/include/thread.h"
26 #include "runtime/interpreter/runtime_interface.h"
27 #include "runtime/handle_scope.h"
28 #include "runtime/handle_scope-inl.h"
29 #include "plugins/ets/runtime/types/ets_void.h"
30
31 namespace panda::ets::intrinsics {
32
StdCoreStackTraceLines()33 extern "C" EtsArray *StdCoreStackTraceLines()
34 {
35 auto runtime = Runtime::GetCurrent();
36 auto linker = runtime->GetClassLinker();
37 auto ext = linker->GetExtension(panda_file::SourceLang::ETS);
38 auto klass = ext->GetClassRoot(ClassRoot::ARRAY_STRING);
39
40 auto ctx = runtime->GetLanguageContext(panda_file::SourceLang::ETS);
41
42 auto thread = ManagedThread::GetCurrent();
43 auto walker = StackWalker::Create(thread);
44
45 std::vector<std::string> lines;
46
47 for (auto stack = StackWalker::Create(thread); stack.HasFrame(); stack.NextFrame()) {
48 Method *method = stack.GetMethod();
49 auto *source = method->GetClassSourceFile().data;
50 auto lineNum = method->GetLineNumFromBytecodeOffset(stack.GetBytecodePc());
51
52 if (source == nullptr) {
53 source = utf::CStringAsMutf8("<unknown>");
54 }
55
56 std::stringstream ss;
57 ss << method->GetClass()->GetName() << "." << method->GetName().data << " at " << source << ":" << lineNum;
58 lines.push_back(ss.str());
59 }
60
61 auto coroutine = Coroutine::GetCurrent();
62 [[maybe_unused]] HandleScope<ObjectHeader *> scope(coroutine);
63 auto *arr = panda::coretypes::Array::Create(klass, lines.size());
64
65 VMHandle<coretypes::Array> arrayHandle(coroutine, arr);
66
67 for (panda::ArraySizeT i = 0; i < (panda::ArraySizeT)lines.size(); i++) {
68 auto *str = coretypes::String::CreateFromMUtf8(utf::CStringAsMutf8(lines[i].data()), lines[i].length(), ctx,
69 thread->GetVM());
70 arrayHandle.GetPtr()->Set(i, str);
71 }
72
73 return reinterpret_cast<EtsArray *>(arrayHandle.GetPtr());
74 }
75
StdCorePrintStackTrace()76 extern "C" EtsVoid *StdCorePrintStackTrace()
77 {
78 panda::PrintStackTrace();
79 return EtsVoid::GetInstance();
80 }
81
ResolveLibraryName(const PandaString & name)82 static PandaString ResolveLibraryName(const PandaString &name)
83 {
84 #ifdef PANDA_TARGET_UNIX
85 return PandaString("lib") + name + ".so";
86 #else
87 // Unsupported on windows platform
88 UNREACHABLE();
89 #endif // PANDA_TARGET_UNIX
90 }
91
LoadLibrary(panda::ets::EtsString * name)92 extern "C" EtsVoid *LoadLibrary(panda::ets::EtsString *name)
93 {
94 ASSERT(name->AsObject()->IsStringClass());
95
96 if (name->IsUtf16()) {
97 LOG(FATAL, RUNTIME) << "UTF-16 native library pathes are not supported";
98 return EtsVoid::GetInstance();
99 }
100
101 auto coroutine = EtsCoroutine::GetCurrent();
102 auto nameStr = name->GetMutf8();
103 if (nameStr.empty()) {
104 ThrowEtsException(coroutine, panda_file_items::class_descriptors::FILE_NOT_FOUND_EXCEPTION,
105 "The native library path is empty");
106 return EtsVoid::GetInstance();
107 }
108
109 ScopedNativeCodeThread snct(coroutine);
110
111 auto env = coroutine->GetEtsNapiEnv();
112 if (!coroutine->GetPandaVM()->LoadNativeLibrary(env, ResolveLibraryName(nameStr))) {
113 ScopedManagedCodeThread smct(coroutine);
114
115 PandaStringStream ss;
116 ss << "Cannot load native library " << nameStr;
117
118 ThrowEtsException(coroutine, panda_file_items::class_descriptors::EXCEPTION_IN_INITIALIZER_ERROR, ss.str());
119 }
120 return EtsVoid::GetInstance();
121 }
122
StdSystemScheduleCoroutine()123 extern "C" EtsVoid *StdSystemScheduleCoroutine()
124 {
125 auto *cm = static_cast<CoroutineManager *>(Coroutine::GetCurrent()->GetVM()->GetThreadManager());
126 cm->Schedule();
127 return ets::EtsVoid::GetInstance();
128 }
129
130 } // namespace panda::ets::intrinsics
131