forked from focus-creative-games/il2cpp_plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenericMethod.cpp
More file actions
201 lines (174 loc) · 7.86 KB
/
GenericMethod.cpp
File metadata and controls
201 lines (174 loc) · 7.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "il2cpp-config.h"
#include "metadata/GenericMetadata.h"
#include "metadata/GenericMethod.h"
#include "metadata/GenericSharing.h"
#include "metadata/Il2CppGenericMethodCompare.h"
#include "metadata/Il2CppGenericMethodHash.h"
#include "os/Mutex.h"
#include "utils/Memory.h"
#include "vm/Class.h"
#include "vm/Exception.h"
#include "vm/GenericClass.h"
#include "vm/MetadataAlloc.h"
#include "vm/MetadataCache.h"
#include "vm/MetadataLock.h"
#include "vm/Method.h"
#include "vm/Runtime.h"
#include "vm/Type.h"
#include "utils/Il2CppHashMap.h"
#include "il2cpp-class-internals.h"
#include "il2cpp-runtime-metadata.h"
#include "il2cpp-runtime-stats.h"
#include <string>
// ==={{ huatuo
#include "huatuo/metadata/MetadataUtil.h"
#include "huatuo/metadata/MetadataModule.h"
#include "huatuo/interpreter/InterpreterModule.h"
// ===}} huatuo
using il2cpp::metadata::GenericMetadata;
using il2cpp::metadata::GenericSharing;
using il2cpp::os::FastAutoLock;
using il2cpp::vm::Class;
using il2cpp::vm::GenericClass;
using il2cpp::vm::MetadataCalloc;
using il2cpp::vm::MetadataCache;
using il2cpp::vm::Method;
using il2cpp::vm::Runtime;
using il2cpp::vm::Type;
namespace il2cpp
{
namespace metadata
{
typedef Il2CppHashMap<const Il2CppGenericMethod*, MethodInfo*, Il2CppGenericMethodHash, Il2CppGenericMethodCompare> Il2CppGenericMethodMap;
static Il2CppGenericMethodMap s_GenericMethodMap;
static void AGenericMethodWhichIsTooDeeplyNestedWasInvoked()
{
vm::Exception::Raise(vm::Exception::GetMaxmimumNestedGenericsException());
}
const MethodInfo* GenericMethod::GetMethod(const Il2CppGenericMethod* gmethod, bool copyMethodPtr)
{
FastAutoLock lock(&il2cpp::vm::g_MetadataLock);
// This can be NULL only when we have hit the generic recursion depth limit.
if (gmethod == NULL)
{
MethodInfo* newMethod = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
newMethod->methodPointer = AGenericMethodWhichIsTooDeeplyNestedWasInvoked;
return newMethod;
}
Il2CppGenericMethodMap::const_iterator iter = s_GenericMethodMap.find(gmethod);
if (iter != s_GenericMethodMap.end())
return iter->second;
if (copyMethodPtr)
{
Il2CppGenericMethod *newGMethod = vm::MetadataAllocGenericMethod();
newGMethod->methodDefinition = gmethod->methodDefinition;
newGMethod->context = gmethod->context;
gmethod = newGMethod;
}
const MethodInfo* methodDefinition = gmethod->methodDefinition;
Il2CppClass* declaringClass = methodDefinition->klass;
if (gmethod->context.class_inst)
{
IL2CPP_ASSERT(!declaringClass->generic_class);
Il2CppGenericClass* genericClassDeclaringType = GenericMetadata::GetGenericClass(methodDefinition->klass, gmethod->context.class_inst);
declaringClass = GenericClass::GetClass(genericClassDeclaringType);
// we may fail if we cannot construct generic type
if (!declaringClass)
return NULL;
}
MethodInfo* newMethod = (MethodInfo*)MetadataCalloc(1, sizeof(MethodInfo));
// we set this here because the initialization may recurse and try to retrieve the same generic method
// this is safe because we *always* take the lock when retrieving the MethodInfo from a generic method.
// if we move lock to only if MethodInfo needs constructed then we need to revisit this since we could return a partially initialized MethodInfo
s_GenericMethodMap.insert(std::make_pair(gmethod, newMethod));
newMethod->klass = declaringClass;
newMethod->flags = methodDefinition->flags;
newMethod->iflags = methodDefinition->iflags;
newMethod->slot = methodDefinition->slot;
newMethod->name = methodDefinition->name;
newMethod->is_generic = false;
newMethod->is_inflated = true;
newMethod->token = methodDefinition->token;
newMethod->return_type = GenericMetadata::InflateIfNeeded(methodDefinition->return_type, &gmethod->context, true);
newMethod->parameters_count = methodDefinition->parameters_count;
newMethod->parameters = GenericMetadata::InflateParameters(methodDefinition->parameters, methodDefinition->parameters_count, &gmethod->context, true);
newMethod->genericMethod = gmethod;
if (!gmethod->context.method_inst)
{
if (methodDefinition->is_generic)
newMethod->is_generic = true;
if (!declaringClass->generic_class)
{
newMethod->genericContainerHandle = methodDefinition->genericContainerHandle;
}
newMethod->methodMetadataHandle = methodDefinition->methodMetadataHandle;
}
else
{
// we only need RGCTX for generic instance methods
newMethod->rgctx_data = GenericMetadata::InflateRGCTX(gmethod->methodDefinition->klass->image, gmethod->methodDefinition->token, &gmethod->context);
}
// ==={{ huatuo
if (huatuo::metadata::IsInterpreterMethod(newMethod))
{
newMethod->invoker_method = huatuo::interpreter::InterpreterModule::GetMethodInvoker(newMethod);
newMethod->methodPointer = newMethod->klass->valuetype && huatuo::metadata::IsInstanceMethod(newMethod) ?
huatuo::interpreter::InterpreterModule::GetAdjustThunkMethodPointer(newMethod)
: huatuo::interpreter::InterpreterModule::GetMethodPointer(newMethod);
}
else
{
newMethod->invoker_method = MetadataCache::GetInvokerMethodPointer(methodDefinition, &gmethod->context);
newMethod->methodPointer = MetadataCache::GetMethodPointer(methodDefinition, &gmethod->context);
if (!newMethod->invoker_method && huatuo::metadata::MetadataModule::IsImplementedByInterpreter(newMethod))
{
newMethod->invoker_method = huatuo::interpreter::InterpreterModule::GetMethodInvoker(newMethod);
}
if (!newMethod->methodPointer && huatuo::metadata::MetadataModule::IsImplementedByInterpreter(newMethod))
{
newMethod->methodPointer = newMethod->klass->valuetype && huatuo::metadata::IsInstanceMethod(newMethod) ?
huatuo::interpreter::InterpreterModule::GetAdjustThunkMethodPointer(newMethod)
: huatuo::interpreter::InterpreterModule::GetMethodPointer(newMethod);
}
}
// ===}} huatuo
++il2cpp_runtime_stats.inflated_method_count;
return newMethod;
}
const Il2CppGenericContext* GenericMethod::GetContext(const Il2CppGenericMethod* gmethod)
{
return &gmethod->context;
}
static std::string FormatGenericArguments(const Il2CppGenericInst* inst)
{
std::string output;
if (inst)
{
output.append("<");
for (size_t i = 0; i < inst->type_argc; ++i)
{
if (i != 0)
output.append(", ");
output.append(Type::GetName(inst->type_argv[i], IL2CPP_TYPE_NAME_FORMAT_FULL_NAME));
}
output.append(">");
}
return output;
}
std::string GenericMethod::GetFullName(const Il2CppGenericMethod* gmethod)
{
const MethodInfo* method = gmethod->methodDefinition;
std::string output;
output.append(Type::GetName(&gmethod->methodDefinition->klass->byval_arg, IL2CPP_TYPE_NAME_FORMAT_FULL_NAME));
output.append(FormatGenericArguments(gmethod->context.class_inst));
output.append("::");
output.append(Method::GetName(method));
output.append(FormatGenericArguments(gmethod->context.method_inst));
return output;
}
void GenericMethod::ClearStatics()
{
s_GenericMethodMap.clear();
}
} /* namespace vm */
} /* namespace il2cpp */