Skip to content

Commit

Permalink
Merge pull request #323 from gircore/wip/mjake/signals
Browse files Browse the repository at this point in the history
Signals, Cairo, Events + General Cleanup
  • Loading branch information
badcel authored Aug 9, 2021
2 parents cc07322 + d73171b commit 897d945
Show file tree
Hide file tree
Showing 150 changed files with 1,816 additions and 1,538 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,13 @@ jobs:
if: ${{ matrix.os == 'ubuntu-latest' }}
run: dotnet run -- --release --xml-documentation --targets integrationTest samples
working-directory: './Build'

- name: (Failure) Upload Generated Files
if: ${{ failure() }}
uses: actions/upload-artifact@v2
with:
name: ${{ matrix.os }}-generated
path: |
${{ github.workspace }}
!**/bin
!**/obj
13 changes: 9 additions & 4 deletions Build/Projects.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,18 @@ public static class Projects
private const string GTK3_WINDOW = SAMPLE + "Gtk3/Window";
private const string GTK3_DECLARATIVE_UI = SAMPLE + "Gtk3/DeclarativeUi";
private const string GTK3_ABOUT_DIALOG = SAMPLE + "Gtk3/AboutDialog";

private const string GTK3_QUICKSTART = SAMPLE + "Gtk3/QuickStart";
private const string GTK3_DRAWINGAREA = SAMPLE + "Gtk3/DrawingArea";
private const string GTK3_TEXTEDITOR = SAMPLE + "Gtk3/TextEditor";

// private const string DBUS_SAMPLE = SAMPLE + "DBus/";
// private const string GSTREAMER_SAMPLE = SAMPLE + "GStreamer/";
// private const string GTK3_APP_SAMPLE = SAMPLE + "Gtk3/GtkApp/";
// private const string GTK3_BUILDER_SAMPLE = SAMPLE + "Gtk3/Builder";
// private const string GTK3_QUICKSTART = SAMPLE + "Gtk3/QuickStart";

// private const string GTK3_COMPOSITE_TEMPLATE_SOURCEGENERATOR = SAMPLE + "Gtk3/CompositeTemplates/UsingSourceGenerator";
// private const string GTK3_COMPOSITE_TEMPLATE_NO_SOURCEGENERATOR = SAMPLE + "Gtk3/CompositeTemplates/NoSourceGenerator";
// private const string GTK4_SIMPLE_WINDOW_SAMPLE = SAMPLE + "Gtk4/SimpleWindow/";
// private const string GDKPIXBUF_TEST_MEMORY_LEAKS = SAMPLE + "GdkPixbuf/TestMemoryLeaks";

private const string INTEGRATION = "../Integration/";

Expand All @@ -65,7 +67,10 @@ public static class Projects
GDK_PIXBUF_TEST_MEMORY_LEAKS,
GTK3_WINDOW,
GTK3_DECLARATIVE_UI,
GTK3_ABOUT_DIALOG
GTK3_ABOUT_DIALOG,
GTK3_QUICKSTART,
GTK3_DRAWINGAREA,
GTK3_TEXTEDITOR
};

public static IEnumerable<Project> AllLibraries = new[]
Expand Down
8 changes: 4 additions & 4 deletions Generator/Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ internal static string NativeToManaged(TransferableAnyType transferable, string
// Record Conversions (safe handles)
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: true } } } when useSafeHandle => $"{fromParam}.Select(x => new {r.Write(Target.Managed, currentNamespace)}(x)).ToArray()",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } when useSafeHandle => $"new {r.Write(Target.Managed, currentNamespace)}({fromParam})",

// Record Conversions (raw pointers)
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: true } } } when !useSafeHandle => $"{fromParam}.Select(x => new {r.Write(Target.Managed, currentNamespace)}(new {SafeHandleFromRecord(r)}(x))).ToArray()",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } when !useSafeHandle => $"new {r.Write(Target.Managed, currentNamespace)}(new {SafeHandleFromRecord(r)}({fromParam}))",

