1 /*
2 * Copyright (c) 2021 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 <cstdlib>
17
18 #include "runtime/include/runtime.h"
19 #include "runtime/include/runtime_options.h"
20 #include "runtime/include/runtime_notification.h"
21 #include "runtime/core/core_vm.h"
22 #include "runtime/include/panda_vm.h"
23 #include "runtime/mem/gc/reference-processor/reference_processor.h"
24
25 namespace panda {
26
27 /* static */
Create(Runtime * runtime,const RuntimeOptions & options,std::string_view runtime_type)28 PandaVM *PandaVM::Create(Runtime *runtime, const RuntimeOptions &options, std::string_view runtime_type)
29 {
30 PandaVM *panda_vm = runtime->GetLanguageContext(std::string(runtime_type)).CreateVM(runtime, options);
31 if (panda_vm == nullptr) {
32 return nullptr;
33 }
34
35 // WORKAROUND(v.cherkashin): EcmaScript doesn't have GC and HeapManager
36 if (runtime_type != "ecmascript") {
37 panda_vm->GetGC()->SetPandaVM(panda_vm);
38 panda_vm->GetHeapManager()->SetPandaVM(panda_vm);
39 }
40 return panda_vm;
41 }
42
InvokeEntrypoint(Method * entrypoint,const std::vector<std::string> & args)43 Expected<int, Runtime::Error> PandaVM::InvokeEntrypoint(Method *entrypoint, const std::vector<std::string> &args)
44 {
45 if (!CheckEntrypointSignature(entrypoint)) {
46 LOG(ERROR, RUNTIME) << "Method '" << entrypoint << "' has invalid signature";
47 return Unexpected(Runtime::Error::INVALID_ENTRY_POINT);
48 }
49 Expected<int, Runtime::Error> ret = InvokeEntrypointImpl(entrypoint, args);
50 ManagedThread *thread = ManagedThread::GetCurrent();
51 if (thread->HasPendingException()) {
52 auto *exception = thread->GetException();
53 HandleUncaughtException(exception);
54 ret = EXIT_FAILURE;
55 }
56
57 return ret;
58 }
59
60 } // namespace panda
61