mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-17 15:46:05 +02:00
Added Variant support to reflected method invocation
This commit is contained in:
parent
5bdaeadc25
commit
2eb7ce3e1a
7 changed files with 387 additions and 7 deletions
|
@ -53,6 +53,259 @@ namespace System.Reflection
|
|||
case FFIError;
|
||||
}
|
||||
|
||||
public Result<Variant, CallError> Invoke(Variant target, params Span<Variant> args)
|
||||
{
|
||||
var retType = Type.[Friend]GetType(mMethodData.mReturnType);
|
||||
|
||||
FFIABI abi = .Default;
|
||||
#if BF_PLATFORM_WINDOWS && BF_32_BIT
|
||||
if (mMethodData.mFlags.HasFlag(.ThisCall))
|
||||
abi = .ThisCall;
|
||||
else if (!mMethodData.mFlags.HasFlag(.Static))
|
||||
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;
|
||||
for (int fieldIdx < type.[Friend]mFieldDataCount)
|
||||
{
|
||||
let fieldData = ref type.[Friend]mFieldDataPtr[fieldIdx];
|
||||
let fieldType = Type.[Friend]GetType(fieldData.mFieldTypeId);
|
||||
if (fieldData.mFlags.HasFlag(.Static))
|
||||
{
|
||||
if (isEnum)
|
||||
break; // Already got payload and discriminator
|
||||
continue;
|
||||
}
|
||||
if (fieldType.[Friend]mSize == 0)
|
||||
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, var arg, void* argPtr, Type paramType, bool splat)
|
||||
{
|
||||
var argType = arg.VariantType;
|
||||
void* dataPtr = arg.DataPtr;
|
||||
bool isPtrToPtr = false;
|
||||
bool isValid = true;
|
||||
|
||||
if (paramType.IsValueType)
|
||||
{
|
||||
if (argType.IsPointer)
|
||||
{
|
||||
if (!paramType.IsPointer)
|
||||
{
|
||||
isPtrToPtr = true;
|
||||
argType = argType.UnderlyingType;
|
||||
}
|
||||
/*else
|
||||
{
|
||||
dataPtr = *(void**)dataPtr;
|
||||
}*/
|
||||
}
|
||||
|
||||
if (!argType.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)
|
||||
{
|
||||
if (isPtrToPtr)
|
||||
dataPtr = *((void**)dataPtr);
|
||||
|
||||
if (paramTypeInst.[Friend]mFieldDataCount > 0)
|
||||
{
|
||||
SplatArg(paramTypeInst, dataPtr);
|
||||
}
|
||||
else
|
||||
{
|
||||
let splatData = (TypeInstance.FieldSplatData*)paramTypeInst.[Friend]mFieldDataPtr;
|
||||
for (int splatIdx < 3)
|
||||
{
|
||||
let splatTypeId = splatData.mSplatTypes[splatIdx];
|
||||
if (splatTypeId == 0)
|
||||
break;
|
||||
|
||||
let splatType = Type.[Friend]GetType(splatTypeId);
|
||||
ffiParamList.Add(GetFFIType!:mixin(splatType));
|
||||
ffiArgList.Add((uint8*)dataPtr + splatData.mSplatOffsets[splatIdx]);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!isPtrToPtr)
|
||||
{
|
||||
int* stackDataPtr = scope:mixin int();
|
||||
*stackDataPtr = (int)dataPtr;
|
||||
ffiArgList.Add(stackDataPtr);
|
||||
}
|
||||
else
|
||||
ffiArgList.Add(dataPtr);
|
||||
// Pass by ref
|
||||
ffiParamList.Add(&FFIType.Pointer);
|
||||
}
|
||||
}
|
||||
else if (paramType.IsValueType)
|
||||
{
|
||||
ffiParamList.Add(GetFFIType!:mixin(paramType));
|
||||
ffiArgList.Add(dataPtr);
|
||||
}
|
||||
else
|
||||
{
|
||||
ffiParamList.Add(&FFIType.Pointer);
|
||||
ffiArgList.Add(argPtr);
|
||||
}
|
||||
}
|
||||
|
||||
if (mMethodData.mFlags.HasFlag(.Static))
|
||||
{
|
||||
if (target.HasValue)
|
||||
return .Err(.TargetNotExpected);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (!target.HasValue)
|
||||
return .Err(.TargetExpected);
|
||||
|
||||
bool splatThis = mTypeInstance.IsSplattable && !mMethodData.mFlags.HasFlag(.Mutating);
|
||||
AddArg!::(-1, ref target, &target, mTypeInstance, splatThis);
|
||||
}
|
||||
|
||||
if (args.Length != 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.Index];
|
||||
let argType = Type.[Friend]GetType(paramData.mType);
|
||||
AddArg!::(@arg.Index, ref arg, &arg, argType, paramData.mParamFlags.HasFlag(.Splat));
|
||||
}
|
||||
|
||||
FFICaller caller = .();
|
||||
if (ffiParamList.Count > 0)
|
||||
{
|
||||
if (caller.Prep(abi, (.)ffiParamList.Count, ffiRetType, &ffiParamList[0]) case .Err)
|
||||
return .Err(.FFIError);
|
||||
}
|
||||
else
|
||||
{
|
||||
if (caller.Prep(abi, 0, ffiRetType, null) case .Err)
|
||||
return .Err(.FFIError);
|
||||
}
|
||||
|
||||
void* funcPtr = mMethodData.mFuncPtr;
|
||||
if (mMethodData.mFlags.HasFlag(.Virtual))
|
||||
{
|
||||
Object objTarget = target.Get<Object>();
|
||||
|
||||
#if BF_ENABLE_OBJECT_DEBUG_FLAGS
|
||||
void* classVData = (void*)(objTarget.[Friend]mClassVData & ~0xFF);
|
||||
#else
|
||||
void* classVData = objTarget.[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;
|
||||
}
|
||||
|
||||
public Result<Variant, CallError> Invoke(Object target, params Object[] args)
|
||||
{
|
||||
var retType = Type.[Friend]GetType(mMethodData.mReturnType);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue