Skip to content

Commit

Permalink
callsite: writeback fix, tolerates unsupported struct argument
Browse files Browse the repository at this point in the history
fixes #1117
  • Loading branch information
jakubmisek committed Aug 28, 2023
1 parent e4b8351 commit cdcb934
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 8 deletions.
22 changes: 18 additions & 4 deletions src/Peachpie.Runtime/Dynamic/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,35 @@ public static class Expressions
/// <summary>
/// Gets method info in given type.
/// </summary>
public static MethodInfo GetMethod(this Type type, string name, params Type[] ptypes)
public static bool TryGetMethod(Type type, string name, Type[] ptypes, out MethodInfo method)
{
var result = type.GetRuntimeMethod(name, ptypes);
if (result != null)
{
return result;
method = result;
return true;
}

foreach (var m in type.GetTypeInfo().GetDeclaredMethods(name)) // non public methods
{
if (ParamsMatch(m.GetParameters(), ptypes))
return m;
{
method = m;
return true;
}
}

throw new InvalidOperationException($"{type.Name}.{name}({string.Join<Type>(", ", ptypes)}) was not resolved.");
//
method = null;
return false;
}

public static MethodInfo GetMethod(this Type type, string name, params Type[] ptypes)
{
return TryGetMethod(type, name, ptypes, out var method)
? method
: throw new InvalidOperationException($"{type.Name}.{name}({string.Join<Type>(", ", ptypes)}) was not resolved.")
;
}

static MethodInfo GetOpImplicit(this Type type, Type resultType) =>
Expand Down
22 changes: 19 additions & 3 deletions src/Peachpie.Runtime/Dynamic/ConvertExpression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ public static Expression Bind(Expression arg, Type target, Expression ctx)
// BindToValue(arg));

//
throw new NotImplementedException($"{arg.Type} -> {target}");
return Expression.Block(
ThrowTypeError($"{arg.Type} -> {target}"),
BindDefault(target)
);
}

private static bool IsNullConstant(Expression arg)
Expand Down Expand Up @@ -964,7 +967,12 @@ static Expression BindCostFromValue(Expression arg, Type target)
}

//
if (!target.IsValueType)
if (target.IsValueType)
{
// conversion to a struct?

}
else
{
if (target.IsByRef)
{
Expand All @@ -986,7 +994,15 @@ static Expression BindCostFromValue(Expression arg, Type target)
}

// fallback
return Expression.Call(typeof(CostOf).GetMethod("To" + target.Name, arg.Type), arg);
if (Cache.TryGetMethod(typeof(CostOf), "To" + target.Name, new[] { arg.Type }, out var method))
{
return Expression.Call(method, arg);
}
else
{
// TODO
return Expression.Constant(ConversionCost.NoConversion);
}
}

static ConversionCost BindCostFromBool(Expression arg, Type target)
Expand Down
2 changes: 1 addition & 1 deletion src/Peachpie.Runtime/Dynamic/OverloadBinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ public override Expression BindArgument(int srcarg, ParameterInfo targetparam =
public override Expression BindWriteBack(int targetarg, Expression expression)
{
// args[ targetarg ]
var element = Expression.ArrayIndex(_argsarray, Expression.Constant(targetarg));
var element = Expression.ArrayAccess(_argsarray, Expression.Constant(targetarg));

// args[ targetarg ] = (T)expression
return Expression.Assign(element, ConvertExpression.Bind(expression, element.Type, _ctx));
Expand Down

0 comments on commit cdcb934

Please sign in to comment.