Skip to content

Commit

Permalink
Fix: Test if the operator overload is correct
Browse files Browse the repository at this point in the history
  • Loading branch information
neolithos committed Mar 6, 2015
1 parent 682687e commit ab4209a
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
23 changes: 13 additions & 10 deletions NeoLua.Test/Expressions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -909,16 +909,19 @@ public void TestCompare08()
new KeyValuePair<string, Type>("b", typeof(object))
);

TestResult(g.dochunk(c, 1, 2), false);
TestResult(g.dochunk(c, 2, 1), false);
TestResult(g.dochunk(c, 2, 2), true);
TestResult(g.dochunk(c, 2, (short)2), true);
TestResult(g.dochunk(c, new TestOperator(1), 2), false);
TestResult(g.dochunk(c, 2, new TestOperator(2)), false);
object a = new object();
TestResult(g.dochunk(c, a, a), true);
TestResult(g.dochunk(c, "a", "a"), true);
}
TestResult(g.dochunk(c, 1, 2), false);
TestResult(g.dochunk(c, 2, 1), false);
TestResult(g.dochunk(c, 2, 2), true);
TestResult(g.dochunk(c, 2, (short)2), true);
TestResult(g.dochunk(c, new TestOperator(1), 2), false);
TestResult(g.dochunk(c, 2, new TestOperator(2)), false);
object a = new object();
TestResult(g.dochunk(c, a, a), true);
TestResult(g.dochunk(c, "a", "a"), true);
TestResult(g.dochunk(c, 3.0m, null), false);
TestResult(g.dochunk(c, null, 3.0m), false);
TestResult(g.dochunk(c, null, null), true);
}
}

[TestMethod]
Expand Down
26 changes: 19 additions & 7 deletions NeoLua/LuaEmit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ private static bool IsFloatType(TypeCode typeCode)
private static bool TryConvertType(Lua runtime, Type typeTo, ref Expression expr, ref Type exprType)
{
bool lExact;
if (TypesMatch(typeTo, exprType, out lExact))// is the type compitible
if (TypesMatch(typeTo, exprType, out lExact)) // is the type compitible
{
expr = Convert(runtime, expr, exprType, typeTo, false);
exprType = typeTo;
Expand Down Expand Up @@ -1049,15 +1049,27 @@ private static Expression BinaryOperationArithmeticExpression(Lua runtime, Expre
MethodInfo miOperator = FindMethod(members3, parameterTypes, t => t, false);
if (miOperator != null)
{
// Get the argumentslist
ParameterInfo[] parameterInfo = miOperator.GetParameters();
if (op == Lua.IntegerDivide)
op = ExpressionType.Divide;
return Expression.MakeBinary(op,
Convert(runtime, expr1, type1, parameterInfo[0].ParameterType, lParse),
Convert(runtime, expr2, type2, parameterInfo[1].ParameterType, lParse),
true,
miOperator
);

// Check if the arguments are valid
Expression exprOperatorArgument1 = expr1;
Type typeOperatorArgument1 = type1;
Expression exprOperatorArgument2 = expr2;
Type typeOperatorArgument2 = type2;

if (TryConvertType(runtime, parameterInfo[0].ParameterType, ref exprOperatorArgument1, ref typeOperatorArgument1) &&
TryConvertType(runtime, parameterInfo[1].ParameterType, ref exprOperatorArgument2, ref typeOperatorArgument2))
{
return Expression.MakeBinary(op,
exprOperatorArgument1,
exprOperatorArgument2,
true,
miOperator
);
}
}
}
}
Expand Down

0 comments on commit ab4209a

Please sign in to comment.