ClassInlines.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include "ClassInlines.h"
  2. #include "vm/Class.h"
  3. #include "vm/Exception.h"
  4. #include "vm/Method.h"
  5. #include "vm/RCW.h"
  6. #include "gc/GCHandle.h"
  7. #include "metadata/GenericMethod.h"
  8. namespace il2cpp
  9. {
  10. namespace vm
  11. {
  12. Il2CppClass* ClassInlines::InitFromCodegenSlow(Il2CppClass *klass)
  13. {
  14. IL2CPP_ASSERT(klass != il2cpp_defaults.il2cpp_fully_shared_type);
  15. Class::Init(klass);
  16. if (klass->initializationExceptionGCHandle)
  17. il2cpp::vm::Exception::Raise((Il2CppException*)gc::GCHandle::GetTarget(klass->initializationExceptionGCHandle));
  18. return klass;
  19. }
  20. Il2CppClass* ClassInlines::InitFromCodegenSlow(Il2CppClass *klass, bool throwOnError)
  21. {
  22. IL2CPP_ASSERT(klass != il2cpp_defaults.il2cpp_fully_shared_type);
  23. if (throwOnError)
  24. return InitFromCodegenSlow(klass);
  25. Class::Init(klass);
  26. if (klass->initializationExceptionGCHandle)
  27. return NULL;
  28. return klass;
  29. }
  30. const MethodInfo* ClassInlines::InitRgctxFromCodegenSlow(const MethodInfo* method)
  31. {
  32. il2cpp::metadata::GenericMethod::InflateRGCTX(method);
  33. return method;
  34. }
  35. NORETURN static void RaiseExceptionForNotFoundInterface(const Il2CppClass* klass, const Il2CppClass* itf, Il2CppMethodSlot slot)
  36. {
  37. std::string message;
  38. message = "Attempt to access method '" + Type::GetName(&itf->byval_arg, IL2CPP_TYPE_NAME_FORMAT_IL) + "." + Method::GetName(itf->methods[slot])
  39. + "' on type '" + Type::GetName(&klass->byval_arg, IL2CPP_TYPE_NAME_FORMAT_IL) + "' failed.";
  40. Exception::Raise(il2cpp::vm::Exception::GetMethodAccessException(message.c_str()));
  41. }
  42. const VirtualInvokeData* ClassInlines::GetInterfaceInvokeDataFromVTableSlowPath(const Il2CppClass* klass, const Il2CppClass* itf, Il2CppMethodSlot slot)
  43. {
  44. if (itf->generic_class != NULL)
  45. {
  46. for (uint16_t i = 0; i < klass->interface_offsets_count; ++i)
  47. {
  48. const Il2CppRuntimeInterfaceOffsetPair* pair = klass->interfaceOffsets + i;
  49. if (Class::IsGenericClassAssignableFromVariance(itf, pair->interfaceType, klass))
  50. {
  51. IL2CPP_ASSERT(pair->offset + slot < klass->vtable_count);
  52. return &klass->vtable[pair->offset + slot];
  53. }
  54. }
  55. }
  56. return NULL;
  57. }
  58. const VirtualInvokeData& ClassInlines::GetInterfaceInvokeDataFromVTableSlowPath(Il2CppObject* obj, const Il2CppClass* itf, Il2CppMethodSlot slot)
  59. {
  60. const Il2CppClass* klass = obj->klass;
  61. const VirtualInvokeData* data;
  62. data = GetInterfaceInvokeDataFromVTableSlowPath(klass, itf, slot);
  63. if (data)
  64. return *data;
  65. if (klass->is_import_or_windows_runtime)
  66. {
  67. Il2CppComObject* rcw = static_cast<Il2CppComObject*>(obj);
  68. // It might be null if it's called on a dead (already released) or fake object
  69. if (rcw->identity != NULL)
  70. {
  71. const VirtualInvokeData* invokeData = RCW::GetComInterfaceInvokeData(rcw, itf, slot);
  72. if (invokeData != NULL)
  73. {
  74. // Nothing will be referencing these types directly, so we need to initialize them here
  75. Class::Init(invokeData->method->klass);
  76. return *invokeData;
  77. }
  78. }
  79. }
  80. RaiseExceptionForNotFoundInterface(klass, itf, slot);
  81. IL2CPP_UNREACHABLE;
  82. }
  83. }
  84. }