virtualx-engine/modules/mono/glue/GodotSharp
Ignacio Roldán Etcheverry 282bd37e5c C#: Remove need for reflection to invoking callable delegates
We aim to make the C# API reflection-free, mainly for concerns about
performance, and to be able to target NativeAOT in refletion-free mode,
which reduces the binary size.

One of the main usages of reflection still left was the dynamic
invokation of callable delegates, and for some time I wasn't sure
I would find an alternative solution that I'd be happy with.

The new solution uses trampoline functions to invoke the delegates:

```
static void Trampoline(object delegateObj, NativeVariantPtrArgs args, out godot_variant ret)
{
    if (args.Count != 1)
        throw new ArgumentException($"Callable expected 1 arguments but received {args.Count}.");

    string res = ((Func<int, string>)delegateObj)(
        VariantConversionCallbacks.GetToManagedCallback<int>()(args[0])
    );

    ret = VariantConversionCallbacks.GetToVariantCallback<string>()(res);
}

Callable.CreateWithUnsafeTrampoline((int num) => "Foo" + num, &Trampoline);
```

Of course, this is too much boilerplate for user code. To improve this,
the `Callable.From` methods were added. These are overloads that take
`Action` and `Func` delegates, which covers the most common use cases:
lambdas and method groups:

```
// Lambda
Callable.From((int num) => "Foo" + num);

// Method group
string AppendNum(int num) => "Foo" + num;
Callable.From(AppendNum);
```

Unfortunately, due to limitations in the C# language, implicit
conversions from delegates to `Callable` are not supported.

`Callable.From` does not support custom delegates. These should be
uncommon, but the Godot C# API actually uses them for event signals.
As such, the bindings generator was updated to generate trampoline
functions for event signals. It was also optimized to use `Action`
instead of a custom delegate for parameterless signals, which removes
the need for the trampoline functions for those signals.

The change to reflection-free invokation removes one of the last needs
for `ConvertVariantToManagedObjectOfType`. The only remaining usage is
from calling script constructors with parameters from the engine
(`CreateManagedForGodotObjectScriptInstance`). Once that one is made
reflection-free, `ConvertVariantToManagedObjectOfType` can be removed.
2022-10-30 01:24:15 +02:00
..
ExternalAnnotations C#: Re-implement assembly reloading with ALCs 2022-08-22 03:36:51 +02:00
Godot.SourceGenerators.Internal C#: Replace P/Invoke with delegate pointers 2022-08-22 03:36:52 +02:00
GodotPlugins Fix various C# exceptions 2022-08-26 16:56:00 +02:00
GodotSharp C#: Remove need for reflection to invoking callable delegates 2022-10-30 01:24:15 +02:00
GodotSharpEditor C#: Generate symbols packages 2022-10-08 13:59:06 +02:00
.editorconfig C#: Code cleanup and greatly reduce use of C# pointers 2022-08-22 03:36:51 +02:00
.gitignore Mono/C#: Re-structure API solution and GodotTools post-build target 2019-12-28 20:48:55 +01:00
GodotSharp.sln C#: Replace P/Invoke with delegate pointers 2022-08-22 03:36:52 +02:00
GodotSharp.sln.DotSettings C#: Re-implement assembly reloading with ALCs 2022-08-22 03:36:51 +02:00