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

Fix generic method return type handling under interop #1743

Merged
merged 1 commit into from
Jan 15, 2024
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
27 changes: 27 additions & 0 deletions Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3421,5 +3421,32 @@ public void CanSelectShadowedPropertiesBasedOnReadableAndWritable()
var result = engine.Evaluate("test.metadata['abc']");
Assert.Equal("from-wrapper", result);
}

[Fact]
public void ShouldSomethingFoo()
{
var engine = new Engine(opt =>
{
opt.AddExtensionMethods(typeof(Enumerable)); // Allow LINQ extension methods.
});

var result = new List<string>();
void Debug(object? o)
{
result.Add($"{o?.GetType().Name ?? "null"}: {o ?? "null"}");
}

engine.SetValue("debug", Debug);
engine.SetValue("dict", new Dictionary<string, string> { ["test"] = "val" });

engine.Execute("var t = dict.last(kvp => { debug(kvp); debug(kvp.key); return kvp.key != null; } );");
engine.Execute("debug(t); debug(t.key);");

Assert.Equal(4, result.Count);
Assert.Equal("KeyValuePair`2: [test, val]", result[0]);
Assert.Equal("String: test", result[1]);
Assert.Equal("KeyValuePair`2: [test, val]", result[2]);
Assert.Equal("String: test", result[3]);
}
}
}
15 changes: 4 additions & 11 deletions Jint/Runtime/Interop/MethodInfoFunction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -226,23 +226,16 @@ JsValue[] ArgumentProvider(MethodDescriptor method)
continue;
}

Type? returnType = null;
if (method.Method is MethodInfo methodInfo)
{
returnType = methodInfo.ReturnType;
}

// todo: cache method info
try
{
if (method.Method.IsGenericMethodDefinition && method.Method is MethodInfo)
if (method.Method is MethodInfo { IsGenericMethodDefinition: true })
{
var genericMethodInfo = resolvedMethod;
var result = genericMethodInfo.Invoke(thisObj, parameters);
return FromObjectWithType(Engine, result, returnType);
var result = resolvedMethod.Invoke(thisObj, parameters);
return FromObjectWithType(Engine, result, type: (resolvedMethod as MethodInfo)?.ReturnType);
}

return FromObjectWithType(Engine, method.Method.Invoke(thisObj, parameters), returnType);
return FromObjectWithType(Engine, method.Method.Invoke(thisObj, parameters), type: (method.Method as MethodInfo)?.ReturnType);
}
catch (TargetInvocationException exception)
{
Expand Down