Avoid pre-allocating custom attributes for managed types
This commit is contained in:
@@ -1365,6 +1365,18 @@ namespace FlaxEngine
|
||||
*classAttributesCount = attributeTypes.Length;
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
internal static IntPtr GetCustomAttribute(IntPtr typeHandle, IntPtr attribHandle)
|
||||
{
|
||||
Type type = (Type)GCHandle.FromIntPtr(typeHandle).Target;
|
||||
Type attribType = (Type)GCHandle.FromIntPtr(attribHandle).Target;
|
||||
object attrib = type.GetCustomAttributes(false).FirstOrDefault(x => x.GetType() == attribType);
|
||||
|
||||
if (attrib != null)
|
||||
return GCHandle.ToIntPtr(GCHandle.Alloc(attrib, GCHandleType.Weak));
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
|
||||
[UnmanagedCallersOnly]
|
||||
internal static void GetClassInterfaces(IntPtr typeHandle, IntPtr* classInterfaces, int* classInterfacesCount)
|
||||
{
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#include <ThirdParty/mono-2.0/mono/metadata/blob.h>
|
||||
|
||||
#include "Engine/Core/Types/String.h"
|
||||
#include "Engine/Core/Collections/Array.h"
|
||||
#include "Engine/Scripting/Types.h"
|
||||
|
||||
#if defined(_WIN32)
|
||||
@@ -46,4 +47,9 @@ public:
|
||||
static gchandle NewGCHandleWeakref(void* obj, bool track_resurrection);
|
||||
static void* GetGCHandleTarget(const gchandle& gchandle);
|
||||
static void FreeGCHandle(const gchandle& gchandle);
|
||||
|
||||
static bool HasCustomAttribute(void* klass, void* attribClass);
|
||||
static bool HasCustomAttribute(void* klass);
|
||||
static void* GetCustomAttribute(void* klass, void* attribClass);
|
||||
static Array<void*> GetCustomAttributes(void* klass);
|
||||
};
|
||||
|
||||
@@ -587,6 +587,30 @@ const char* CoreCLR::GetClassFullname(void* klass)
|
||||
return ((CoreCLRClass*)klass)->GetFullname().Get();
|
||||
}
|
||||
|
||||
bool CoreCLR::HasCustomAttribute(void* klass, void* attribClass)
|
||||
{
|
||||
return CoreCLR::GetCustomAttribute(klass, attribClass) != nullptr;
|
||||
}
|
||||
bool CoreCLR::HasCustomAttribute(void* klass)
|
||||
{
|
||||
return CoreCLR::GetCustomAttribute(klass, nullptr) != nullptr;
|
||||
}
|
||||
void* CoreCLR::GetCustomAttribute(void* klass, void* attribClass)
|
||||
{
|
||||
return CoreCLR::CallStaticMethodInternal<void*, void*, void*>(TEXT("GetCustomAttribute"), ((CoreCLRClass*)klass)->GetTypeHandle(), ((CoreCLRClass*)attribClass)->GetTypeHandle());
|
||||
}
|
||||
Array<void*> CoreCLR::GetCustomAttributes(void* klass)
|
||||
{
|
||||
Array<CoreCLRCustomAttribute*> attrib = ((CoreCLRClass*)klass)->GetCustomAttributes();
|
||||
|
||||
Array<void*> attributes;
|
||||
attributes.Resize(attrib.Count(), false);
|
||||
for (int i = 0; i < attrib.Count(); i++)
|
||||
attributes.Add(attrib[i]->GetHandle());
|
||||
|
||||
return attributes;
|
||||
}
|
||||
|
||||
/*
|
||||
* loader.h
|
||||
*/
|
||||
|
||||
@@ -14,6 +14,9 @@
|
||||
#include <ThirdParty/mono-2.0/mono/metadata/attrdefs.h>
|
||||
#define GET_CUSTOM_ATTR() (MonoCustomAttrInfo*)(_attrInfo ? _attrInfo : _attrInfo = mono_custom_attrs_from_class(_monoClass))
|
||||
#endif
|
||||
#if USE_NETCORE
|
||||
#include "Engine/Scripting/DotNet/CoreCLR.h"
|
||||
#endif
|
||||
|
||||
#if USE_MONO
|
||||
MClass::MClass(const MAssembly* parentAssembly, MonoClass* monoClass, const MString& fullname)
|
||||
@@ -375,7 +378,9 @@ MObject* MClass::CreateInstance(void** params, uint32 numParams)
|
||||
|
||||
bool MClass::HasAttribute(const MClass* monoClass) const
|
||||
{
|
||||
#if USE_MONO
|
||||
#if USE_NETCORE
|
||||
return CoreCLR::HasCustomAttribute(_monoClass, monoClass->GetNative());
|
||||
#elif USE_MONO
|
||||
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
|
||||
return attrInfo != nullptr && mono_custom_attrs_has_attr(attrInfo, monoClass->GetNative()) != 0;
|
||||
#else
|
||||
@@ -385,7 +390,9 @@ bool MClass::HasAttribute(const MClass* monoClass) const
|
||||
|
||||
bool MClass::HasAttribute() const
|
||||
{
|
||||
#if USE_MONO
|
||||
#if USE_NETCORE
|
||||
return CoreCLR::HasCustomAttribute(_monoClass);
|
||||
#elif USE_MONO
|
||||
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
|
||||
return attrInfo && attrInfo->num_attrs > 0;
|
||||
#else
|
||||
@@ -395,7 +402,9 @@ bool MClass::HasAttribute() const
|
||||
|
||||
MObject* MClass::GetAttribute(const MClass* monoClass) const
|
||||
{
|
||||
#if USE_MONO
|
||||
#if USE_NETCORE
|
||||
return (MObject*)CoreCLR::GetCustomAttribute(_monoClass, monoClass->GetNative());
|
||||
#elif USE_MONO
|
||||
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
|
||||
return attrInfo ? mono_custom_attrs_get_attr(attrInfo, monoClass->GetNative()) : nullptr;
|
||||
#else
|
||||
@@ -409,7 +418,9 @@ const Array<MObject*>& MClass::GetAttributes()
|
||||
return _attributes;
|
||||
|
||||
_hasCachedAttributes = true;
|
||||
#if USE_MONO
|
||||
#if USE_NETCORE
|
||||
_attributes = *(Array<MObject*>*)(&CoreCLR::GetCustomAttributes(_monoClass));
|
||||
#elif USE_MONO
|
||||
MonoCustomAttrInfo* attrInfo = GET_CUSTOM_ATTR();
|
||||
if (attrInfo == nullptr)
|
||||
return _attributes;
|
||||
|
||||
Reference in New Issue
Block a user