//Record Conversions without pointers are not working yet
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } => $"({r.Write(Target.Managed, currentNamespace)}[]) default!; //TODO: Fixme",
ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } => $"({qualifiedType}[]) {fromParam}.Select(x => new {qualifiedType}({SafeHandleFromRecord(r, true)}(x))).ToArray();",
ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: false } } => $"({r.Write(Target.Managed, currentNamespace)}) default!; //TODO: Fixme",

// Class Conversions
Expand All @@ -97,9 +97,9 @@ internal static string NativeToManaged(TransferableAnyType transferable, string
};
}

private static string SafeHandleFromRecord(Record r)
private static string SafeHandleFromRecord(Record r, bool managedHandle = false)
{
var type = r.GetMetadataString("SafeHandleRefName");
var type = r.GetMetadataString(managedHandle ? "SafeHandleRefManagedFunc" : "SafeHandleRefName");
var nspace = $"{r.Repository.Namespace}.Native";
return nspace + "." + type;
}
Expand Down
27 changes: 20 additions & 7 deletions Generator/Extensions/AnyTypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,32 @@ private static string WriteNativeType(AnyType anyType, Namespace currentNamespac
// Arrays of byte can be marshalled automatically, no IntPtr needed
{ TypeReference: ArrayTypeReference { Type: { } s } } when s.Name == "byte" => "byte[]",

//References to records which are not using a pointer
// References to records which are not using a pointer
{ TypeReference: ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: { IsPointer: false } } } } => GetStructName(r, currentNamespace) + "[]",
{ TypeReference: ArrayTypeReference { Type: Record r, TypeReference: { CTypeReference: null } } } => GetStructName(r, currentNamespace) + "[]",
{ TypeReference: { Type: Record r, CTypeReference: { IsPointer: false } } } => GetStructName(r, currentNamespace),


//References to records which are using a pointer

// References to records which are using a pointer
{ TypeReference: ArrayTypeReference { Type: Record, TypeReference: { CTypeReference: { IsPointer: true } } } } => "IntPtr[]", //Array of SafeHandle not supported by runtime
{ TypeReference: ResolveableTypeReference { Type: Record r, CTypeReference: { IsPointer: true } } } => GetSafeHandleName(r, currentNamespace, useSafeHandle),


// References to unions which are not using a pointer
{ TypeReference: ArrayTypeReference { Type: Union u, TypeReference: { CTypeReference: { IsPointer: false } } } } => GetStructName(u, currentNamespace) + "[]",
{ TypeReference: ArrayTypeReference { Type: Union u, TypeReference: { CTypeReference: null } } } => GetStructName(u, currentNamespace) + "[]",
{ TypeReference: { Type: Union u, CTypeReference: { IsPointer: false } } } => GetStructName(u, currentNamespace),

// References to unions which are using a pointer
{ TypeReference: ArrayTypeReference { Type: Union, TypeReference: { CTypeReference: { IsPointer: true } } } } => "IntPtr[]", //Array of SafeHandle not supported by runtime
{ TypeReference: ResolveableTypeReference { Type: Union, CTypeReference: { IsPointer: true } } } => "IntPtr",

// Primitives - Marshal directly
{ TypeReference: ArrayTypeReference { Type: PrimitiveValueType s } } => s.Write(Target.Native, currentNamespace) + "[]",
{ TypeReference: { Type: PrimitiveValueType s } } => s.Write(Target.Native, currentNamespace),

// Enumerations - Marshal directly
{ TypeReference: ArrayTypeReference { Type: Enumeration } } => anyType.TypeReference.Type.Write(Target.Native, currentNamespace) + "[]",
{ TypeReference: { Type: Enumeration } } => anyType.TypeReference.Type.Write(Target.Native, currentNamespace),

// Short path for strings as strings are pointers which should not be handled as pointers
{ TypeReference: { Type: String r } } => r.Name,

Expand All @@ -68,7 +76,12 @@ private static string GetStructName(Record r, Namespace currentNamespace)
{
return AddNamespace(currentNamespace, r.Repository.Namespace, r.GetMetadataString("StructRefName"), Target.Native);
}


