Skip to content

Commit

Permalink
Fixed a bug with ValueType binder
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Sep 4, 2018
1 parent 71264a9 commit 6c6f33c
Show file tree
Hide file tree
Showing 16 changed files with 819 additions and 24 deletions.
10 changes: 9 additions & 1 deletion ILRuntime/Runtime/Intepreter/ILIntepreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4168,11 +4168,19 @@ void CloneStackValueType(StackObject* src, StackObject* dst, IList<object> mStac
}
}

bool CanCastTo(StackObject* src, StackObject* dst)
{
var sType = AppDomain.GetType(src->Value);
var dType = AppDomain.GetType(dst->Value);

return sType.CanAssignTo(dType);
}

void CopyStackValueType(StackObject* src, StackObject* dst, IList<object> mStack)
{
StackObject* descriptor = *(StackObject**)&src->Value;
StackObject* dstDescriptor = *(StackObject**)&dst->Value;
if (descriptor->Value != dstDescriptor->Value)
if (!CanCastTo(descriptor, dstDescriptor))
throw new InvalidCastException();
int cnt = descriptor->ValueLow;
for(int i = 0; i < cnt; i++)
Expand Down
35 changes: 33 additions & 2 deletions ILRuntimeTest/Adapters/TestVector3Binder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public override void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDoma
method = type.GetMethod("op_Multiply", flag, null, args, null);
appdomain.RegisterCLRMethodRedirection(method, Vector3_Multiply);

args = new Type[] { };
args = new Type[] { };
method = type.GetMethod("get_One2", flag, null, args, null);
appdomain.RegisterCLRMethodRedirection(method, Vector3_One2);
}
Expand Down Expand Up @@ -192,7 +192,7 @@ public override void RegisterCLRRedirection(ILRuntime.Runtime.Enviorment.AppDoma
}
else
{

}
}
return ret;
Expand Down Expand Up @@ -247,4 +247,35 @@ public override unsafe void AssignFromStack(ref TestVectorStruct2 ins, StackObje
AssignFromStack(ref ins.Vector, v, mStack);
}
}

public unsafe class KeyValuePairUInt32ILTypeInstanceBinder : ValueTypeBinder<KeyValuePair<UInt32, ILTypeInstance>>
{
public override unsafe void AssignFromStack(ref KeyValuePair<UInt32, ILTypeInstance> ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
var key = *(UInt32*)&v->Value;
v = ILIntepreter.Minus(ptr, 2);
object val = mStack[v->Value];
ins = new KeyValuePair<uint, ILTypeInstance>(key, (ILTypeInstance)val);
}

public override unsafe void CopyValueTypeToStack(ref KeyValuePair<UInt32, ILTypeInstance> ins, StackObject* ptr, IList<object> mStack)
{
var v = ILIntepreter.Minus(ptr, 1);
*(UInt32*)&v->Value = ins.Key;
v = ILIntepreter.Minus(ptr, 2);
mStack[v->Value] = ins.Value;
}
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(KeyValuePair<UInt32, ILTypeInstance>);
args = new Type[] { typeof(UInt32), typeof(ILTypeInstance) };
method = type.GetConstructor(flag, null, args, null);
//appdomain.RegisterCLRMethodRedirection(method, NewKV);
//_appdomain_ = appdomain;
}
}
}
1 change: 1 addition & 0 deletions ILRuntimeTest/Adapters/helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public static void Init(ILRuntime.Runtime.Enviorment.AppDomain app)
app.RegisterValueTypeBinder(typeof(TestVector3), new TestVector3Binder());
app.RegisterValueTypeBinder(typeof(TestVectorStruct), new TestVectorStructBinder());
app.RegisterValueTypeBinder(typeof(TestVectorStruct2), new TestVectorStruct2Binder());
app.RegisterValueTypeBinder(typeof(System.Collections.Generic.KeyValuePair<uint, ILRuntime.Runtime.Intepreter.ILTypeInstance>), new KeyValuePairUInt32ILTypeInstanceBinder());

