CommonDef.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. Il2CppString* typeName = GetKlassFullName(type);
  61. return il2cpp::utils::StringUtils::Utf16ToUtf8(typeName->chars);
  62. }
  63. inline void RaiseNotSupportedException(const char* msg)
  64. {
  65. TEMP_FORMAT(errMsg, "hybridclr doesn't support %s", msg);
  66. return il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetNotSupportedException(errMsg));
  67. }
  68. inline void RaiseExecutionEngineException(const char* msg)
  69. {
  70. return il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetExecutionEngineException(msg));
  71. }
  72. inline void RaiseMethodNotFindException(const Il2CppType* type, const char* methodName)
  73. {
  74. if (!type)
  75. {
  76. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException("type not exists"));
  77. }
  78. std::string fullName = GetKlassCStringFullName(type);
  79. TEMP_FORMAT(errMsg, "MethodNotFind %s::%s", fullName.c_str(), methodName);
  80. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(errMsg));
  81. }
  82. inline void AppendTypeName(std::string& s, const Il2CppType* type)
  83. {
  84. s.append(GetKlassCStringFullName(type));
  85. }
  86. inline std::string GetMethodNameWithSignature(const MethodInfo* method)
  87. {
  88. std::string name;
  89. AppendTypeName(name, method->return_type);
  90. name.append(" ");
  91. name.append(GetKlassCStringFullName(&method->klass->byval_arg));
  92. name.append("::");
  93. name.append(method->name);
  94. if (method->genericMethod && method->genericMethod->context.method_inst)
  95. {
  96. name.append("<");
  97. const Il2CppGenericInst* gi= method->genericMethod->context.method_inst;
  98. for (uint32_t i = 0; i < gi->type_argc; i++)
  99. {
  100. if (i > 0)
  101. {
  102. name.append(",");
  103. }
  104. AppendTypeName(name, gi->type_argv[i]);
  105. }
  106. name.append(">");
  107. }
  108. name.append("(");
  109. for (uint8_t i = 0; i < method->parameters_count; i++)
  110. {
  111. if (i > 0)
  112. {
  113. name.append(",");
  114. }
  115. AppendTypeName(name, GET_METHOD_PARAMETER_TYPE(method->parameters[i]));
  116. }
  117. name.append(")");
  118. return name;
  119. }
  120. inline void RaiseAOTGenericMethodNotInstantiatedException(const MethodInfo* method)
  121. {
  122. std::string methodName = GetMethodNameWithSignature(method);
  123. TEMP_FORMAT(errMsg, "AOT generic method not instantiated in aot. assembly:%s, method:%s", method->klass->image->name, methodName.c_str());
  124. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingMethodException(errMsg));
  125. }
  126. inline void RaiseMissingFieldException(const Il2CppType* type, const char* fieldName)
  127. {
  128. if (!type)
  129. {
  130. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetTypeLoadException("type not exists"));
  131. }
  132. std::string stdFullName = GetKlassCStringFullName(type);
  133. TEMP_FORMAT(errMsg, "field %s::%s not exists", stdFullName.c_str(), fieldName);
  134. il2cpp::vm::Exception::Raise(il2cpp::vm::Exception::GetMissingFieldException(errMsg));
  135. }
  136. }