Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 65 additions & 1 deletion Jint.Tests/Runtime/InteropTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,70 @@ public void ExtraParametersAreIgnored()
");
}

class Example()
{
public T ExchangeGenericViaFunc<T>(Func<T> objViaFunc)
{
return objViaFunc();
}

public object ExchangeObjectViaFunc(Func<object> objViaFunc)
{
return objViaFunc();
}

public int ExchangeValueViaFunc(Func<int> objViaFunc)
{
return objViaFunc();
}
}

[Fact]
public void ExchangeGenericViaFunc()
{
_engine.SetValue("Example", new Example());

RunTest(@"
const result = Example.ExchangeGenericViaFunc(() => {
return {
value: 42
};
});

assert(result.value === 42);
");
}

[Fact]
public void ExchangeObjectViaFunc()
{
_engine.SetValue("Example", new Example());

RunTest(@"
const result = Example.ExchangeObjectViaFunc(() => {
return {
value: 42
};
});

assert(result.value === 42);
");
}

[Fact]
public void ExchangeValueViaFunc()
{
_engine.SetValue("Example", new Example());

RunTest(@"
const result = Example.ExchangeValueViaFunc(() => {
return 42;
});

assert(result === 42);
");
}

private delegate string callParams(params object[] values);

private delegate string callArgumentAndParams(string firstParam, params object[] values);
Expand Down Expand Up @@ -2925,7 +2989,7 @@ public void ArrayPrototypeFindWithInteropList()
{
var engine = new Jint.Engine();
var list = new List<string> { "A", "B", "C" };

engine.SetValue("list", list);

Assert.Equal(1, engine.Evaluate("list.findIndex((x) => x === 'B')"));
Expand Down
14 changes: 12 additions & 2 deletions Jint/Runtime/Interop/DefaultTypeConverter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class DefaultTypeConverter : ITypeConverter
private static readonly Type engineType = typeof(Engine);
private static readonly Type typeType = typeof(Type);

private static readonly MethodInfo convertChangeType = typeof(Convert).GetMethod("ChangeType", new[] { objectType, typeType, typeof(IFormatProvider) })!;
private static readonly MethodInfo changeTypeIfConvertible = typeof(DefaultTypeConverter).GetMethod(
"ChangeTypeOnlyIfConvertible", BindingFlags.NonPublic | BindingFlags.Static)!;
private static readonly MethodInfo jsValueFromObject = jsValueType.GetMethod(nameof(JsValue.FromObject))!;
private static readonly MethodInfo jsValueToObject = jsValueType.GetMethod(nameof(JsValue.ToObject))!;

Expand Down Expand Up @@ -323,7 +324,7 @@ private Delegate BuildDelegate(
Expression.Convert(
Expression.Call(
null,
convertChangeType,
changeTypeIfConvertible,
Expression.Call(callExpression, jsValueToObject),
Expression.Constant(method.ReturnType),
Expression.Constant(System.Globalization.CultureInfo.InvariantCulture, typeof(IFormatProvider))
Expand All @@ -339,6 +340,15 @@ private Delegate BuildDelegate(
new ReadOnlyCollection<ParameterExpression>(parameters)).Compile();
}

[return: NotNullIfNotNull(nameof(value))]
private static object? ChangeTypeOnlyIfConvertible(object? value, Type conversionType, IFormatProvider? provider)
{
if (value == null || value is IConvertible)
return System.Convert.ChangeType(value, conversionType, provider);

return value;
}

private static bool TryCastWithOperators(object value, Type type, Type valueType, [NotNullWhen(true)] out object? converted)
{
var key = new TypeConversionKey(valueType, type);
Expand Down