Skip to content

Commit

Permalink
Finish migration to V9
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser committed Sep 5, 2024
1 parent f6a5243 commit e32366f
Show file tree
Hide file tree
Showing 11 changed files with 72 additions and 43 deletions.
6 changes: 3 additions & 3 deletions crates/bindings-csharp/Codegen.Tests/Tests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,9 +97,9 @@ static void AssertCompilationSuccessful(Compilation compilation)
emitResult.Success,
string.Join(
"\n",
emitResult.Diagnostics.Select(diag =>
CSharpDiagnosticFormatter.Instance.Format(diag)
)
emitResult
.Diagnostics.Where(diag => diag.Severity != DiagnosticSeverity.Hidden)
.Select(diag => CSharpDiagnosticFormatter.Instance.Format(diag))
)
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ namespace NestingNamespaces
{
public static partial class AndClasses
{
[SpacetimeDB.Reducer("test_custom_name_and_reducer_ctx")]
[SpacetimeDB.Reducer]
public static void InsertData2(ReducerContext ctx, PublicTable data)
{
data.Insert();
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.

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.

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.

29 changes: 20 additions & 9 deletions crates/bindings-csharp/Codegen/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@ enum ColumnAttrs : byte
PrimaryKeyAuto = PrimaryKey | AutoInc,
}

enum Lifecycle
{
Init,
OnConnect,
OnDisconnect,
}

record ColumnDeclaration : MemberDeclaration
{
public readonly ColumnAttrs Attrs;
Expand Down Expand Up @@ -283,9 +290,9 @@ public ReducerParamDeclaration(IParameterSymbol param)
record ReducerDeclaration
{
public readonly string Name;
public readonly string ExportName;
public readonly string FullName;
public readonly EquatableArray<ReducerParamDeclaration> Args;
public Lifecycle? Lifecycle;
public readonly Scope Scope;

public ReducerDeclaration(GeneratorAttributeSyntaxContext context)
Expand All @@ -299,14 +306,15 @@ public ReducerDeclaration(GeneratorAttributeSyntaxContext context)
throw new Exception($"Reducer {method} must return void");
}

var exportName = (string?)attr.ConstructorArguments.SingleOrDefault().Value;

Name = method.Name;
ExportName = exportName ?? Name;
FullName = SymbolToName(method);
Args = new(
method.Parameters.Select(p => new ReducerParamDeclaration(p)).ToImmutableArray()
);
Lifecycle = attr
.ConstructorArguments.Select(a => (Lifecycle)a.Value!)
.Cast<Lifecycle?>()
.SingleOrDefault();
Scope = new Scope(methodSyntax.Parent as MemberDeclarationSyntax);
}

Expand All @@ -318,9 +326,10 @@ public KeyValuePair<string, string> GenerateClass()
class {{Name}}: SpacetimeDB.Internal.IReducer {
{{MemberDeclaration.GenerateBsatnFields(Accessibility.Private, NonContextArgs)}}
public SpacetimeDB.Internal.ReducerDef MakeReducerDef(SpacetimeDB.BSATN.ITypeRegistrar registrar) => new (
"{{ExportName}}",
[{{MemberDeclaration.GenerateDefs(NonContextArgs)}}]
public SpacetimeDB.Internal.RawReducerDefV9 MakeReducerDef(SpacetimeDB.BSATN.ITypeRegistrar registrar) => new (
nameof({{Name}}),
[{{MemberDeclaration.GenerateDefs(NonContextArgs)}}],
{{(Lifecycle is {} lifeCycle ? $"SpacetimeDB.Internal.Lifecycle.{lifeCycle}" : "null")}}
);
public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) =>
Expand All @@ -336,7 +345,7 @@ public void Invoke(BinaryReader reader, SpacetimeDB.ReducerContext ctx) =>
return new(Name, class_);
}

public Scope.Extensions GenerateSchedule()
public Scope.Extensions? GenerateSchedule()
{
var extensions = new Scope.Extensions(Scope, FullName);

Expand All @@ -352,7 +361,7 @@ public Scope.Extensions GenerateSchedule()
"\n",
NonContextArgs.Select(a => $"new {a.TypeInfo}().Write(writer, {a.Name});")
)}}
SpacetimeDB.Internal.IReducer.VolatileNonatomicScheduleImmediate("{{ExportName}}", stream);
SpacetimeDB.Internal.IReducer.VolatileNonatomicScheduleImmediate(nameof({{Name}}), stream);
}
"""
);
Expand Down Expand Up @@ -391,6 +400,8 @@ public void Initialize(IncrementalGeneratorInitializationContext context)

reducers
.Select((r, ct) => r.GenerateSchedule())
.Where(res => res is not null)
.Select((res, ct) => res!)
.WithTrackingName("SpacetimeDB.Reducer.GenerateSchedule")
.RegisterSourceOutputs(context);

Expand Down
26 changes: 18 additions & 8 deletions crates/bindings-csharp/Runtime/Attrs.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
namespace SpacetimeDB;

public static class ReducerKind
/// <summary>
/// This enum provides constants for special reducer kinds.
/// Do not rely on the type or values of these constants - they are only meant to be passed to the [SpacetimeDB.Reducer] attribute.
/// </summary>
public enum ReducerKind
{
public const string Init = "__init__";
public const string Update = "__update__";
public const string Connect = "__identity_connected__";
public const string Disconnect = "__identity_disconnected__";
Init = Internal.Lifecycle.Init,
Connect = Internal.Lifecycle.OnConnect,
Disconnect = Internal.Lifecycle.OnDisconnect,
}

[AttributeUsage(AttributeTargets.Method, Inherited = false, AllowMultiple = false)]
public sealed class ReducerAttribute(string? name = null) : Attribute
public sealed class ReducerAttribute : Attribute
{
public string? Name => name;
public ReducerAttribute() { }

public ReducerAttribute(ReducerKind kind)
{
_ = kind;
}
}

[AttributeUsage(
Expand All @@ -36,7 +44,9 @@ public enum ColumnAttrs : byte
PrimaryKey = Unique | 0b1000,
PrimaryKeyAuto = PrimaryKey | AutoInc,

// A legacy alias, originally defined as `PrimaryKey | Identity` which is numerically same as above.
/// <summary>
/// A legacy alias, originally defined as `PrimaryKey | Identity` which is numerically same as above.
/// </summary>.
PrimaryKeyIdentity = PrimaryKeyAuto,
}

Expand Down
5 changes: 2 additions & 3 deletions crates/bindings-csharp/Runtime/Internal/Module.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@ private void RegisterTypeName<T>(AlgebraicType.Ref typeRef)
{
return;
}
MiscExports.Add(
new MiscModuleExport.TypeAlias(new(GetFriendlyName(typeof(T)), (uint)typeRef.Ref_))
);
var scopedName = new RawScopedTypeNameV9([], GetFriendlyName(typeof(T)));
Types.Add(new(scopedName, (uint)typeRef.Ref_, CustomOrdering: false));
}

internal AlgebraicType.Ref RegisterType<T>(Func<AlgebraicType.Ref, AlgebraicType> makeType)
Expand Down

0 comments on commit e32366f

Please sign in to comment.