il2cpp-codegen-il2cpp.cpp 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289
  1. #include <string>
  2. #include <stdarg.h>
  3. #include "il2cpp-config.h"
  4. #include "il2cpp-codegen.h"
  5. #include "utils/Exception.h"
  6. #if !RUNTIME_TINY
  7. #include "os/Atomic.h"
  8. #include "metadata/GenericMethod.h"
  9. #include "metadata/GenericMetadata.h"
  10. #include "gc/WriteBarrier.h"
  11. #include "vm/Array.h"
  12. #include "vm/CCW.h"
  13. #include "vm/COM.h"
  14. #include "vm/Class.h"
  15. #include "vm/Exception.h"
  16. #include "vm/Field.h"
  17. #include "vm/InternalCalls.h"
  18. #include "vm/LastError.h"
  19. #include "vm/MarshalAlloc.h"
  20. #include "vm/MetadataCache.h"
  21. #include "vm/Method.h"
  22. #include "vm/Object.h"
  23. #include "vm/PlatformInvoke.h"
  24. #include "vm/Profiler.h"
  25. #include "vm/RCW.h"
  26. #include "vm/Reflection.h"
  27. #include "vm/Runtime.h"
  28. #include "vm/StackTrace.h"
  29. #include "vm/String.h"
  30. #include "vm/Thread.h"
  31. #include "vm/ThreadPoolMs.h"
  32. #include "vm/Type.h"
  33. #include "vm/WindowsRuntime.h"
  34. #include "vm-utils/VmThreadUtils.h"
  35. void* il2cpp_codegen_atomic_compare_exchange_pointer(void** dest, void* exchange, void* comparand)
  36. {
  37. return il2cpp::os::Atomic::CompareExchangePointer(dest, exchange, comparand);
  38. }
  39. void il2cpp_codegen_marshal_store_last_error()
  40. {
  41. il2cpp::vm::LastError::StoreLastError();
  42. }
  43. Il2CppAsyncResult* il2cpp_codegen_delegate_begin_invoke(RuntimeDelegate* delegate, void** params, RuntimeDelegate* asyncCallback, RuntimeObject* state)
  44. {
  45. return il2cpp::vm::ThreadPoolMs::DelegateBeginInvoke(delegate, params, asyncCallback, state);
  46. }
  47. RuntimeObject* il2cpp_codegen_delegate_end_invoke(Il2CppAsyncResult* asyncResult, void **out_args)
  48. {
  49. return il2cpp::vm::ThreadPoolMs::DelegateEndInvoke(asyncResult, out_args);
  50. }
  51. void il2cpp_codegen_set_closed_delegate_invoke(RuntimeObject* delegate, RuntimeObject* target, void* methodPtr)
  52. {
  53. IL2CPP_ASSERT(delegate->klass->parent == il2cpp_defaults.multicastdelegate_class);
  54. il2cpp::vm::Type::SetClosedDelegateInvokeMethod((RuntimeDelegate*)delegate, target, (Il2CppMethodPointer)methodPtr);
  55. }
  56. Il2CppMethodPointer il2cpp_codegen_resolve_icall(const char* name)
  57. {
  58. Il2CppMethodPointer method = il2cpp::vm::InternalCalls::Resolve(name);
  59. if (!method)
  60. {
  61. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(name));
  62. }
  63. return method;
  64. }
  65. Type_t* il2cpp_codegen_type_get_object(const RuntimeType* type)
  66. {
  67. return (Type_t*)il2cpp::vm::Reflection::GetTypeObject(type);
  68. }
  69. MethodBase_t* il2cpp_codegen_get_method_object_internal(const RuntimeMethod* method, RuntimeClass* refclass)
  70. {
  71. return (MethodBase_t*)il2cpp::vm::Reflection::GetMethodObject(method, method->klass);
  72. }
  73. Assembly_t* il2cpp_codegen_get_executing_assembly(const RuntimeMethod* method)
  74. {
  75. return (Assembly_t*)il2cpp::vm::Reflection::GetAssemblyObject(method->klass->image->assembly);
  76. }
  77. void il2cpp_codegen_register(const Il2CppCodeRegistration* const codeRegistration, const Il2CppMetadataRegistration* const metadataRegistration, const Il2CppCodeGenOptions* const codeGenOptions)
  78. {
  79. il2cpp::vm::MetadataCache::Register(codeRegistration, metadataRegistration, codeGenOptions);
  80. }
  81. extern MetadataInitializerCleanupFunc g_ClearMethodMetadataInitializedFlags;
  82. void il2cpp_codegen_register_metadata_initialized_cleanup(MetadataInitializerCleanupFunc cleanup)
  83. {
  84. g_ClearMethodMetadataInitializedFlags = cleanup;
  85. }
  86. void il2cpp_codegen_initialize_runtime_metadata(uintptr_t* metadataPointer)
  87. {
  88. il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(metadataPointer);
  89. // We don't need a memory barrier here, InitializeRuntimeMetadata already has one
  90. // What we need is a barrier before setting s_Il2CppMethodInitialized = true in the generated code
  91. // but adding that to every function increases code size, so instead we rely on this function
  92. // being called before s_Il2CppCodeRegistrationInitialized is set to true
  93. il2cpp::os::Atomic::FullMemoryBarrier();
  94. }
  95. void* il2cpp_codegen_initialize_runtime_metadata_inline(uintptr_t* metadataPointer)
  96. {
  97. return il2cpp::vm::MetadataCache::InitializeRuntimeMetadata(metadataPointer);
  98. }
  99. const RuntimeClass* il2cpp_codegen_get_generic_type_definition(const RuntimeClass* klass)
  100. {
  101. IL2CPP_ASSERT(klass->generic_class);
  102. return il2cpp::vm::Class::FromIl2CppType(klass->generic_class->type);
  103. }
  104. const RuntimeMethod* il2cpp_codegen_get_generic_method_definition(const RuntimeMethod* method)
  105. {
  106. return il2cpp::vm::MetadataCache::GetGenericMethodDefinition(method);
  107. }
  108. const RuntimeMethod* il2cpp_codegen_get_generic_instance_method_from_method_definition(RuntimeClass* genericInstanceClass, const RuntimeMethod* methodDefinition)
  109. {
  110. return il2cpp::vm::Class::GetGenericInstanceMethodFromDefintion(genericInstanceClass, methodDefinition);
  111. }
  112. void* il2cpp_codegen_get_thread_static_data(RuntimeClass* klass)
  113. {
  114. return il2cpp::vm::Thread::GetThreadStaticData(klass->thread_static_fields_offset);
  115. }
  116. void il2cpp_codegen_assert_field_size(RuntimeField* field, size_t size)
  117. {
  118. IL2CPP_ASSERT(size == il2cpp_codegen_sizeof(InitializedTypeInfo(il2cpp::vm::Class::FromIl2CppType(field->type))));
  119. }
  120. void* il2cpp_codegen_get_instance_field_data_pointer(void* instance, RuntimeField* field)
  121. {
  122. return il2cpp::vm::Field::GetInstanceFieldDataPointer(instance, field);
  123. }
  124. void il2cpp_codegen_write_instance_field_data(void* instance, RuntimeField* field, void* data, uint32_t size)
  125. {
  126. il2cpp_codegen_assert_field_size(field, size);
  127. IL2CPP_ASSERT(il2cpp::vm::Field::IsInstance(field));
  128. void* fieldPointer = il2cpp_codegen_get_instance_field_data_pointer(instance, field);
  129. il2cpp_codegen_memcpy(fieldPointer, data, size);
  130. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  131. }
  132. void* il2cpp_codegen_get_static_field_data_pointer(RuntimeField* field)
  133. {
  134. IL2CPP_ASSERT(il2cpp::vm::Field::IsNormalStatic(field));
  135. return ((uint8_t*)field->parent->static_fields) + field->offset;
  136. }
  137. void il2cpp_codegen_write_static_field_data(RuntimeField* field, void* data, uint32_t size)
  138. {
  139. il2cpp_codegen_assert_field_size(field, size);
  140. IL2CPP_ASSERT(il2cpp::vm::Field::IsNormalStatic(field));
  141. void* fieldPointer = il2cpp_codegen_get_static_field_data_pointer(field);
  142. il2cpp_codegen_memcpy(fieldPointer, data, size);
  143. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  144. }
  145. void* il2cpp_codegen_get_thread_static_field_data_pointer(RuntimeField* field)
  146. {
  147. IL2CPP_ASSERT(il2cpp::vm::Field::IsThreadStatic(field));
  148. int threadStaticFieldOffset = il2cpp::vm::MetadataCache::GetThreadLocalStaticOffsetForField(field);
  149. void* threadStaticData = il2cpp::vm::Thread::GetThreadStaticData(field->parent->thread_static_fields_offset);
  150. return static_cast<uint8_t*>(threadStaticData) + threadStaticFieldOffset;
  151. }
  152. void il2cpp_codegen_write_thread_static_field_data(RuntimeField* field, void* data, uint32_t size)
  153. {
  154. il2cpp_codegen_assert_field_size(field, size);
  155. IL2CPP_ASSERT(il2cpp::vm::Field::IsThreadStatic(field));
  156. void* fieldPointer = il2cpp_codegen_get_thread_static_field_data_pointer(field);
  157. il2cpp_codegen_memcpy(fieldPointer, data, size);
  158. Il2CppCodeGenWriteBarrierForType(field->type, (void**)fieldPointer, NULL);
  159. }
  160. void il2cpp_codegen_memory_barrier()
  161. {
  162. il2cpp::vm::Thread::FullMemoryBarrier();
  163. }
  164. void SetGenericValueImpl(RuntimeArray* thisPtr, int32_t pos, void* value)
  165. {
  166. il2cpp_array_setrefwithsize(thisPtr, thisPtr->klass->element_size, pos, value);
  167. }
  168. RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length)
  169. {
  170. return il2cpp::vm::Array::NewSpecific(arrayType, length);
  171. }
  172. RuntimeArray* GenArrayNew(RuntimeClass* arrayType, il2cpp_array_size_t* dimensions)
  173. {
  174. return il2cpp::vm::Array::NewFull(arrayType, dimensions, NULL);
  175. }
  176. bool il2cpp_codegen_method_is_generic_instance_method(RuntimeMethod* method)
  177. {
  178. return il2cpp::vm::Method::IsGenericInstanceMethod(method);
  179. }
  180. bool il2cpp_codegen_method_is_generic_instance(RuntimeClass* klass)
  181. {
  182. return il2cpp::vm::Class::IsInflated(klass);
  183. }
  184. RuntimeClass* il2cpp_codegen_method_get_declaring_type(const RuntimeMethod* method)
  185. {
  186. return il2cpp::vm::Method::GetClass(method);
  187. }
  188. bool MethodIsStatic(const RuntimeMethod* method)
  189. {
  190. return !il2cpp::vm::Method::IsInstance(method);
  191. }
  192. bool MethodHasParameters(const RuntimeMethod* method)
  193. {
  194. return il2cpp::vm::Method::GetParamCount(method) != 0;
  195. }
  196. NORETURN void il2cpp_codegen_raise_profile_exception(const RuntimeMethod* method)
  197. {
  198. std::string methodName = il2cpp::vm::Method::GetFullName(method);
  199. il2cpp_codegen_raise_exception(il2cpp_codegen_get_not_supported_exception(methodName.c_str()));
  200. }
  201. const RuntimeMethod* il2cpp_codegen_get_generic_virtual_method_internal(const RuntimeMethod* vtableSlotMethod, const RuntimeMethod* genericVirtualMethod)
  202. {
  203. return il2cpp::metadata::GenericMethod::GetGenericVirtualMethod(vtableSlotMethod, genericVirtualMethod);
  204. }
  205. void il2cpp_codegen_runtime_class_init(RuntimeClass* klass)
  206. {
  207. il2cpp::vm::Runtime::ClassInit(klass);
  208. }
  209. void il2cpp_codegen_raise_execution_engine_exception(const RuntimeMethod* method)
  210. {
  211. il2cpp::vm::Runtime::AlwaysRaiseExecutionEngineException(method);
  212. }
  213. void il2cpp_codegen_raise_execution_engine_exception_missing_virtual(const RuntimeMethod* method)
  214. {
  215. il2cpp::vm::Runtime::AlwaysRaiseExecutionEngineExceptionOnVirtualCall(method);
  216. }
  217. RuntimeObject* IsInst(RuntimeObject *obj, RuntimeClass* targetType)
  218. {
  219. return il2cpp::vm::Object::IsInst(obj, targetType);
  220. }
  221. RuntimeObject* Box(RuntimeClass* type, void* data)
  222. {
  223. return il2cpp::vm::Object::Box(type, data);
  224. }
  225. void* Unbox_internal(Il2CppObject* obj)
  226. {
  227. return il2cpp::vm::Object::Unbox(obj);
  228. }
  229. void UnBoxNullable_internal(RuntimeObject* obj, RuntimeClass* nullableClass, void* storage)
  230. {
  231. il2cpp::vm::Object::UnboxNullable(obj, nullableClass, storage);
  232. }
  233. void* UnBox_Any(RuntimeObject* obj, RuntimeClass* expectedBoxedClass, void* unboxStorage)
  234. {
  235. IL2CPP_ASSERT(unboxStorage != NULL);
  236. // We assume unboxStorage is on the stack, if not we'll need a write barrier
  237. IL2CPP_ASSERT_STACK_PTR(unboxStorage);
  238. if (il2cpp::vm::Class::IsValuetype(expectedBoxedClass))
  239. {
  240. if (il2cpp::vm::Class::IsNullable(expectedBoxedClass))
  241. {
  242. UnBoxNullable(obj, expectedBoxedClass, unboxStorage);
  243. return unboxStorage;
  244. }
  245. return UnBox(obj, expectedBoxedClass);
  246. }
  247. // Use unboxStorage to return a pointer to obj
  248. // This keeps the return value of UnBox_Any consistent; it always returns a pointer to the data we want
  249. // This saves a runtime check on the class type
  250. *((void**)unboxStorage) = Castclass(obj, expectedBoxedClass);
  251. return unboxStorage;
  252. }
  253. bool il2cpp_codegen_would_box_to_non_null(RuntimeClass* klass, void* objBuffer)
  254. {
  255. if (il2cpp::vm::Class::IsValuetype(klass))
  256. {
  257. if (il2cpp::vm::Class::IsNullable(klass))
  258. return il2cpp::vm::Object::NullableHasValue(klass, objBuffer);
  259. return true;
  260. }
  261. return *(void**)objBuffer != NULL;
  262. }
  263. RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass)
  264. {
  265. return il2cpp::vm::Object::New(klass);
  266. }
  267. void* il2cpp_codegen_marshal_allocate(size_t size)
  268. {
  269. return il2cpp::vm::MarshalAlloc::Allocate(size);
  270. }
  271. #if _DEBUG
  272. void il2cpp_codegen_marshal_allocate_push_allocation_frame()
  273. {
  274. il2cpp::vm::MarshalAlloc::PushAllocationFrame();
  275. }
  276. void il2cpp_codegen_marshal_allocate_pop_allocation_frame()
  277. {
  278. il2cpp::vm::MarshalAlloc::PopAllocationFrame();
  279. }
  280. bool il2cpp_codegen_marshal_allocate_has_unfreed_allocations()
  281. {
  282. return il2cpp::vm::MarshalAlloc::HasUnfreedAllocations();
  283. }
  284. void il2cpp_codegen_marshal_allocate_clear_all_tracked_allocations()
  285. {
  286. il2cpp::vm::MarshalAlloc::ClearAllTrackedAllocations();
  287. }
  288. #endif
  289. #if IL2CPP_ENABLE_PROFILER
  290. void il2cpp_codegen_profiler_method_enter(const RuntimeMethod* method)
  291. {
  292. il2cpp::vm::Profiler::MethodEnter(method);
  293. }
  294. void il2cpp_codegen_profiler_method_exit(const RuntimeMethod* method)
  295. {
  296. il2cpp::vm::Profiler::MethodExit(method);
  297. }
  298. #endif
  299. NORETURN void il2cpp_codegen_raise_exception(Exception_t *ex, MethodInfo* lastManagedFrame)
  300. {
  301. RuntimeException* exc = (RuntimeException*)ex;
  302. #if !IL2CPP_TINY
  303. IL2CPP_OBJECT_SETREF_NULL(exc, trace_ips);
  304. IL2CPP_OBJECT_SETREF_NULL(exc, stack_trace);
  305. #endif
  306. il2cpp::vm::Exception::Raise(exc, lastManagedFrame);
  307. }
  308. NORETURN void il2cpp_codegen_rethrow_exception(Exception_t *ex)
  309. {
  310. il2cpp::vm::Exception::Rethrow((RuntimeException*)ex);
  311. }
  312. NORETURN void il2cpp_codegen_raise_exception(il2cpp_hresult_t hresult, bool defaultToCOMException)
  313. {
  314. il2cpp::vm::Exception::Raise(hresult, defaultToCOMException);
  315. }
  316. NORETURN void il2cpp_codegen_raise_out_of_memory_exception()
  317. {
  318. il2cpp::vm::Exception::RaiseOutOfMemoryException();
  319. }
  320. NORETURN void il2cpp_codegen_raise_null_reference_exception()
  321. {
  322. il2cpp::vm::Exception::RaiseNullReferenceException();
  323. }
  324. NORETURN void il2cpp_codegen_raise_divide_by_zero_exception()
  325. {
  326. il2cpp::vm::Exception::RaiseDivideByZeroException();
  327. }
  328. NORETURN void il2cpp_codegen_raise_index_out_of_range_exception()
  329. {
  330. il2cpp::vm::Exception::RaiseIndexOutOfRangeException();
  331. }
  332. NORETURN void il2cpp_codegen_raise_overflow_exception(const RuntimeMethod* method)
  333. {
  334. IL2CPP_RAISE_MANAGED_EXCEPTION(il2cpp_codegen_get_overflow_exception(), method);
  335. }
  336. Exception_t* il2cpp_codegen_get_argument_exception(const char* param, const char* msg)
  337. {
  338. return (Exception_t*)il2cpp::vm::Exception::GetArgumentException(param, msg);
  339. }
  340. Exception_t* il2cpp_codegen_get_argument_null_exception(const char* param)
  341. {
  342. return (Exception_t*)il2cpp::vm::Exception::GetArgumentNullException(param);
  343. }
  344. Exception_t* il2cpp_codegen_get_overflow_exception()
  345. {
  346. return (Exception_t*)il2cpp::vm::Exception::GetOverflowException("Arithmetic operation resulted in an overflow.");
  347. }
  348. Exception_t* il2cpp_codegen_get_not_supported_exception(const char* msg)
  349. {
  350. return (Exception_t*)il2cpp::vm::Exception::GetNotSupportedException(msg);
  351. }
  352. Exception_t* il2cpp_codegen_get_array_type_mismatch_exception()
  353. {
  354. return (Exception_t*)il2cpp::vm::Exception::GetArrayTypeMismatchException();
  355. }
  356. Exception_t* il2cpp_codegen_get_invalid_cast_exception(const char* msg)
  357. {
  358. return (Exception_t*)il2cpp::vm::Exception::GetInvalidCastException(msg);
  359. }
  360. Exception_t* il2cpp_codegen_get_invalid_operation_exception(const char* msg)
  361. {
  362. return (Exception_t*)il2cpp::vm::Exception::GetInvalidOperationException(msg);
  363. }
  364. Exception_t* il2cpp_codegen_get_marshal_directive_exception(const char* msg)
  365. {
  366. return (Exception_t*)il2cpp::vm::Exception::GetMarshalDirectiveException(msg);
  367. }
  368. Exception_t* il2cpp_codegen_get_missing_method_exception(const char* msg)
  369. {
  370. return (Exception_t*)il2cpp::vm::Exception::GetMissingMethodException(msg);
  371. }
  372. Exception_t* il2cpp_codegen_get_maximum_nested_generics_exception()
  373. {
  374. return (Exception_t*)il2cpp::vm::Exception::GetMaximumNestedGenericsException();
  375. }
  376. Exception_t* il2cpp_codegen_get_engine_execution_exception(const char* msg)
  377. {
  378. return (Exception_t*)il2cpp::vm::Exception::GetExecutionEngineException(msg);
  379. }
  380. Exception_t* il2cpp_codegen_get_index_out_of_range_exception()
  381. {
  382. return (Exception_t*)il2cpp::vm::Exception::GetIndexOutOfRangeException();
  383. }
  384. Exception_t* il2cpp_codegen_get_exception(il2cpp_hresult_t hresult, bool defaultToCOMException)
  385. {
  386. return (Exception_t*)il2cpp::vm::Exception::Get(hresult, defaultToCOMException);
  387. }
  388. void il2cpp_codegen_store_exception_info(RuntimeException* ex, String_t* exceptionString)
  389. {
  390. il2cpp::vm::Exception::StoreExceptionInfo(ex, reinterpret_cast<RuntimeString*>(exceptionString));
  391. }
  392. void il2cpp_codegen_com_marshal_variant(RuntimeObject* obj, Il2CppVariant* variant)
  393. {
  394. #if IL2CPP_TRIM_COM
  395. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_variant);
  396. #else
  397. il2cpp::vm::COM::MarshalVariant(obj, variant);
  398. #endif
  399. }
  400. RuntimeObject* il2cpp_codegen_com_marshal_variant_result(const Il2CppVariant* variant)
  401. {
  402. #if IL2CPP_TRIM_COM
  403. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_variant_result);
  404. return NULL;
  405. #else
  406. return il2cpp::vm::COM::MarshalVariantResult(variant);
  407. #endif
  408. }
  409. void il2cpp_codegen_com_destroy_variant(Il2CppVariant* variant)
  410. {
  411. #if IL2CPP_TRIM_COM
  412. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_destroy_variant);
  413. #else
  414. il2cpp::vm::COM::DestroyVariant(variant);
  415. #endif
  416. }
  417. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array(Il2CppChar type, RuntimeArray* managedArray)
  418. {
  419. #if IL2CPP_TRIM_COM
  420. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_safe_array);
  421. return NULL;
  422. #else
  423. return il2cpp::vm::COM::MarshalSafeArray(type, managedArray);
  424. #endif
  425. }
  426. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_result(Il2CppChar variantType, RuntimeClass* type, Il2CppSafeArray* safeArray)
  427. {
  428. #if IL2CPP_TRIM_COM
  429. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_safe_array_result);
  430. return NULL;
  431. #else
  432. return il2cpp::vm::COM::MarshalSafeArrayResult(variantType, type, safeArray);
  433. #endif
  434. }
  435. Il2CppSafeArray* il2cpp_codegen_com_marshal_safe_array_bstring(RuntimeArray* managedArray)
  436. {
  437. #if IL2CPP_TRIM_COM
  438. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_safe_array_bstring);
  439. return NULL;
  440. #else
  441. return il2cpp::vm::COM::MarshalSafeArrayBString(managedArray);
  442. #endif
  443. }
  444. RuntimeArray* il2cpp_codegen_com_marshal_safe_array_bstring_result(RuntimeClass* type, Il2CppSafeArray* safeArray)
  445. {
  446. #if IL2CPP_TRIM_COM
  447. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_marshal_safe_array_bstring_result);
  448. return NULL;
  449. #else
  450. return il2cpp::vm::COM::MarshalSafeArrayBStringResult(type, safeArray);
  451. #endif
  452. }
  453. void il2cpp_codegen_com_destroy_safe_array(Il2CppSafeArray* safeArray)
  454. {
  455. #if IL2CPP_TRIM_COM
  456. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_destroy_safe_array);
  457. #else
  458. il2cpp::vm::COM::DestroySafeArray(safeArray);
  459. #endif
  460. }
  461. void il2cpp_codegen_com_create_instance(const Il2CppGuid& clsid, Il2CppIUnknown** identity)
  462. {
  463. #if IL2CPP_TRIM_COM
  464. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_create_instance);
  465. #else
  466. il2cpp::vm::COM::CreateInstance(clsid, identity);
  467. #endif
  468. }
  469. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(const char* fromType, const char* toType)
  470. {
  471. #if IL2CPP_TRIM_COM
  472. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_handle_invalid_iproperty_conversion);
  473. return 0;
  474. #else
  475. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(fromType, toType);
  476. #endif
  477. }
  478. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_iproperty_conversion(RuntimeObject* value, const char* fromType, const char* toType)
  479. {
  480. #if IL2CPP_TRIM_COM
  481. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_handle_invalid_iproperty_conversion);
  482. return 0;
  483. #else
  484. return il2cpp::vm::CCW::HandleInvalidIPropertyConversion(value, fromType, toType);
  485. #endif
  486. }
  487. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
  488. {
  489. #if IL2CPP_TRIM_COM
  490. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion);
  491. return 0;
  492. #else
  493. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(fromArrayType, fromElementType, toElementType, index);
  494. #endif
  495. }
  496. il2cpp_hresult_t il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion(RuntimeObject* value, const char* fromArrayType, const char* fromElementType, const char* toElementType, il2cpp_array_size_t index)
  497. {
  498. #if IL2CPP_TRIM_COM
  499. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_handle_invalid_ipropertyarray_conversion);
  500. return 0;
  501. #else
  502. return il2cpp::vm::CCW::HandleInvalidIPropertyArrayConversion(value, fromArrayType, fromElementType, toElementType, index);
  503. #endif
  504. }
  505. Il2CppIUnknown* il2cpp_codegen_com_get_or_create_ccw_internal(RuntimeObject* obj, const Il2CppGuid& iid)
  506. {
  507. #if IL2CPP_TRIM_COM
  508. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_ccw_internal);
  509. return NULL;
  510. #else
  511. return il2cpp::vm::CCW::GetOrCreate(obj, iid);
  512. #endif
  513. }
  514. Il2CppObject* il2cpp_codegen_com_unpack_ccw(Il2CppIUnknown* obj)
  515. {
  516. #if IL2CPP_TRIM_COM
  517. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_unpack_ccw);
  518. return NULL;
  519. #else
  520. return il2cpp::vm::CCW::Unpack(obj);
  521. #endif
  522. }
  523. void il2cpp_codegen_com_register_rcw(Il2CppComObject* rcw)
  524. {
  525. #if IL2CPP_TRIM_COM
  526. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_register_rcw);
  527. #else
  528. il2cpp::vm::RCW::Register(rcw);
  529. #endif
  530. }
  531. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal(Il2CppIUnknown* unknown, RuntimeClass* fallbackClass)
  532. {
  533. #if IL2CPP_TRIM_COM
  534. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_from_iunknown_internal);
  535. return NULL;
  536. #else
  537. return il2cpp::vm::RCW::GetOrCreateFromIUnknown(unknown, fallbackClass);
  538. #endif
  539. }
  540. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal(Il2CppIInspectable* unknown, RuntimeClass* fallbackClass)
  541. {
  542. #if IL2CPP_TRIM_COM
  543. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_from_iinspectable_internal);
  544. return NULL;
  545. #else
  546. return il2cpp::vm::RCW::GetOrCreateFromIInspectable(unknown, fallbackClass);
  547. #endif
  548. }
  549. RuntimeObject* il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal(Il2CppIUnknown* unknown, RuntimeClass* objectClass)
  550. {
  551. #if IL2CPP_TRIM_COM
  552. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_get_or_create_rcw_for_sealed_class_internal);
  553. return NULL;
  554. #else
  555. return il2cpp::vm::RCW::GetOrCreateForSealedClass(unknown, objectClass);
  556. #endif
  557. }
  558. Il2CppIUnknown* il2cpp_codegen_com_query_interface_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  559. {
  560. #if IL2CPP_TRIM_COM
  561. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_query_interface_internal);
  562. return NULL;
  563. #else
  564. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<true>(rcw, guid);
  565. #endif
  566. }
  567. Il2CppIUnknown* il2cpp_codegen_com_query_interface_no_throw_internal(Il2CppComObject* rcw, const Il2CppGuid& guid)
  568. {
  569. #if IL2CPP_TRIM_COM
  570. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_query_interface_no_throw_internal);
  571. return NULL;
  572. #else
  573. return il2cpp::vm::RCW::QueryInterfaceNoAddRef<false>(rcw, guid);
  574. #endif
  575. }
  576. void il2cpp_codegen_com_cache_queried_interface(Il2CppComObject* rcw, const Il2CppGuid& iid, Il2CppIUnknown* queriedInterface)
  577. {
  578. #if IL2CPP_TRIM_COM
  579. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_com_cache_queried_interface);
  580. #else
  581. if (il2cpp::vm::RCW::CacheQueriedInterface(rcw, iid, queriedInterface))
  582. queriedInterface->AddRef();
  583. #endif
  584. }
  585. void il2cpp_codegen_il2cpp_com_object_cleanup(Il2CppComObject* rcw)
  586. {
  587. #if IL2CPP_TRIM_COM
  588. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_il2cpp_com_object_cleanup);
  589. #else
  590. il2cpp::vm::RCW::Cleanup(rcw);
  591. #endif
  592. }
  593. String_t* il2cpp_codegen_string_new_wrapper(const char* str)
  594. {
  595. return (String_t*)il2cpp::vm::String::NewWrapper(str);
  596. }
  597. String_t* il2cpp_codegen_string_new_utf16(const il2cpp::utils::StringView<Il2CppChar>& str)
  598. {
  599. return (String_t*)il2cpp::vm::String::NewUtf16(str.Str(), static_cast<int32_t>(str.Length()));
  600. }
  601. RuntimeString* il2cpp_codegen_type_append_assembly_name_if_necessary(RuntimeString* typeName, const RuntimeMethod* callingMethod)
  602. {
  603. return il2cpp::vm::Type::AppendAssemblyNameIfNecessary(typeName, callingMethod);
  604. }
  605. Type_t* il2cpp_codegen_get_type(String_t* typeName, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  606. {
  607. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  608. // 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.
  609. Il2CppException* exc = NULL;
  610. void* params[] = {assemblyQualifiedTypeName};
  611. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  612. if (exc)
  613. il2cpp::vm::Exception::Raise(exc);
  614. if (type == NULL)
  615. {
  616. params[0] = typeName;
  617. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  618. if (exc)
  619. il2cpp::vm::Exception::Raise(exc);
  620. }
  621. return type;
  622. }
  623. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, const RuntimeMethod* getTypeMethod, const RuntimeMethod* callingMethod)
  624. {
  625. typedef Type_t* (*getTypeFuncType)(String_t*, bool);
  626. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  627. // 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.
  628. Il2CppException* exc = NULL;
  629. void* params[] = {assemblyQualifiedTypeName, &throwOnError};
  630. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  631. if (exc)
  632. il2cpp::vm::Exception::Raise(exc);
  633. if (type == NULL)
  634. {
  635. params[0] = typeName;
  636. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  637. if (exc)
  638. il2cpp::vm::Exception::Raise(exc);
  639. }
  640. return type;
  641. }
  642. Type_t* il2cpp_codegen_get_type(String_t* typeName, bool throwOnError, bool ignoreCase, const RuntimeMethod* getTypeMethod , const RuntimeMethod* callingMethod)
  643. {
  644. typedef Type_t* (*getTypeFuncType)(String_t*, bool, bool);
  645. RuntimeString* assemblyQualifiedTypeName = il2cpp_codegen_type_append_assembly_name_if_necessary((RuntimeString*)typeName, callingMethod);
  646. // 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.
  647. Il2CppException* exc = NULL;
  648. void* params[] = {assemblyQualifiedTypeName, &throwOnError, &ignoreCase};
  649. Type_t* type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  650. if (exc)
  651. il2cpp::vm::Exception::Raise(exc);
  652. if (type == NULL)
  653. {
  654. params[0] = typeName;
  655. type = (Type_t*)il2cpp::vm::Runtime::Invoke(getTypeMethod, NULL, params, &exc);
  656. if (exc)
  657. il2cpp::vm::Exception::Raise(exc);
  658. }
  659. return type;
  660. }
  661. NORETURN void RaiseInvalidCastException(RuntimeObject* obj, RuntimeClass* targetType)
  662. {
  663. std::string exceptionMessage = il2cpp::utils::Exception::FormatInvalidCastException(obj->klass->element_class, targetType);
  664. Exception_t* exception = il2cpp_codegen_get_invalid_cast_exception(exceptionMessage.c_str());
  665. il2cpp_codegen_raise_exception(exception);
  666. }
  667. bool il2cpp_codegen_method_is_interface_method(RuntimeMethod* method)
  668. {
  669. return il2cpp::vm::Class::IsInterface(il2cpp_codegen_method_get_declaring_type(method));
  670. }
  671. bool il2cpp_codegen_class_is_assignable_from(RuntimeClass *klass, RuntimeClass *oklass)
  672. {
  673. return il2cpp::vm::Class::IsAssignableFrom(klass, oklass);
  674. }
  675. bool il2cpp_codegen_class_is_nullable(RuntimeClass* type)
  676. {
  677. return il2cpp::vm::Class::IsNullable(type);
  678. }
  679. RuntimeClass* il2cpp_codegen_get_generic_argument(RuntimeClass* klass, uint32_t argNum)
  680. {
  681. const Il2CppGenericInst* classInst = il2cpp_codegen_get_generic_class_inst(klass);
  682. IL2CPP_ASSERT(argNum < classInst->type_argc);
  683. return il2cpp::vm::Class::FromIl2CppType(classInst->type_argv[argNum]);
  684. }
  685. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const Il2CppGenericInst* genericInst)
  686. {
  687. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, genericInst);
  688. }
  689. RuntimeClass* il2cpp_codegen_inflate_generic_class(RuntimeClass* genericClassDefinition, const RuntimeType* p1, /*const RuntimeType*, const RuntimeType* */ ...)
  690. {
  691. IL2CPP_ASSERT(genericClassDefinition->is_generic);
  692. const uint32_t genericParameterCount = il2cpp::vm::MetadataCache::GetGenericContainerCount(genericClassDefinition->genericContainerHandle);
  693. const RuntimeType** types = (const RuntimeType**)alloca(sizeof(RuntimeType*) * genericParameterCount);
  694. types[0] = p1;
  695. if (genericParameterCount > 1)
  696. {
  697. va_list genericArguments;
  698. va_start(genericArguments, p1);
  699. for (uint32_t i = 1; i < genericParameterCount; i++)
  700. types[i] = va_arg(genericArguments, const RuntimeType*);
  701. va_end(genericArguments);
  702. }
  703. return il2cpp::vm::Class::GetInflatedGenericInstanceClass(genericClassDefinition, il2cpp::vm::MetadataCache::GetGenericInst(types, genericParameterCount));
  704. }
  705. int32_t il2cpp_codgen_class_get_instance_size(RuntimeClass* klass)
  706. {
  707. return il2cpp::vm::Class::GetInstanceSize(klass);
  708. }
  709. RuntimeClass* il2cpp_codegen_class_from_type_internal(const RuntimeType* type)
  710. {
  711. return il2cpp::vm::Class::FromIl2CppType(type);
  712. }
  713. char* il2cpp_codegen_marshal_string(String_t* string)
  714. {
  715. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppString((RuntimeString*)string);
  716. }
  717. void il2cpp_codegen_marshal_string_fixed(String_t* string, char* buffer, int numberOfCharacters)
  718. {
  719. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  720. }
  721. Il2CppChar* il2cpp_codegen_marshal_wstring(String_t* string)
  722. {
  723. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWString((RuntimeString*)string);
  724. }
  725. void il2cpp_codegen_marshal_wstring_fixed(String_t* string, Il2CppChar* buffer, int numberOfCharacters)
  726. {
  727. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppWStringFixed((RuntimeString*)string, buffer, numberOfCharacters);
  728. }
  729. Il2CppChar* il2cpp_codegen_marshal_bstring(String_t* string)
  730. {
  731. return il2cpp::vm::PlatformInvoke::MarshalCSharpStringToCppBString((RuntimeString*)string);
  732. }
  733. String_t* il2cpp_codegen_marshal_string_result(const char* value)
  734. {
  735. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppStringToCSharpStringResult(value);
  736. }
  737. String_t* il2cpp_codegen_marshal_wstring_result(const Il2CppChar* value)
  738. {
  739. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppWStringToCSharpStringResult(value);
  740. }
  741. String_t* il2cpp_codegen_marshal_bstring_result(const Il2CppChar* value)
  742. {
  743. return (String_t*)il2cpp::vm::PlatformInvoke::MarshalCppBStringToCSharpStringResult(value);
  744. }
  745. void il2cpp_codegen_marshal_free_bstring(Il2CppChar* value)
  746. {
  747. il2cpp::vm::PlatformInvoke::MarshalFreeBString(value);
  748. }
  749. char* il2cpp_codegen_marshal_empty_string_builder(StringBuilder_t* stringBuilder)
  750. {
  751. return il2cpp::vm::PlatformInvoke::MarshalEmptyStringBuilder((RuntimeStringBuilder*)stringBuilder);
  752. }
  753. char* il2cpp_codegen_marshal_string_builder(StringBuilder_t* stringBuilder)
  754. {
  755. return il2cpp::vm::PlatformInvoke::MarshalStringBuilder((RuntimeStringBuilder*)stringBuilder);
  756. }
  757. Il2CppChar* il2cpp_codegen_marshal_empty_wstring_builder(StringBuilder_t* stringBuilder)
  758. {
  759. return il2cpp::vm::PlatformInvoke::MarshalEmptyWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  760. }
  761. Il2CppChar* il2cpp_codegen_marshal_wstring_builder(StringBuilder_t* stringBuilder)
  762. {
  763. return il2cpp::vm::PlatformInvoke::MarshalWStringBuilder((RuntimeStringBuilder*)stringBuilder);
  764. }
  765. void il2cpp_codegen_marshal_string_builder_result(StringBuilder_t* stringBuilder, char* buffer)
  766. {
  767. il2cpp::vm::PlatformInvoke::MarshalStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  768. }
  769. void il2cpp_codegen_marshal_wstring_builder_result(StringBuilder_t* stringBuilder, Il2CppChar* buffer)
  770. {
  771. il2cpp::vm::PlatformInvoke::MarshalWStringBuilderResult((RuntimeStringBuilder*)stringBuilder, buffer);
  772. }
  773. void il2cpp_codegen_marshal_free(void* ptr)
  774. {
  775. il2cpp::vm::PlatformInvoke::MarshalFree(ptr);
  776. }
  777. Il2CppMethodPointer il2cpp_codegen_marshal_delegate(MulticastDelegate_t* d)
  778. {
  779. return (Il2CppMethodPointer)il2cpp::vm::PlatformInvoke::MarshalDelegate((RuntimeDelegate*)d);
  780. }
  781. Il2CppDelegate* il2cpp_codegen_marshal_function_ptr_to_delegate_internal(void* functionPtr, Il2CppClass* delegateType)
  782. {
  783. return il2cpp::vm::PlatformInvoke::MarshalFunctionPointerToDelegate(functionPtr, delegateType);
  784. }
  785. bool il2cpp_codegen_is_marshalled_delegate(MulticastDelegate_t* d)
  786. {
  787. return il2cpp::vm::PlatformInvoke::IsFakeDelegateMethodMarshaledFromNativeCode((const RuntimeDelegate*)d);
  788. }
  789. Il2CppMethodPointer il2cpp_codegen_resolve(const PInvokeArguments& pinvokeArgs)
  790. {
  791. return il2cpp::vm::PlatformInvoke::Resolve(pinvokeArgs);
  792. }
  793. Il2CppHString il2cpp_codegen_create_hstring(String_t* str)
  794. {
  795. #if IL2CPP_TRIM_COM
  796. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_create_hstring);
  797. return NULL;
  798. #else
  799. return il2cpp::vm::WindowsRuntime::CreateHString(reinterpret_cast<RuntimeString*>(str));
  800. #endif
  801. }
  802. String_t* il2cpp_codegen_marshal_hstring_result(Il2CppHString hstring)
  803. {
  804. #if IL2CPP_TRIM_COM
  805. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_marshal_hstring_result);
  806. return NULL;
  807. #else
  808. return reinterpret_cast<String_t*>(il2cpp::vm::WindowsRuntime::HStringToManagedString(hstring));
  809. #endif
  810. }
  811. void il2cpp_codegen_marshal_free_hstring(Il2CppHString hstring)
  812. {
  813. #if IL2CPP_TRIM_COM
  814. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_marshal_free_hstring);
  815. #else
  816. il2cpp::vm::WindowsRuntime::DeleteHString(hstring);
  817. #endif
  818. }
  819. void il2cpp_codegen_marshal_type_to_native(Type_t* type, Il2CppWindowsRuntimeTypeName& nativeType)
  820. {
  821. #if IL2CPP_TRIM_COM
  822. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_marshal_type_to_native);
  823. #else
  824. return il2cpp::vm::WindowsRuntime::MarshalTypeToNative(type != NULL ? reinterpret_cast<Il2CppReflectionType*>(type)->type : NULL, nativeType);
  825. #endif
  826. }
  827. const Il2CppType* il2cpp_codegen_marshal_type_from_native_internal(Il2CppWindowsRuntimeTypeName& nativeType)
  828. {
  829. #if IL2CPP_TRIM_COM
  830. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_marshal_type_from_native_internal);
  831. return NULL;
  832. #else
  833. return il2cpp::vm::WindowsRuntime::MarshalTypeFromNative(nativeType);
  834. #endif
  835. }
  836. void il2cpp_codegen_delete_native_type(Il2CppWindowsRuntimeTypeName& nativeType)
  837. {
  838. #if IL2CPP_TRIM_COM
  839. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_delete_native_type);
  840. #else
  841. return il2cpp::vm::WindowsRuntime::DeleteNativeType(nativeType);
  842. #endif
  843. }
  844. Il2CppIActivationFactory* il2cpp_codegen_windows_runtime_get_activation_factory(const il2cpp::utils::StringView<Il2CppNativeChar>& runtimeClassName)
  845. {
  846. #if IL2CPP_TRIM_COM
  847. IL2CPP_NOT_IMPLEMENTED(il2cpp_codegen_windows_runtime_get_activation_factory);
  848. return NULL;
  849. #else
  850. return il2cpp::vm::WindowsRuntime::GetActivationFactory(runtimeClassName);
  851. #endif
  852. }
  853. void il2cpp_codegen_stacktrace_push_frame(Il2CppStackFrameInfo& frame)
  854. {
  855. il2cpp::vm::StackTrace::PushFrame(frame);
  856. }
  857. void il2cpp_codegen_stacktrace_pop_frame()
  858. {
  859. il2cpp::vm::StackTrace::PopFrame();
  860. }
  861. void il2cpp_codegen_array_unsafe_mov(RuntimeClass * destClass, void* dest, RuntimeClass * srcClass, void* src)
  862. {
  863. // A runtime implementation of System.Array::UnsafeMov
  864. IL2CPP_ASSERT(destClass);
  865. IL2CPP_ASSERT(dest);
  866. IL2CPP_ASSERT(srcClass);
  867. IL2CPP_ASSERT(src);
  868. uint32_t destSize = il2cpp_codegen_sizeof(destClass);
  869. uint32_t srcSize = il2cpp_codegen_sizeof(srcClass);
  870. // If the types are the same size we can just memcpy them
  871. // otherwise we need to "move" them using the correct casting rules for primitive types
  872. if (destSize == srcSize)
  873. {
  874. il2cpp_codegen_memcpy(dest, src, destSize);
  875. return;
  876. }
  877. const Il2CppType* destType = il2cpp::vm::Class::IsEnum(destClass) ? il2cpp::vm::Class::GetEnumBaseType(destClass) : &destClass->byval_arg;
  878. const Il2CppType* srcType = il2cpp::vm::Class::IsEnum(srcClass) ? il2cpp::vm::Class::GetEnumBaseType(srcClass) : &srcClass->byval_arg;
  879. switch (destType->type)
  880. {
  881. case IL2CPP_TYPE_BOOLEAN:
  882. il2cpp_codegen_array_unsafe_mov_primitive<bool>(destType, (bool*)dest, srcType, src);
  883. break;
  884. case IL2CPP_TYPE_I1:
  885. il2cpp_codegen_array_unsafe_mov_primitive<int8_t>(destType, (int8_t *)dest, srcType, src);
  886. break;
  887. case IL2CPP_TYPE_U1:
  888. il2cpp_codegen_array_unsafe_mov_primitive<uint8_t>(destType, (uint8_t *)dest, srcType, src);
  889. break;
  890. case IL2CPP_TYPE_I2:
  891. il2cpp_codegen_array_unsafe_mov_primitive<int16_t>(destType, (int16_t *)dest, srcType, src);
  892. break;
  893. case IL2CPP_TYPE_CHAR:
  894. case IL2CPP_TYPE_U2:
  895. il2cpp_codegen_array_unsafe_mov_primitive<uint16_t>(destType, (uint16_t *)dest, srcType, src);
  896. break;
  897. case IL2CPP_TYPE_I4:
  898. il2cpp_codegen_array_unsafe_mov_primitive<int32_t>(destType, (int32_t *)dest, srcType, src);
  899. break;
  900. case IL2CPP_TYPE_U4:
  901. il2cpp_codegen_array_unsafe_mov_primitive<uint32_t>(destType, (uint32_t *)dest, srcType, src);
  902. break;
  903. case IL2CPP_TYPE_I8:
  904. il2cpp_codegen_array_unsafe_mov_primitive<int64_t>(destType, (int64_t *)dest, srcType, src);
  905. break;
  906. case IL2CPP_TYPE_U8:
  907. il2cpp_codegen_array_unsafe_mov_primitive<uint64_t>(destType, (uint64_t *)dest, srcType, src);
  908. break;
  909. case IL2CPP_TYPE_I:
  910. il2cpp_codegen_array_unsafe_mov_primitive<intptr_t>(destType, (intptr_t *)dest, srcType, src);
  911. break;
  912. case IL2CPP_TYPE_U:
  913. il2cpp_codegen_array_unsafe_mov_primitive<uintptr_t>(destType, (uintptr_t *)dest, srcType, src);
  914. break;
  915. default:
  916. il2cpp_codegen_array_unsafe_mov_type_exception(destType, srcType);
  917. }
  918. }
  919. NORETURN void il2cpp_codegen_array_unsafe_mov_type_exception(const RuntimeType * destType, const RuntimeType* srcType)
  920. {
  921. // No other primitive types are supported by Array::UnsafeMov (floating point types are not supported)
  922. // Or we're trying to assign structs of different sizes
  923. IL2CPP_ASSERT(false);
  924. std::string msg;
  925. msg += "Unsupported call to ";
  926. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(destType));
  927. msg += " System.Array::UnsafeMov(";
  928. msg += il2cpp::vm::Class::GetName(il2cpp::vm::Class::FromIl2CppType(srcType));
  929. msg += ");";
  930. il2cpp_codegen_raise_exception(il2cpp_codegen_get_not_supported_exception(msg.c_str()));
  931. }
  932. void il2cpp_codegen_runtime_constrained_call(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* boxBuffer, void* objBuffer, void** args, void* retVal)
  933. {
  934. // Reference types use a virtual method call
  935. if (!type->byval_arg.valuetype)
  936. {
  937. RuntimeObject* refObj = *(RuntimeObject**)objBuffer;
  938. NullCheck(refObj);
  939. const MethodInfo* virtualMethod = il2cpp::vm::Object::GetVirtualMethod(refObj, constrainedMethod);
  940. virtualMethod->invoker_method(virtualMethod->virtualMethodPointer, virtualMethod, refObj, args, retVal);
  941. }
  942. // For value types, the constrained RGCTX does our lookup for us
  943. else if (type == constrainedMethod->klass)
  944. {
  945. il2cpp_codegen_runtime_class_init_inline(type);
  946. // If the value type overrode the method, do a direct call wiht the pointer to the struct
  947. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, objBuffer, args, retVal);
  948. }
  949. else if (il2cpp::vm::Class::IsInterface(constrainedMethod->klass))
  950. {
  951. // We are invoking a default interface method on a struct
  952. // We need to box to call the interface method, and the boxing is observable
  953. IL2CPP_ASSERT(type->byval_arg.valuetype);
  954. RuntimeObject* boxed = il2cpp::vm::Object::Box(type, objBuffer);
  955. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  956. }
  957. else
  958. {
  959. IL2CPP_ASSERT(constrainedMethod->klass == il2cpp_defaults.object_class || constrainedMethod->klass == il2cpp_defaults.value_type_class || constrainedMethod->klass == il2cpp_defaults.enum_class);
  960. // The value type did not override the method, so we're making a call to a method declared on
  961. // System.Object, System.ValueType, or System.Enum so we need to box, but fake boxing should work
  962. // because these methods will not mutate "this" and we can assume that they do not store the "this" pointer past the call
  963. if (il2cpp::vm::Class::IsNullable(type))
  964. {
  965. if (!il2cpp::vm::Object::NullableHasValue(type, objBuffer))
  966. il2cpp_codegen_raise_null_reference_exception();
  967. type = il2cpp::vm::Class::GetNullableArgument(type);
  968. }
  969. Il2CppFakeBoxBuffer* boxed = new(boxBuffer) Il2CppFakeBoxBuffer(type, objBuffer);
  970. constrainedMethod->invoker_method(constrainedMethod->methodPointer, constrainedMethod, boxed, args, retVal);
  971. }
  972. }
  973. void* il2cpp_codegen_runtime_box_constrained_this(RuntimeClass* type, const RuntimeMethod* constrainedMethod, void* obj)
  974. {
  975. // We are calling a method defined on the type, no need to box
  976. IL2CPP_ASSERT(il2cpp::vm::Class::IsValuetype(type));
  977. if (type == constrainedMethod->klass)
  978. return obj;
  979. // We are calling a default interface method with a value type, we have to box
  980. IL2CPP_ASSERT(il2cpp::vm::Class::IsInterface(constrainedMethod->klass));
  981. return il2cpp::vm::Object::Box(type, obj);
  982. }
  983. bool il2cpp_codegen_is_reference_or_contains_references(const RuntimeMethod* method)
  984. {
  985. IL2CPP_ASSERT(il2cpp::vm::Method::IsGenericInstance(method));
  986. const Il2CppGenericContext* context = il2cpp::metadata::GenericMethod::GetContext(method->genericMethod);
  987. IL2CPP_ASSERT(context->method_inst);
  988. IL2CPP_ASSERT(context->method_inst->type_argc == 1);
  989. IL2CPP_ASSERT(!il2cpp::metadata::Il2CppTypeEqualityComparer::AreEqual(context->method_inst->type_argv[0], &il2cpp_defaults.il2cpp_fully_shared_type->byval_arg));
  990. const Il2CppType* type = context->method_inst->type_argv[0];
  991. if (!type->valuetype)
  992. return true;
  993. Il2CppClass* klass = il2cpp::vm::Class::FromIl2CppType(type);
  994. il2cpp::vm::ClassInlines::InitFromCodegen(klass);
  995. return klass->has_references;
  996. }
  997. bool il2cpp_codegen_is_unmanaged(const RuntimeMethod* method)
  998. {
  999. return !il2cpp_codegen_is_reference_or_contains_references(method);
  1000. }
  1001. RuntimeClass* il2cpp_rgctx_data_no_init(const Il2CppRGCTXData* rgctxVar, int32_t index)
  1002. {
  1003. if (rgctxVar[index].klass == NULL)
  1004. {
  1005. il2cpp::metadata::GenericMetadata::InflateRGCTXClass(rgctxVar, index);
  1006. }
  1007. return rgctxVar[index].klass;
  1008. }
  1009. const Il2CppType* il2cpp_rgctx_type(const Il2CppRGCTXData* rgctxVar, int32_t index)
  1010. {
  1011. if (rgctxVar[index].type == NULL)
  1012. {
  1013. il2cpp::metadata::GenericMetadata::InflateRGCTXType(rgctxVar, index);
  1014. }
  1015. return rgctxVar[index].type;
  1016. }
  1017. const MethodInfo* il2cpp_rgctx_method(const Il2CppRGCTXData* rgctxVar, int32_t index)
  1018. {
  1019. if (rgctxVar[index].method == NULL)
  1020. {
  1021. il2cpp::metadata::GenericMetadata::InflateRGCTXMethod(rgctxVar, index);
  1022. }
  1023. return rgctxVar[index].method;
  1024. }
  1025. #endif // !RUNTIME_TINY
  1026. #if IL2CPP_TINY_DEBUGGER
  1027. #include "vm/Image.h"
  1028. #include "gc/WriteBarrier.h"
  1029. MulticastDelegate_t* il2cpp_codegen_create_combined_delegate(Type_t* type, Il2CppArray* delegates, int delegateCount)
  1030. {
  1031. Il2CppClass* klass = il2cpp::vm::Class::FromSystemType((Il2CppReflectionType*)type);
  1032. Il2CppMulticastDelegate* result = reinterpret_cast<Il2CppMulticastDelegate*>(il2cpp_codegen_object_new(klass));
  1033. il2cpp::gc::WriteBarrier::GenericStore(&result->delegates, delegates);
  1034. il2cpp::gc::WriteBarrier::GenericStore(&result->delegate.m_target, (Il2CppObject*)result);
  1035. result->delegateCount = delegateCount;
  1036. result->delegate.invoke_impl = il2cpp_array_get(delegates, Il2CppDelegate*, 0)->multicast_invoke_impl;
  1037. result->delegate.multicast_invoke_impl = result->delegate.invoke_impl;
  1038. return reinterpret_cast<MulticastDelegate_t*>(result);
  1039. }
  1040. Type_t* il2cpp_codegen_get_type(Il2CppObject* obj)
  1041. {
  1042. return (Type_t*)il2cpp::vm::Reflection::GetTypeObject(&obj->klass->byval_arg);
  1043. }
  1044. Type_t* il2cpp_codegen_get_base_type(const Type_t* t)
  1045. {
  1046. Il2CppClass* klass = il2cpp::vm::Class::FromSystemType((Il2CppReflectionType*)t);
  1047. if (klass->parent == NULL)
  1048. return NULL;
  1049. return (Type_t*)il2cpp::vm::Reflection::GetTypeObject(&klass->parent->byval_arg);
  1050. }
  1051. bool il2cpp_codegen_is_assignable_from(Type_t* left, Type_t* right)
  1052. {
  1053. return il2cpp::vm::Class::IsAssignableFrom((Il2CppReflectionType*)left, (Il2CppReflectionType*)right);
  1054. }
  1055. void il2cpp_codegen_no_reverse_pinvoke_wrapper(const char* methodName, const char* reason)
  1056. {
  1057. std::string message = "No reverse pinvoke wrapper exists for method: '";
  1058. message += methodName;
  1059. message += "' because ";
  1060. message += reason;
  1061. il2cpp_codegen_raise_exception(il2cpp_codegen_get_invalid_operation_exception(message.c_str()));
  1062. }
  1063. bool il2cpp_codegen_type_is_interface(Type_t* t)
  1064. {
  1065. Il2CppClass* klass = il2cpp::vm::Class::FromSystemType((Il2CppReflectionType*)t);
  1066. return il2cpp::vm::Class::IsInterface(klass);
  1067. }
  1068. bool il2cpp_codegen_type_is_abstract(Type_t* t)
  1069. {
  1070. Il2CppClass* klass = il2cpp::vm::Class::FromSystemType((Il2CppReflectionType*)t);
  1071. return il2cpp::vm::Class::IsAbstract(klass);
  1072. }
  1073. bool il2cpp_codegen_type_is_pointer(Type_t* t)
  1074. {
  1075. Il2CppClass* klass = il2cpp::vm::Class::FromSystemType((Il2CppReflectionType*)t);
  1076. return il2cpp::vm::Class::GetType(klass)->type == IL2CPP_TYPE_PTR;
  1077. }
  1078. NORETURN void il2cpp_codegen_raise_exception(const char* message)
  1079. {
  1080. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::FromNameMsg(il2cpp::vm::Image::GetCorlib(), "System", "Exception", message));
  1081. IL2CPP_UNREACHABLE;
  1082. }
  1083. #endif