// delegate register

Expand Down
12 changes: 12 additions & 0 deletions ILRuntimeTest/AutoGenerate/CLRBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class CLRBindings
internal static ILRuntime.Runtime.Enviorment.ValueTypeBinder<ILRuntimeTest.TestFramework.TestVector3> s_ILRuntimeTest_TestFramework_TestVector3_Binding_Binder = null;
internal static ILRuntime.Runtime.Enviorment.ValueTypeBinder<ILRuntimeTest.TestFramework.TestVectorStruct> s_ILRuntimeTest_TestFramework_TestVectorStruct_Binding_Binder = null;
internal static ILRuntime.Runtime.Enviorment.ValueTypeBinder<ILRuntimeTest.TestFramework.TestVectorStruct2> s_ILRuntimeTest_TestFramework_TestVectorStruct2_Binding_Binder = null;
internal static ILRuntime.Runtime.Enviorment.ValueTypeBinder<System.Collections.Generic.KeyValuePair<System.UInt32, ILRuntime.Runtime.Intepreter.ILTypeInstance>> s_System_Collections_Generic_KeyValuePair_2_UInt32_ILTypeInstance_Binding_Binder = null;

/// <summary>
/// Initialize the CLR binding, please invoke this AFTER CLR Redirection registration
Expand Down Expand Up @@ -45,6 +46,10 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
System_Reflection_FieldInfo_Binding.Register(app);
System_Exception_Binding.Register(app);
System_Reflection_PropertyInfo_Binding.Register(app);
ILRuntimeTest_TestMainForm_Binding.Register(app);
ILRuntime_Runtime_Enviorment_AppDomain_Binding.Register(app);
System_Collections_Generic_KeyValuePair_2_String_IType_Binding.Register(app);
ILRuntime_CLR_TypeSystem_IType_Binding.Register(app);
System_Collections_Generic_List_1_ILRuntimeTest_TestFramework_ClassInheritanceTestAdaptor_Binding_Adaptor_Binding.Register(app);
ILRuntimeTest_TestFramework_ClassInheritanceTest2_1_ILRuntimeTest_TestFramework_ClassInheritanceTest2Adaptor_Binding_Adaptor_Binding.Register(app);
ILRuntimeTest_TestFramework_TestClass2_Binding.Register(app);
Expand All @@ -62,6 +67,7 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
ILRuntimeTest_TestFramework_TestStruct_Binding.Register(app);
ILRuntimeTest_TestFramework_TestClass3_Binding.Register(app);
System_Byte_Binding.Register(app);
System_Char_Binding.Register(app);
System_IO_File_Binding.Register(app);
System_IO_FileStream_Binding.Register(app);
System_IO_Stream_Binding.Register(app);
Expand Down Expand Up @@ -105,6 +111,9 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
ILRuntimeTest_TestFramework_TestVectorStruct2_Binding.Register(app);
System_DateTime_Binding.Register(app);
ILRuntimeTest_TestFramework_TestVectorClass_Binding.Register(app);
System_Collections_Generic_Dictionary_2_UInt32_ILTypeInstance_Binding.Register(app);
System_Collections_Generic_Dictionary_2_UInt32_ILTypeInstance_Binding_Enumerator_Binding.Register(app);
System_Collections_Generic_KeyValuePair_2_UInt32_ILTypeInstance_Binding.Register(app);
System_Collections_Generic_Dictionary_2_Object_Object_Binding.Register(app);
System_IComparable_1_Int32_Binding.Register(app);
System_Collections_Generic_Dictionary_2_Int32_Int32_Binding.Register(app);
Expand All @@ -117,6 +126,8 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app)
s_ILRuntimeTest_TestFramework_TestVectorStruct_Binding_Binder = __clrType.ValueTypeBinder as ILRuntime.Runtime.Enviorment.ValueTypeBinder<ILRuntimeTest.TestFramework.TestVectorStruct>;
__clrType = (ILRuntime.CLR.TypeSystem.CLRType)app.GetType (typeof(ILRuntimeTest.TestFramework.TestVectorStruct2));
s_ILRuntimeTest_TestFramework_TestVectorStruct2_Binding_Binder = __clrType.ValueTypeBinder as ILRuntime.Runtime.Enviorment.ValueTypeBinder<ILRuntimeTest.TestFramework.TestVectorStruct2>;
__clrType = (ILRuntime.CLR.TypeSystem.CLRType)app.GetType (typeof(System.Collections.Generic.KeyValuePair<System.UInt32, ILRuntime.Runtime.Intepreter.ILTypeInstance>));
s_System_Collections_Generic_KeyValuePair_2_UInt32_ILTypeInstance_Binding_Binder = __clrType.ValueTypeBinder as ILRuntime.Runtime.Enviorment.ValueTypeBinder<System.Collections.Generic.KeyValuePair<System.UInt32, ILRuntime.Runtime.Intepreter.ILTypeInstance>>;
}

