il2cpp-api.cpp 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493
  1. #include "il2cpp-api.h"
  2. #include "il2cpp-object-internals.h"
  3. #include "il2cpp-runtime-stats.h"
  4. #include "gc/WriteBarrier.h"
  5. #include "os/StackTrace.h"
  6. #include "os/Image.h"
  7. #include "vm/AndroidRuntime.h"
  8. #include "vm/Array.h"
  9. #include "vm/Assembly.h"
  10. #include "vm/Class.h"
  11. #include "vm/Domain.h"
  12. #include "vm/Exception.h"
  13. #include "vm/Field.h"
  14. #include "vm/Image.h"
  15. #include "vm/InternalCalls.h"
  16. #include "vm/Liveness.h"
  17. #include "vm/MemoryInformation.h"
  18. #include "vm/Method.h"
  19. #include "vm/Monitor.h"
  20. #include "vm/Object.h"
  21. #include "vm/Path.h"
  22. #include "vm/PlatformInvoke.h"
  23. #include "vm/Profiler.h"
  24. #include "vm/Property.h"
  25. #include "vm/Reflection.h"
  26. #include "vm/Runtime.h"
  27. #include "vm/StackTrace.h"
  28. #include "vm/String.h"
  29. #include "vm/Thread.h"
  30. #include "vm/Type.h"
  31. #include "utils/Exception.h"
  32. #include "utils/Logging.h"
  33. #include "utils/Memory.h"
  34. #include "utils/StringUtils.h"
  35. #include "utils/Runtime.h"
  36. #include "utils/Environment.h"
  37. #include "vm-utils/Debugger.h"
  38. #include "vm-utils/NativeSymbol.h"
  39. #include "gc/GarbageCollector.h"
  40. #include "gc/GCHandle.h"
  41. #include "gc/WriteBarrierValidation.h"
  42. #include <locale.h>
  43. #include <fstream>
  44. #include <string>
  45. using namespace il2cpp::vm;
  46. using il2cpp::utils::Memory;
  47. using namespace il2cpp::gc;
  48. #if IL2CPP_API_DYNAMIC_NO_DLSYM
  49. #include <map>
  50. struct SymbolCompare : public std::binary_function<char*, char*, bool>
  51. {
  52. bool operator()(const char* lhs, const char* rhs) const
  53. {
  54. return strcmp(lhs, rhs) < 0;
  55. }
  56. };
  57. typedef std::map<const char*, void*, SymbolCompare> SymbolTable;
  58. static SymbolTable s_SymbolTable;
  59. static void RegisterAPIFunction(const char* name, void* symbol)
  60. {
  61. s_SymbolTable.insert(std::make_pair(name, symbol));
  62. }
  63. void il2cpp_api_register_symbols(void)
  64. {
  65. #define DO_API(r, n, p) RegisterAPIFunction(#n, (void*)n);
  66. #define DO_API_NO_RETURN(r, n, p) DO_API(r, n, p)
  67. #include "il2cpp-api-functions.h"
  68. #undef DO_API
  69. #undef DO_API_NO_RETURN
  70. }
  71. void* il2cpp_api_lookup_symbol(const char* name)
  72. {
  73. SymbolTable::iterator it = s_SymbolTable.find(name);
  74. if (it != s_SymbolTable.end())
  75. {
  76. return it->second;
  77. }
  78. return NULL;
  79. }
  80. #endif // IL2CPP_API_DYNAMIC_NO_DLSYM
  81. int il2cpp_init(const char* domain_name)
  82. {
  83. // Use environment's default locale
  84. setlocale(LC_ALL, "");
  85. return Runtime::Init(domain_name);
  86. }
  87. int il2cpp_init_utf16(const Il2CppChar* domain_name)
  88. {
  89. return il2cpp_init(il2cpp::utils::StringUtils::Utf16ToUtf8(domain_name).c_str());
  90. }
  91. void il2cpp_shutdown()
  92. {
  93. Runtime::Shutdown();
  94. }
  95. void il2cpp_set_config_dir(const char *config_path)
  96. {
  97. il2cpp::vm::Runtime::SetConfigDir(config_path);
  98. }
  99. void il2cpp_set_data_dir(const char *data_path)
  100. {
  101. il2cpp::utils::Runtime::SetDataDir(data_path);
  102. }
  103. void il2cpp_set_temp_dir(const char *temp_dir)
  104. {
  105. il2cpp::vm::Path::SetTempPath(temp_dir);
  106. }
  107. void il2cpp_set_commandline_arguments(int argc, const char* const argv[], const char* basedir)
  108. {
  109. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  110. }
  111. void il2cpp_set_commandline_arguments_utf16(int argc, const Il2CppChar* const argv[], const char* basedir)
  112. {
  113. il2cpp::utils::Environment::SetMainArgs(argv, argc);
  114. }
  115. void il2cpp_set_config_utf16(const Il2CppChar* executablePath)
  116. {
  117. il2cpp::vm::Runtime::SetConfigUtf16(executablePath);
  118. }
  119. void il2cpp_set_config(const char* executablePath)
  120. {
  121. il2cpp::vm::Runtime::SetConfig(executablePath);
  122. }
  123. void il2cpp_set_memory_callbacks(Il2CppMemoryCallbacks* callbacks)
  124. {
  125. Memory::SetMemoryCallbacks(callbacks);
  126. }
  127. const Il2CppImage* il2cpp_get_corlib()
  128. {
  129. return Image::GetCorlib();
  130. }
  131. void il2cpp_add_internal_call(const char* name, Il2CppMethodPointer method)
  132. {
  133. return InternalCalls::Add(name, method);
  134. }
  135. Il2CppMethodPointer il2cpp_resolve_icall(const char* name)
  136. {
  137. return InternalCalls::Resolve(name);
  138. }
  139. void* il2cpp_alloc(size_t size)
  140. {
  141. return IL2CPP_MALLOC(size);
  142. }
  143. void il2cpp_free(void* ptr)
  144. {
  145. IL2CPP_FREE(ptr);
  146. }
  147. // array
  148. Il2CppClass *il2cpp_array_class_get(Il2CppClass *element_class, uint32_t rank)
  149. {
  150. return Class::GetArrayClass(element_class, rank);
  151. }
  152. uint32_t il2cpp_array_length(Il2CppArray* array)
  153. {
  154. return Array::GetLength(array);
  155. }
  156. uint32_t il2cpp_array_get_byte_length(Il2CppArray *array)
  157. {
  158. return Array::GetByteLength(array);
  159. }
  160. Il2CppArray* il2cpp_array_new(Il2CppClass *elementTypeInfo, il2cpp_array_size_t length)
  161. {
  162. return Array::New(elementTypeInfo, length);
  163. }
  164. Il2CppArray* il2cpp_array_new_specific(Il2CppClass *arrayTypeInfo, il2cpp_array_size_t length)
  165. {
  166. return Array::NewSpecific(arrayTypeInfo, length);
  167. }
  168. Il2CppArray* il2cpp_array_new_full(Il2CppClass *array_class, il2cpp_array_size_t *lengths, il2cpp_array_size_t *lower_bounds)
  169. {
  170. return Array::NewFull(array_class, lengths, lower_bounds);
  171. }
  172. Il2CppClass* il2cpp_bounded_array_class_get(Il2CppClass *element_class, uint32_t rank, bool bounded)
  173. {
  174. return Class::GetBoundedArrayClass(element_class, rank, bounded);
  175. }
  176. int il2cpp_array_element_size(const Il2CppClass* klass)
  177. {
  178. return Array::GetElementSize(klass);
  179. }
  180. // assembly
  181. const Il2CppImage* il2cpp_assembly_get_image(const Il2CppAssembly *assembly)
  182. {
  183. return Assembly::GetImage(assembly);
  184. }
  185. // class
  186. const Il2CppType* il2cpp_class_enum_basetype(Il2CppClass *klass)
  187. {
  188. return Class::GetEnumBaseType(klass);
  189. }
  190. Il2CppClass* il2cpp_class_from_system_type(Il2CppReflectionType *type)
  191. {
  192. return Class::FromSystemType(type);
  193. }
  194. bool il2cpp_class_is_inited(const Il2CppClass *klass)
  195. {
  196. return klass->initialized;
  197. }
  198. bool il2cpp_class_is_generic(const Il2CppClass *klass)
  199. {
  200. return Class::IsGeneric(klass);
  201. }
  202. bool il2cpp_class_is_inflated(const Il2CppClass *klass)
  203. {
  204. return Class::IsInflated(klass);
  205. }
  206. bool il2cpp_class_is_assignable_from(Il2CppClass *klass, Il2CppClass *oklass)
  207. {
  208. return Class::IsAssignableFrom(klass, oklass);
  209. }
  210. bool il2cpp_class_is_subclass_of(Il2CppClass *klass, Il2CppClass *klassc, bool check_interfaces)
  211. {
  212. return Class::IsSubclassOf(klass, klassc, check_interfaces);
  213. }
  214. bool il2cpp_class_has_parent(Il2CppClass *klass, Il2CppClass *klassc)
  215. {
  216. return Class::HasParent(klass, klassc);
  217. }
  218. Il2CppClass* il2cpp_class_from_il2cpp_type(const Il2CppType* type)
  219. {
  220. return Class::FromIl2CppType(type);
  221. }
  222. Il2CppClass* il2cpp_class_from_name(const Il2CppImage* image, const char* namespaze, const char *name)
  223. {
  224. return Class::FromName(image, namespaze, name);
  225. }
  226. Il2CppClass* il2cpp_class_get_element_class(Il2CppClass *klass)
  227. {
  228. return Class::GetElementClass(klass);
  229. }
  230. const EventInfo* il2cpp_class_get_events(Il2CppClass *klass, void* *iter)
  231. {
  232. return Class::GetEvents(klass, iter);
  233. }
  234. FieldInfo* il2cpp_class_get_fields(Il2CppClass *klass, void* *iter)
  235. {
  236. return Class::GetFields(klass, iter);
  237. }
  238. Il2CppClass* il2cpp_class_get_nested_types(Il2CppClass *klass, void* *iter)
  239. {
  240. return Class::GetNestedTypes(klass, iter);
  241. }
  242. Il2CppClass* il2cpp_class_get_interfaces(Il2CppClass *klass, void* *iter)
  243. {
  244. return Class::GetInterfaces(klass, iter);
  245. }
  246. const PropertyInfo* il2cpp_class_get_properties(Il2CppClass *klass, void* *iter)
  247. {
  248. return Class::GetProperties(klass, iter);
  249. }
  250. const PropertyInfo* il2cpp_class_get_property_from_name(Il2CppClass *klass, const char *name)
  251. {
  252. return Class::GetPropertyFromName(klass, name);
  253. }
  254. FieldInfo* il2cpp_class_get_field_from_name(Il2CppClass* klass, const char *name)
  255. {
  256. return Class::GetFieldFromName(klass, name);
  257. }
  258. const MethodInfo* il2cpp_class_get_methods(Il2CppClass *klass, void* *iter)
  259. {
  260. return Class::GetMethods(klass, iter);
  261. }
  262. const MethodInfo* il2cpp_class_get_method_from_name(Il2CppClass *klass, const char* name, int argsCount)
  263. {
  264. return Class::GetMethodFromName(klass, name, argsCount);
  265. }
  266. const char* il2cpp_class_get_name(Il2CppClass *klass)
  267. {
  268. return Class::GetName(klass);
  269. }
  270. const char* il2cpp_class_get_namespace(Il2CppClass *klass)
  271. {
  272. return Class::GetNamespace(klass);
  273. }
  274. Il2CppClass* il2cpp_class_get_parent(Il2CppClass *klass)
  275. {
  276. return Class::GetParent(klass);
  277. }
  278. Il2CppClass* il2cpp_class_get_declaring_type(Il2CppClass* klass)
  279. {
  280. return Class::GetDeclaringType(klass);
  281. }
  282. int32_t il2cpp_class_instance_size(Il2CppClass *klass)
  283. {
  284. return Class::GetInstanceSize(klass);
  285. }
  286. size_t il2cpp_class_num_fields(const Il2CppClass* klass)
  287. {
  288. return Class::GetNumFields(klass);
  289. }
  290. bool il2cpp_class_is_valuetype(const Il2CppClass* klass)
  291. {
  292. return Class::IsValuetype(klass);
  293. }
  294. bool il2cpp_class_is_blittable(const Il2CppClass* klass)
  295. {
  296. return Class::IsBlittable(klass);
  297. }
  298. int32_t il2cpp_class_value_size(Il2CppClass *klass, uint32_t *align)
  299. {
  300. return Class::GetValueSize(klass, align);
  301. }
  302. int il2cpp_class_get_flags(const Il2CppClass *klass)
  303. {
  304. return Class::GetFlags(klass);
  305. }
  306. bool il2cpp_class_is_abstract(const Il2CppClass *klass)
  307. {
  308. return Class::IsAbstract(klass);
  309. }
  310. bool il2cpp_class_is_interface(const Il2CppClass *klass)
  311. {
  312. return Class::IsInterface(klass);
  313. }
  314. int il2cpp_class_array_element_size(const Il2CppClass *klass)
  315. {
  316. return Class::GetArrayElementSize(klass);
  317. }
  318. Il2CppClass* il2cpp_class_from_type(const Il2CppType *type)
  319. {
  320. return Class::FromIl2CppType(type);
  321. }
  322. const Il2CppType* il2cpp_class_get_type(Il2CppClass *klass)
  323. {
  324. return Class::GetType(klass);
  325. }
  326. uint32_t il2cpp_class_get_type_token(Il2CppClass *klass)
  327. {
  328. return klass->token;
  329. }
  330. bool il2cpp_class_has_attribute(Il2CppClass *klass, Il2CppClass *attr_class)
  331. {
  332. return Class::HasAttribute(klass, attr_class);
  333. }
  334. bool il2cpp_class_has_references(Il2CppClass *klass)
  335. {
  336. return Class::HasReferences(klass);
  337. }
  338. bool il2cpp_class_is_enum(const Il2CppClass *klass)
  339. {
  340. return Class::IsEnum(klass);
  341. }
  342. const Il2CppImage* il2cpp_class_get_image(Il2CppClass* klass)
  343. {
  344. return Class::GetImage(klass);
  345. }
  346. const char *il2cpp_class_get_assemblyname(const Il2CppClass *klass)
  347. {
  348. return Class::GetAssemblyNameNoExtension(klass);
  349. }
  350. int il2cpp_class_get_rank(const Il2CppClass *klass)
  351. {
  352. return klass->rank;
  353. }
  354. uint32_t il2cpp_class_get_data_size(const Il2CppClass *klass)
  355. {
  356. return klass->static_fields_size;
  357. }
  358. void* il2cpp_class_get_static_field_data(const Il2CppClass *klass)
  359. {
  360. return klass->static_fields;
  361. }
  362. // testing only
  363. size_t il2cpp_class_get_bitmap_size(const Il2CppClass *klass)
  364. {
  365. return Class::GetBitmapSize(klass);
  366. }
  367. void il2cpp_class_get_bitmap(Il2CppClass *klass, size_t* bitmap)
  368. {
  369. size_t dummy = 0;
  370. Class::GetBitmap(klass, bitmap, dummy);
  371. }
  372. // stats
  373. extern Il2CppRuntimeStats il2cpp_runtime_stats;
  374. bool il2cpp_stats_dump_to_file(const char *path)
  375. {
  376. std::fstream fs;
  377. fs.open(path, std::fstream::out | std::fstream::trunc);
  378. fs << "New object count: " << il2cpp_stats_get_value(IL2CPP_STAT_NEW_OBJECT_COUNT) << "\n";
  379. fs << "Method count: " << il2cpp_stats_get_value(IL2CPP_STAT_METHOD_COUNT) << "\n";
  380. fs << "Class static data size: " << il2cpp_stats_get_value(IL2CPP_STAT_CLASS_STATIC_DATA_SIZE) << "\n";
  381. fs << "Inflated method count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_METHOD_COUNT) << "\n";
  382. fs << "Inflated type count: " << il2cpp_stats_get_value(IL2CPP_STAT_INFLATED_TYPE_COUNT) << "\n";
  383. fs << "Initialized class count: " << il2cpp_stats_get_value(IL2CPP_STAT_INITIALIZED_CLASS_COUNT) << "\n";
  384. fs << "Generic instance count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_INSTANCE_COUNT) << "\n";
  385. fs << "Generic class count: " << il2cpp_stats_get_value(IL2CPP_STAT_GENERIC_CLASS_COUNT) << "\n";
  386. fs.close();
  387. return true;
  388. }
  389. uint64_t il2cpp_stats_get_value(Il2CppStat stat)
  390. {
  391. switch (stat)
  392. {
  393. case IL2CPP_STAT_NEW_OBJECT_COUNT:
  394. return il2cpp_runtime_stats.new_object_count;
  395. case IL2CPP_STAT_INITIALIZED_CLASS_COUNT:
  396. return il2cpp_runtime_stats.initialized_class_count;
  397. /*case IL2CPP_STAT_GENERIC_VTABLE_COUNT:
  398. return il2cpp_runtime_stats.generic_vtable_count;
  399. case IL2CPP_STAT_USED_CLASS_COUNT:
  400. return il2cpp_runtime_stats.used_class_count;*/
  401. case IL2CPP_STAT_METHOD_COUNT:
  402. return il2cpp_runtime_stats.method_count;
  403. /*case IL2CPP_STAT_CLASS_VTABLE_SIZE:
  404. return il2cpp_runtime_stats.class_vtable_size;*/
  405. case IL2CPP_STAT_CLASS_STATIC_DATA_SIZE:
  406. return il2cpp_runtime_stats.class_static_data_size;
  407. case IL2CPP_STAT_GENERIC_INSTANCE_COUNT:
  408. return il2cpp_runtime_stats.generic_instance_count;
  409. case IL2CPP_STAT_GENERIC_CLASS_COUNT:
  410. return il2cpp_runtime_stats.generic_class_count;
  411. case IL2CPP_STAT_INFLATED_METHOD_COUNT:
  412. return il2cpp_runtime_stats.inflated_method_count;
  413. case IL2CPP_STAT_INFLATED_TYPE_COUNT:
  414. return il2cpp_runtime_stats.inflated_type_count;
  415. /*case IL2CPP_STAT_DELEGATE_CREATIONS:
  416. return il2cpp_runtime_stats.delegate_creations;
  417. case IL2CPP_STAT_MINOR_GC_COUNT:
  418. return il2cpp_runtime_stats.minor_gc_count;
  419. case IL2CPP_STAT_MAJOR_GC_COUNT:
  420. return il2cpp_runtime_stats.major_gc_count;
  421. case IL2CPP_STAT_MINOR_GC_TIME_USECS:
  422. return il2cpp_runtime_stats.minor_gc_time_usecs;
  423. case IL2CPP_STAT_MAJOR_GC_TIME_USECS:
  424. return il2cpp_runtime_stats.major_gc_time_usecs;*/
  425. }
  426. return 0;
  427. }
  428. // domain
  429. Il2CppDomain* il2cpp_domain_get()
  430. {
  431. return Domain::GetCurrent();
  432. }
  433. const Il2CppAssembly* il2cpp_domain_assembly_open(Il2CppDomain *domain, const char *name)
  434. {
  435. return Assembly::Load(name);
  436. }
  437. const Il2CppAssembly** il2cpp_domain_get_assemblies(const Il2CppDomain* domain, size_t* size)
  438. {
  439. il2cpp::vm::AssemblyVector* assemblies = Assembly::GetAllAssemblies();
  440. *size = assemblies->size();
  441. return &(*assemblies)[0];
  442. }
  443. // exception
  444. void il2cpp_raise_exception(Il2CppException* exc)
  445. {
  446. Exception::Raise(exc);
  447. }
  448. Il2CppException* il2cpp_exception_from_name_msg(const Il2CppImage* image, const char *name_space, const char *name, const char *msg)
  449. {
  450. return Exception::FromNameMsg(image, name_space, name, msg);
  451. }
  452. Il2CppException* il2cpp_get_exception_argument_null(const char *arg)
  453. {
  454. return Exception::GetArgumentNullException(arg);
  455. }
  456. void il2cpp_format_exception(const Il2CppException* ex, char* message, int message_size)
  457. {
  458. strncpy(message, il2cpp::utils::Exception::FormatException(ex).c_str(), message_size);
  459. }
  460. void il2cpp_format_stack_trace(const Il2CppException* ex, char* output, int output_size)
  461. {
  462. strncpy(output, il2cpp::utils::Exception::FormatStackTrace(ex).c_str(), output_size);
  463. }
  464. void il2cpp_unhandled_exception(Il2CppException* exc)
  465. {
  466. Runtime::UnhandledException(exc);
  467. }
  468. void il2cpp_native_stack_trace(const Il2CppException * ex, uintptr_t** addresses, int* numFrames, char** imageUUID, char** imageName)
  469. {
  470. #if IL2CPP_ENABLE_NATIVE_INSTRUCTION_POINTER_EMISSION && !IL2CPP_TINY
  471. if (ex == NULL || ex->native_trace_ips == NULL)
  472. {
  473. *numFrames = 0;
  474. *addresses = NULL;
  475. *imageUUID = NULL;
  476. *imageName = NULL;
  477. return;
  478. }
  479. *numFrames = il2cpp_array_length(ex->native_trace_ips);
  480. if (*numFrames <= 0)
  481. {
  482. *addresses = NULL;
  483. *imageUUID = NULL;
  484. *imageName = NULL;
  485. }
  486. else
  487. {
  488. *addresses = static_cast<uintptr_t*>(il2cpp_alloc((*numFrames) * sizeof(uintptr_t)));
  489. for (int i = 0; i < *numFrames; i++)
  490. {
  491. uintptr_t ptrAddr = il2cpp_array_get(ex->native_trace_ips, uintptr_t, i);
  492. (*addresses)[i] = ptrAddr;
  493. }
  494. *imageUUID = il2cpp::os::Image::GetImageUUID();
  495. *imageName = il2cpp::os::Image::GetImageName();
  496. }
  497. #endif
  498. }
  499. // field
  500. const char* il2cpp_field_get_name(FieldInfo *field)
  501. {
  502. return Field::GetName(field);
  503. }
  504. int il2cpp_field_get_flags(FieldInfo *field)
  505. {
  506. return Field::GetFlags(field);
  507. }
  508. Il2CppClass* il2cpp_field_get_parent(FieldInfo *field)
  509. {
  510. return Field::GetParent(field);
  511. }
  512. size_t il2cpp_field_get_offset(FieldInfo *field)
  513. {
  514. return Field::GetOffset(field);
  515. }
  516. const Il2CppType* il2cpp_field_get_type(FieldInfo *field)
  517. {
  518. return Field::GetType(field);
  519. }
  520. void il2cpp_field_get_value(Il2CppObject *obj, FieldInfo *field, void *value)
  521. {
  522. return Field::GetValue(obj, field, value);
  523. }
  524. Il2CppObject* il2cpp_field_get_value_object(FieldInfo *field, Il2CppObject *obj)
  525. {
  526. return Field::GetValueObject(field, obj);
  527. }
  528. bool il2cpp_field_has_attribute(FieldInfo *field, Il2CppClass *attr_class)
  529. {
  530. return Field::HasAttribute(field, attr_class);
  531. }
  532. void il2cpp_field_set_value(Il2CppObject *obj, FieldInfo *field, void *value)
  533. {
  534. Field::SetValue(obj, field, value);
  535. }
  536. void il2cpp_field_set_value_object(Il2CppObject* objectInstance, FieldInfo* field, Il2CppObject* value)
  537. {
  538. Field::SetInstanceFieldValueObject(objectInstance, field, value);
  539. }
  540. void il2cpp_field_static_get_value(FieldInfo *field, void *value)
  541. {
  542. Field::StaticGetValue(field, value);
  543. }
  544. void il2cpp_field_static_set_value(FieldInfo *field, void *value)
  545. {
  546. Field::StaticSetValue(field, value);
  547. }
  548. bool il2cpp_field_is_literal(FieldInfo *field)
  549. {
  550. return (field->type->attrs & FIELD_ATTRIBUTE_LITERAL) != 0;
  551. }
  552. // gc
  553. void il2cpp_gc_collect(int maxGenerations)
  554. {
  555. GarbageCollector::Collect(maxGenerations);
  556. }
  557. int32_t il2cpp_gc_collect_a_little()
  558. {
  559. return GarbageCollector::CollectALittle();
  560. }
  561. void il2cpp_gc_start_incremental_collection()
  562. {
  563. GarbageCollector::StartIncrementalCollection();
  564. }
  565. void il2cpp_gc_enable()
  566. {
  567. GarbageCollector::Enable();
  568. }
  569. void il2cpp_gc_disable()
  570. {
  571. GarbageCollector::Disable();
  572. }
  573. bool il2cpp_gc_is_disabled()
  574. {
  575. return GarbageCollector::IsDisabled();
  576. }
  577. void il2cpp_gc_set_mode(Il2CppGCMode mode)
  578. {
  579. GarbageCollector::SetMode(mode);
  580. }
  581. bool il2cpp_gc_is_incremental()
  582. {
  583. return GarbageCollector::IsIncremental();
  584. }
  585. int64_t il2cpp_gc_get_max_time_slice_ns()
  586. {
  587. return GarbageCollector::GetMaxTimeSliceNs();
  588. }
  589. void il2cpp_gc_set_max_time_slice_ns(int64_t maxTimeSlice)
  590. {
  591. GarbageCollector::SetMaxTimeSliceNs(maxTimeSlice);
  592. }
  593. int64_t il2cpp_gc_get_used_size()
  594. {
  595. return GarbageCollector::GetUsedHeapSize();
  596. }
  597. int64_t il2cpp_gc_get_heap_size()
  598. {
  599. return GarbageCollector::GetAllocatedHeapSize();
  600. }
  601. void il2cpp_gc_foreach_heap(void(*func)(void* data, void* context), void* userData)
  602. {
  603. MemoryInformation::IterationContext ctx;
  604. ctx.callback = func;
  605. ctx.userData = userData;
  606. il2cpp::gc::GarbageCollector::ForEachHeapSection(&ctx, MemoryInformation::ReportGcHeapSection);
  607. }
  608. void il2cpp_stop_gc_world()
  609. {
  610. il2cpp::gc::GarbageCollector::StopWorld();
  611. }
  612. void il2cpp_start_gc_world()
  613. {
  614. il2cpp::gc::GarbageCollector::StartWorld();
  615. }
  616. void* il2cpp_gc_alloc_fixed(size_t size)
  617. {
  618. return il2cpp::gc::GarbageCollector::AllocateFixed(size, NULL);
  619. }
  620. void il2cpp_gc_free_fixed(void* address)
  621. {
  622. il2cpp::gc::GarbageCollector::FreeFixed(address);
  623. }
  624. // gchandle
  625. uint32_t il2cpp_gchandle_new(Il2CppObject *obj, bool pinned)
  626. {
  627. return GCHandle::New(obj, pinned);
  628. }
  629. uint32_t il2cpp_gchandle_new_weakref(Il2CppObject *obj, bool track_resurrection)
  630. {
  631. // Note that the call to Get will assert if an error occurred.
  632. return GCHandle::NewWeakref(obj, track_resurrection).Get();
  633. }
  634. Il2CppObject* il2cpp_gchandle_get_target(uint32_t gchandle)
  635. {
  636. return GCHandle::GetTarget(gchandle);
  637. }
  638. void il2cpp_gchandle_foreach_get_target(void(*func)(void*, void*), void* userData)
  639. {
  640. MemoryInformation::IterationContext ctx;
  641. ctx.callback = func;
  642. ctx.userData = userData;
  643. il2cpp::gc::GCHandle::WalkStrongGCHandleTargets(MemoryInformation::ReportGcHandleTarget, &ctx);
  644. }
  645. void il2cpp_gc_wbarrier_set_field(Il2CppObject *obj, void **targetAddress, void *object)
  646. {
  647. il2cpp::gc::WriteBarrier::GenericStore(targetAddress, object);
  648. }
  649. bool il2cpp_gc_has_strict_wbarriers()
  650. {
  651. #if IL2CPP_ENABLE_STRICT_WRITE_BARRIERS
  652. return true;
  653. #else
  654. return false;
  655. #endif
  656. }
  657. void il2cpp_gc_set_external_allocation_tracker(void(*func)(void*, size_t, int))
  658. {
  659. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  660. il2cpp::gc::WriteBarrierValidation::SetExternalAllocationTracker(func);
  661. #endif
  662. }
  663. void il2cpp_gc_set_external_wbarrier_tracker(void(*func)(void**))
  664. {
  665. #if IL2CPP_ENABLE_WRITE_BARRIER_VALIDATION
  666. il2cpp::gc::WriteBarrierValidation::SetExternalWriteBarrierTracker(func);
  667. #endif
  668. }
  669. void il2cpp_gchandle_free(uint32_t gchandle)
  670. {
  671. GCHandle::Free(gchandle);
  672. }
  673. // vm runtime info
  674. uint32_t il2cpp_object_header_size()
  675. {
  676. return static_cast<uint32_t>(sizeof(Il2CppObject));
  677. }
  678. uint32_t il2cpp_array_object_header_size()
  679. {
  680. return static_cast<uint32_t>(kIl2CppSizeOfArray);
  681. }
  682. uint32_t il2cpp_offset_of_array_length_in_array_object_header()
  683. {
  684. return kIl2CppOffsetOfArrayLength;
  685. }
  686. uint32_t il2cpp_offset_of_array_bounds_in_array_object_header()
  687. {
  688. return kIl2CppOffsetOfArrayBounds;
  689. }
  690. uint32_t il2cpp_allocation_granularity()
  691. {
  692. return static_cast<uint32_t>(2 * sizeof(void*));
  693. }
  694. // liveness
  695. void* il2cpp_unity_liveness_allocate_struct(Il2CppClass* filter, int max_object_count, il2cpp_register_object_callback callback, void* userdata, il2cpp_liveness_reallocate_callback reallocate)
  696. {
  697. return Liveness::AllocateStruct(filter, max_object_count, callback, userdata, reallocate);
  698. }
  699. void il2cpp_unity_liveness_calculation_from_root(Il2CppObject* root, void* state)
  700. {
  701. Liveness::FromRoot(root, state);
  702. }
  703. void il2cpp_unity_liveness_calculation_from_statics(void* state)
  704. {
  705. Liveness::FromStatics(state);
  706. }
  707. void il2cpp_unity_liveness_finalize(void* state)
  708. {
  709. Liveness::Finalize(state);
  710. }
  711. void il2cpp_unity_liveness_free_struct(void* state)
  712. {
  713. Liveness::FreeStruct(state);
  714. }
  715. // method
  716. const Il2CppType* il2cpp_method_get_return_type(const MethodInfo* method)
  717. {
  718. return Method::GetReturnType(method);
  719. }
  720. const MethodInfo* il2cpp_method_get_from_reflection(const Il2CppReflectionMethod *method)
  721. {
  722. return Reflection::GetMethod(method);
  723. }
  724. Il2CppReflectionMethod* il2cpp_method_get_object(const MethodInfo *method, Il2CppClass *refclass)
  725. {
  726. return Reflection::GetMethodObject(method, refclass);
  727. }
  728. const char* il2cpp_method_get_name(const MethodInfo *method)
  729. {
  730. return Method::GetName(method);
  731. }
  732. bool il2cpp_method_is_generic(const MethodInfo *method)
  733. {
  734. return Method::IsGeneric(method);
  735. }
  736. bool il2cpp_method_is_inflated(const MethodInfo *method)
  737. {
  738. return Method::IsInflated(method);
  739. }
  740. bool il2cpp_method_is_instance(const MethodInfo *method)
  741. {
  742. return Method::IsInstance(method);
  743. }
  744. uint32_t il2cpp_method_get_param_count(const MethodInfo *method)
  745. {
  746. return Method::GetParamCount(method);
  747. }
  748. const Il2CppType* il2cpp_method_get_param(const MethodInfo *method, uint32_t index)
  749. {
  750. return Method::GetParam(method, index);
  751. }
  752. Il2CppClass* il2cpp_method_get_class(const MethodInfo *method)
  753. {
  754. return Method::GetClass(method);
  755. }
  756. bool il2cpp_method_has_attribute(const MethodInfo *method, Il2CppClass *attr_class)
  757. {
  758. return Method::HasAttribute(method, attr_class);
  759. }
  760. Il2CppClass* il2cpp_method_get_declaring_type(const MethodInfo* method)
  761. {
  762. return Method::GetDeclaringType(method);
  763. }
  764. uint32_t il2cpp_method_get_flags(const MethodInfo *method, uint32_t *iflags)
  765. {
  766. if (iflags != 0)
  767. *iflags = Method::GetImplementationFlags(method);
  768. return Method::GetFlags(method);
  769. }
  770. uint32_t il2cpp_method_get_token(const MethodInfo *method)
  771. {
  772. return Method::GetToken(method);
  773. }
  774. const char *il2cpp_method_get_param_name(const MethodInfo *method, uint32_t index)
  775. {
  776. return Method::GetParamName(method, index);
  777. }
  778. // profiler
  779. #if IL2CPP_ENABLE_PROFILER
  780. void il2cpp_profiler_install(Il2CppProfiler *prof, Il2CppProfileFunc shutdown_callback)
  781. {
  782. Profiler::Install(prof, shutdown_callback);
  783. }
  784. void il2cpp_profiler_set_events(Il2CppProfileFlags events)
  785. {
  786. Profiler::SetEvents(events);
  787. }
  788. void il2cpp_profiler_install_enter_leave(Il2CppProfileMethodFunc enter, Il2CppProfileMethodFunc fleave)
  789. {
  790. Profiler::InstallEnterLeave(enter, fleave);
  791. }
  792. void il2cpp_profiler_install_allocation(Il2CppProfileAllocFunc callback)
  793. {
  794. Profiler::InstallAllocation(callback);
  795. }
  796. void il2cpp_profiler_install_gc(Il2CppProfileGCFunc callback, Il2CppProfileGCResizeFunc heap_resize_callback)
  797. {
  798. Profiler::InstallGC(callback, heap_resize_callback);
  799. }
  800. void il2cpp_profiler_install_fileio(Il2CppProfileFileIOFunc callback)
  801. {
  802. Profiler::InstallFileIO(callback);
  803. }
  804. void il2cpp_profiler_install_thread(Il2CppProfileThreadFunc start, Il2CppProfileThreadFunc end)
  805. {
  806. Profiler::InstallThread(start, end);
  807. }
  808. #endif
  809. // property
  810. const char* il2cpp_property_get_name(PropertyInfo *prop)
  811. {
  812. return Property::GetName(prop);
  813. }
  814. const MethodInfo* il2cpp_property_get_get_method(PropertyInfo *prop)
  815. {
  816. return Property::GetGetMethod(prop);
  817. }
  818. const MethodInfo* il2cpp_property_get_set_method(PropertyInfo *prop)
  819. {
  820. return Property::GetSetMethod(prop);
  821. }
  822. Il2CppClass* il2cpp_property_get_parent(PropertyInfo *prop)
  823. {
  824. return Property::GetParent(prop);
  825. }
  826. uint32_t il2cpp_property_get_flags(PropertyInfo *prop)
  827. {
  828. return Property::GetFlags(prop);
  829. }
  830. // object
  831. Il2CppClass* il2cpp_object_get_class(Il2CppObject* obj)
  832. {
  833. return Object::GetClass(obj);
  834. }
  835. uint32_t il2cpp_object_get_size(Il2CppObject* obj)
  836. {
  837. return Object::GetSize(obj);
  838. }
  839. const MethodInfo* il2cpp_object_get_virtual_method(Il2CppObject *obj, const MethodInfo *method)
  840. {
  841. return Object::GetVirtualMethod(obj, method);
  842. }
  843. Il2CppObject* il2cpp_object_new(const Il2CppClass *klass)
  844. {
  845. try
  846. {
  847. return Object::New(const_cast<Il2CppClass*>(klass));
  848. }
  849. catch (const Il2CppExceptionWrapper&)
  850. {
  851. // If a static constructor throws, that exception will occur here.
  852. // We don't want that to escape across the embedding API.
  853. return NULL;
  854. }
  855. }
  856. void* il2cpp_object_unbox(Il2CppObject* obj)
  857. {
  858. return Object::Unbox(obj);
  859. }
  860. Il2CppObject* il2cpp_value_box(Il2CppClass *klass, void* data)
  861. {
  862. return Object::Box(klass, data);
  863. }
  864. // monitor
  865. void il2cpp_monitor_enter(Il2CppObject* obj)
  866. {
  867. Monitor::Enter(obj);
  868. }
  869. bool il2cpp_monitor_try_enter(Il2CppObject* obj, uint32_t timeout)
  870. {
  871. return Monitor::TryEnter(obj, timeout);
  872. }
  873. void il2cpp_monitor_exit(Il2CppObject* obj)
  874. {
  875. Monitor::Exit(obj);
  876. }
  877. void il2cpp_monitor_pulse(Il2CppObject* obj)
  878. {
  879. Monitor::Pulse(obj);
  880. }
  881. void il2cpp_monitor_pulse_all(Il2CppObject* obj)
  882. {
  883. Monitor::PulseAll(obj);
  884. }
  885. void il2cpp_monitor_wait(Il2CppObject* obj)
  886. {
  887. Monitor::Wait(obj);
  888. }
  889. bool il2cpp_monitor_try_wait(Il2CppObject* obj, uint32_t timeout)
  890. {
  891. return Monitor::TryWait(obj, timeout);
  892. }
  893. // runtime
  894. Il2CppObject* il2cpp_runtime_invoke_convert_args(const MethodInfo *method, void *obj, Il2CppObject **params, int paramCount, Il2CppException **exc)
  895. {
  896. return Runtime::InvokeConvertArgs(method, obj, params, paramCount, exc);
  897. }
  898. Il2CppObject* il2cpp_runtime_invoke(const MethodInfo *method,
  899. void *obj, void **params, Il2CppException **exc)
  900. {
  901. return Runtime::Invoke(method, obj, params, exc);
  902. }
  903. void il2cpp_runtime_class_init(Il2CppClass* klass)
  904. {
  905. return Runtime::ClassInit(klass);
  906. }
  907. void il2cpp_runtime_object_init(Il2CppObject *obj)
  908. {
  909. Runtime::ObjectInit(obj);
  910. }
  911. void il2cpp_runtime_object_init_exception(Il2CppObject *obj, Il2CppException **exc)
  912. {
  913. Runtime::ObjectInitException(obj, exc);
  914. }
  915. void il2cpp_runtime_unhandled_exception_policy_set(Il2CppRuntimeUnhandledExceptionPolicy value)
  916. {
  917. Runtime::SetUnhandledExceptionPolicy(value);
  918. }
  919. // string
  920. int32_t il2cpp_string_length(Il2CppString* str)
  921. {
  922. return il2cpp::utils::StringUtils::GetLength(str);
  923. }
  924. Il2CppChar* il2cpp_string_chars(Il2CppString* str)
  925. {
  926. return il2cpp::utils::StringUtils::GetChars(str);
  927. }
  928. // Same as il2cpp_string_new_wrapper, because other normally takes a domain
  929. Il2CppString* il2cpp_string_new(const char* str)
  930. {
  931. return String::New(str);
  932. }
  933. Il2CppString* il2cpp_string_new_wrapper(const char* str)
  934. {
  935. return String::NewWrapper(str);
  936. }
  937. Il2CppString* il2cpp_string_new_utf16(const Il2CppChar *text, int32_t len)
  938. {
  939. return String::NewUtf16(text, len);
  940. }
  941. Il2CppString* il2cpp_string_new_len(const char* str, uint32_t length)
  942. {
  943. return String::NewLen(str, length);
  944. }
  945. Il2CppString* il2cpp_string_intern(Il2CppString* str)
  946. {
  947. return String::Intern(str);
  948. }
  949. Il2CppString* il2cpp_string_is_interned(Il2CppString* str)
  950. {
  951. return String::IsInterned(str);
  952. }
  953. // thread
  954. Il2CppThread *il2cpp_thread_current()
  955. {
  956. return Thread::Current();
  957. }
  958. Il2CppThread *il2cpp_thread_attach(Il2CppDomain *domain)
  959. {
  960. return Thread::Attach(domain);
  961. }
  962. void il2cpp_thread_detach(Il2CppThread *thread)
  963. {
  964. Thread::Detach(thread);
  965. }
  966. Il2CppThread **il2cpp_thread_get_all_attached_threads(size_t *size)
  967. {
  968. return Thread::GetAllAttachedThreads(*size);
  969. }
  970. bool il2cpp_is_vm_thread(Il2CppThread *thread)
  971. {
  972. return Thread::IsVmThread(thread);
  973. }
  974. // stacktrace
  975. void il2cpp_current_thread_walk_frame_stack(Il2CppFrameWalkFunc func, void* user_data)
  976. {
  977. StackTrace::WalkFrameStack(func, user_data);
  978. }
  979. void il2cpp_thread_walk_frame_stack(Il2CppThread *thread, Il2CppFrameWalkFunc func, void *user_data)
  980. {
  981. return StackTrace::WalkThreadFrameStack(thread, func, user_data);
  982. }
  983. bool il2cpp_current_thread_get_top_frame(Il2CppStackFrameInfo* frame)
  984. {
  985. IL2CPP_ASSERT(frame);
  986. return StackTrace::GetTopStackFrame(*frame);
  987. }
  988. bool il2cpp_thread_get_top_frame(Il2CppThread* thread, Il2CppStackFrameInfo* frame)
  989. {
  990. IL2CPP_ASSERT(frame);
  991. return StackTrace::GetThreadTopStackFrame(thread, *frame);
  992. }
  993. bool il2cpp_current_thread_get_frame_at(int32_t offset, Il2CppStackFrameInfo* frame)
  994. {
  995. IL2CPP_ASSERT(frame);
  996. return StackTrace::GetStackFrameAt(offset, *frame);
  997. }
  998. bool il2cpp_thread_get_frame_at(Il2CppThread* thread, int32_t offset, Il2CppStackFrameInfo* frame)
  999. {
  1000. IL2CPP_ASSERT(frame);
  1001. return StackTrace::GetThreadStackFrameAt(thread, offset, *frame);
  1002. }
  1003. int32_t il2cpp_current_thread_get_stack_depth()
  1004. {
  1005. return static_cast<int32_t>(StackTrace::GetStackDepth());
  1006. }
  1007. int32_t il2cpp_thread_get_stack_depth(Il2CppThread *thread)
  1008. {
  1009. return StackTrace::GetThreadStackDepth(thread);
  1010. }
  1011. void il2cpp_set_default_thread_affinity(int64_t affinity_mask)
  1012. {
  1013. Thread::SetDefaultAffinityMask(affinity_mask);
  1014. }
  1015. void il2cpp_override_stack_backtrace(Il2CppBacktraceFunc stackBacktraceFunc)
  1016. {
  1017. il2cpp::os::StackTrace::OverrideStackBacktrace(stackBacktraceFunc);
  1018. }
  1019. // type
  1020. Il2CppObject* il2cpp_type_get_object(const Il2CppType *type)
  1021. {
  1022. return (Il2CppObject*)Reflection::GetTypeObject(type);
  1023. }
  1024. int il2cpp_type_get_type(const Il2CppType *type)
  1025. {
  1026. return Type::GetType(type);
  1027. }
  1028. Il2CppClass* il2cpp_type_get_class_or_element_class(const Il2CppType *type)
  1029. {
  1030. return Type::GetClassOrElementClass(type);
  1031. }
  1032. char* il2cpp_type_get_name(const Il2CppType *type)
  1033. {
  1034. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_IL);
  1035. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1036. memcpy(buffer, name.c_str(), name.length() + 1);
  1037. return buffer;
  1038. }
  1039. char* il2cpp_type_get_assembly_qualified_name(const Il2CppType * type)
  1040. {
  1041. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_ASSEMBLY_QUALIFIED);
  1042. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1043. memcpy(buffer, name.c_str(), name.length() + 1);
  1044. return buffer;
  1045. }
  1046. char* il2cpp_type_get_reflection_name(const Il2CppType *type)
  1047. {
  1048. std::string name = Type::GetName(type, IL2CPP_TYPE_NAME_FORMAT_REFLECTION);
  1049. char* buffer = static_cast<char*>(il2cpp_alloc(name.length() + 1));
  1050. memcpy(buffer, name.c_str(), name.length() + 1);
  1051. return buffer;
  1052. }
  1053. bool il2cpp_type_is_byref(const Il2CppType *type)
  1054. {
  1055. return type->byref;
  1056. }
  1057. uint32_t il2cpp_type_get_attrs(const Il2CppType *type)
  1058. {
  1059. return type->attrs;
  1060. }
  1061. bool il2cpp_type_equals(const Il2CppType* type, const Il2CppType *otherType)
  1062. {
  1063. return Type::IsEqualToType(type, otherType);
  1064. }
  1065. bool il2cpp_type_is_static(const Il2CppType *type)
  1066. {
  1067. return (type->attrs & FIELD_ATTRIBUTE_STATIC) != 0;
  1068. }
  1069. bool il2cpp_type_is_pointer_type(const Il2CppType *type)
  1070. {
  1071. return type->type == IL2CPP_TYPE_PTR;
  1072. }
  1073. // image
  1074. const Il2CppAssembly* il2cpp_image_get_assembly(const Il2CppImage *image)
  1075. {
  1076. return Image::GetAssembly(image);
  1077. }
  1078. const char* il2cpp_image_get_name(const Il2CppImage *image)
  1079. {
  1080. return Image::GetName(image);
  1081. }
  1082. const char* il2cpp_image_get_filename(const Il2CppImage *image)
  1083. {
  1084. return Image::GetFileName(image);
  1085. }
  1086. const MethodInfo* il2cpp_image_get_entry_point(const Il2CppImage *image)
  1087. {
  1088. return Image::GetEntryPoint(image);
  1089. }
  1090. size_t il2cpp_image_get_class_count(const Il2CppImage * image)
  1091. {
  1092. return Image::GetNumTypes(image);
  1093. }
  1094. const Il2CppClass* il2cpp_image_get_class(const Il2CppImage * image, size_t index)
  1095. {
  1096. return Image::GetType(image, static_cast<AssemblyTypeIndex>(index));
  1097. }
  1098. Il2CppManagedMemorySnapshot* il2cpp_capture_memory_snapshot()
  1099. {
  1100. return MemoryInformation::CaptureManagedMemorySnapshot();
  1101. }
  1102. void il2cpp_free_captured_memory_snapshot(Il2CppManagedMemorySnapshot* snapshot)
  1103. {
  1104. MemoryInformation::FreeCapturedManagedMemorySnapshot(snapshot);
  1105. }
  1106. void il2cpp_set_find_plugin_callback(Il2CppSetFindPlugInCallback method)
  1107. {
  1108. il2cpp::vm::PlatformInvoke::SetFindPluginCallback(method);
  1109. }
  1110. // Logging
  1111. void il2cpp_register_log_callback(Il2CppLogCallback method)
  1112. {
  1113. il2cpp::utils::Logging::SetLogCallback(method);
  1114. }
  1115. // Debugger
  1116. void il2cpp_debugger_set_agent_options(const char* options)
  1117. {
  1118. #if IL2CPP_MONO_DEBUGGER
  1119. il2cpp::utils::Debugger::SetAgentOptions(options);
  1120. #endif
  1121. }
  1122. bool il2cpp_is_debugger_attached()
  1123. {
  1124. return il2cpp::utils::Debugger::GetIsDebuggerAttached();
  1125. }
  1126. void il2cpp_register_debugger_agent_transport(Il2CppDebuggerTransport * debuggerTransport)
  1127. {
  1128. #if IL2CPP_MONO_DEBUGGER
  1129. il2cpp::utils::Debugger::RegisterTransport(debuggerTransport);
  1130. #endif
  1131. }
  1132. bool il2cpp_debug_get_method_info(const MethodInfo* method, Il2CppMethodDebugInfo* methodDebugInfo)
  1133. {
  1134. #if IL2CPP_ENABLE_NATIVE_STACKTRACES
  1135. return il2cpp::utils::NativeSymbol::GetMethodDebugInfo(method, methodDebugInfo);
  1136. #else
  1137. return false;
  1138. #endif
  1139. }
  1140. void il2cpp_unity_install_unitytls_interface(const void* unitytlsInterfaceStruct)
  1141. {
  1142. il2cpp::vm::Runtime::SetUnityTlsInterface(unitytlsInterfaceStruct);
  1143. }
  1144. // Custom Attributes
  1145. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_class(Il2CppClass *klass)
  1146. {
  1147. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(klass->image, klass->token));
  1148. }
  1149. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_method(const MethodInfo * method)
  1150. {
  1151. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(method->klass->image, method->token));
  1152. }
  1153. Il2CppCustomAttrInfo* il2cpp_custom_attrs_from_field(const FieldInfo * field)
  1154. {
  1155. return (Il2CppCustomAttrInfo*)(MetadataCache::GetCustomAttributeTypeToken(field->parent->image, field->token));
  1156. }
  1157. bool il2cpp_custom_attrs_has_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1158. {
  1159. return Reflection::HasAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1160. }
  1161. Il2CppObject* il2cpp_custom_attrs_get_attr(Il2CppCustomAttrInfo *ainfo, Il2CppClass *attr_klass)
  1162. {
  1163. return Reflection::GetCustomAttribute(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo), attr_klass);
  1164. }
  1165. Il2CppArray* il2cpp_custom_attrs_construct(Il2CppCustomAttrInfo *ainfo)
  1166. {
  1167. return Reflection::ConstructCustomAttributes(reinterpret_cast<Il2CppMetadataCustomAttributeHandle>(ainfo));
  1168. }
  1169. void il2cpp_custom_attrs_free(Il2CppCustomAttrInfo *ainfo)
  1170. {
  1171. // nothing to free, we cache everything
  1172. }
  1173. void il2cpp_type_get_name_chunked(const Il2CppType * type, void(*chunkReportFunc)(void* data, void* userData), void* userData)
  1174. {
  1175. Type::GetNameChunkedRecurse(type, IL2CPP_TYPE_NAME_FORMAT_IL, chunkReportFunc, userData);
  1176. }
  1177. void il2cpp_class_set_userdata(Il2CppClass* klass, void* userdata)
  1178. {
  1179. klass->unity_user_data = userdata;
  1180. }
  1181. int il2cpp_class_get_userdata_offset()
  1182. {
  1183. return offsetof(struct Il2CppClass, unity_user_data);
  1184. }
  1185. void il2cpp_class_for_each(void(*klassReportFunc)(Il2CppClass* klass, void* userData), void* userData)
  1186. {
  1187. MemoryInformation::ReportIL2CppClasses(klassReportFunc, userData);
  1188. }
  1189. // Android
  1190. void il2cpp_unity_set_android_network_up_state_func(Il2CppAndroidUpStateFunc func)
  1191. {
  1192. AndroidRuntime::SetNetworkUpStateFunc(func);
  1193. }