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 "libpandabase/utils/utils.h"
33 #include "libpandafile/bytecode_emitter.h"
34 #include "libpandafile/file.h"
35 #include "libpandafile/file_items.h"
36 #include "libpandafile/value.h"
37 #include "runtime/bridge/bridge.h"
38 #include "runtime/include/class_linker.h"
39 #include "runtime/include/compiler_interface.h"
40 #include "runtime/include/mem/allocator.h"
41 #include "runtime/include/method.h"
42 #include "runtime/include/runtime.h"
43 #include "runtime/include/runtime_options.h"
44 #include "runtime/interpreter/frame.h"
45 #include "runtime/mem/gc/gc.h"
46 #include "runtime/mem/internal_allocator.h"
47 #include "runtime/core/core_class_linker_extension.h"
48 #include "runtime/tests/class_linker_test_extension.h"
49 #include "runtime/tests/interpreter/test_interpreter.h"
50 #include "runtime/tests/interpreter/test_runtime_interface.h"
51 #include "runtime/include/coretypes/dyn_objects.h"
52 #include "runtime/include/hclass.h"
53 #include "runtime/handle_base-inl.h"
54 #include "runtime/handle_scope-inl.h"
55 #include "runtime/include/coretypes/native_pointer.h"
56
57 namespace ark::interpreter::test {
58
59 using DynClass = ark::coretypes::DynClass;
60 using DynObject = ark::coretypes::DynObject;
61
62 class InterpreterTestResolveField : public testing::Test {
63 public:
InterpreterTestResolveField()64 InterpreterTestResolveField()
65 {
66 RuntimeOptions options;
67 options.SetShouldLoadBootPandaFiles(false);
68 options.SetShouldInitializeIntrinsics(false);
69 options.SetRunGcInPlace(true);
70 options.SetVerifyCallStack(false);
71 options.SetGcType("epsilon");
72 Runtime::Create(options);
73 thread_ = ark::MTManagedThread::GetCurrent();
74 thread_->ManagedCodeBegin();
75 }
76
~InterpreterTestResolveField()77 ~InterpreterTestResolveField() override
78 {
79 thread_->ManagedCodeEnd();
80 Runtime::Destroy();
81 }
82
83 NO_COPY_SEMANTIC(InterpreterTestResolveField);
84 NO_MOVE_SEMANTIC(InterpreterTestResolveField);
85
86 private:
87 ark::MTManagedThread *thread_;
88 };
89
TEST_F(InterpreterTestResolveField,ResolveField)90 TEST_F(InterpreterTestResolveField, ResolveField)
91 {
92 auto pf = ark::panda_file::File::Open("../bin-gtests/pre-build/interpreter_test_resolve_field.abc");
93 ASSERT_NE(pf, nullptr);
94
95 ClassLinker *classLinker = Runtime::GetCurrent()->GetClassLinker();
96 classLinker->AddPandaFile(std::move(pf));
97 auto *extension = classLinker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
98
99 PandaString descriptor;
100
101 {
102 Class *klass = extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R1"), &descriptor));
103 ASSERT_NE(klass, nullptr);
104
105 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("get"));
106 ASSERT_NE(method, nullptr);
107
108 std::vector<Value> args;
109 Value v = method->Invoke(ManagedThread::GetCurrent(), args.data());
110 ASSERT_FALSE(ManagedThread::GetCurrent()->HasPendingException());
111
112 auto ret = v.GetAs<int32_t>();
113 ASSERT_EQ(ret, 10_I);
114 }
115
116 {
117 Class *klass = extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R2"), &descriptor));
118 ASSERT_NE(klass, nullptr);
119
120 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("get"));
121 ASSERT_NE(method, nullptr);
122
123 std::vector<Value> args;
124 Value v = method->Invoke(ManagedThread::GetCurrent(), args.data());
125 ASSERT_FALSE(ManagedThread::GetCurrent()->HasPendingException());
126
127 auto ret = v.GetAs<int32_t>();
128 ASSERT_EQ(ret, 20_I);
129 }
130 }
131
132 } // namespace ark::interpreter::test
133