/// <summary>
Expand All @@ -127,6 +138,7 @@ public static void Shutdown(ILRuntime.Runtime.Enviorment.AppDomain app)
s_ILRuntimeTest_TestFramework_TestVector3_Binding_Binder = null;
s_ILRuntimeTest_TestFramework_TestVectorStruct_Binding_Binder = null;
s_ILRuntimeTest_TestFramework_TestVectorStruct2_Binding_Binder = null;
s_System_Collections_Generic_KeyValuePair_2_UInt32_ILTypeInstance_Binding_Binder = null;
}
}
}
46 changes: 46 additions & 0 deletions ILRuntimeTest/AutoGenerate/ILRuntimeTest_TestMainForm_Binding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;

namespace ILRuntime.Runtime.Generated
{
unsafe class ILRuntimeTest_TestMainForm_Binding
{
public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
FieldInfo field;
Type[] args;
Type type = typeof(ILRuntimeTest.TestMainForm);

field = type.GetField("_app", flag);
app.RegisterCLRFieldGetter(field, get__app_0);
app.RegisterCLRFieldSetter(field, set__app_0);


}



static object get__app_0(ref object o)
{
return ILRuntimeTest.TestMainForm._app;
}
static void set__app_0(ref object o, object v)
{
ILRuntimeTest.TestMainForm._app = (ILRuntime.Runtime.Enviorment.AppDomain)v;
}


}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;

namespace ILRuntime.Runtime.Generated
{
unsafe class ILRuntime_CLR_TypeSystem_IType_Binding
{
public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
Type[] args;
Type type = typeof(ILRuntime.CLR.TypeSystem.IType);
args = new Type[]{};
method = type.GetMethod("get_ReflectionType", flag, null, args, null);
app.RegisterCLRMethodRedirection(method, get_ReflectionType_0);


}


static StackObject* get_ReflectionType_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
StackObject* __ret = ILIntepreter.Minus(__esp, 1);

ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
ILRuntime.CLR.TypeSystem.IType instance_of_this_method = (ILRuntime.CLR.TypeSystem.IType)typeof(ILRuntime.CLR.TypeSystem.IType).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
__intp.Free(ptr_of_this_method);

var result_of_this_method = instance_of_this_method.ReflectionType;

object obj_result_of_this_method = result_of_this_method;
if(obj_result_of_this_method is CrossBindingAdaptorType)
{
return ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance);
}
return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);
}



}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;