private static string GetStructName(Union u, Namespace currentNamespace)
{
return AddNamespace(currentNamespace, u.Repository.Namespace, u.GetMetadataString("StructRefName"), Target.Native);
}

private static string GetSafeHandleName(Record r, Namespace currentNamespace, bool useSafeHandle)
{
if (useSafeHandle)
Expand Down
2 changes: 1 addition & 1 deletion Generator/Extensions/FieldsExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public static string WriteUnionStructFields(this IEnumerable<Field> fields, Name

foreach (Field field in fields)
{
builder.AppendLine("//[FieldOffset(0)] TODO Enable offset");
builder.AppendLine("[FieldOffset(0)]");
builder.AppendLine(field.WriteNative(currentNamespace));
}

Expand Down
19 changes: 13 additions & 6 deletions Generator/Extensions/ParameterListExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,19 @@ public static string WriteSignalArgsProperties(this ParameterList parameterList,
var index = 0;
foreach (var argument in parameterList.GetParameters())
{
index += 1;
var type = argument.WriteType(Target.Native, currentNamespace);
var name = new GirLoader.Helper.String(argument.Name).ToPascalCase();

builder.AppendLine($"//TODO: public {type} {name} => Args[{index}].Extract<{type}>();");
builder.AppendLine($"public string {name} => \"\";");
try
{
index += 1;
var type = argument.WriteType(Target.Managed, currentNamespace);
var name = new GirLoader.Helper.String(argument.Name).ToPascalCase();

builder.AppendLine($"public {type} {name} => Args[{index}].Extract<{type}>();");
}
catch (Exception e)
{
Log.Error($"Could not write signal properties for {argument.Name}: {e.Message}");
throw;
}
}

return builder.ToString();
Expand Down
9 changes: 7 additions & 2 deletions Generator/Extensions/TypeExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,18 @@ internal static string Write(this Type type, Target target, Namespace currentNam
ComplexType c when !c.Repository.Namespace.IsForeignTo(currentNamespace) => c.Name,
ComplexType c => $"{c.Repository.Namespace.GetName(target)}.{c.Name}",

_ => throw new Exception($"Unknown type {type}.")
_ => throw new Exception($"Unknown type: {type}")
};
}

public static string WriteTypeRegistration(this Type type)
public static string WriteTypeRegistrationClass(this Type type)
{
return $"TypeDictionary.Add(typeof({type.Name}), new GObject.Type(Native.{type.Name}.Instance.Methods.GetGType()));\r\n";
}

public static string WriteTypeRegistrationBoxed(this Type type)
{
return $"TypeDictionary.Add(typeof({type.Name}), new GObject.Type(Native.{type.Name}.Methods.GetGType()));\r\n";
}
}
}
5 changes: 4 additions & 1 deletion Generator/Services/TypeRenamer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,9 @@ private void SetUnionMetadata(Union union)
{
union.Metadata["Name"] = union.Name;
union.Metadata["StructName"] = "Struct";
union.Metadata["StructRefName"] = $"{union.Name}.Struct";

union.Name = new TypeName($"{union.Name}.Struct");
union.Name = new TypeName($"{union.Name}");
}

private void SetRecordMetadata(IEnumerable<Record> records)
Expand All @@ -121,6 +122,7 @@ private void SetClassStructMetadata(Record record)
record.Metadata["StructRefName"] = $"{className}.Class";
record.Metadata["SafeHandleName"] = "Handle";
record.Metadata["SafeHandleRefName"] = $"{className}.Handle";
record.Metadata["SafeHandleRefManagedFunc"] = $"{className}.ManagedHandle.Create";

record.Name = new TypeName($"{className}.Class");
}
Expand All @@ -132,6 +134,7 @@ private void SetRecordMetadata(Record record)
record.Metadata["StructRefName"] = $"{record.Name}.Struct";
record.Metadata["SafeHandleName"] = "Handle";
record.Metadata["SafeHandleRefName"] = $"{record.Name}.Handle";
record.Metadata["SafeHandleRefManagedFunc"] = $"{record.Name}.ManagedHandle.Create";

