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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18
19 #include <algorithm>
20 #include <cmath>
21 #include <cstdint>
22 #include <functional>
23 #include <limits>
24 #include <memory>
25 #include <sstream>
26 #include <type_traits>
27 #include <vector>
28
29 #include "assembly-parser.h"
30 #include "libpandabase/mem/pool_manager.h"
31 #include "libpandabase/utils/utf.h"
32 #include "libpandafile/bytecode_emitter.h"
33 #include "libpandafile/file.h"
34 #include "libpandafile/file_items.h"
35 #include "libpandafile/value.h"
36 #include "runtime/bridge/bridge.h"
37 #include "runtime/include/class_linker.h"
38 #include "runtime/include/compiler_interface.h"
39 #include "runtime/include/mem/allocator.h"
40 #include "runtime/include/method.h"
41 #include "runtime/include/runtime.h"
42 #include "runtime/include/runtime_options.h"
43 #include "runtime/interpreter/frame.h"
44 #include "runtime/mem/gc/gc.h"
45 #include "runtime/mem/internal_allocator.h"
46 #include "runtime/core/core_class_linker_extension.h"
47 #include "runtime/tests/class_linker_test_extension.h"
48 #include "runtime/tests/interpreter/test_interpreter.h"
49 #include "runtime/tests/interpreter/test_runtime_interface.h"
50 #include "runtime/include/coretypes/dyn_objects.h"
51 #include "runtime/include/hclass.h"
52 #include "runtime/handle_base-inl.h"
53 #include "runtime/handle_scope-inl.h"
54 #include "runtime/include/coretypes/native_pointer.h"
55
56 namespace ark::interpreter::test {
57
58 using DynClass = ark::coretypes::DynClass;
59 using DynObject = ark::coretypes::DynObject;
60
61 class InterpreterTestResolveCtorClass : public testing::Test {
62 public:
InterpreterTestResolveCtorClass()63 InterpreterTestResolveCtorClass()
64 {
65 RuntimeOptions options;
66 options.SetShouldLoadBootPandaFiles(false);
67 options.SetShouldInitializeIntrinsics(false);
68 options.SetRunGcInPlace(true);
69 options.SetVerifyCallStack(false);
70 options.SetGcType("epsilon");
71 Runtime::Create(options);
72 thread_ = ark::MTManagedThread::GetCurrent();
73 thread_->ManagedCodeBegin();
74 }
75
~InterpreterTestResolveCtorClass()76 ~InterpreterTestResolveCtorClass() override
77 {
78 thread_->ManagedCodeEnd();
79 Runtime::Destroy();
80 }
81
82 NO_COPY_SEMANTIC(InterpreterTestResolveCtorClass);
83 NO_MOVE_SEMANTIC(InterpreterTestResolveCtorClass);
84
85 private:
86 ark::MTManagedThread *thread_;
87 };
88
TEST_F(InterpreterTestResolveCtorClass,ResolveCtorClass)89 TEST_F(InterpreterTestResolveCtorClass, ResolveCtorClass)
90 {
91 auto pf = ark::panda_file::File::Open("../bin-gtests/pre-build/interpreter_test_resolve_ctor_class.abc");
92 ASSERT_NE(pf, nullptr);
93
94 ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
95 classLinker->AddPandaFile(std::move(pf));
96
97 PandaString descriptor;
98
99 auto *ext = classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
100 Class *klass = ext->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R2"), &descriptor));
101 ASSERT_NE(klass, nullptr);
102
103 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("foo"));
104 ASSERT_NE(method, nullptr);
105
106 std::vector<Value> args;
107 Value v = method->Invoke(ManagedThread::GetCurrent(), args.data());
108 ASSERT_FALSE(ManagedThread::GetCurrent()->HasPendingException());
109
110 auto *ret = v.GetAs<ObjectHeader *>();
111 ASSERT_NE(ret, nullptr);
112
113 ASSERT_EQ(ret->ClassAddr<ark::Class>()->GetName(), "R1");
114 }
115
116 } // namespace ark::interpreter::test