namespace ILRuntime.Runtime.Generated
{
unsafe class ILRuntime_Runtime_Enviorment_AppDomain_Binding
{
public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
Type[] args;
Type type = typeof(ILRuntime.Runtime.Enviorment.AppDomain);
args = new Type[]{};
method = type.GetMethod("get_LoadedTypes", flag, null, args, null);
app.RegisterCLRMethodRedirection(method, get_LoadedTypes_0);


}


static StackObject* get_LoadedTypes_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
StackObject* __ret = ILIntepreter.Minus(__esp, 1);

ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
ILRuntime.Runtime.Enviorment.AppDomain instance_of_this_method = (ILRuntime.Runtime.Enviorment.AppDomain)typeof(ILRuntime.Runtime.Enviorment.AppDomain).CheckCLRTypes(StackObject.ToObject(ptr_of_this_method, __domain, __mStack));
__intp.Free(ptr_of_this_method);

var result_of_this_method = instance_of_this_method.LoadedTypes;

object obj_result_of_this_method = result_of_this_method;
if(obj_result_of_this_method is CrossBindingAdaptorType)
{
return ILIntepreter.PushObject(__ret, __mStack, ((CrossBindingAdaptorType)obj_result_of_this_method).ILInstance);
}
return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);
}



}
}
95 changes: 95 additions & 0 deletions ILRuntimeTest/AutoGenerate/System_Char_Binding.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.InteropServices;

using ILRuntime.CLR.TypeSystem;
using ILRuntime.CLR.Method;
using ILRuntime.Runtime.Enviorment;
using ILRuntime.Runtime.Intepreter;
using ILRuntime.Runtime.Stack;
using ILRuntime.Reflection;
using ILRuntime.CLR.Utils;

namespace ILRuntime.Runtime.Generated
{
unsafe class System_Char_Binding
{
public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app)
{
BindingFlags flag = BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly;
MethodBase method;
Type[] args;
Type type = typeof(System.Char);
args = new Type[]{};
method = type.GetMethod("ToString", flag, null, args, null);
app.RegisterCLRMethodRedirection(method, ToString_0);


}

static System.Char GetInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, StackObject* ptr_of_this_method, IList<object> __mStack)
{
ptr_of_this_method = ILIntepreter.GetObjectAndResolveReference(ptr_of_this_method);
System.Char instance_of_this_method;
switch(ptr_of_this_method->ObjectType)
{
case ObjectTypes.FieldReference:
{
var instance_of_fieldReference = __mStack[ptr_of_this_method->Value];
if(instance_of_fieldReference is ILTypeInstance)
{
instance_of_this_method = (System.Char)typeof(System.Char).CheckCLRTypes(((ILTypeInstance)instance_of_fieldReference)[ptr_of_this_method->ValueLow]);
}
else
{
var t = __domain.GetType(instance_of_fieldReference.GetType()) as CLRType;
instance_of_this_method = (System.Char)t.GetFieldValue(ptr_of_this_method->ValueLow, instance_of_fieldReference);
}
}
break;
case ObjectTypes.StaticFieldReference:
{
var t = __domain.GetType(ptr_of_this_method->Value);
if(t is ILType)
{
instance_of_this_method = (System.Char)typeof(System.Char).CheckCLRTypes(((ILType)t).StaticInstance[ptr_of_this_method->ValueLow]);
}
else
{
instance_of_this_method = (System.Char)((CLRType)t).GetFieldValue(ptr_of_this_method->ValueLow, null);
}
}
break;
case ObjectTypes.ArrayReference:
{
var instance_of_arrayReference = __mStack[ptr_of_this_method->Value] as System.Char[];
instance_of_this_method = instance_of_arrayReference[ptr_of_this_method->ValueLow];
}
break;
default:
instance_of_this_method = (char)ptr_of_this_method->Value;
break;
}
return instance_of_this_method;
}

static StackObject* ToString_0(ILIntepreter __intp, StackObject* __esp, IList<object> __mStack, CLRMethod __method, bool isNewObj)
{
ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain;
StackObject* ptr_of_this_method;
StackObject* __ret = ILIntepreter.Minus(__esp, 1);

ptr_of_this_method = ILIntepreter.Minus(__esp, 1);
System.Char instance_of_this_method = GetInstance(__domain, ptr_of_this_method, __mStack);

var result_of_this_method = instance_of_this_method.ToString();

return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method);
}



}
}
Loading

0 comments on commit 6c6f33c

Please sign in to comment.