Skip to content

Commit

Permalink
Added test case for Ourpalm#717
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Aug 5, 2022
1 parent 1bf7b7c commit 075a480
Show file tree
Hide file tree
Showing 4 changed files with 333 additions and 0 deletions.
199 changes: 199 additions & 0 deletions ILRuntimeTestBase/Adapters/TestVector3Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,205 @@

namespace ILRuntimeTest.TestFramework
{
public unsafe class Fixed64Binder : ValueTypeBinder<Fixed64>
{
public override unsafe void AssignFromStack(ref Fixed64 ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
ins = new Fixed64(*(long*)&v->Value);
}

public override unsafe void CopyValueTypeToStack(ref Fixed64 ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
*(long*)&v->Value = ins.RawValue;
}
public override void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
Type[] args;
Type type = typeof(Fixed64);
args = new Type[] { typeof(long) };
method = type.GetConstructor(flag, null, args, null);
appdomain.RegisterCLRMethodRedirection(method, NewFixed64);
}

StackObject* NewFixed64(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
{
StackObject* ret;
if (isNewObj)
{
ret = ILIntepreter.Minus(esp, 1);
Fixed64 vec;
var ptr = ILIntepreter.Minus(esp, 1);
var val = *(long*)&ptr->Value;
vec = new Fixed64(val);
PushFixed64(ref vec, intp, ptr, mStack);
}
else
{
ret = ILIntepreter.Minus(esp, 2);
var instance = ILIntepreter.GetObjectAndResolveReference(ret);
var dst = *(StackObject**)&instance->Value;
var f = ILIntepreter.Minus(dst, 1);
var v = ILIntepreter.Minus(esp, 1);
var val = *(long*)&v->Value;
Fixed64 vec = new Fixed64(val);
*(long*)&f->Value = vec.RawValue;
}
return ret;
}

public static void ParseFixed64(out Fixed64 vec, ILIntepreter intp, StackObject* ptr, IList<object> mStack)
{
var a = ILIntepreter.GetObjectAndResolveReference(ptr);
if (a->ObjectType == ObjectTypes.ValueTypeObjectReference)
{
var src = *(StackObject**)&a->Value;
long value = *(long*)&ILIntepreter.Minus(src, 1)->Value;
vec = new Fixed64(value);
intp.FreeStackValueType(ptr);
}
else
{
vec = (Fixed64)StackObject.ToObject(a, intp.AppDomain, mStack);
intp.Free(ptr);
}
}

public void PushFixed64(ref Fixed64 vec, ILIntepreter intp, StackObject* ptr, IList<object> mStack)
{
intp.AllocValueType(ptr, CLRType);
var dst = *((StackObject**)&ptr->Value);
CopyValueTypeToStack(ref vec, dst, mStack);
}
}

public unsafe class FixedVector2Binder : ValueTypeBinder<Fixed64Vector2>
{
public override unsafe void AssignFromStack(ref Fixed64Vector2 ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
ins.x = GetFixed64(v, mStack);
v = ILIntepreter.Minus(ptr, 2);
ins.y = GetFixed64(v, mStack);
}

private Fixed64 GetFixed64(StackObject* ptr, IList<object> mStack)
{
var a = ILIntepreter.GetObjectAndResolveReference(ptr);
Fixed64 res;
if (a->ObjectType == ObjectTypes.ValueTypeObjectReference)
{
var src = *(StackObject**)&a->Value;
var val = *(long*)&ILIntepreter.Minus(src, 1)->Value;
res = new Fixed64(val);
}
else
{
var raw = (Fixed64)StackObject.ToObject(a, domain, mStack);
res = raw;
}
return res;
}

public override unsafe void CopyValueTypeToStack(ref Fixed64Vector2 ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
var fix64Ptr = GetFix64Ptr(v, mStack);
*(long*)&fix64Ptr->Value = ins.x.RawValue;

v = ILIntepreter.Minus(ptr, 2);
fix64Ptr = GetFix64Ptr(v, mStack);
*(long*)&fix64Ptr->Value = ins.y.RawValue;
}

private StackObject* GetFix64Ptr(StackObject* ptr, IList<object> mStack)
{
StackObject* res = null;
var a = ILIntepreter.GetObjectAndResolveReference(ptr);
if (a->ObjectType == ObjectTypes.ValueTypeObjectReference)
{
var src = *(StackObject**)&a->Value;
res = ILIntepreter.Minus(src, 1);
}
return res;
}

public override void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDomain appdomain)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
Type[] args;
Type type = typeof(Fixed64Vector2);
args = new Type[] { typeof(int), typeof(int) };
method = type.GetConstructor(flag, null, args, null);
appdomain.RegisterCLRMethodRedirection(method, NewFixed64Vector2);
}

