Skip to content

Commit

Permalink
Fix generic method return type handling under interop (#1743)
Browse files Browse the repository at this point in the history
  • Loading branch information
lahma committed Jan 15, 2024
1 parent d23b32c commit f4f257c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 11 deletions.
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

0 comments on commit f4f257c

Please sign in to comment.