• 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 "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 "plugins/ets/runtime/types/ets_atomic_flag.h"
23 #include "runtime/include/thread_scopes.h"
24 
25 #include "runtime/include/stack_walker.h"
26 #include "runtime/include/thread.h"
27 #include "runtime/interpreter/runtime_interface.h"
28 #include "runtime/handle_scope.h"
29 #include "runtime/handle_scope-inl.h"
30 
31 namespace ark::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 = ark::coretypes::Array::Create(klass, lines.size());
64 
65     VMHandle<coretypes::Array> arrayHandle(coroutine, arr);
66 
67     for (ark::ArraySizeT i = 0; i < (ark::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" void StdCorePrintStackTrace()
77 {
78     ark::PrintStackTrace();
79 }
80 
ResolveLibraryName(const PandaString & name)81 static PandaString ResolveLibraryName(const PandaString &name)
82 {
83 #ifdef PANDA_TARGET_UNIX
84     return PandaString("lib") + name + ".so";
85 #else
86     // Unsupported on windows platform
87     UNREACHABLE();
88 #endif  // PANDA_TARGET_UNIX
89 }
90 
LoadLibrary(ark::ets::EtsString * name)91 extern "C" void LoadLibrary(ark::ets::EtsString *name)
92 {
93     ASSERT(name->AsObject()->IsStringClass());
94 
95     if (name->IsUtf16()) {
96         LOG(FATAL, RUNTIME) << "UTF-16 native library pathes are not supported";
97         return;
98     }
99 
100     auto coroutine = EtsCoroutine::GetCurrent();
101     auto nameStr = name->GetMutf8();
102     if (nameStr.empty()) {
103         ThrowEtsException(coroutine, panda_file_items::class_descriptors::FILE_NOT_FOUND_EXCEPTION,
104                           "The native library path is empty");
105         return;
106     }
107 
108     ScopedNativeCodeThread snct(coroutine);
109 
110     auto env = coroutine->GetEtsNapiEnv();
111     if (!coroutine->GetPandaVM()->LoadNativeLibrary(env, ResolveLibraryName(nameStr))) {
112         ScopedManagedCodeThread smct(coroutine);
113 
114         PandaStringStream ss;
115         ss << "Cannot load native library " << nameStr;
116 
117         ThrowEtsException(coroutine, panda_file_items::class_descriptors::EXCEPTION_IN_INITIALIZER_ERROR, ss.str());
118     }
119 }
120 
StdSystemScheduleCoroutine()121 extern "C" void StdSystemScheduleCoroutine()
122 {
123     auto *cm = static_cast<CoroutineManager *>(Coroutine::GetCurrent()->GetVM()->GetThreadManager());
124     cm->Schedule();
125 }
126 
StdSystemSetCoroutineSchedulingPolicy(int32_t policy)127 extern "C" void StdSystemSetCoroutineSchedulingPolicy(int32_t policy)
128 {
129     constexpr auto POLICIES_MAPPING =
130         std::array {CoroutineSchedulingPolicy::DEFAULT, CoroutineSchedulingPolicy::NON_MAIN_WORKER};
131     ASSERT((policy >= 0) && (static_cast<size_t>(policy) < POLICIES_MAPPING.size()));
132     CoroutineSchedulingPolicy newPolicy = POLICIES_MAPPING[policy];
133 
134     auto *cm = static_cast<CoroutineManager *>(Coroutine::GetCurrent()->GetVM()->GetThreadManager());
135     cm->SetSchedulingPolicy(newPolicy);
136 }
137 
StdSystemGetCoroutineId()138 extern "C" int32_t StdSystemGetCoroutineId()
139 {
140     return EtsCoroutine::GetCurrent()->GetCoroutineId();
141 }
142 
StdSystemAtomicFlagSet(EtsAtomicFlag * instance,EtsBoolean v)143 extern "C" void StdSystemAtomicFlagSet(EtsAtomicFlag *instance, EtsBoolean v)
144 {
145     instance->SetValue(v);
146 }
147 
StdSystemAtomicFlagGet(EtsAtomicFlag * instance)148 extern "C" EtsBoolean StdSystemAtomicFlagGet(EtsAtomicFlag *instance)
149 {
150     return instance->GetValue();
151 }
152 
153 }  // namespace ark::ets::intrinsics
154