StackObject* NewFixed64Vector2(ILIntepreter intp, StackObject* esp, IList<object> mStack, CLRMethod method, bool isNewObj)
{
StackObject* ret;
if (isNewObj)
{
ret = ILIntepreter.Minus(esp, 1);
Fixed64Vector2 vec;
var ptr = ILIntepreter.Minus(esp, 1);
var y = ptr->Value;
ptr = ILIntepreter.Minus(esp, 2);
var x = ptr->Value;
vec = new Fixed64Vector2(x, y);

PushVector2(ref vec, intp, ptr, mStack);
}
else
{
ret = ILIntepreter.Minus(esp, 3);
var instance = ILIntepreter.GetObjectAndResolveReference(ret);
var dst = *(StackObject**)&instance->Value;
var ptr = ILIntepreter.Minus(dst, 1);
var f = GetFix64Ptr(ptr, mStack);
var v = ILIntepreter.Minus(esp, 2);
Fixed64 fix = new Fixed64(v->Value);
*(long*)&f->Value = fix.RawValue;

ptr = ILIntepreter.Minus(dst, 2);
f = GetFix64Ptr(ptr, mStack);
v = ILIntepreter.Minus(esp, 1);
fix = new Fixed64(v->Value);
*(long*)&f->Value = fix.RawValue;
}
return ret;
}

public void ParseFixed64Vector2(out Fixed64Vector2 vec, ILIntepreter intp, StackObject* ptr, IList<object> mStack)
{
var a = ILIntepreter.GetObjectAndResolveReference(ptr);
if (a->ObjectType == ObjectTypes.ValueTypeObjectReference)
{
var src = *(StackObject**)&a->Value;
var fixPtr = ILIntepreter.Minus(src, 1);
vec.x = GetFixed64(fixPtr, mStack);
fixPtr = ILIntepreter.Minus(src, 2);
vec.y = GetFixed64(fixPtr, mStack);

intp.FreeStackValueType(ptr);
}
else
{
vec = (Fixed64Vector2)StackObject.ToObject(a, intp.AppDomain, mStack);
intp.Free(ptr);
}
}

