CommonDef.h 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #pragma once
  2. #include <stdint.h>
  3. #include <cstring>
  4. #include <memory>
  5. #include "Il2CppCompatibleDef.h"
  6. #include "utils/Memory.h"
  7. #include "utils/StringView.h"
  8. #include "utils/Il2CppHashSet.h"
  9. #include "utils/Il2CppHashMap.h"
  10. #include "utils/HashUtils.h"
  11. #include "vm/GlobalMetadataFileInternals.h"
  12. #include "vm/Exception.h"
  13. #include "vm/Class.h"
  14. #include "icalls/mscorlib/System/Type.h"
  15. #ifdef HYBRIDCLR_UNITY_2021_OR_NEW
  16. #include "icalls/mscorlib/System/RuntimeType.h"
  17. #else
  18. #include "icalls/mscorlib/System/MonoType.h"
  19. #endif
  20. namespace hybridclr
  21. {
  22. typedef uint8_t byte;
  23. #define TEMP_FORMAT(var, fmt, ...) char var[600]; \
  24. snprintf(var, sizeof(var), fmt, __VA_ARGS__);
  25. void LogPanic(const char* errMsg);
  26. const char* GetAssemblyNameFromPath(const char* assPath);
  27. const char* CopyString(const char* src);
  28. const char* ConcatNewString(const char* s1, const char* s2);
  29. void* CopyBytes(const void* src, size_t length);
  30. struct CStringHash
  31. {
  32. size_t operator()(const char* s) const noexcept
  33. {
  34. uint32_t hash = 0;
  35. for (; *s; ++s)
  36. {
  37. hash += *s;
  38. hash += (hash << 10);
  39. hash ^= (hash >> 6);
  40. }
  41. hash += (hash << 3);
  42. hash ^= (hash >> 11);
  43. hash += (hash << 15);
  44. return hash;
  45. }
  46. };
  47. struct CStringEqualTo
  48. {
  49. bool operator()(const char* _Left, const char* _Right) const
  50. {
  51. return std::strcmp(_Left, _Right) == 0;
  52. }
  53. };
  54. inline il2cpp::utils::StringView<char> CStringToStringView(const char* str)
  55. {
  56. return il2cpp::utils::StringView<char>(str, std::strlen(str));
  57. }
  58. inline std::string GetKlassCStringFullName(const Il2CppType* type)
  59. {
  60. return GetKlassFullName2(type);
  61. }
  62. inline void RaiseNotSupportedException(const char* msg)
  63. {
  64. TEMP_FORMAT(errMsg, "hybridclr doesn't support %s", msg);
  65. return il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetNotSupportedException(errMsg));
  66. }
  67. inline void RaiseExecutionEngineException(const char* msg)
  68. {
  69. return il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetExecutionEngineException(msg));
  70. }
  71. inline void RaiseMethodNotFindException(const Il2CppType* type, const char* methodName)
  72. {
  73. if (!type)
  74. {
  75. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException("type not exists"));
  76. }
  77. std::string fullName = GetKlassCStringFullName(type);
  78. TEMP_FORMAT(errMsg, "MethodNotFind %s::%s", fullName.c_str(), methodName);
  79. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(errMsg));
  80. }
  81. inline void AppendTypeName(std::string& s, const Il2CppType* type)
  82. {
  83. s.append(GetKlassCStringFullName(type));
  84. }
  85. inline std::string GetMethodNameWithSignature(const MethodInfo* method)
  86. {
  87. std::string name;
  88. AppendTypeName(name, method->return_type);
  89. name.append(" ");
  90. name.append(GetKlassCStringFullName(&method->klass->byval_arg));
  91. name.append("::");
  92. name.append(method->name);
  93. if (method->genericMethod && method->genericMethod->context.method_inst)
  94. {
  95. name.append("<");
  96. const Il2CppGenericInst* gi= method->genericMethod->context.method_inst;
  97. for (uint32_t i = 0; i < gi->type_argc; i++)
  98. {
  99. if (i > 0)
  100. {
  101. name.append(",");
  102. }
  103. AppendTypeName(name, gi->type_argv[i]);
  104. }
  105. name.append(">");
  106. }
  107. name.append("(");
  108. for (uint8_t i = 0; i < method->parameters_count; i++)
  109. {
  110. if (i > 0)
  111. {
  112. name.append(",");
  113. }
  114. AppendTypeName(name, GET_METHOD_PARAMETER_TYPE(method->parameters[i]));
  115. }
  116. name.append(")");
  117. return name;
  118. }
  119. inline void RaiseAOTGenericMethodNotInstantiatedException(const MethodInfo* method)
  120. {
  121. std::string methodName = GetMethodNameWithSignature(method);
  122. TEMP_FORMAT(errMsg, "AOT generic method not instantiated in aot. assembly:%s, method:%s", method->klass->image->name, methodName.c_str());
  123. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(errMsg));
  124. }
  125. inline void RaiseMissingFieldException(const Il2CppType* type, const char* fieldName)
  126. {
  127. if (!type)
  128. {
  129. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException("type not exists"));
  130. }
  131. std::string stdFullName = GetKlassCStringFullName(type);
  132. TEMP_FORMAT(errMsg, "field %s::%s not exists", stdFullName.c_str(), fieldName);
  133. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingFieldException(errMsg));
  134. }
  135. }