From a6ff9d8754774b186c3265f7eec812d072df2504 Mon Sep 17 00:00:00 2001 From: liiir1985 Date: Thu, 2 Jul 2020 15:56:37 +0800 Subject: [PATCH] fixed #331 --- ILRuntime/CLR/TypeSystem/CLRType.cs | 14 +++--- ILRuntime/Runtime/Intepreter/ILIntepreter.cs | 5 +- ILRuntimeTest/AutoGenerate/CLRBindings.cs | 2 +- ...tFramework_TestVector3NoBinding_Binding.cs | 48 +++++++++++++++++++ TestCases/TestValueTypeBinding.cs | 34 +++++++++++++ 5 files changed, 94 insertions(+), 9 deletions(-) diff --git a/ILRuntime/CLR/TypeSystem/CLRType.cs b/ILRuntime/CLR/TypeSystem/CLRType.cs index 92e2dc5c..704926cf 100644 --- a/ILRuntime/CLR/TypeSystem/CLRType.cs +++ b/ILRuntime/CLR/TypeSystem/CLRType.cs @@ -404,18 +404,20 @@ public void SetStaticFieldValue(int hash, object value) } } - public unsafe void SetFieldValue(int hash, ref object target, object value) + public unsafe void SetFieldValue(int hash, ref object target, object value, bool directSet = false) { if (fieldMapping == null) InitializeFields(); - var setter = GetFieldSetter(hash); - if (setter != null) + if (!directSet) { - setter(ref target, value); - return; + var setter = GetFieldSetter(hash); + if (setter != null) + { + setter(ref target, value); + return; + } } - var fieldInfo = GetField(hash); if (fieldInfo != null) { diff --git a/ILRuntime/Runtime/Intepreter/ILIntepreter.cs b/ILRuntime/Runtime/Intepreter/ILIntepreter.cs index 3d8fc8e8..c9e3bebc 100644 --- a/ILRuntime/Runtime/Intepreter/ILIntepreter.cs +++ b/ILRuntime/Runtime/Intepreter/ILIntepreter.cs @@ -4900,10 +4900,11 @@ void StoreValueToFieldReference(ref object obj, int idx, StackObject* val, IList else { CLRType t = AppDomain.GetType(obj.GetType()) as CLRType; - if (!t.AssignFieldFromStack(idx, ref obj, this, val, mStack)) + //It's impossible to garantee this field reference is a direct reference, it'll cause problem if it's not + //if (!t.AssignFieldFromStack(idx, ref obj, this, val, mStack)) { var v = obj.GetType().CheckCLRTypes(CheckAndCloneValueType(StackObject.ToObject(val, AppDomain, mStack), AppDomain)); - t.SetFieldValue(idx, ref obj, v); + t.SetFieldValue(idx, ref obj, v, true); } } } diff --git a/ILRuntimeTest/AutoGenerate/CLRBindings.cs b/ILRuntimeTest/AutoGenerate/CLRBindings.cs index 5c065c29..c47de6b4 100644 --- a/ILRuntimeTest/AutoGenerate/CLRBindings.cs +++ b/ILRuntimeTest/AutoGenerate/CLRBindings.cs @@ -65,6 +65,7 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain 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); + System_IDisposable_Binding.Register(app); System_Collections_Generic_Dictionary_2_Int32_ILRuntimeTest_TestFramework_ClassInheritanceTestAdaptor_Binding_Adaptor_Binding.Register(app); ILRuntimeTest_TestFramework_DelegateTest_Binding.Register(app); ILRuntimeTest_TestFramework_IntDelegate_Binding.Register(app); @@ -83,7 +84,6 @@ public static void Initialize(ILRuntime.Runtime.Enviorment.AppDomain app) System_IO_File_Binding.Register(app); System_IO_FileStream_Binding.Register(app); System_IO_Stream_Binding.Register(app); - System_IDisposable_Binding.Register(app); System_Collections_Generic_Dictionary_2_Int64_Int32_Binding.Register(app); System_Enum_Binding.Register(app); System_Collections_IEnumerator_Binding.Register(app); diff --git a/ILRuntimeTest/AutoGenerate/ILRuntimeTest_TestFramework_TestVector3NoBinding_Binding.cs b/ILRuntimeTest/AutoGenerate/ILRuntimeTest_TestFramework_TestVector3NoBinding_Binding.cs index 39c1f705..ad926f72 100644 --- a/ILRuntimeTest/AutoGenerate/ILRuntimeTest_TestFramework_TestVector3NoBinding_Binding.cs +++ b/ILRuntimeTest/AutoGenerate/ILRuntimeTest_TestFramework_TestVector3NoBinding_Binding.cs @@ -26,11 +26,18 @@ public static void Register(ILRuntime.Runtime.Enviorment.AppDomain app) args = new Type[]{}; method = type.GetMethod("get_zero", flag, null, args, null); app.RegisterCLRMethodRedirection(method, get_zero_0); + args = new Type[]{}; + method = type.GetMethod("get_one", flag, null, args, null); + app.RegisterCLRMethodRedirection(method, get_one_1); field = type.GetField("y", flag); app.RegisterCLRFieldGetter(field, get_y_0); app.RegisterCLRFieldSetter(field, set_y_0); app.RegisterCLRFieldBinding(field, CopyToStack_y_0, AssignFromStack_y_0); + field = type.GetField("x", flag); + app.RegisterCLRFieldGetter(field, get_x_1); + app.RegisterCLRFieldSetter(field, set_x_1); + app.RegisterCLRFieldBinding(field, CopyToStack_x_1, AssignFromStack_x_1); app.RegisterCLRMemberwiseClone(type, PerformMemberwiseClone); @@ -99,6 +106,17 @@ static void WriteBackInstance(ILRuntime.Runtime.Enviorment.AppDomain __domain, S return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); } + static StackObject* get_one_1(ILIntepreter __intp, StackObject* __esp, IList __mStack, CLRMethod __method, bool isNewObj) + { + ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; + StackObject* __ret = ILIntepreter.Minus(__esp, 0); + + + var result_of_this_method = ILRuntimeTest.TestFramework.TestVector3NoBinding.one; + + return ILIntepreter.PushObject(__ret, __mStack, result_of_this_method); + } + static object get_y_0(ref object o) { @@ -130,6 +148,36 @@ static void set_y_0(ref object o, object v) return ptr_of_this_method; } + static object get_x_1(ref object o) + { + return ((ILRuntimeTest.TestFramework.TestVector3NoBinding)o).x; + } + + static StackObject* CopyToStack_x_1(ref object o, ILIntepreter __intp, StackObject* __ret, IList __mStack) + { + var result_of_this_method = ((ILRuntimeTest.TestFramework.TestVector3NoBinding)o).x; + __ret->ObjectType = ObjectTypes.Float; + *(float*)&__ret->Value = result_of_this_method; + return __ret + 1; + } + + static void set_x_1(ref object o, object v) + { + ILRuntimeTest.TestFramework.TestVector3NoBinding ins =(ILRuntimeTest.TestFramework.TestVector3NoBinding)o; + ins.x = (System.Single)v; + o = ins; + } + + static StackObject* AssignFromStack_x_1(ref object o, ILIntepreter __intp, StackObject* ptr_of_this_method, IList __mStack) + { + ILRuntime.Runtime.Enviorment.AppDomain __domain = __intp.AppDomain; + System.Single @x = *(float*)&ptr_of_this_method->Value; + ILRuntimeTest.TestFramework.TestVector3NoBinding ins =(ILRuntimeTest.TestFramework.TestVector3NoBinding)o; + ins.x = @x; + o = ins; + return ptr_of_this_method; + } + static object PerformMemberwiseClone(ref object o) { diff --git a/TestCases/TestValueTypeBinding.cs b/TestCases/TestValueTypeBinding.cs index c534e67b..8316ca08 100644 --- a/TestCases/TestValueTypeBinding.cs +++ b/TestCases/TestValueTypeBinding.cs @@ -329,5 +329,39 @@ void UnitTest_10039Sub2(TestVector3NoBinding arg) throw new Exception(); } } + + public static void UnitTest_10040() + { + TestVector3 dot = new TestVector3(); + dot.X = 10; + dot.Y = 10; + + for (int i = 0; i < 50; i++) + { + dot.X++; + } + + Console.WriteLine("dot.X == " + dot.X); + if (dot.X != 60) + throw new AccessViolationException(); + + } + + public static void UnitTest_10041() + { + TestVector3NoBinding dot = new TestVector3NoBinding(); + dot.x = 10; + dot.x = 10; + + for (int i = 0; i < 50; i++) + { + dot.x++; + } + + Console.WriteLine("dot.X == " + dot.x); + if (dot.x != 60) + throw new AccessViolationException(); + + } } } \ No newline at end of file