public void PushVector2(ref Fixed64Vector2 vec, ILIntepreter intp, StackObject* ptr, IList<object> mStack)
{
intp.AllocValueType(ptr, CLRType);
var dst = *((StackObject**)&ptr->Value);
CopyValueTypeToStack(ref vec, dst, mStack);
}
}
public unsafe class TestStructABinder : ValueTypeBinder<TestStructA>
{
public override unsafe void CopyValueTypeToStack(ref TestStructA ins, StackObject* ptr, IList<object> stack)
Expand Down
10 changes: 10 additions & 0 deletions ILRuntimeTestBase/Adapters/helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ public static void Init(ILRuntime.Runtime.Enviorment.AppDomain app)

app.RegisterValueTypeBinder(typeof(TestStructA), new TestStructABinder());
app.RegisterValueTypeBinder(typeof(TestStructB), new TestStructBBinder());
app.RegisterValueTypeBinder(typeof(Fixed64), new Fixed64Binder());
app.RegisterValueTypeBinder(typeof(Fixed64Vector2), new FixedVector2Binder());

// delegate register

Expand Down Expand Up @@ -63,7 +65,15 @@ public static void Init(ILRuntime.Runtime.Enviorment.AppDomain app)
app.DelegateManager.RegisterFunctionDelegate<ILRuntimeTest.TestFramework.TestVector3, System.Single>();
app.DelegateManager.RegisterMethodDelegate<ILRuntimeTest.TestFramework.TestVector3>();
app.DelegateManager.RegisterFunctionDelegate<System.Reflection.FieldInfo, System.String>();
app.DelegateManager.RegisterFunctionDelegate<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>();
// delegate convertor
app.DelegateManager.RegisterDelegateConvertor<System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>>((act) =>
{
return new System.Comparison<ILRuntime.Runtime.Intepreter.ILTypeInstance>((x, y) =>
{
return ((Func<ILRuntime.Runtime.Intepreter.ILTypeInstance, ILRuntime.Runtime.Intepreter.ILTypeInstance, System.Int32>)act)(x, y);
});
});
app.DelegateManager.RegisterDelegateConvertor<ILRuntimeTest.TestFramework.TestValueTypeDelegate>((act) =>
{
return new ILRuntimeTest.TestFramework.TestValueTypeDelegate((vec) =>
Expand Down
64 changes: 64 additions & 0 deletions ILRuntimeTestBase/TestFramework/TestVector3.cs
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,70 @@ public struct TestStructA
{
public long value;
}
public struct Fixed64Vector2
{
public Fixed64 x;
public Fixed64 y;

public static readonly Fixed64Vector2 Zero2;

public Fixed64Vector2(int x, int y)
{
this.x = new Fixed64(x);
this.y = new Fixed64(y);
}

static Fixed64Vector2()
{
Zero2 = new Fixed64Vector2(0, 0);
}
}


public struct Fixed64
{
private long m_rawValue;

public static readonly Fixed64 Zero;

public Fixed64(long value)
{
m_rawValue = value;
}

public long RawValue
{
get
{
return m_rawValue;
}
}

static Fixed64()
{
Zero = new Fixed64(0);
}

public static bool operator <(Fixed64 x, Fixed64 y)
{
return x.m_rawValue < y.m_rawValue;
}

public static bool operator >(Fixed64 x, Fixed64 y)
{
return x.m_rawValue > y.m_rawValue;
}

public static bool operator ==(Fixed64 x, Fixed64 y)
{
return x.m_rawValue == y.m_rawValue;
}

public static bool operator !=(Fixed64 x, Fixed64 y)
{
return x.m_rawValue != y.m_rawValue;
}
}

public struct TestStructB
{
Expand Down
60 changes: 60 additions & 0 deletions TestCases/TestValueTypeBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -551,5 +551,65 @@ public static void UnitTest_10050()
}
}
}

public static void UnitTest_10051()
{
var trts = new TestRunTimeStack();
trts.Run();
//Debug.LogFormat("RunTest4");
}


public class BaseObj
{
private Fixed64Vector2 fv2;

public BaseObj()
{
int x, y;
x = 123;
y = 456;
fv2 = new Fixed64Vector2(x, y);
}

public Fixed64Vector2 V2
{
get
{
return fv2;
}
}
}

public class TestObj : BaseObj
{
private int n;
}

public class TestRunTimeStack
{
public TestRunTimeStack()
{
}

public void Run()
{
List<TestObj> list = new List<TestObj>();
for (int i = 0; i < 10000; i++)
{
var obj = new TestObj();
list.Add(obj);
}

list.Sort((a, b) => {
if (a.V2.x > b.V2.x)
return 1;
else if (a.V2.x < b.V2.x)
return -1;
return 0;
});
}
}

}
}

0 comments on commit 075a480

Please sign in to comment.