record.Name = new TypeName($"{record.Name}");
}
Expand Down
17 changes: 13 additions & 4 deletions Generator/Services/Writer/WriteModuleService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using Generator.Factories;
Expand Down Expand Up @@ -37,17 +38,25 @@ public void Write(Namespace ns, string outputDir)

private void WriteTypeDictionaryInitialization(Namespace ns, string outputDir)
{
var classes = ns.Classes.Where(x => !x.IsFundamental);
IEnumerable<Type> classes = ns.Classes.Where(x => !x.IsFundamental);
IEnumerable<Type> records = ns.Records.Where(x => x.GetTypeFunction is not null);
IEnumerable<Type> unions = ns.Unions.Where(x => x.GetTypeFunction is not null);

IEnumerable<Type> types = classes.Concat(records);

if (!classes.Any())
if (!types.Any())
return;

var scriptObject = new ScriptObject()
{
{ "namespace", ns },
{ "classes", classes},
{ "classes", classes },
{ "records", records },
{ "unions", unions }
};
scriptObject.Import("write_type_registration", new Func<Type, string>(s => s.WriteTypeRegistration()));
scriptObject.Import("write_type_registration", new Func<Type, string>(s => s.WriteTypeRegistrationClass()));
scriptObject.Import("write_type_registration_record", new Func<Type, string>(s => s.WriteTypeRegistrationBoxed()));
scriptObject.Import("write_type_registration_union", new Func<Type, string>(s => s.WriteTypeRegistrationBoxed()));

_writeHelperService.Write(
projectName: ns.ToCanonicalName(),
Expand Down
28 changes: 27 additions & 1 deletion Generator/Templates/module_type_registration.sbntxt
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{{ if namespace.name != "GLib" # TODO: Temporary until we merge GLib + GObject }}
using GObject.Native;
{{ end }}
using System;

namespace {{ namespace.name }}
Expand All @@ -7,16 +9,40 @@ namespace {{ namespace.name }}
{
static partial void RegisterTypes()
{
{{ if namespace.name != "GLib" # TODO: Temporary until we merge GLib + GObject }}
{{ for $class in classes}}
try
{
{{ $class | write_type_registration }}
}
catch (Exception e)
{
Console.WriteLine($"Could not register type '{{ $class.name }}': {e.Message}");
Console.WriteLine($"Could not register class type '{{ $class.name }}': {e.Message}");
}
{{ end }}

{{ for $record in records }}
try
{
{{ $record | write_type_registration_record }}
}
catch (Exception e)
{
Console.WriteLine($"Could not register record type '{{ $record.name }}': {e.Message}");
}
{{ end }}

{{ for $union in unions }}
try
{
{{ $union | write_type_registration_union }}
}
catch (Exception e)
{
Console.WriteLine($"Could not register union type '{{ $union.name }}': {e.Message}");
}
{{ end }}
{{ end }}
}
}
}
2 changes: 2 additions & 0 deletions Generator/Templates/record.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ namespace {{ namespace.name }}
// Override this to perform additional steps in the constructor
partial void Initialize();

public {{ $record_name }}(IntPtr ptr) : this(new {{$safe_handle}}(ptr)){ }

public {{ $record_name }}({{ $safe_handle }} handle)
{
_handle = handle;
Expand Down
3 changes: 1 addition & 2 deletions Generator/Templates/union.native.struct.sbntxt
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//TODO: Enable explicit union generation if it does not break documentation generation anymore
//[StructLayout(LayoutKind.Explicit)]
[StructLayout(LayoutKind.Explicit)]
public partial struct {{ get_metadata "StructName" }}
{
{{ include 'union.native.struct.fields.sbntxt' }}
Expand Down
3 changes: 2 additions & 1 deletion Generator/Templates/union.sbntxt
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ using System.Runtime.InteropServices;
namespace {{ namespace.name }}
{
// AUTOGENERATED FILE - DO NOT MODIFY
public partial record {{get_metadata "Name"}}
public partial class {{ get_metadata "Name" }}
{
// TODO: Handle Unions
}
}
Loading

0 comments on commit 897d945

Please sign in to comment.