Skip to content

Commit

Permalink
Removing the Lambda invoke functionality.
Browse files Browse the repository at this point in the history
  • Loading branch information
holdenmai committed Aug 24, 2022
1 parent 0c508af commit 0ac0258
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 129 deletions.
56 changes: 8 additions & 48 deletions src/DynamicExpresso.Core/Lambda.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,63 +83,23 @@ public object Invoke(params object[] args)
var parameters = new List<Parameter>();
var declaredParameters = DeclaredParameters.ToArray();

int[] actualArgOrdering = null;
object[] orderedArgs = args;
var argsAreReordered = false;
if (args != null && args.Length > 0)
if (args != null)
{
if (declaredParameters.Length != args.Length)
throw new InvalidOperationException("Arguments count mismatch.");

actualArgOrdering = new int[args.Length];
var usedParametersIndex = new Dictionary<string, int>(_parserArguments.Settings.KeyComparer);
foreach (var v in UsedParameters)
{
if (declaredParameters.Any(x => string.Equals(x.Name, v.Name, _parserArguments.Settings.KeyComparison)))
{
usedParametersIndex[v.Name] = usedParametersIndex.Count;
}
}

for (var i = 0; i < args.Length; i++)
{
if (!usedParametersIndex.TryGetValue(declaredParameters[i].Name, out var actualArgIndex))
{
actualArgIndex = -1;
}
if (actualArgIndex != i)
{
if (!argsAreReordered)
{
orderedArgs = (object[])orderedArgs.Clone();
argsAreReordered = true;
}
if (actualArgIndex == -1)
{
Array.Resize(ref orderedArgs, args.Length - 1);
}
else
{
orderedArgs[actualArgIndex] = args[i];
}
}
actualArgOrdering[i] = actualArgIndex;
}
}
var parameter = new Parameter(
declaredParameters[i].Name,
declaredParameters[i].Type,
args[i]);

var result = InvokeWithUsedParameters(orderedArgs);
if (argsAreReordered)
{
for (var i = 0; i < actualArgOrdering.Length; i++)
{
var pullFrom = actualArgOrdering[i];
if (pullFrom >= 0)
{
args[i] = orderedArgs[pullFrom];
}
parameters.Add(parameter);
}
}
return result;

return Invoke(parameters);
}

private object InvokeWithUsedParameters(object[] orderedArgs)
Expand Down
2 changes: 0 additions & 2 deletions test/DynamicExpresso.UnitTest/ConstructorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -225,8 +225,6 @@ public void Ctor_NewMyClassWithCross()
strProp.Value
};
Assert.AreEqual(new MyClassAdder() { { 1, 2, 3, 4, 5 }, "6", 7 }, target.Parse("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7}", strProp).Invoke(args));
Assert.AreEqual("6", args[0]);
args[0] = "0";
Assert.AreEqual(new MyClassAdder() { { 1, 2, 3, 4, 5 }, string.Empty, 7 }, target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},string.Empty, 7}"));
Assert.AreEqual(new MyClassAdder() {StrProp = string.Empty, MyArr = new[] { 1, 2, 3, 4, 5 }, IntField = int.MinValue }, target.Eval<MyClassAdder>("new MyClassAdder() {StrProp = string.Empty, MyArr = new int[] {1, 2, 3, 4, 5}, IntField = int.MinValue }"));
Assert.Throws<ParseException>(() => target.Eval<MyClassAdder>("new MyClassAdder(){{ 1, 2, 3, 4, 5},{StrProp = \"6\" },7 }"));
Expand Down
79 changes: 0 additions & 79 deletions test/DynamicExpresso.UnitTest/ParametersTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -332,85 +332,6 @@ public void When_lambda_is_invoked_input_parameters_must_follow_in_the_same_orde
Assert.AreEqual(4, lambda.Invoke(1, 5));
}

[Test]
public void When_lambda_is_invoked_byref_parameters_are_updated_on_invoke()
{
var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;

var parameters = new[]{
new Parameter(nameof(a), typeof(int).MakeByRefType()),
new Parameter(nameof(b), typeof(int).MakeByRefType()),
new Parameter(nameof(c), typeof(int).MakeByRefType()),
new Parameter(nameof(d), typeof(int).MakeByRefType()),
new Parameter(nameof(e), typeof(int).MakeByRefType()),
};

var args = new object[]
{
a,
b,
c,
d,
e
};

a += b *= c -= d /= e %= 3;

var target = new Interpreter();

var lambda = target.Parse("a = a + (b = b * (c = c - (d = d / (e = e % 3))))", parameters);

Assert.AreEqual(a, lambda.Invoke(args));
Assert.AreEqual(a, args[0]);
Assert.AreEqual(b, args[1]);
Assert.AreEqual(c, args[2]);
Assert.AreEqual(d, args[3]);
Assert.AreEqual(e, args[4]);
}

[Test]
public void When_lambda_is_invoked_byref_parameters_are_updated_on_invoke_and_I_can_omit_parameters_not_used()
{
var target = new Interpreter();

var a = 1;
var b = 2;
var c = 3;
var d = 4;
var e = 5;

var parameters = new[]{
new Parameter(nameof(a), typeof(int).MakeByRefType()),
new Parameter(nameof(b), typeof(int).MakeByRefType()),
new Parameter(nameof(c), typeof(int).MakeByRefType()),
new Parameter(nameof(d), typeof(int).MakeByRefType()),
new Parameter(nameof(e), typeof(int).MakeByRefType()),
};

var args = new object[]
{
a,
b,
c,
d,
e
};

a += b *= d /= e %= 3;

var lambda = target.Parse("a = a + (b = b * (d = d / (e = e % 3)))", parameters);

Assert.AreEqual(a, lambda.Invoke(args));
Assert.AreEqual(a, args[0]);
Assert.AreEqual(b, args[1]);
Assert.AreEqual(c, args[2]);
Assert.AreEqual(d, args[3]);
Assert.AreEqual(e, args[4]);
}

[Test]
public void When_lambda_is_invoked_I_can_omit_parameters_not_used()
Expand Down

0 comments on commit 0ac0258

Please sign in to comment.