• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 InterpreterTestResolveCtorClass : public testing::Test {
64 public:
InterpreterTestResolveCtorClass()65     InterpreterTestResolveCtorClass()
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 
~InterpreterTestResolveCtorClass()78     ~InterpreterTestResolveCtorClass()
79     {
80         thread_->ManagedCodeEnd();
81         Runtime::Destroy();
82     }
83 
84 protected:
85     panda::MTManagedThread *thread_;
86 };
87 
TEST_F(InterpreterTestResolveCtorClass,ResolveCtorClass)88 TEST_F(InterpreterTestResolveCtorClass, ResolveCtorClass)
89 {
90     auto pf = panda::panda_file::File::Open("../bin-gtests/pre-build/interpreter_test_resolve_ctor_class.abc");
91     ASSERT_NE(pf, nullptr);
92 
93     ClassLinker *class_linker = Runtime::GetCurrent()->GetClassLinker();
94     class_linker->AddPandaFile(std::move(pf));
95 
96     PandaString descriptor;
97 
98     auto *ext = class_linker->GetExtension(panda_file::SourceLang::PANDA_ASSEMBLY);
99     Class *klass = ext->GetClass(ClassHelper::GetDescriptor(utf::CStringAsMutf8("R2"), &descriptor));
100     ASSERT_NE(klass, nullptr);
101 
102     Method *method = klass->GetDirectMethod(utf::CStringAsMutf8("foo"));
103     ASSERT_NE(method, nullptr);
104 
105     std::vector<Value> args;
106     Value v = method->Invoke(ManagedThread::GetCurrent(), args.data());
107     ASSERT_FALSE(ManagedThread::GetCurrent()->HasPendingException());
108 
109     auto *ret = v.GetAs<ObjectHeader *>();
110     ASSERT_NE(ret, nullptr);
111 
112     ASSERT_EQ(ret->ClassAddr<panda::Class>()->GetName(), "R1");
113 }
114 
115 }  // namespace test
116 }  // namespace panda::interpreter