Skip to content

Commit

Permalink
fixed #331
Browse files Browse the repository at this point in the history
  • Loading branch information
liiir1985 committed Jul 2, 2020
1 parent f1935f6 commit a6ff9d8
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 9 deletions.
14 changes: 8 additions & 6 deletions ILRuntime/CLR/TypeSystem/CLRType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions ILRuntime/Runtime/Intepreter/ILIntepreter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion ILRuntimeTest/AutoGenerate/CLRBindings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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<object> __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)
{
Expand Down Expand Up @@ -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<object> __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<object> __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)
{
Expand Down
34 changes: 34 additions & 0 deletions TestCases/TestValueTypeBinding.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

}
}
}

0 comments on commit a6ff9d8

Please sign in to comment.