Skip to content

Commit

Permalink
Can delegate conversion be this cheap?
Browse files Browse the repository at this point in the history
  • Loading branch information
leppie committed Jun 3, 2022
1 parent f5fa581 commit b9678f8
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions IronScheme/IronScheme/Runtime/Helpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -449,11 +449,28 @@ public static T ConvertToDelegate<T>(object proc)
{
return (T)proc;
}

if (proc is Delegate)
{
var d = (Delegate)proc;

try
{
return (T)(object)Delegate.CreateDelegate(typeof(T), d.Target, d.Method);
}
catch (Exception ex)
{
Builtins.AssertionViolation("ConvertToDelegate", "delegate is not compatible: " + ex.Message, proc, typeof(T));
}
}

if (!(proc is Callable))
{
Builtins.AssertionViolation("ConvertToDelegate", "not a procedure", proc);
}

// TODO: see if we have a (typed) callable that we can directly pass without conversion

MethodInfo meth = typeof(T).GetMethod("Invoke");
ParameterInfo[] pars = meth.GetParameters();
if (meth.ReturnType == typeof(void))
Expand Down

0 comments on commit b9678f8

Please sign in to comment.