gircore / gir.core

A C# binding generator for GObject based libraries providing a C# friendly API surface
https://gircore.github.io/
MIT License
299 stars 28 forks source link

Optimize code to allow trimming / native aot #975

Open badcel opened 10 months ago

badcel commented 10 months ago

Optimize code to allow trimming / native AOT:

A first step is probably to enable analyzers and fix the resulting warnings, like @kant2002 mentioned in https://github.com/gircore/gir.core/pull/468#issuecomment-1807558061

Probably depends on #698.

DeafMan1983 commented 9 months ago

Hello @badcel

Great news!

I made Gtk4 into C# with modern function pointer ( delegate* <...> ) and some referenced instances like GTK_WINDOW, G_APPLICATION etc...

Example:

namespace Gtk4Test;

using static DeafMan1983.ConvFunctions;

using DeafMan1983.Interop.GTK4;
using static DeafMan1983.Interop.GTK4.Gtk;
using static DeafMan1983.Interop.GTK4.GObject;
using static DeafMan1983.Interop.GTK4.Gio;

unsafe class Program
{
    static void activate(GtkApplication* app, void* user_data)
    {
        GtkWidget* window = gtk_application_window_new(app);
        gtk_window_set_title(GTK_WINDOW(window), SBytePointerFromString("Window"));
        gtk_window_set_default_size(GTK_WINDOW (window), 200, 200);
        gtk_window_present(GTK_WINDOW(window));
    }

    static int Main(string[] args)
    {
        GtkApplication *app = gtk_application_new(SBytePointerFromString("org.gtk.example"), GApplicationFlags.G_APPLICATION_FLAGS_NONE);
        g_signal_connect(app, SBytePointerFromString("activate"), G_CALLBACK (&activate), null);
        int status = g_application_run(G_APPLICATION (app), args.Length, SByteDoublePointersFromStringArray(args));
        g_object_unref (app);
        return status;
    }
}

But It is still working in process with Gtk4 :) Check out my workstatiion if you want know about this. image image

And I want to tell about GObject will to be as void* because C# understands hard with instance like other structures example GtkButton or GdkDisplay etc....

They should to know void*

I hope I can complete Gtk4 then I will release my complete Gtk4 in C#.

I publish into native aot. image

Woohoo! It works fine :D

DeafMan1983 commented 9 months ago

And more details: adding button... image

badcel commented 9 months ago

Great that it works.

In general I think unsafe code should only be used in areas where it makes sense to allow native aot to work. Making the whole internal namespace completely unsafe is probably not the way to go.

If you want to help out you could create a small test program which creates a new GObject (I think you need to derive from GObject.Object to use this constructor) and compile this program into native code and see what it is complaining about.