2
0

BsMonoMethod.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "BsMonoMethod.h"
  4. #include "BsMonoManager.h"
  5. #include "BsMonoUtil.h"
  6. #include "BsMonoClass.h"
  7. #include "BsException.h"
  8. #include <mono/jit/jit.h>
  9. namespace BansheeEngine
  10. {
  11. MonoMethod::MonoMethod(::MonoMethod* method)
  12. :mMethod(method), mCachedReturnType(nullptr), mCachedParameters(nullptr),
  13. mCachedNumParameters(0), mIsStatic(false), mHasCachedSignature(false)
  14. {
  15. }
  16. MonoMethod::~MonoMethod()
  17. {
  18. if (mCachedParameters != nullptr)
  19. bs_free(mCachedParameters);
  20. }
  21. MonoObject* MonoMethod::invoke(MonoObject* instance, void** params)
  22. {
  23. MonoObject* exception = nullptr;
  24. MonoObject* retVal = mono_runtime_invoke(mMethod, instance, params, &exception);
  25. MonoUtil::throwIfException(exception);
  26. return retVal;
  27. }
  28. MonoObject* MonoMethod::invokeVirtual(MonoObject* instance, void** params)
  29. {
  30. ::MonoMethod* virtualMethod = mono_object_get_virtual_method(instance, mMethod);
  31. MonoObject* exception = nullptr;
  32. MonoObject* retVal = mono_runtime_invoke(virtualMethod, instance, params, &exception);
  33. MonoUtil::throwIfException(exception);
  34. return retVal;
  35. }
  36. void* MonoMethod::getThunk() const
  37. {
  38. return mono_method_get_unmanaged_thunk(mMethod);
  39. }
  40. String MonoMethod::getName() const
  41. {
  42. return String(mono_method_get_name(mMethod));
  43. }
  44. MonoClass* MonoMethod::getReturnType() const
  45. {
  46. if (!mHasCachedSignature)
  47. cacheSignature();
  48. return mCachedReturnType;
  49. }
  50. UINT32 MonoMethod::getNumParameters() const
  51. {
  52. if (!mHasCachedSignature)
  53. cacheSignature();
  54. return mCachedNumParameters;
  55. }
  56. MonoClass* MonoMethod::getParameterType(UINT32 paramIdx) const
  57. {
  58. if (!mHasCachedSignature)
  59. cacheSignature();
  60. if (paramIdx >= mCachedNumParameters)
  61. BS_EXCEPT(InvalidParametersException, "Parameter index out of range. Valid range is [0, " + toString(mCachedNumParameters - 1) + "]");
  62. return mCachedParameters[paramIdx];
  63. }
  64. bool MonoMethod::isStatic() const
  65. {
  66. if (!mHasCachedSignature)
  67. cacheSignature();
  68. return mIsStatic;
  69. }
  70. bool MonoMethod::hasAttribute(MonoClass* monoClass) const
  71. {
  72. // TODO - Consider caching custom attributes or just initializing them all at load
  73. MonoCustomAttrInfo* attrInfo = mono_custom_attrs_from_method(mMethod);
  74. if (attrInfo == nullptr)
  75. return false;
  76. bool hasAttr = mono_custom_attrs_has_attr(attrInfo, monoClass->_getInternalClass()) != 0;
  77. mono_custom_attrs_free(attrInfo);
  78. return hasAttr;
  79. }
  80. MonoObject* MonoMethod::getAttribute(MonoClass* monoClass) const
  81. {
  82. // TODO - Consider caching custom attributes or just initializing them all at load
  83. MonoCustomAttrInfo* attrInfo = mono_custom_attrs_from_method(mMethod);
  84. if (attrInfo == nullptr)
  85. return nullptr;
  86. MonoObject* foundAttr = mono_custom_attrs_get_attr(attrInfo, monoClass->_getInternalClass());
  87. mono_custom_attrs_free(attrInfo);
  88. return foundAttr;
  89. }
  90. void MonoMethod::cacheSignature() const
  91. {
  92. MonoMethodSignature* methodSignature = mono_method_signature(mMethod);
  93. MonoType* returnType = mono_signature_get_return_type(methodSignature);
  94. if (returnType != nullptr)
  95. {
  96. ::MonoClass* returnClass = mono_class_from_mono_type(returnType);
  97. if (returnClass != nullptr)
  98. mCachedReturnType = MonoManager::instance().findClass(returnClass);
  99. }
  100. mCachedNumParameters = (UINT32)mono_signature_get_param_count(methodSignature);
  101. if (mCachedParameters != nullptr)
  102. {
  103. bs_free(mCachedParameters);
  104. mCachedParameters = nullptr;
  105. }
  106. if (mCachedNumParameters > 0)
  107. {
  108. mCachedParameters = (MonoClass**)bs_alloc(mCachedNumParameters * sizeof(MonoClass*));
  109. void* iter = nullptr;
  110. for (UINT32 i = 0; i < mCachedNumParameters; i++)
  111. {
  112. MonoType* curParamType = mono_signature_get_params(methodSignature, &iter);
  113. ::MonoClass* rawClass = mono_class_from_mono_type(curParamType);
  114. mCachedParameters[i] = MonoManager::instance().findClass(rawClass);
  115. }
  116. }
  117. mIsStatic = !mono_signature_is_instance(methodSignature);
  118. mHasCachedSignature = true;
  119. }
  120. }