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 <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 panda::interpreter {
57
58 namespace test {
59
60 using DynClass = panda::coretypes::DynClass;
61 using DynObject = panda::coretypes::DynObject;
62
63 class InterpreterTestResolveField : public testing::Test {
64 public:
InterpreterTestResolveField()65 InterpreterTestResolveField()
66 {
67 RuntimeOptions options;
68 options.SetShouldLoadBootPandaFiles(false);
69 options.SetShouldInitializeIntrinsics(false);
70 options.SetRunGcInPlace(true);
71 options.SetVerifyCallStack(false);
72 options.SetGcType("epsilon");
73 Runtime::Create(options);
74 thread_ = panda::MTManagedThread::GetCurrent();
75 thread_->ManagedCodeBegin();
76 }
77
~InterpreterTestResolveField()78 ~InterpreterTestResolveField()
79 {
80 thread_->ManagedCodeEnd();
81 Runtime::Destroy();
82 }
83
84 protected:
85 panda::MTManagedThread *thread_;
86 };
87
TEST_F(InterpreterTestResolveField,ResolveField)88 TEST_F(InterpreterTestResolveField, ResolveField)
89 {
90 auto pf = panda::panda_file::File::Open("../bin-gtests/pre-build/interpreter_test_resolve_field.abc");
91 ASSERT_NE(pf, nullptr);
92
93 ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
94 class_linker->AddPandaFile(std::move(pf));
95 auto *extension = class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
96
97 PandaString descriptor;
98
99 {
100 Class *klass = extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R1"), &descriptor));
101 ASSERT_NE(klass, nullptr);
102
103 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("get"));
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<int32_t>();
111 ASSERT_EQ(ret, 10);
112 }
113
114 {
115 Class *klass = extension->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R2"), &descriptor));
116 ASSERT_NE(klass, nullptr);
117
118 Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("get"));
119 ASSERT_NE(method, nullptr);
120
121 std::vector<Value> args;
122 Value v = method->Invoke(ManagedThread::GetCurrent(), args.data());
123 ASSERT_FALSE(ManagedThread::GetCurrent()->HasPendingException());
124
125 auto ret = v.GetAs<int32_t>();
126 ASSERT_EQ(ret, 20);
127 }
128 }
129
130 } // namespace test
131 } // namespace panda::interpreter