2019-08-23 11:56:54 -07:00
|
|
|
using System;
|
|
|
|
using System.Reflection;
|
|
|
|
using System.FFI;
|
|
|
|
using System.Diagnostics;
|
2020-04-29 06:40:03 -07:00
|
|
|
using System.Collections;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
namespace System.Reflection
|
|
|
|
{
|
|
|
|
[CRepr, AlwaysInclude]
|
|
|
|
public struct MethodInfo
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
TypeInstance mTypeInstance;
|
|
|
|
TypeInstance.MethodData* mMethodData;
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
public StringView Name
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
return mMethodData.[Friend]mName;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int ParamCount
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
return mMethodData.[Friend]mParamCount;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsConstructor
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// String pooling allows us to do identity comparison
|
|
|
|
return (Object)mMethodData.mName == "__BfCtor" || ((Object)mMethodData.mName == "__BfStaticCtor");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsDestructor
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
// String pooling allows us to do identity comparison
|
|
|
|
return (Object)mMethodData.mName == "__BfStaticDtor" || (Object)mMethodData.mName == "__BfStaticDtor";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Type GetParamType(int paramIdx)
|
|
|
|
{
|
|
|
|
Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
|
2020-03-09 06:34:16 -07:00
|
|
|
return Type.[Friend]GetType(mMethodData.mParamData[paramIdx].mType);
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public StringView GetParamName(int paramIdx)
|
|
|
|
{
|
|
|
|
Debug.Assert((uint)paramIdx < (uint)mMethodData.mParamCount);
|
|
|
|
return mMethodData.mParamData[paramIdx].mName;
|
|
|
|
}
|
|
|
|
|
|
|
|
public this(TypeInstance typeInstance, TypeInstance.MethodData* methodData)
|
|
|
|
{
|
|
|
|
mTypeInstance = typeInstance;
|
|
|
|
mMethodData = methodData;
|
|
|
|
}
|
|
|
|
|
|
|
|
public enum CallError
|
|
|
|
{
|
|
|
|
case None;
|
|
|
|
case TargetExpected;
|
|
|
|
case TargetNotExpected;
|
|
|
|
case InvalidTarget;
|
|
|
|
case InvalidArgument(int32 paramIdx);
|
|
|
|
case ParamCountMismatch;
|
2019-08-29 14:19:07 -07:00
|
|
|
case FFIError;
|
2019-08-23 11:56:54 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public Result<Variant, CallError> Invoke(Object target, params Object[] args)
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
var retType = Type.[Friend]GetType(mMethodData.mReturnType);
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
FFIABI abi = .Default;
|
2019-12-21 05:48:44 -08:00
|
|
|
#if BF_PLATFORM_WINDOWS && BF_32_BIT
|
|
|
|
if (mMethodData.mFlags.HasFlag(.ThisCall))
|
|
|
|
abi = .ThisCall;
|
|
|
|
else if (!mMethodData.mFlags.HasFlag(.Static))
|
2019-08-23 11:56:54 -07:00
|
|
|
abi = .StdCall;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
List<FFIType*> ffiParamList = scope .(16);
|
|
|
|
List<void*> ffiArgList = scope .(16);
|
|
|
|
List<Variant> tempVariants = scope .(4);
|
|
|
|
|
|
|
|
var target;
|
|
|
|
|
|
|
|
mixin GetFFIType(Type type)
|
|
|
|
{
|
|
|
|
int wantSize = 0;
|
|
|
|
FFIType* ffiType = FFIType.Get(type, null, &wantSize);
|
|
|
|
if ((ffiType == null) && (wantSize != 0))
|
|
|
|
{
|
|
|
|
void* allocBytes = scope:mixin uint8[wantSize]*;
|
|
|
|
ffiType = FFIType.Get(type, allocBytes, &wantSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
ffiType
|
|
|
|
}
|
|
|
|
|
|
|
|
void SplatArg(TypeInstance type, void* ptr)
|
|
|
|
{
|
|
|
|
if (type.BaseType != null)
|
|
|
|
SplatArg(type.BaseType, ptr);
|
|
|
|
|
|
|
|
bool isEnum = type.IsEnum;
|
2020-03-09 06:34:16 -07:00
|
|
|
for (int fieldIdx < type.[Friend]mFieldDataCount)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
let fieldData = ref type.[Friend]mFieldDataPtr[fieldIdx];
|
|
|
|
let fieldType = Type.[Friend]GetType(fieldData.mFieldTypeId);
|
2019-08-23 11:56:54 -07:00
|
|
|
if (fieldData.mFlags.HasFlag(.Static))
|
|
|
|
{
|
|
|
|
if (isEnum)
|
|
|
|
break; // Already got payload and discriminator
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-09 06:34:16 -07:00
|
|
|
if (fieldType.[Friend]mSize == 0)
|
2019-08-23 11:56:54 -07:00
|
|
|
continue;
|
|
|
|
|
|
|
|
if (fieldType.IsStruct)
|
|
|
|
{
|
|
|
|
SplatArg((TypeInstance)fieldType, (uint8*)ptr + fieldData.mDataOffset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ffiParamList.Add(FFIType.Get(fieldType, null, null));
|
|
|
|
ffiArgList.Add((uint8*)ptr + fieldData.mDataOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mixin AddArg(int argIdx, Object arg, void* argPtr, Type paramType, bool splat)
|
|
|
|
{
|
|
|
|
bool unbox = false;
|
|
|
|
bool unboxToPtr = false;
|
|
|
|
|
2020-03-09 06:34:16 -07:00
|
|
|
let argType = arg.[Friend]RawGetType();
|
|
|
|
void* dataPtr = (uint8*)Internal.UnsafeCastToPtr(arg) + argType.[Friend]mMemberDataOffset;
|
2019-08-23 11:56:54 -07:00
|
|
|
bool isValid = true;
|
|
|
|
|
|
|
|
if (paramType.IsValueType)
|
|
|
|
{
|
|
|
|
bool handled = true;
|
|
|
|
|
|
|
|
if (!argType.IsBoxed)
|
|
|
|
return .Err(.InvalidArgument((.)argIdx));
|
|
|
|
|
|
|
|
Type underlyingType = argType.UnderlyingType;
|
|
|
|
if ((paramType.IsPrimitive) && (underlyingType.IsTypedPrimitive)) // Boxed primitive?
|
|
|
|
underlyingType = underlyingType.UnderlyingType;
|
2019-10-09 16:16:01 -07:00
|
|
|
|
|
|
|
if (argType.IsBoxedStructPtr)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
2019-10-09 16:16:01 -07:00
|
|
|
dataPtr = *(void**)dataPtr;
|
|
|
|
handled = true;
|
|
|
|
}
|
2019-10-11 05:58:08 -07:00
|
|
|
|
2019-08-23 11:56:54 -07:00
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
if (!underlyingType.IsSubtypeOf(paramType))
|
|
|
|
{
|
|
|
|
if (Convert.ConvertTo(arg, paramType) case .Ok(var variant))
|
|
|
|
{
|
|
|
|
tempVariants.Add(variant);
|
|
|
|
dataPtr = variant.GetValueData();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
isValid = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!argType.IsSubtypeOf(paramType))
|
|
|
|
isValid = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isValid)
|
|
|
|
{
|
|
|
|
if (argIdx == -1)
|
|
|
|
return .Err(.InvalidTarget);
|
|
|
|
else
|
|
|
|
return .Err(.InvalidArgument((.)argIdx));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (paramType.IsStruct)
|
|
|
|
{
|
|
|
|
TypeInstance paramTypeInst = (TypeInstance)paramType;
|
|
|
|
|
|
|
|
if (paramType.Size == 0)
|
|
|
|
{
|
|
|
|
// Do nothing
|
|
|
|
}
|
|
|
|
else if (splat)
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
if (paramTypeInst.[Friend]mFieldDataCount > 0)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
SplatArg(paramTypeInst, dataPtr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
let splatData = (TypeInstance.FieldSplatData*)paramTypeInst.[Friend]mFieldDataPtr;
|
2019-08-23 11:56:54 -07:00
|
|
|
for (int splatIdx < 3)
|
|
|
|
{
|
|
|
|
let splatTypeId = splatData.mSplatTypes[splatIdx];
|
|
|
|
if (splatTypeId == 0)
|
|
|
|
break;
|
|
|
|
|
2020-03-09 06:34:16 -07:00
|
|
|
let splatType = Type.[Friend]GetType(splatTypeId);
|
2019-08-23 11:56:54 -07:00
|
|
|
ffiParamList.Add(GetFFIType!:mixin(splatType));
|
|
|
|
ffiArgList.Add((uint8*)dataPtr + splatData.mSplatOffsets[splatIdx]);
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Pass by ref
|
|
|
|
ffiParamList.Add(&FFIType.Pointer);
|
|
|
|
unboxToPtr = true;
|
|
|
|
unbox = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (paramType.IsValueType)
|
|
|
|
{
|
|
|
|
ffiParamList.Add(GetFFIType!:mixin(paramType));
|
|
|
|
unbox = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ffiParamList.Add(&FFIType.Pointer);
|
|
|
|
ffiArgList.Add(argPtr);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unbox)
|
|
|
|
{
|
|
|
|
if (unboxToPtr)
|
|
|
|
{
|
|
|
|
int* stackDataPtr = scope:mixin int;
|
|
|
|
*stackDataPtr = (int)dataPtr;
|
|
|
|
ffiArgList.Add(stackDataPtr);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ffiArgList.Add(dataPtr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mMethodData.mFlags.HasFlag(.Static))
|
|
|
|
{
|
|
|
|
if (target != null)
|
|
|
|
return .Err(.TargetNotExpected);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (target == null)
|
|
|
|
return .Err(.TargetExpected);
|
|
|
|
|
|
|
|
bool splatThis = mTypeInstance.IsSplattable && !mMethodData.mFlags.HasFlag(.Mutating);
|
|
|
|
AddArg!::(-1, target, &target, mTypeInstance, splatThis);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (args.Count != mMethodData.mParamCount)
|
|
|
|
return .Err(.ParamCountMismatch);
|
|
|
|
|
|
|
|
Variant retVal;
|
|
|
|
void* variantData = Variant.Alloc(retType, out retVal);
|
|
|
|
void* retData = variantData;
|
|
|
|
|
|
|
|
// Struct return? Manually add it as an arg after 'this'. Revisit this - this is architecture-dependent.
|
|
|
|
int unusedRetVal;
|
|
|
|
FFIType* ffiRetType = null;
|
|
|
|
if (retType.IsStruct)
|
|
|
|
{
|
|
|
|
ffiRetType = &FFIType.Void;
|
|
|
|
ffiParamList.Add(&FFIType.Pointer);
|
|
|
|
ffiArgList.Add(&variantData);
|
|
|
|
retData = &unusedRetVal;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ffiRetType = GetFFIType!::(retType);
|
|
|
|
|
|
|
|
for (var arg in ref args)
|
|
|
|
{
|
|
|
|
let paramData = ref mMethodData.mParamData[@arg];
|
2020-03-09 06:34:16 -07:00
|
|
|
let argType = Type.[Friend]GetType(paramData.mType);
|
2019-08-23 11:56:54 -07:00
|
|
|
AddArg!::(@arg, arg, &arg, argType, paramData.mParamFlags.HasFlag(.Splat));
|
|
|
|
}
|
|
|
|
|
|
|
|
FFICaller caller = .();
|
|
|
|
if (ffiParamList.Count > 0)
|
2019-08-29 14:19:07 -07:00
|
|
|
{
|
|
|
|
if (caller.Prep(abi, (.)ffiParamList.Count, ffiRetType, &ffiParamList[0]) case .Err)
|
|
|
|
return .Err(.FFIError);
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
else
|
2019-08-29 14:19:07 -07:00
|
|
|
{
|
|
|
|
if (caller.Prep(abi, 0, ffiRetType, null) case .Err)
|
|
|
|
return .Err(.FFIError);
|
|
|
|
}
|
2019-08-23 11:56:54 -07:00
|
|
|
|
|
|
|
void* funcPtr = mMethodData.mFuncPtr;
|
|
|
|
if (mMethodData.mFlags.HasFlag(.Virtual))
|
|
|
|
{
|
|
|
|
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
|
|
|
|
void* classVData = (void*)(target.[Friend]mClassVData & ~0xFF);
|
|
|
|
#else
|
|
|
|
void* classVData = target.[Friend]mClassVData;
|
|
|
|
#endif
|
|
|
|
if (mMethodData.mVirtualIdx >= 0x100000)
|
|
|
|
{
|
|
|
|
void* extAddr = (void*)*((int*)classVData + (mMethodData.mVirtualIdx>>20 - 1));
|
|
|
|
funcPtr = (void*)*((int*)extAddr + (mMethodData.mVirtualIdx & 0xFFFFF));
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
funcPtr = (void*)*(int*)((uint8*)classVData + mMethodData.mVirtualIdx);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ffiArgList.Count > 0)
|
|
|
|
caller.Call(funcPtr, retData, &ffiArgList[0]);
|
|
|
|
else
|
|
|
|
caller.Call(funcPtr, retData, null);
|
|
|
|
|
|
|
|
for (var variant in ref tempVariants)
|
|
|
|
variant.Dispose();
|
|
|
|
|
|
|
|
return retVal;
|
|
|
|
}
|
|
|
|
|
2020-03-09 06:34:16 -07:00
|
|
|
public struct Enumerator : IEnumerator<MethodInfo>
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
BindingFlags mBindingFlags;
|
|
|
|
TypeInstance mTypeInstance;
|
|
|
|
int32 mIdx;
|
|
|
|
|
2020-03-09 06:34:16 -07:00
|
|
|
public this(TypeInstance typeInst, BindingFlags bindingFlags)
|
2019-08-23 11:56:54 -07:00
|
|
|
{
|
|
|
|
mTypeInstance = typeInst;
|
|
|
|
mBindingFlags = bindingFlags;
|
|
|
|
mIdx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Reset() mut
|
|
|
|
{
|
|
|
|
mIdx = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void Dispose()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool MoveNext() mut
|
|
|
|
{
|
|
|
|
if (mTypeInstance == null)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
for (;;)
|
|
|
|
{
|
|
|
|
mIdx++;
|
2020-03-09 06:34:16 -07:00
|
|
|
if (mIdx == mTypeInstance.[Friend]mMethodDataCount)
|
2019-08-23 11:56:54 -07:00
|
|
|
return false;
|
|
|
|
#unwarn
|
2020-03-09 06:34:16 -07:00
|
|
|
var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
|
2019-08-23 11:56:54 -07:00
|
|
|
/*bool matches = (mBindingFlags.HasFlag(BindingFlags.Static) && (methodData.mFlags.HasFlag(FieldFlags.Static)));
|
|
|
|
matches |= (mBindingFlags.HasFlag(BindingFlags.Instance) && (!methodData.mFlags.HasFlag(FieldFlags.Static)));*/
|
|
|
|
bool matches = true;
|
|
|
|
if (matches)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public MethodInfo Current
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-03-09 06:34:16 -07:00
|
|
|
var methodData = &mTypeInstance.[Friend]mMethodDataPtr[mIdx];
|
2019-08-23 11:56:54 -07:00
|
|
|
return MethodInfo(mTypeInstance, methodData);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public Result<MethodInfo> GetNext() mut
|
|
|
|
{
|
|
|
|
if (!MoveNext())
|
|
|
|
return .Err;
|
|
|
|
return Current;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|