Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow delegates to return values from .NET #854

Merged
merged 2 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 64 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2387,5 +2387,69 @@ public void UnsignedIntegerEnumResolutionShouldWork()
engine.SetValue("E", TypeReference.CreateTypeReference(engine, typeof(UintEnum)));
Assert.Equal(1, engine.Execute("E.b;").GetCompletionValue().AsNumber());
}

#region DelegateCanReturnValue

public class TestItem
{
public double Cost { get; set; }

public double Age { get; set; }

public string Name { get; set; }
}

public class TestItemList : List<TestItem>
{
public double Sum(Func<TestItem, double> calc)
{
double rc = 0;

foreach (var item in this)
{
rc += calc(item);
}

return rc;
}

public TestItemList Where(Func<TestItem, bool> cond)
{
var rc = new TestItemList();

foreach (var item in this)
{
if (cond(item))
{
rc.Add(item);
}
}

return rc;
}
}

[Fact]
public void DelegateCanReturnValue()
{
var engine = new Engine(options => options.AllowClr(GetType().Assembly));

var lst = new TestItemList();

lst.Add(new TestItem() { Name = "a", Cost = 1, Age = 10 });
lst.Add(new TestItem() { Name = "a", Cost = 1, Age = 10 });
lst.Add(new TestItem() { Name = "b", Cost = 1, Age = 10 });
lst.Add(new TestItem() { Name = "b", Cost = 1, Age = 10 });
lst.Add(new TestItem() { Name = "b", Cost = 1, Age = 10 });

engine.SetValue("lst", lst);

Assert.Equal(5, engine.Execute("lst.Sum(x => x.Cost);").GetCompletionValue().AsNumber());
Assert.Equal(50, engine.Execute("lst.Sum(x => x.Age);").GetCompletionValue().AsNumber());
Assert.Equal(3, engine.Execute("lst.Where(x => x.Name == 'b').Count;").GetCompletionValue().AsNumber());
Assert.Equal(30, engine.Execute("lst.Where(x => x.Name == 'b').Sum(x => x.Age);").GetCompletionValue().AsNumber());
}

#endregion
}
}
6 changes: 2 additions & 4 deletions Jint/Runtime/Interop/DefaultTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,14 +181,12 @@ public virtual object Convert(object value, Type type, IFormatProvider formatPro

var @vars = Expression.NewArrayInit(jsValueType, initializers);

var callExpression = Expression.Block(
Expression.Call(
var callExpression = Expression.Call(
Expression.Call(Expression.Constant(function.Target),
function.Method,
Expression.Constant(JsValue.Undefined, jsValueType),
@vars),
jsValueType.GetMethod("ToObject")),
Expression.Empty());
jsValueType.GetMethod("ToObject"));

var dynamicExpression = Expression.Invoke(Expression.Lambda(callExpression, new ReadOnlyCollection<ParameterExpression>(@params)), new ReadOnlyCollection<ParameterExpression>(@params));

Expand Down