il2cpp-codegen-mono.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978
  1. #pragma once
  2. #include "il2cpp-codegen-common.h"
  3. #include "il2cpp-mono-support.h"
  4. #include "mono-api.h"
  5. struct ProfilerMethodSentry
  6. {
  7. ProfilerMethodSentry(const RuntimeMethod* method)
  8. #if IL2CPP_ENABLE_PROFILER
  9. : m_method(method)
  10. #endif
  11. {
  12. NOT_IMPLEMENTED("Unity profiler hooks are not implemented yet for the libmonoruntime backend.");
  13. }
  14. ~ProfilerMethodSentry()
  15. {
  16. NOT_IMPLEMENTED("Unity profiler hooks are not implemented yet for the libmonoruntime backend.");
  17. }
  18. private:
  19. const RuntimeMethod* m_method;
  20. };
  21. struct StackTraceSentry
  22. {
  23. StackTraceSentry(RuntimeMethod* method) : m_method(method)
  24. {
  25. MonoStackFrameInfo frame_info;
  26. frame_info.method = method;
  27. frame_info.actual_method = method;
  28. frame_info.type = FRAME_TYPE_MANAGED;
  29. frame_info.managed = 1;
  30. frame_info.il_offset = 0;
  31. frame_info.native_offset = 0;
  32. frame_info.ji = (MonoJitInfo*)(void*)-1;
  33. mono::vm::StackTrace::PushFrame(frame_info);
  34. }
  35. ~StackTraceSentry()
  36. {
  37. mono::vm::StackTrace::PopFrame();
  38. }
  39. private:
  40. const RuntimeMethod* m_method;
  41. };
  42. #define IL2CPP_FAKE_BOX_SENTRY (MonoThreadsSync*)UINTPTR_MAX
  43. template<typename T>
  44. struct Il2CppFakeBox : RuntimeObject
  45. {
  46. T m_Value;
  47. Il2CppFakeBox(RuntimeClass* boxedType, T* value)
  48. {
  49. vtable = il2cpp_mono_class_vtable(g_MonoDomain, boxedType);
  50. synchronisation = IL2CPP_FAKE_BOX_SENTRY;
  51. m_Value = *value;
  52. }
  53. };
  54. inline bool il2cpp_codegen_is_fake_boxed_object(RuntimeObject* object)
  55. {
  56. return object->synchronisation == IL2CPP_FAKE_BOX_SENTRY;
  57. }
  58. // TODO: This file should contain all the functions and type declarations needed for the generated code.
  59. // Hopefully, we stop including everything in the generated code and know exactly what dependencies we have.
  60. // Note that all parameter and return types should match the generated types not the runtime types.
  61. // type registration
  62. inline String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  63. {
  64. return (String_t*)mono_string_new_utf16(g_MonoDomain, (const mono_unichar2*)str.Str(), (int32_t)str.Length());
  65. }
  66. inline NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame = NULL)
  67. {
  68. mono_raise_exception((RuntimeException*)ex);
  69. il2cpp_codegen_no_return();
  70. }
  71. inline void il2cpp_codegen_raise_execution_engine_exception_if_method_is_not_found(const RuntimeMethod* method)
  72. {
  73. il2cpp_mono_raise_execution_engine_exception_if_method_is_not_found(const_cast<RuntimeMethod*>(method));
  74. }
  75. inline void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method)
  76. {
  77. mono_raise_exception(mono_get_exception_execution_engine(mono_unity_method_get_name(method)));
  78. }
  79. inline Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  80. {
  81. return (Exception_t*)mono_get_exception_argument(param, msg);
  82. }
  83. inline Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param)
  84. {
  85. return (Exception_t*)mono_get_exception_argument_null(param);
  86. }
  87. inline Exception_t* il2cpp_codegen_get_overflow_exception()
  88. {
  89. return (Exception_t*)mono_get_exception_overflow();
  90. }
  91. inline Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg)
  92. {
  93. return (Exception_t*)mono_get_exception_not_supported(msg);
  94. }
  95. inline Exception_t* il2cpp_codegen_get_array_type_mismatch_exception()
  96. {
  97. return (Exception_t*)mono_get_exception_array_type_mismatch();
  98. }
  99. inline Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg)
  100. {
  101. return (Exception_t*)mono_get_exception_invalid_operation(msg);
  102. }
  103. inline Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  104. {
  105. return (Exception_t*)mono_unity_exception_get_marshal_directive(msg);
  106. }
  107. inline Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  108. {
  109. return (Exception_t*)mono_get_exception_missing_method(msg, "ctor");
  110. }
  111. inline Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception()
  112. {
  113. return (Exception_t*)mono_get_exception_not_supported(MAXIMUM_NESTED_GENERICS_EXCEPTION_MESSAGE);
  114. }
  115. inline RuntimeClass* il2cpp_codegen_object_class(RuntimeObject* obj)
  116. {
  117. return mono_object_get_class(obj);
  118. }
  119. // OpCode.IsInst
  120. inline RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType)
  121. {
  122. return mono_object_isinst(obj, targetType);
  123. }
  124. inline RuntimeObject* IsInstSealed(RuntimeObject *obj, RuntimeClass* targetType)
  125. {
  126. if (!obj)
  127. return NULL;
  128. // optimized version to compare sealed classes
  129. return mono_unity_object_isinst_sealed(obj, targetType);
  130. }
  131. inline RuntimeObject* IsInstClass(RuntimeObject *obj, RuntimeClass* targetType)
  132. {
  133. if (!obj)
  134. return NULL;
  135. return mono_unity_class_has_parent_unsafe(mono_object_get_class(obj), targetType) ? obj : NULL;
  136. }
  137. // OpCode.Castclass
  138. inline RuntimeObject* Castclass(RuntimeObject *obj, RuntimeClass* targetType)
  139. {
  140. if (!obj)
  141. return NULL;
  142. RuntimeObject* result = mono_object_isinst(obj, targetType);
  143. if (result)
  144. return result;
  145. mono_raise_exception(mono_get_exception_invalid_cast());
  146. return NULL;
  147. }
  148. inline RuntimeObject* CastclassSealed(RuntimeObject *obj, RuntimeClass* targetType)
  149. {
  150. if (!obj)
  151. return NULL;
  152. RuntimeObject* result = IsInstSealed(obj, targetType);
  153. if (result)
  154. return result;
  155. mono_raise_exception(mono_get_exception_invalid_cast());
  156. return NULL;
  157. }
  158. inline RuntimeObject* CastclassClass(RuntimeObject *obj, RuntimeClass* targetType)
  159. {
  160. if (!obj)
  161. return NULL;
  162. RuntimeObject* result = IsInstClass(obj, targetType);
  163. if (result)
  164. return result;
  165. mono_raise_exception(mono_get_exception_invalid_cast());
  166. return NULL;
  167. }
  168. // OpCode.Box
  169. inline RuntimeObject* Box(RuntimeClass* type, void* data)
  170. {
  171. return mono_value_box(g_MonoDomain, type, data);
  172. }
  173. // OpCode.UnBox
  174. inline void* UnBox(RuntimeObject* obj)
  175. {
  176. if (!obj)
  177. {
  178. mono_raise_exception(mono_get_exception_null_reference());
  179. il2cpp_codegen_no_return();
  180. }
  181. return mono_object_unbox(obj);
  182. }
  183. inline void* UnBox(RuntimeObject* obj, RuntimeClass* klass)
  184. {
  185. if (!obj)
  186. {
  187. mono_raise_exception(mono_get_exception_null_reference());
  188. il2cpp_codegen_no_return();
  189. }
  190. if (!mono_unity_object_check_box_cast(obj, klass))
  191. {
  192. mono_raise_exception(mono_get_exception_invalid_cast());
  193. il2cpp_codegen_no_return();
  194. }
  195. return mono_object_unbox(obj);
  196. }
  197. inline void UnBoxNullable(RuntimeObject* obj, RuntimeClass* klass, void* storage)
  198. {
  199. mono_unity_object_unbox_nullable(obj, klass, storage);
  200. }
  201. inline uint32_t il2cpp_codegen_sizeof(RuntimeClass* klass)
  202. {
  203. if (!mono_class_is_valuetype(klass))
  204. return sizeof(void*);
  205. return mono_class_instance_size(klass) - sizeof(RuntimeObject);
  206. }
  207. inline bool il2cpp_codegen_method_is_virtual(RuntimeMethod* method)
  208. {
  209. return method->slot != -1;
  210. }
  211. inline bool il2cpp_codegen_object_is_of_sealed_type(RuntimeObject* obj)
  212. {
  213. return obj != NULL && mono_class_is_sealed(mono_object_get_class(obj));
  214. }
  215. inline bool il2cpp_codegen_method_is_generic_instance(RuntimeMethod* method)
  216. {
  217. return unity_mono_method_is_generic(method);
  218. }
  219. inline bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method)
  220. {
  221. return MONO_CLASS_IS_INTERFACE(mono_method_get_class(method));
  222. }
  223. inline uint16_t il2cpp_codegen_method_get_slot(RuntimeMethod* method)
  224. {
  225. return method->slot;
  226. }
  227. inline RuntimeClass* il2cpp_codegen_method_get_declaring_type(RuntimeMethod* method)
  228. {
  229. return mono_method_get_class(method);
  230. }
  231. FORCE_INLINE const VirtualInvokeData il2cpp_codegen_get_virtual_invoke_data(RuntimeMethod* method, void* obj)
  232. {
  233. VirtualInvokeData invokeData;
  234. il2cpp_mono_get_virtual_invoke_data(method, obj, &invokeData);
  235. return invokeData;
  236. }
  237. FORCE_INLINE const VirtualInvokeData il2cpp_codegen_get_interface_invoke_data(RuntimeMethod* method, void* obj, RuntimeClass* declaringInterface)
  238. {
  239. VirtualInvokeData invokeData;
  240. il2cpp_mono_get_interface_invoke_data(method, obj, &invokeData);
  241. return invokeData;
  242. }
  243. FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method(const RuntimeMethod* method, const RuntimeObject* obj)
  244. {
  245. return il2cpp_mono_get_virtual_target_method(const_cast<RuntimeMethod*>(method), const_cast<RuntimeObject*>(obj));
  246. }
  247. FORCE_INLINE void il2cpp_codegen_get_generic_virtual_invoke_data(const RuntimeMethod* method, void* obj, VirtualInvokeData* invokeData)
  248. {
  249. il2cpp_mono_get_invoke_data(const_cast<RuntimeMethod*>(method), obj, invokeData);
  250. }
  251. FORCE_INLINE const RuntimeMethod* il2cpp_codegen_get_generic_interface_method(const RuntimeMethod* method, const RuntimeObject* obj)
  252. {
  253. return il2cpp_mono_get_virtual_target_method(const_cast<RuntimeMethod*>(method), const_cast<RuntimeObject*>(obj));
  254. }
  255. FORCE_INLINE void il2cpp_codegen_get_generic_interface_invoke_data(RuntimeMethod* method, void* obj, VirtualInvokeData* invokeData)
  256. {
  257. il2cpp_mono_get_invoke_data(method, obj, invokeData);
  258. }
  259. FORCE_INLINE void il2cpp_codegen_get_generic_interface_invoke_data(const RuntimeMethod* method, void* obj, VirtualInvokeData* invokeData)
  260. {
  261. il2cpp_codegen_get_generic_interface_invoke_data(const_cast<RuntimeMethod*>(method), obj, invokeData);
  262. }
  263. // OpCode.Ldtoken
  264. inline RuntimeClass* il2cpp_codegen_class_from_type(RuntimeType* type)
  265. {
  266. return mono_class_from_mono_type(type);
  267. }
  268. inline RuntimeClass* il2cpp_codegen_class_from_type(const RuntimeType *type)
  269. {
  270. return il2cpp_codegen_class_from_type(const_cast<RuntimeType*>(type));
  271. }
  272. template<typename T>
  273. inline T InterlockedCompareExchangeImpl(T* location, T value, T comparand)
  274. {
  275. return (T)mono_unity_object_compare_exchange((RuntimeObject**)location, (RuntimeObject*)value, (RuntimeObject*)comparand);
  276. }
  277. template<typename T>
  278. inline T InterlockedExchangeImpl(T* location, T value)
  279. {
  280. return (T)mono_unity_object_exchange((RuntimeObject**)location, (RuntimeObject*)value);
  281. }
  282. inline void ArrayGetGenericValueImpl(RuntimeArray* __this, int32_t pos, void* value)
  283. {
  284. int elementSize = mono_unity_array_get_element_size(__this);
  285. memcpy(value, mono_array_addr_with_size(__this, elementSize, pos), elementSize);
  286. }
  287. inline void ArraySetGenericValueImpl(RuntimeArray * __this, int32_t pos, void* value)
  288. {
  289. int elementSize = mono_unity_array_get_element_size(__this);
  290. memcpy(mono_array_addr_with_size(__this, elementSize, pos), value, elementSize);
  291. }
  292. inline RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length)
  293. {
  294. mono_class_init(arrayType);
  295. MonoError error;
  296. RuntimeArray *retVal = mono_array_new_specific_checked(il2cpp_mono_class_vtable(g_MonoDomain, arrayType), length, &error);
  297. RuntimeException *exc = mono_error_convert_to_exception(&error);
  298. if (exc)
  299. mono_raise_exception(exc);
  300. return retVal;
  301. }
  302. inline RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions)
  303. {
  304. MonoError error;
  305. RuntimeArray *retVal = mono_array_new_full_checked(g_MonoDomain, arrayType, dimensions, NULL, &error);
  306. RuntimeException *exc = mono_error_convert_to_exception(&error);
  307. if (exc)
  308. mono_raise_exception(exc);
  309. return retVal;
  310. }
  311. // Performance optimization as detailed here: http://blogs.msdn.com/b/clrcodegeneration/archive/2009/08/13/array-bounds-check-elimination-in-the-clr.aspx
  312. // Since array size is a signed int32_t, a single unsigned check can be performed to determine if index is less than array size.
  313. // Negative indices will map to a unsigned number greater than or equal to 2^31 which is larger than allowed for a valid array.
  314. #define IL2CPP_ARRAY_BOUNDS_CHECK(index, length) \
  315. do { \
  316. if (((uint32_t)(index)) >= ((uint32_t)length)) mono_raise_exception(mono_get_exception_index_out_of_range()); \
  317. } while (0)
  318. inline bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass)
  319. {
  320. return mono_class_is_assignable_from(klass, oklass);
  321. }
  322. inline RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass)
  323. {
  324. return mono_object_new(g_MonoDomain, klass);
  325. }
  326. inline Il2CppMethodPointer il2cpp_codegen_resolve_icall(const RuntimeMethod* icallMethod)
  327. {
  328. return (Il2CppMethodPointer)mono_lookup_internal_call(const_cast<RuntimeMethod*>(icallMethod));
  329. }
  330. template<typename FunctionPointerType>
  331. inline FunctionPointerType il2cpp_codegen_resolve_pinvoke(const RuntimeMethod* pinvokeMethod)
  332. {
  333. const char *exc_class, *exc_arg;
  334. FunctionPointerType result = reinterpret_cast<FunctionPointerType>(mono_lookup_pinvoke_call(const_cast<RuntimeMethod*>(pinvokeMethod), &exc_class, &exc_arg));
  335. if (exc_class)
  336. {
  337. mono_raise_exception(mono_exception_from_name_msg(mono_unity_image_get_mscorlib(), "System", exc_class, exc_arg));
  338. il2cpp_codegen_no_return();
  339. }
  340. return result;
  341. }
  342. template<typename T>
  343. inline T* il2cpp_codegen_marshal_allocate_array(size_t length)
  344. {
  345. MonoError unused;
  346. return (T*)mono_marshal_alloc((il2cpp_array_size_t)(sizeof(T) * length), &unused);
  347. }
  348. inline char* il2cpp_codegen_marshal_string(String_t* string)
  349. {
  350. return mono::vm::PlatformInvoke::MarshalCSharpStringToCppString((RuntimeString*)string);
  351. }
  352. inline void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters)
  353. {
  354. return mono::vm::PlatformInvoke::MarshalCSharpStringToCppStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  355. }
  356. inline Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  357. {
  358. return (Il2CppChar*)mono::vm::PlatformInvoke::MarshalCSharpStringToCppWString((RuntimeString*)string);
  359. }
  360. inline void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters)
  361. {
  362. return mono::vm::PlatformInvoke::MarshalCSharpStringToCppWStringFixed((RuntimeString*)string, (mono_unichar2*)buffer, numberOfCharacters);
  363. }
  364. inline Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string)
  365. {
  366. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  367. return NULL;
  368. }
  369. inline String_t* il2cpp_codegen_marshal_string_result(const char* value)
  370. {
  371. return (String_t*)mono::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value);
  372. }
  373. inline String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  374. {
  375. return (String_t*)mono::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult((const mono_unichar2*)value);
  376. }
  377. inline String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value)
  378. {
  379. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  380. return NULL;
  381. }
  382. inline void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value)
  383. {
  384. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  385. }
  386. inline char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder)
  387. {
  388. return mono::vm::PlatformInvoke::MarshalStringBuilder((RuntimeStringBuilder*)stringBuilder);
  389. }
  390. inline Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder)
  391. {
  392. return (Il2CppChar*)mono::vm::PlatformInvoke::MarshalWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  393. }
  394. inline void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer)
  395. {
  396. mono::vm::PlatformInvoke::MarshalStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  397. }
  398. inline void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer)
  399. {
  400. mono::vm::PlatformInvoke::MarshalWStringBuilderResult((RuntimeStringBuilder*)stringBuilder, (mono_unichar2*)buffer);
  401. }
  402. inline Il2CppHString il2cpp_codegen_create_hstring(String_t* str)
  403. {
  404. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  405. return NULL;
  406. }
  407. inline String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring)
  408. {
  409. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  410. return NULL;
  411. }
  412. inline void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring)
  413. {
  414. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  415. }
  416. inline void il2cpp_codegen_marshal_free(void* ptr)
  417. {
  418. mono_marshal_free(ptr);
  419. }
  420. inline Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d)
  421. {
  422. return (Il2CppMethodPointer)mono::vm::PlatformInvoke::MarshalDelegate((RuntimeDelegate*)d);
  423. }
  424. template<typename T>
  425. inline T* il2cpp_codegen_marshal_function_ptr_to_delegate(Il2CppMethodPointer functionPtr, RuntimeClass* delegateType)
  426. {
  427. return (T*)mono::vm::PlatformInvoke::MarshalFunctionPointerToDelegate(reinterpret_cast<void*>(functionPtr), delegateType);
  428. }
  429. inline void il2cpp_codegen_marshal_store_last_error()
  430. {
  431. mono_marshal_set_last_error();
  432. }
  433. class il2cpp_native_wrapper_vm_thread_attacher
  434. {
  435. public:
  436. il2cpp_native_wrapper_vm_thread_attacher() :
  437. _threadWasAttached(false)
  438. {
  439. if (!mono_thread_is_attached())
  440. {
  441. mono_thread_attach(mono_get_root_domain());
  442. _threadWasAttached = true;
  443. }
  444. }
  445. ~il2cpp_native_wrapper_vm_thread_attacher()
  446. {
  447. if (_threadWasAttached)
  448. mono_thread_detach(mono_thread_current());
  449. }
  450. private:
  451. bool _threadWasAttached;
  452. bool mono_thread_is_attached()
  453. {
  454. return mono_domain_get() != NULL;
  455. }
  456. };
  457. #if _DEBUG
  458. struct ScopedMarshallingAllocationCheck
  459. {
  460. };
  461. struct ScopedMarshalingAllocationClearer
  462. {
  463. };
  464. #endif
  465. inline void NullCheck(void* this_ptr)
  466. {
  467. if (this_ptr != NULL)
  468. return;
  469. mono_raise_exception(mono_get_exception_null_reference());
  470. }
  471. inline void DivideByZeroCheck(int64_t denominator)
  472. {
  473. if (denominator != 0)
  474. return;
  475. mono_raise_exception(mono_get_exception_divide_by_zero());
  476. }
  477. inline void il2cpp_codegen_initobj(void* value, size_t size)
  478. {
  479. memset(value, 0, size);
  480. }
  481. inline bool MethodIsStatic(const RuntimeMethod* method)
  482. {
  483. return mono_unity_method_is_static(const_cast<RuntimeMethod*>(method));
  484. }
  485. inline bool MethodHasParameters(const RuntimeMethod* method)
  486. {
  487. return mono_signature_get_param_count(mono_method_signature(const_cast<RuntimeMethod*>(method))) != 0;
  488. }
  489. //#define IL2CPP_RUNTIME_CLASS_INIT(klass) do { if((klass)->has_cctor && !(klass)->cctor_finished) il2cpp::vm::Runtime::ClassInit ((klass)); } while (0)
  490. #define IL2CPP_RUNTIME_CLASS_INIT(klass) RuntimeInit(klass)
  491. inline void* il2cpp_codegen_mono_class_rgctx(RuntimeClass* klass, Il2CppRGCTXDataType rgctxType, int rgctxIndex, bool useSharedVersion)
  492. {
  493. return il2cpp_mono_class_rgctx(klass, rgctxType, rgctxIndex, useSharedVersion);
  494. }
  495. inline void* il2cpp_codegen_mono_method_rgctx(RuntimeMethod* method, Il2CppRGCTXDataType rgctxType, int rgctxIndex, bool useSharedVersion)
  496. {
  497. return il2cpp_mono_method_rgctx(method, rgctxType, rgctxIndex, useSharedVersion);
  498. }
  499. inline void ArrayElementTypeCheck(RuntimeArray* array, void* value)
  500. {
  501. if (!value)
  502. return;
  503. RuntimeClass *aclass = mono_unity_array_get_class(array);
  504. RuntimeClass *eclass = mono_unity_class_get_element_class(aclass);
  505. RuntimeClass *oclass = mono_unity_object_get_class((RuntimeObject*)value);
  506. if (!mono_class_is_assignable_from(eclass, oclass))
  507. mono_raise_exception(mono_get_exception_array_type_mismatch());
  508. }
  509. inline const RuntimeMethod* GetVirtualMethodInfo(RuntimeObject* pThis, const RuntimeMethod* method)
  510. {
  511. if (!pThis)
  512. mono_raise_exception(mono_get_exception_null_reference());
  513. return mono_object_get_virtual_method(pThis, const_cast<RuntimeMethod*>(method));
  514. }
  515. inline const RuntimeMethod* GetInterfaceMethodInfo(RuntimeObject* pThis, RuntimeMethod *slot, RuntimeClass* declaringInterface)
  516. {
  517. if (!pThis)
  518. mono_raise_exception(mono_get_exception_null_reference());
  519. return mono_object_get_virtual_method(pThis, slot);
  520. }
  521. inline void il2cpp_codegen_memory_barrier()
  522. {
  523. mono_unity_memory_barrier();
  524. }
  525. inline void il2cpp_codegen_initialize_method(uint32_t index)
  526. {
  527. il2cpp_mono_initialize_method_metadata(index);
  528. }
  529. inline bool il2cpp_codegen_type_implements_virtual_method(RuntimeClass* type, RuntimeMethod *slot)
  530. {
  531. return mono_unity_method_get_class(slot) == type;
  532. }
  533. inline MethodBase_t* il2cpp_codegen_get_method_object(const RuntimeMethod* method)
  534. {
  535. if (unity_mono_method_is_inflated(const_cast<RuntimeMethod*>(method)))
  536. method = mono_unity_method_get_generic_definition(const_cast<RuntimeMethod*>(method));
  537. return (MethodBase_t*)mono_unity_method_get_object(const_cast<RuntimeMethod*>(method));
  538. }
  539. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, const char* assemblyName)
  540. {
  541. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, const RuntimeMethod*);
  542. MonoString* assemblyQualifiedTypeName = mono_unity_string_append_assembly_name_if_necessary((MonoString*)typeName, assemblyName);
  543. // 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.
  544. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, NULL);
  545. if (type == NULL)
  546. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, NULL);
  547. return type;
  548. }
  549. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, bool throwOnError, const char* assemblyName)
  550. {
  551. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, bool, const RuntimeMethod*);
  552. MonoString* assemblyQualifiedTypeName = mono_unity_string_append_assembly_name_if_necessary((MonoString*)typeName, assemblyName);
  553. // 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.
  554. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, throwOnError, NULL);
  555. if (type == NULL)
  556. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, throwOnError, NULL);
  557. return type;
  558. }
  559. inline Type_t* il2cpp_codegen_get_type(Il2CppMethodPointer getTypeFunction, String_t* typeName, bool throwOnError, bool ignoreCase, const char* assemblyName)
  560. {
  561. typedef Type_t* (*getTypeFuncType)(RuntimeObject*, String_t*, bool, bool, const RuntimeMethod*);
  562. MonoString* assemblyQualifiedTypeName = mono_unity_string_append_assembly_name_if_necessary((MonoString*)typeName, assemblyName);
  563. // 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.
  564. Type_t* type = ((getTypeFuncType)getTypeFunction)(NULL, (String_t*)assemblyQualifiedTypeName, throwOnError, ignoreCase, NULL);
  565. if (type == NULL)
  566. return ((getTypeFuncType)getTypeFunction)(NULL, typeName, throwOnError, ignoreCase, NULL);
  567. return type;
  568. }
  569. inline Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method)
  570. {
  571. return (Assembly_t*)mono_assembly_get_object(g_MonoDomain, mono_unity_class_get_assembly(mono_unity_method_get_class(method)));
  572. }
  573. // Atomic
  574. inline void* il2cpp_codegen_atomic_compare_exchange_pointer(void* volatile* dest, void* exchange, void* comparand)
  575. {
  576. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  577. return NULL;
  578. }
  579. template<typename T>
  580. inline T* il2cpp_codegen_atomic_compare_exchange_pointer(T* volatile* dest, T* newValue, T* oldValue)
  581. {
  582. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  583. return NULL;
  584. }
  585. // COM
  586. inline void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant)
  587. {
  588. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  589. }
  590. inline RuntimeObject* il2cpp_codegen_com_marshal_variant_result(Il2CppVariant* variant)
  591. {
  592. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  593. return NULL;
  594. }
  595. inline void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant)
  596. {
  597. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  598. }
  599. inline Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray)
  600. {
  601. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  602. return NULL;
  603. }
  604. inline RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray)
  605. {
  606. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  607. return NULL;
  608. }
  609. inline Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray)
  610. {
  611. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  612. return NULL;
  613. }
  614. inline RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray)
  615. {
  616. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  617. return NULL;
  618. }
  619. inline void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray)
  620. {
  621. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  622. }
  623. inline void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity)
  624. {
  625. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  626. }
  627. inline void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw)
  628. {
  629. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  630. }
  631. template<typename T>
  632. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iunknown(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  633. {
  634. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  635. return NULL;
  636. }
  637. template<typename T>
  638. inline T* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  639. {
  640. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  641. return NULL;
  642. }
  643. template<typename T>
  644. inline T* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  645. {
  646. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  647. return NULL;
  648. }
  649. inline void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw)
  650. {
  651. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  652. }
  653. template<typename InterfaceType>
  654. inline InterfaceType* il2cpp_codegen_com_get_or_create_ccw(RuntimeObject* obj)
  655. {
  656. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  657. return NULL;
  658. }
  659. inline intptr_t il2cpp_codegen_com_get_iunknown_for_object(RuntimeObject* obj)
  660. {
  661. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  662. return 0;
  663. }
  664. inline void il2cpp_codegen_com_raise_exception(il2cpp_hresult_t hr)
  665. {
  666. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  667. }
  668. inline void il2cpp_codegen_com_raise_exception_if_failed(il2cpp_hresult_t hr, bool defaultToCOMException)
  669. {
  670. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  671. }
  672. inline RuntimeException* il2cpp_codegen_com_get_exception(il2cpp_hresult_t hr, bool defaultToCOMException)
  673. {
  674. NOT_IMPLEMENTED("Not implemented yet.");
  675. return NULL;
  676. }
  677. inline RuntimeException* il2cpp_codegen_com_get_exception_for_invalid_iproperty_cast(RuntimeObject* value, const char* a, const char* b)
  678. {
  679. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  680. return NULL;
  681. }
  682. inline Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName)
  683. {
  684. NOT_IMPLEMENTED("COM is not yet supported with the libmonoruntime backend.");
  685. return NULL;
  686. }
  687. // delegate
  688. inline Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state)
  689. {
  690. return il2cpp_mono_delegate_begin_invoke(delegate, params, asyncCallback, state);
  691. }
  692. inline RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args)
  693. {
  694. return il2cpp_mono_delegate_end_invoke(asyncResult, out_args);
  695. }
  696. inline const Il2CppGenericInst* il2cpp_codegen_get_generic_class_inst(RuntimeClass* genericClass)
  697. {
  698. NOT_IMPLEMENTED("Windows runtime is not yet supported with the libmonoruntime backend.");
  699. return NULL;
  700. }
  701. inline RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst)
  702. {
  703. //return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, genericInst);
  704. NOT_IMPLEMENTED("Windows runtime is not yet supported with the libmonoruntime backend.");
  705. return NULL;
  706. }
  707. inline RuntimeAssembly* il2cpp_codegen_mono_corlib()
  708. {
  709. return mono_unity_assembly_get_mscorlib();
  710. }
  711. inline RuntimeClass* il2cpp_codegen_mono_class(AssemblyIndex assemblyIndex, uint32_t classToken)
  712. {
  713. return mono_class_get(mono_assembly_get_image(il2cpp_mono_assembly_from_index(assemblyIndex)), classToken);
  714. }
  715. inline RuntimeClass* il2cpp_codegen_mono_class(RuntimeAssembly* assembly, uint32_t classToken)
  716. {
  717. return mono_class_get(mono_assembly_get_image(assembly), classToken);
  718. }
  719. inline RuntimeMethod* il2cpp_codegen_mono_method(AssemblyIndex index, uint32_t methodToken)
  720. {
  721. return mono_get_method(mono_assembly_get_image(il2cpp_mono_assembly_from_index(index)), methodToken, NULL);
  722. }
  723. inline RuntimeMethod* il2cpp_codegen_mono_method(RuntimeAssembly* assembly, uint32_t methodToken)
  724. {
  725. return mono_get_method(mono_assembly_get_image(assembly), methodToken, NULL);
  726. }
  727. inline void* il2cpp_codegen_mono_get_static_field_address(RuntimeClass* klass, RuntimeField* field)
  728. {
  729. return il2cpp_mono_get_static_field_address(klass, field);
  730. }
  731. inline void* il2cpp_codegen_mono_get_thread_static_field_address(RuntimeClass* klass, RuntimeField* field)
  732. {
  733. return il2cpp_mono_get_thread_static_field_address(klass, field);
  734. }
  735. inline RuntimeField* il2cpp_codegen_mono_class_get_field(RuntimeClass* klass, uint32_t fieldToken)
  736. {
  737. return mono_class_get_field(klass, fieldToken);
  738. }
  739. inline Il2CppMethodPointer il2cpp_codegen_get_method_pointer(const RuntimeMethod* method)
  740. {
  741. MonoError unused;
  742. il2cpp_mono_method_initialize_function_pointers(const_cast<RuntimeMethod*>(method), &unused);
  743. return (Il2CppMethodPointer)mono_unity_method_get_method_pointer(const_cast<RuntimeMethod*>(method));
  744. }
  745. inline RuntimeType* il2cpp_codegen_method_return_type(const RuntimeMethod* method)
  746. {
  747. return mono_signature_get_return_type(mono_method_signature(const_cast<RuntimeMethod*>(method)));
  748. }
  749. inline int il2cpp_codegen_method_parameter_count(const RuntimeMethod* method)
  750. {
  751. return mono_signature_get_param_count(mono_method_signature(const_cast<RuntimeMethod*>(method)));
  752. }
  753. template<class T>
  754. T il2cpp_mono_cast_nullable_method_param(const RuntimeMethod *method, int index, void *value)
  755. {
  756. if (value)
  757. return *((T*)value);
  758. T retVal;
  759. RuntimeClass *klass = mono_unity_signature_get_class_for_param(mono_method_signature(const_cast<RuntimeMethod*>(method)), index);
  760. mono_nullable_init((uint8_t*)&retVal, NULL, klass);
  761. return retVal;
  762. }
  763. inline const RuntimeMethod* il2cpp_codegen_vtable_slot_method(const RuntimeClass* klass, RuntimeMethod* slot)
  764. {
  765. return slot;
  766. }
  767. inline Il2CppMethodPointer il2cpp_codegen_vtable_slot_method_pointer(const RuntimeClass* klass, RuntimeMethod* slot)
  768. {
  769. MonoError unused;
  770. il2cpp_mono_method_initialize_function_pointers(slot, &unused);
  771. return (Il2CppMethodPointer)mono_unity_method_get_method_pointer(slot);
  772. }
  773. inline bool il2cpp_codegen_is_import_or_windows_runtime(const RuntimeObject *object)
  774. {
  775. assert(0 && "Not implemented yet.");
  776. return false;
  777. }
  778. inline std::string il2cpp_codegen_format_exception(const RuntimeException* ex)
  779. {
  780. return il2cpp_mono_format_exception(ex);
  781. }
  782. inline intptr_t il2cpp_codegen_get_com_interface_for_object(RuntimeObject* object, Type_t* type)
  783. {
  784. assert(0 && "Not implemented yet.");
  785. return 0;
  786. }