Skip to content

Commit

Permalink
Auto-generate C# ModuleDef bindings from Rust (#1680)
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser authored Sep 9, 2024
1 parent 2cccac4 commit 607f7ce
Show file tree
Hide file tree
Showing 59 changed files with 1,603 additions and 508 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/module_bindings/** linguist-generated=true eol=lf
*.verified.cs linguist-generated=true eol=lf
/crates/bindings-csharp/Runtime/Internal/Autogen/*.cs linguist-generated=true
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 6 additions & 10 deletions crates/bindings-csharp/BSATN.Codegen/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,10 @@ IEnumerable<MemberDeclaration> members
}

public static string GenerateDefs(IEnumerable<MemberDeclaration> members) =>
$$"""
new SpacetimeDB.BSATN.AggregateElement[] {
{{string.Join(
",\n",
members.Select(m => $"new(nameof({m.Name}), {m.Name}.GetAlgebraicType(registrar))")
)}}
}
""";
string.Join(
",\n",
members.Select(m => $"new(nameof({m.Name}), {m.Name}.GetAlgebraicType(registrar))")
);
}

public enum TypeKind
Expand Down Expand Up @@ -230,9 +226,9 @@ public void Write(System.IO.BinaryWriter writer, {{ShortName}} value) {
}
public SpacetimeDB.BSATN.AlgebraicType GetAlgebraicType(SpacetimeDB.BSATN.ITypeRegistrar registrar) =>
registrar.RegisterType<{{ShortName}}>(_ => new SpacetimeDB.BSATN.AlgebraicType.{{Kind}}(
registrar.RegisterType<{{ShortName}}>(_ => new SpacetimeDB.BSATN.AlgebraicType.{{Kind}}(new SpacetimeDB.BSATN.AggregateElement[] {
{{MemberDeclaration.GenerateDefs(Members)}}
));
}));
}
"""
);
Expand Down
5 changes: 3 additions & 2 deletions crates/bindings-csharp/Codegen.Tests/TestInit.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,17 @@ public static void Initialize()
unformattedCode,
new() { IncludeGenerated = true, EndOfLine = CSharpier.EndOfLine.LF }
);
sb.Append(result.Code);
// Print errors in the end so that their line numbers are still meaningful.
if (result.CompilationErrors.Any())
{
sb.AppendLine();
sb.AppendLine("// Generated code produced compilation errors:");
foreach (var diag in result.CompilationErrors)
{
sb.Append("// ").AppendLine(diag.ToString());
}
sb.AppendLine();
}
sb.Append(result.Code);
},
ScrubberLocation.Last
);
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

2 comments on commit 607f7ce

@github-actions
Copy link

@github-actions github-actions bot commented on 607f7ce Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

@github-actions
Copy link

@github-actions github-actions bot commented on 607f7ce Sep 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarking failed. Please check the workflow run for details.

Please sign in to comment.