Skip to content

Commit

Permalink
another case for #241
Browse files Browse the repository at this point in the history
  • Loading branch information
maximv committed Jul 14, 2020
1 parent b6d5ce3 commit fc74ccb
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace FastExpressionCompiler.IssueTests
#endif
{
[TestFixture]
public class Issue146_bool_par_error
public class Issue146_bool_par
{
public int Run()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,25 @@ public class Issue248_Calling_method_with_in_out_parameters_in_expression_lead_t
public int Run()
{
Test_1();
return 1;
Test_2();
return 2;
}

[Test]
public void Test_1()
{
var serializer = Parameter(typeof(ISerializer), "serializer");
var method = typeof(ISerializer).GetMethod("WriteDecimal");
var data = Parameter(typeof(Test).MakeByRefType(), "data");
var field = Field(data, typeof(Test).GetField("Field1"));
var call = Call(serializer, method, field);
var expr = Lambda<SerializerDelegate>(call, serializer, data);
var data = Parameter(typeof(Test).MakeByRefType(), "data");

var expr = Lambda<SerializerDelegate>(
Call(serializer, typeof(ISerializer).GetMethod(nameof(ISerializer.WriteDecimal)),
Field(data, typeof(Test).GetField(nameof(Test.Field1)))),
serializer, data);

expr.PrintCSharpString();

var serialize = expr.CompileFast(true);
Assert.IsNotNull(serialize);

serialize.PrintIL();

serialize.AssertOpCodes(
Expand All @@ -40,20 +41,44 @@ public void Test_1()
OpCodes.Ldflda,
OpCodes.Callvirt,
OpCodes.Ret);
/*
Expected IL:
IL_0000: ldarg.1
IL_0001: ldarg.2
IL_0002: ldflda valuetype [System.Private.CoreLib]System.Decimal C/Test::Field1
IL_0007: callvirt instance void C/ISerializer::WriteDecimal(valuetype [System.Private.CoreLib]System.Decimal& modreq([System.Private.CoreLib]System.Runtime.InteropServices.InAttribute))
IL_000c: ret
*/

var test = new Test { Field1 = 35m };
serialize(new MySerializer(), ref test); // does nothing
Assert.AreEqual(35m, test.Field1);
}

[Test]
public void Test_2()
{
var serializer = Parameter(typeof(ISerializer), "serializer");
var data = Parameter(typeof(Test).MakeByRefType(), "data");

var expr = Lambda<SerializerDelegate>(
Call(serializer, typeof(ISerializer).GetMethod(nameof(ISerializer.WriteDecimal)),
Field(
Field(data, typeof(Test).GetField(nameof(Test.NestedTest))),
typeof(NestedTest).GetField(nameof(NestedTest.Field1)))),
serializer, data);

expr.PrintCSharpString();

var serialize = expr.CompileFast(true);
Assert.IsNotNull(serialize);
serialize.PrintIL();

serialize.AssertOpCodes(
OpCodes.Ldarg_1,
OpCodes.Ldarg_2,
OpCodes.Ldflda,
OpCodes.Ldflda,
OpCodes.Callvirt,
OpCodes.Ret);

var test = new Test { NestedTest = { Field1 = 35m }};
serialize(new MySerializer(), ref test); // does nothing
Assert.AreEqual(35m, test.NestedTest.Field1);
}

public interface ISerializer
{
void WriteDecimal(in decimal value);
Expand All @@ -67,6 +92,12 @@ public void WriteDecimal(in decimal value) {
}

public struct Test
{
public decimal Field1;
public NestedTest NestedTest;
}

public struct NestedTest
{
public decimal Field1;
}
Expand Down
4 changes: 2 additions & 2 deletions test/FastExpressionCompiler.TestsRunner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ void Run(Func<int> run, string name = null)
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue107_Assign_also_works_for_variables().Run);
Run(new Issue127_Switch_is_supported().Run);
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue127_Switch_is_supported().Run);
Run(new Issue146_bool_par_error().Run);
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue146_bool_par_error().Run);
Run(new Issue146_bool_par().Run);
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue146_bool_par().Run);
Run(new Issue147_int_try_parse().Run);
Run(new FastExpressionCompiler.LightExpression.IssueTests.Issue147_int_try_parse().Run);
Run(new Issue150_New_AttemptToReadProtectedMemory().Run);
Expand Down

0 comments on commit fc74ccb

Please sign in to comment.