il2cpp-codegen-il2cpp.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989
  1. #pragma once
  2. #include "il2cpp-codegen-common.h"
  3. #include "icallincludes.h"
  4. #include "utils/Il2CppHStringReference.h"
  5. #include "utils/RegisterRuntimeInitializeAndCleanup.h"
  6. #include "metadata/GenericMethod.h"
  7. #include "vm/Array.h"
  8. #include "vm/Assembly.h"
  9. #include "vm/Atomic.h"
  10. #include "vm/CCW.h"
  11. #include "vm/Class.h"
  12. #include "vm/COM.h"
  13. #include "vm/Domain.h"
  14. #include "vm/Exception.h"
  15. #include "vm/InternalCalls.h"
  16. #include "vm/LastError.h"
  17. #include "vm/MarshalAlloc.h"
  18. #include "vm/MetadataCache.h"
  19. #include "vm/Method.h"
  20. #include "vm/Object.h"
  21. #include "vm/Profiler.h"
  22. #include "vm/RCW.h"
  23. #include "vm/Reflection.h"
  24. #include "vm/Runtime.h"
  25. #include "vm/String.h"
  26. #include "vm/Thread.h"
  27. #include "vm/ThreadPool.h"
  28. #include "vm/Type.h"
  29. #include "vm/WindowsRuntime.h"
  30. #include "vm/ThreadPoolMs.h"
  31. struct ProfilerMethodSentry
  32. {
  33. ProfilerMethodSentry(const RuntimeMethod* method)
  34. #if IL2CPP_ENABLE_PROFILER
  35. : m_method(method)
  36. #endif
  37. {
  38. #if IL2CPP_ENABLE_PROFILER
  39. il2cpp::vm::Profiler::MethodEnter(m_method);
  40. #endif
  41. }
  42. ~ProfilerMethodSentry()
  43. {
  44. #if IL2CPP_ENABLE_PROFILER
  45. il2cpp::vm::Profiler::MethodExit(m_method);
  46. #endif
  47. }
  48. private:
  49. const RuntimeMethod* m_method;
  50. };
  51. struct StackTraceSentry
  52. {
  53. StackTraceSentry(const RuntimeMethod* method) : m_method(method)
  54. {
  55. Il2CppStackFrameInfo frame_info;
  56. frame_info.method = method;
  57. il2cpp::vm::StackTrace::PushFrame(frame_info);
  58. }
  59. ~StackTraceSentry()
  60. {
  61. il2cpp::vm::StackTrace::PopFrame();
  62. }
  63. private:
  64. const RuntimeMethod* m_method;
  65. };
  66. #define IL2CPP_FAKE_BOX_SENTRY (MonitorData*)UINTPTR_MAX
  67. template<typename T>
  68. struct Il2CppFakeBox : RuntimeObject
  69. {
  70. T m_Value;
  71. Il2CppFakeBox(RuntimeClass* boxedType, T* value)
  72. {
  73. klass = boxedType;
  74. monitor = IL2CPP_FAKE_BOX_SENTRY;
  75. m_Value = *value;
  76. }
  77. };
  78. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  79. {
  80. return object->monitor == IL2CPP_FAKE_BOX_SENTRY;
  81. }
  82. // TODO: This file should contain all the functions and type declarations needed for the generated code.
  83. // Hopefully, we stop including everything in the generated code and know exactly what dependencies we have.
  84. // Note that all parameter and return types should match the generated types not the runtime types.
  85. inline void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
  86. {
  87. il2cpp::vm::MetadataCache::Register(codeRegistration, metadataRegistration, codeGenOptions);
  88. }
  89. // type registration
  90. inline void* il2cpp_codegen_get_thread_static_data(RuntimeClass* klass)
  91. {
  92. return il2cpp::vm::Thread::GetThreadStaticData(klass->thread_static_fields_offset);
  93. }
  94. inline String_t* il2cpp_codegen_string_new_wrapper(const char* str)
  95. {
  96. return (String_t*)il2cpp::vm::String::NewWrapper(str);
  97. }
  98. inline String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  99. {
  100. return (String_t*)il2cpp::vm::String::NewUtf16(str.Str(), static_cast<int32_t>(str.Length()));
  101. }
  102. inline Type_t* il2cpp_codegen_type_get_object(const RuntimeType* type)
  103. {
  104. return (Type_t*)il2cpp::vm::Reflection::GetTypeObject(type);
  105. }
  106. inline NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame = NULL)
  107. {
  108. il2cpp::vm::Exception::Raise((RuntimeException*)ex, lastManagedFrame);
  109. }
  110. inline void il2cpp_codegen_raise_execution_engine_exception_if_method_is_not_found(const RuntimeMethod* method)
  111. {
  112. il2cpp::vm::Runtime::RaiseExecutionEngineExceptionIfMethodIsNotFound(method);
  113. }
  114. inline void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method)
  115. {
  116. il2cpp::vm::Runtime::AlwaysRaiseExecutionEngineException(method);
  117. }
  118. inline void il2cpp_codegen_raise_out_of_memory_exception()
  119. {
  120. il2cpp::vm::Exception::RaiseOutOfMemoryException();
  121. }
  122. inline Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  123. {
  124. return (Exception_t*)il2cpp::vm::Exception::GetArgumentException(param, msg);
  125. }
  126. inline Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param)
  127. {
  128. return (Exception_t*)il2cpp::vm::Exception::GetArgumentNullException(param);
  129. }
  130. inline Exception_t* il2cpp_codegen_get_overflow_exception()
  131. {
  132. return (Exception_t*)il2cpp::vm::Exception::GetOverflowException("Arithmetic operation resulted in an overflow.");
  133. }
  134. inline Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg)
  135. {
  136. return (Exception_t*)il2cpp::vm::Exception::GetNotSupportedException(msg);
  137. }
  138. inline Exception_t* il2cpp_codegen_get_array_type_mismatch_exception()
  139. {
  140. return (Exception_t*)il2cpp::vm::Exception::GetArrayTypeMismatchException();
  141. }
  142. inline Exception_t* il2cpp_codegen_get_invalid_cast_exception(const char* msg)
  143. {
  144. return (Exception_t*)il2cpp::vm::Exception::GetInvalidCastException(msg);
  145. }
  146. inline Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg)
  147. {
  148. return (Exception_t*)il2cpp::vm::Exception::GetInvalidOperationException(msg);
  149. }
  150. inline Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  151. {
  152. return (Exception_t*)il2cpp::vm::Exception::GetMarshalDirectiveException(msg);
  153. }
  154. inline Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  155. {
  156. return (Exception_t*)il2cpp::vm::Exception::GetMissingMethodException(msg);
  157. }
  158. inline Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception()
  159. {
  160. return (Exception_t*)il2cpp::vm::Exception::GetMaxmimumNestedGenericsException();
  161. }
  162. inline RuntimeClass* il2cpp_codegen_object_class(RuntimeObject* obj)
  163. {
  164. return obj->klass;
  165. }
  166. // OpCode.IsInst
  167. inline RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType)
  168. {
  169. return il2cpp::vm::Object::IsInst(obj, targetType);
  170. }
  171. inline RuntimeObject* IsInstSealed(RuntimeObject *obj, RuntimeClass* targetType)
  172. {
  173. #if IL2CPP_DEBUG
  174. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_SEALED) != 0);
  175. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  176. #endif
  177. if (!obj)
  178. return NULL;
  179. // optimized version to compare sealed classes
  180. return (obj->klass == targetType ? obj : NULL);
  181. }
  182. inline RuntimeObject* IsInstClass(RuntimeObject *obj, RuntimeClass* targetType)
  183. {
  184. #if IL2CPP_DEBUG
  185. IL2CPP_ASSERT((targetType->flags & TYPE_ATTRIBUTE_INTERFACE) == 0);
  186. #endif
  187. if (!obj)
  188. return NULL;
  189. // optimized version to compare classes
  190. return il2cpp::vm::Class::HasParentUnsafe(obj->klass, targetType) ? obj : NULL;
  191. }
  192. // OpCode.Castclass
  193. NORETURN inline void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType)
  194. {
  195. std::string exceptionMessage = il2cpp::utils::Exception::FormatInvalidCastException(obj->klass->element_class, targetType);
  196. RuntimeException* exception = il2cpp::vm::Exception::GetInvalidCastException(exceptionMessage.c_str());
  197. il2cpp::vm::Exception::Raise(exception);
  198. }
  199. inline RuntimeObject* Castclass(RuntimeObject *obj, RuntimeClass* targetType)
  200. {
  201. if (!obj)
  202. return NULL;
  203. RuntimeObject* result = il2cpp::vm::Object::IsInst(obj, targetType);
  204. if (result)
  205. return result;
  206. RaiseInvalidCastException(obj, targetType);
  207. return NULL;
  208. }
  209. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, RuntimeClass* targetType)
  210. {
  211. if (!obj)
  212. return NULL;
  213. RuntimeObject* result = IsInstSealed(obj, targetType);
  214. if (result)
  215. return result;
  216. RaiseInvalidCastException(obj, targetType);
  217. return NULL;
  218. }
  219. inline RuntimeObject* CastclassClass(RuntimeObject *obj, RuntimeClass* targetType)
  220. {
  221. if (!obj)
  222. return NULL;
  223. RuntimeObject* result = IsInstClass(obj, targetType);
  224. if (result)
  225. return result;
  226. RaiseInvalidCastException(obj, targetType);
  227. return NULL;
  228. }
  229. inline void NullCheck(void* this_ptr)
  230. {
  231. if (this_ptr != NULL)
  232. return;
  233. il2cpp::vm::Exception::RaiseNullReferenceException();
  234. }
  235. // OpCode.Box
  236. inline RuntimeObject* Box(RuntimeClass* type, void* data)
  237. {
  238. return il2cpp::vm::Object::Box(type, data);
  239. }
  240. // OpCode.UnBox
  241. inline void* UnBox(RuntimeObject* obj)
  242. {
  243. NullCheck(obj);
  244. return il2cpp::vm::Object::Unbox(obj);
  245. }
  246. inline void* UnBox(RuntimeObject* obj, RuntimeClass* expectedBoxedClass)
  247. {
  248. NullCheck(obj);
  249. if (obj->klass->element_class == expectedBoxedClass->element_class)
  250. return il2cpp::vm::Object::Unbox(obj);
  251. RaiseInvalidCastException(obj, expectedBoxedClass);
  252. return NULL;
  253. }
  254. inline void UnBoxNullable(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* storage)
  255. {
  256. // We only need to do type checks if obj is not null
  257. // Unboxing null nullable is perfectly valid and returns an instance that has no value
  258. if (obj != NULL)
  259. {
  260. if (obj->klass->element_class != expectedBoxedClass->element_class)
  261. RaiseInvalidCastException(obj, expectedBoxedClass);
  262. }
  263. il2cpp::vm::Object::UnboxNullable(obj, expectedBoxedClass, storage);
  264. }
  265. inline uint32_t il2cpp_codegen_sizeof(RuntimeClass* klass)
  266. {
  267. if (!klass->valuetype)
  268. {
  269. return sizeof(void*);
  270. }
  271. return il2cpp::vm::Class::GetInstanceSize(klass) - sizeof(RuntimeObject);
  272. }
  273. inline bool il2cpp_codegen_method_is_virtual(RuntimeMethod* method)
  274. {
  275. return method->slot != kInvalidIl2CppMethodSlot;
  276. }
  277. inline bool il2cpp_codegen_object_is_of_sealed_type(RuntimeObject* obj)
  278. {
  279. return obj != NULL && (obj->klass->flags & TYPE_ATTRIBUTE_SEALED) != 0;
  280. }
  281. inline bool il2cpp_codegen_method_is_generic_instance(RuntimeMethod* method)
  282. {
  283. return il2cpp::vm::Method::IsGenericInstance(method);
  284. }
  285. inline bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method)
  286. {
  287. return il2cpp::vm::Class::IsInterface(il2cpp::vm::Method::GetClass(method));
  288. }
  289. inline uint16_t il2cpp_codegen_method_get_slot(RuntimeMethod* method)
  290. {
  291. return method->slot;
  292. }
  293. inline RuntimeClass* il2cpp_codegen_method_get_declaring_type(RuntimeMethod* method)
  294. {
  295. return il2cpp::vm::Method::GetClass(method);
  296. }
  297. FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_virtual_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj)
  298. {
  299. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_virtual_invoke_data got called on a non-virtual method");
  300. return obj->klass->vtable[slot];
  301. }
  302. FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj, const RuntimeClass* declaringInterface)
  303. {
  304. Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_interface_invoke_data got called on a non-virtual method");
  305. return il2cpp::vm::Class::GetInterfaceInvokeDataFromVTable(obj, declaringInterface, slot);
  306. }
  307. FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method(const RuntimeMethod* method, const RuntimeObject* obj)
  308. {
  309. uint16_t slot = method->slot;
  310. const RuntimeMethod* methodDefinition = obj->klass->vtable[slot].method;
  311. return il2cpp::vm::Runtime::GetGenericVirtualMethod(methodDefinition, method);
  312. }
  313. FORCE_INLINE void il2cpp_codegen_get_generic_virtual_invoke_data(const RuntimeMethod* method, const RuntimeObject* obj, VirtualInvokeData* invokeData)
  314. {
  315. const RuntimeMethod* targetRuntimeMethod = il2cpp_codegen_get_generic_virtual_method(method, obj);
  316. #if IL2CPP_DEBUG
  317. IL2CPP_ASSERT(targetRuntimeMethod);
  318. #endif
  319. invokeData->methodPtr = targetRuntimeMethod->methodPointer;
  320. invokeData->method = targetRuntimeMethod;
  321. }
  322. FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_interface_method(const RuntimeMethod* method, const RuntimeObject* obj)
  323. {
  324. const RuntimeMethod* methodDefinition = il2cpp::vm::Class::GetInterfaceInvokeDataFromVTable(obj, method->declaring_type, method->slot).method;
  325. return il2cpp::vm::Runtime::GetGenericVirtualMethod(methodDefinition, method);
  326. }
  327. FORCE_INLINE void il2cpp_codegen_get_generic_interface_invoke_data(const RuntimeMethod* method, const RuntimeObject* obj, VirtualInvokeData* invokeData)
  328. {
  329. const RuntimeMethod* targetRuntimeMethod = il2cpp_codegen_get_generic_interface_method(method, obj);
  330. #if IL2CPP_DEBUG
  331. IL2CPP_ASSERT(targetRuntimeMethod);
  332. #endif
  333. invokeData->methodPtr = targetRuntimeMethod->methodPointer;
  334. invokeData->method = targetRuntimeMethod;
  335. }
  336. inline RuntimeClass* InitializedTypeInfo(RuntimeClass* klass)
  337. {
  338. il2cpp::vm::Class::Init(klass);
  339. return klass;
  340. }
  341. inline RuntimeClass* il2cpp_codegen_class_from_type(const RuntimeType *type)
  342. {
  343. return InitializedTypeInfo(il2cpp::vm::Class::FromIl2CppType(type));
  344. }
  345. inline void* InterlockedExchangeImplRef(void** location, void* value)
  346. {
  347. return il2cpp::icalls::mscorlib::System::Threading::Interlocked::ExchangePointer(location, value);
  348. }
  349. template<typename T>
  350. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  351. {
  352. return (T)il2cpp::icalls::mscorlib::System::Threading::Interlocked::CompareExchange_T((void**)location, value, comparand);
  353. }
  354. template<typename T>
  355. inline T InterlockedExchangeImpl(T* location, T value)
  356. {
  357. return (T)InterlockedExchangeImplRef((void**)location, value);
  358. }
  359. inline void il2cpp_codegen_memory_barrier()
  360. {
  361. il2cpp::vm::Thread::FullMemoryBarrier();
  362. }
  363. inline void ArrayGetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value)
  364. {
  365. memcpy(value, ((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * thisPtr->klass->element_size, thisPtr->klass->element_size);
  366. }
  367. inline void ArraySetGenericValueImpl(RuntimeArray * thisPtr, int32_t pos, void* value)
  368. {
  369. memcpy(((uint8_t*)thisPtr) + sizeof(RuntimeArray) + pos * thisPtr->klass->element_size, value, thisPtr->klass->element_size);
  370. }
  371. inline RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length)
  372. {
  373. il2cpp::vm::Class::Init(arrayType);
  374. return il2cpp::vm::Array::NewSpecific(arrayType, length);
  375. }
  376. inline RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions)
  377. {
  378. return il2cpp::vm::Array::NewFull(arrayType, dimensions, NULL);
  379. }
  380. // Performance optimization as detailed here: http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx
  381. // Since array size is a signed int32_t, a single unsigned check can be performed to determine if index is less than array size.
  382. // Negative indices will map to a unsigned number greater than or equal to 2^31 which is larger than allowed for a valid array.
  383. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  384. do { \
  385. if (((uint32_t)(index)) >= ((uint32_t)length)) il2cpp::vm::Exception::Raise (il2cpp::vm::Exception::GetIndexOutOfRangeException()); \
  386. } while (0)
  387. inline bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass)
  388. {
  389. return il2cpp::vm::Class::IsAssignableFrom(klass, oklass);
  390. }
  391. inline RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass)
  392. {
  393. return il2cpp::vm::Object::New(klass);
  394. }
  395. inline Il2CppMethodPointer il2cpp_codegen_resolve_icall(const char* name)
  396. {
  397. Il2CppMethodPointer method = il2cpp::vm::InternalCalls::Resolve(name);
  398. if (!method)
  399. {
  400. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(name));
  401. }
  402. return method;
  403. }
  404. template<typename FunctionPointerType, size_t dynamicLibraryLength, size_t entryPointLength>
  405. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const Il2CppNativeChar(&nativeDynamicLibrary)[dynamicLibraryLength], const char(&entryPoint)[entryPointLength],
  406. Il2CppCallConvention callingConvention, Il2CppCharSet charSet, int parameterSize, bool isNoMangle)
  407. {
  408. const PInvokeArguments pinvokeArgs =
  409. {
  410. il2cpp::utils::StringView<Il2CppNativeChar>(nativeDynamicLibrary),
  411. il2cpp::utils::StringView<char>(entryPoint),
  412. callingConvention,
  413. charSet,
  414. parameterSize,
  415. isNoMangle
  416. };
  417. return reinterpret_cast<FunctionPointerType>(il2cpp::vm::PlatformInvoke::Resolve(pinvokeArgs));
  418. }
  419. template<typename T>
  420. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  421. {
  422. return static_cast<T*>(il2cpp::vm::MarshalAlloc::Allocate((il2cpp_array_size_t)(sizeof(T) * length)));
  423. }
  424. inline char* il2cpp_codegen_marshal_string(String_t* string)
  425. {
  426. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppString((RuntimeString*)string);
  427. }
  428. inline void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters)
  429. {
  430. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  431. }
  432. inline Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  433. {
  434. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWString((RuntimeString*)string);
  435. }
  436. inline void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters)
  437. {
  438. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  439. }
  440. inline Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string)
  441. {
  442. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppBString((RuntimeString*)string);
  443. }
  444. inline String_t* il2cpp_codegen_marshal_string_result(const char* value)
  445. {
  446. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value);
  447. }
  448. inline String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  449. {
  450. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value);
  451. }
  452. inline String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value)
  453. {
  454. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppBStringToCSharpStringResult(value);
  455. }
  456. inline void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value)
  457. {
  458. il2cpp::vm::PlatformInvoke::MarshalFreeBString(value);
  459. }
  460. inline char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder)
  461. {
  462. return il2cpp::vm::PlatformInvoke::MarshalStringBuilder((RuntimeStringBuilder*)stringBuilder);
  463. }
  464. inline Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder)
  465. {
  466. return il2cpp::vm::PlatformInvoke::MarshalWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  467. }
  468. inline void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer)
  469. {
  470. il2cpp::vm::PlatformInvoke::MarshalStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  471. }
  472. inline void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer)
  473. {
  474. il2cpp::vm::PlatformInvoke::MarshalWStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  475. }
  476. inline Il2CppHString il2cpp_codegen_create_hstring(String_t* str)
  477. {
  478. return il2cpp::vm::WindowsRuntime::CreateHString(reinterpret_cast<RuntimeString*>(str));
  479. }
  480. inline String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring)
  481. {
  482. return reinterpret_cast<String_t*>(il2cpp::vm::WindowsRuntime::HStringToManagedString(hstring));
  483. }
  484. inline void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring)
  485. {
  486. il2cpp::vm::WindowsRuntime::DeleteHString(hstring);
  487. }
  488. inline void il2cpp_codegen_marshal_free(void* ptr)
  489. {
  490. il2cpp::vm::PlatformInvoke::MarshalFree(ptr);
  491. }
  492. inline Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d)
  493. {
  494. return (Il2CppMethodPointer)il2cpp::vm::PlatformInvoke::MarshalDelegate((RuntimeDelegate*)d);
  495. }
  496. template<typename T>
  497. inline T* il2cpp_codegen_marshal_function_ptr_to_delegate(Il2CppMethodPointer functionPtr, RuntimeClass* delegateType)
  498. {
  499. return (T*)il2cpp::vm::PlatformInvoke::MarshalFunctionPointerToDelegate(reinterpret_cast<void*>(functionPtr), delegateType);
  500. }
  501. inline void il2cpp_codegen_marshal_store_last_error()
  502. {
  503. il2cpp::vm::LastError::StoreLastError();
  504. }
  505. class il2cpp_native_wrapper_vm_thread_attacher
  506. {
  507. public:
  508. il2cpp_native_wrapper_vm_thread_attacher()
  509. : m_AttachedThread(NULL)
  510. {
  511. if (il2cpp::vm::Thread::Current() == NULL)
  512. m_AttachedThread = il2cpp::vm::Thread::Attach(il2cpp::vm::Domain::GetRoot());
  513. }
  514. ~il2cpp_native_wrapper_vm_thread_attacher()
  515. {
  516. if (m_AttachedThread != NULL)
  517. il2cpp::vm::Thread::Detach(m_AttachedThread);
  518. }
  519. private:
  520. Il2CppThread* m_AttachedThread;
  521. };
  522. #if _DEBUG
  523. struct ScopedMarshallingAllocationFrame
  524. {
  525. ScopedMarshallingAllocationFrame()
  526. {
  527. il2cpp::vm::MarshalAlloc::PushAllocationFrame();
  528. }
  529. ~ScopedMarshallingAllocationFrame()
  530. {
  531. il2cpp::vm::MarshalAlloc::PopAllocationFrame();
  532. }
  533. };
  534. struct ScopedMarshallingAllocationCheck
  535. {
  536. ~ScopedMarshallingAllocationCheck()
  537. {
  538. if (il2cpp::vm::MarshalAlloc::HasUnfreedAllocations())
  539. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetInvalidOperationException("Error in marshaling allocation. Some memory has been leaked."));
  540. }
  541. private:
  542. ScopedMarshallingAllocationFrame m_AllocationFrame;
  543. };
  544. struct ScopedMarshalingAllocationClearer
  545. {
  546. ~ScopedMarshalingAllocationClearer()
  547. {
  548. il2cpp::vm::MarshalAlloc::ClearAllTrackedAllocations();
  549. }
  550. private:
  551. ScopedMarshallingAllocationFrame m_AllocationFrame;
  552. };
  553. #endif
  554. inline void DivideByZeroCheck(int64_t denominator)
  555. {
  556. if (denominator != 0)
  557. return;
  558. il2cpp::vm::Exception::RaiseDivideByZeroException();
  559. }
  560. inline void il2cpp_codegen_initobj(void* value, size_t size)
  561. {
  562. memset(value, 0, size);
  563. }
  564. inline bool MethodIsStatic(const RuntimeMethod* method)
  565. {
  566. return !il2cpp::vm::Method::IsInstance(method);
  567. }
  568. inline bool MethodHasParameters(const RuntimeMethod* method)
  569. {
  570. return il2cpp::vm::Method::GetParamCount(method) != 0;
  571. }
  572. #define IL2CPP_RUNTIME_CLASS_INIT(klass) do { if((klass)->has_cctor && !(klass)->cctor_finished) il2cpp::vm::Runtime::ClassInit ((klass)); } while (0)
  573. // generic sharing
  574. #define IL2CPP_RGCTX_DATA(rgctxVar, index) (InitializedTypeInfo(rgctxVar[index].klass))
  575. #define IL2CPP_RGCTX_SIZEOF(rgctxVar, index) (il2cpp_codegen_sizeof(IL2CPP_RGCTX_DATA(rgctxVar, index)))
  576. #define IL2CPP_RGCTX_TYPE(rgctxVar, index) (rgctxVar[index].type)
  577. #define IL2CPP_RGCTX_METHOD_INFO(rgctxVar, index) (rgctxVar[index].method)
  578. #define IL2CPP_RGCTX_FIELD_INFO(klass, index) ((klass)->fields+index)
  579. inline void ArrayElementTypeCheck(RuntimeArray* array, void* value)
  580. {
  581. if (value != NULL && il2cpp::vm::Object::IsInst((RuntimeObject*)value, array->klass->element_class) == NULL)
  582. il2cpp_codegen_raise_exception(il2cpp_codegen_get_array_type_mismatch_exception());
  583. }
  584. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot)
  585. {
  586. if (!pThis)
  587. il2cpp::vm::Exception::RaiseNullReferenceException();
  588. return pThis->klass->vtable[slot].method;
  589. }
  590. inline const RuntimeMethod* GetInterfaceMethodInfo(RuntimeObject* pThis, Il2CppMethodSlot slot, RuntimeClass* declaringInterface)
  591. {
  592. if (!pThis)
  593. il2cpp::vm::Exception::RaiseNullReferenceException();
  594. return il2cpp::vm::Class::GetInterfaceInvokeDataFromVTable(pThis, declaringInterface, slot).method;
  595. }
  596. inline void il2cpp_codegen_initialize_method(uint32_t index)
  597. {
  598. il2cpp::vm::MetadataCache::InitializeMethodMetadata(index);
  599. }
  600. inline bool il2cpp_codegen_type_implements_virtual_method(RuntimeClass* type, const RuntimeMethod* method)
  601. {
  602. IL2CPP_ASSERT(il2cpp::vm::Class::IsValuetype(type));
  603. return method->declaring_type == type;
  604. }
  605. inline MethodBase_t* il2cpp_codegen_get_method_object(const RuntimeMethod* method)
  606. {
  607. if (method->is_inflated)
  608. method = il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
  609. return (MethodBase_t*)il2cpp::vm::Reflection::GetMethodObject(method, method->declaring_type);
  610. }
  611. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, const char* assemblyName)
  612. {
  613. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, const RuntimeMethod*);
  614. RuntimeString* assemblyQualifiedTypeName = il2cpp::vm::Type::AppendAssemblyNameIfNecessary((RuntimeString*)typeName, assemblyName);
  615. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  616. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, NULL);
  617. if (type == NULL)
  618. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, NULL);
  619. return type;
  620. }
  621. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, bool throwOnError, const char* assemblyName)
  622. {
  623. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, bool, const RuntimeMethod*);
  624. RuntimeString* assemblyQualifiedTypeName = il2cpp::vm::Type::AppendAssemblyNameIfNecessary((RuntimeString*)typeName, assemblyName);
  625. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  626. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, throwOnError, NULL);
  627. if (type == NULL)
  628. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, throwOnError, NULL);
  629. return type;
  630. }
  631. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, bool throwOnError, bool ignoreCase, const char* assemblyName)
  632. {
  633. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, bool, bool, const RuntimeMethod*);
  634. RuntimeString* assemblyQualifiedTypeName = il2cpp::vm::Type::AppendAssemblyNameIfNecessary((RuntimeString*)typeName, assemblyName);
  635. // Try to find the type using a hint about about calling assembly. If it is not found, fall back to calling GetType without the hint.
  636. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, throwOnError, ignoreCase, NULL);
  637. if (type == NULL)
  638. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, throwOnError, ignoreCase, NULL);
  639. return type;
  640. }
  641. inline Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method)
  642. {
  643. return (Assembly_t*)il2cpp::vm::Reflection::GetAssemblyObject(il2cpp::vm::MetadataCache::GetAssemblyFromIndex(method->declaring_type->image->assemblyIndex));
  644. }
  645. // Atomic
  646. inline void* il2cpp_codegen_atomic_compare_exchange_pointer(void* volatile* dest, void* exchange, void* comparand)
  647. {
  648. return il2cpp::vm::Atomic::CompareExchangePointer(dest, exchange, comparand);
  649. }
  650. template<typename T>
  651. inline T* il2cpp_codegen_atomic_compare_exchange_pointer(T* volatile* dest, T* newValue, T* oldValue)
  652. {
  653. return il2cpp::vm::Atomic::CompareExchangePointer(dest, newValue, oldValue);
  654. }
  655. // COM
  656. inline void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant)
  657. {
  658. il2cpp::vm::COM::MarshalVariant(obj, variant);
  659. }
  660. inline RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant)
  661. {
  662. return il2cpp::vm::COM::MarshalVariantResult(variant);
  663. }
  664. inline void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant)
  665. {
  666. il2cpp::vm::COM::DestroyVariant(variant);
  667. }
  668. inline Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray)
  669. {
  670. return il2cpp::vm::COM::MarshalSafeArray(type, managedArray);
  671. }
  672. inline RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray)
  673. {
  674. return il2cpp::vm::COM::MarshalSafeArrayResult(variantType, type, safeArray);
  675. }
  676. inline Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray)
  677. {
  678. return il2cpp::vm::COM::MarshalSafeArrayBString(managedArray);
  679. }
  680. inline RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray)
  681. {
  682. return il2cpp::vm::COM::MarshalSafeArrayBStringResult(type, safeArray);
  683. }
  684. inline void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray)
  685. {
  686. il2cpp::vm::COM::DestroySafeArray(safeArray);
  687. }
  688. inline void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity)
  689. {
  690. il2cpp::vm::COM::CreateInstance(clsid, identity);
  691. }
  692. inline void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw)
  693. {
  694. il2cpp::vm::RCW::Register(rcw);
  695. }
  696. template<typename T>
  697. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iunknown(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  698. {
  699. return static_cast<T*>(il2cpp::vm::RCW::GetOrCreateFromIUnknown(unknown, fallbackClass));
  700. }
  701. template<typename T>
  702. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  703. {
  704. return static_cast<T*>(il2cpp::vm::RCW::GetOrCreateFromIInspectable(unknown, fallbackClass));
  705. }
  706. template<typename T>
  707. inline T* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  708. {
  709. return static_cast<T*>(il2cpp::vm::RCW::GetOrCreateForSealedClass(unknown, objectClass));
  710. }
  711. inline void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw)
  712. {
  713. il2cpp::vm::RCW::Cleanup(rcw);
  714. }
  715. template<typename InterfaceType>
  716. inline InterfaceType* il2cpp_codegen_com_get_or_create_ccw(RuntimeObject* obj)
  717. {
  718. return static_cast<InterfaceType*>(il2cpp::vm::CCW::GetOrCreate(obj, InterfaceType::IID));
  719. }
  720. inline intptr_t il2cpp_codegen_com_get_iunknown_for_object(RuntimeObject* obj)
  721. {
  722. return reinterpret_cast<intptr_t>(il2cpp::vm::CCW::GetOrCreate(obj, Il2CppIUnknown::IID));
  723. }
  724. inline void il2cpp_codegen_com_raise_exception_if_failed(il2cpp_hresult_t hr, bool defaultToCOMException)
  725. {
  726. il2cpp::vm::Exception::RaiseIfFailed(hr, defaultToCOMException);
  727. }
  728. inline RuntimeException* il2cpp_codegen_com_get_exception(il2cpp_hresult_t hr, bool defaultToCOMException)
  729. {
  730. return il2cpp::vm::Exception::Get(hr, defaultToCOMException);
  731. }
  732. inline RuntimeException* il2cpp_codegen_com_get_exception_for_invalid_iproperty_cast(RuntimeObject* value, const char* a, const char* b)
  733. {
  734. return il2cpp::vm::CCW::GetIPropertyValueInvalidCast(value, a, b);
  735. }
  736. inline void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString)
  737. {
  738. il2cpp::vm::Exception::StoreExceptionInfo(ex, reinterpret_cast<RuntimeString*>(exceptionString));
  739. }
  740. inline Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName)
  741. {
  742. return il2cpp::vm::WindowsRuntime::GetActivationFactory(runtimeClassName);
  743. }
  744. // delegate
  745. inline Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state)
  746. {
  747. #if NET_4_0
  748. return il2cpp::vm::ThreadPoolMs::DelegateBeginInvoke(delegate, params, asyncCallback, state);
  749. #else
  750. return il2cpp::vm::ThreadPool::Queue(delegate, params, asyncCallback, state);
  751. #endif
  752. }
  753. inline RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args)
  754. {
  755. #if NET_4_0
  756. return il2cpp::vm::ThreadPoolMs::DelegateEndInvoke(asyncResult, out_args);
  757. #else
  758. return il2cpp::vm::ThreadPool::Wait(asyncResult, out_args);
  759. #endif
  760. }
  761. inline bool il2cpp_codegen_delegate_has_invoker(Il2CppDelegate* delegate)
  762. {
  763. return delegate->invoke_impl != NULL;
  764. }
  765. inline const Il2CppGenericInst* il2cpp_codegen_get_generic_class_inst(RuntimeClass* genericClass)
  766. {
  767. IL2CPP_ASSERT(genericClass->generic_class);
  768. return genericClass->generic_class->context.class_inst;
  769. }
  770. inline RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst)
  771. {
  772. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, genericInst);
  773. }
  774. inline void* il2cpp_codegen_static_fields_for(RuntimeClass* klass)
  775. {
  776. return klass->static_fields;
  777. }
  778. inline Il2CppMethodPointer il2cpp_codegen_get_method_pointer(const RuntimeMethod* method)
  779. {
  780. return method->methodPointer;
  781. }
  782. inline const RuntimeType* il2cpp_codegen_method_return_type(const RuntimeMethod* method)
  783. {
  784. return method->return_type;
  785. }
  786. inline int il2cpp_codegen_method_parameter_count(const RuntimeMethod* method)
  787. {
  788. return method->parameters_count;
  789. }
  790. inline bool il2cpp_codegen_is_import_or_windows_runtime(const RuntimeObject *object)
  791. {
  792. return object->klass->is_import_or_windows_runtime;
  793. }
  794. inline std::string il2cpp_codegen_format_exception(const RuntimeException* ex)
  795. {
  796. return il2cpp::utils::Exception::FormatException(ex);
  797. }
  798. inline intptr_t il2cpp_codegen_get_com_interface_for_object(Il2CppObject* object, Type_t* type)
  799. {
  800. return il2cpp::icalls::mscorlib::System::Runtime::InteropServices::Marshal::GetCCW(object, reinterpret_cast<Il2CppReflectionType*>(type));
  801. }