Skip to content

Commit

Permalink
Update C# bindings
Browse files Browse the repository at this point in the history
  • Loading branch information
RReverser authored and Centril committed Oct 7, 2023
1 parent fe1e81f commit 23e686c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 76 deletions.
38 changes: 19 additions & 19 deletions crates/bindings-csharp/Codegen/Utils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,23 +60,23 @@ public static string GetTypeInfo(ITypeSymbol type)
INamedTypeSymbol namedType
=> type.SpecialType switch
{
SpecialType.System_Boolean => "SpacetimeDB.SATS.BuiltinType.BoolTypeInfo",
SpecialType.System_SByte => "SpacetimeDB.SATS.BuiltinType.I8TypeInfo",
SpecialType.System_Byte => "SpacetimeDB.SATS.BuiltinType.U8TypeInfo",
SpecialType.System_Int16 => "SpacetimeDB.SATS.BuiltinType.I16TypeInfo",
SpecialType.System_UInt16 => "SpacetimeDB.SATS.BuiltinType.U16TypeInfo",
SpecialType.System_Int32 => "SpacetimeDB.SATS.BuiltinType.I32TypeInfo",
SpecialType.System_UInt32 => "SpacetimeDB.SATS.BuiltinType.U32TypeInfo",
SpecialType.System_Int64 => "SpacetimeDB.SATS.BuiltinType.I64TypeInfo",
SpecialType.System_UInt64 => "SpacetimeDB.SATS.BuiltinType.U64TypeInfo",
SpecialType.System_Boolean => "SpacetimeDB.SATS.AlgebraicType.BoolTypeInfo",
SpecialType.System_SByte => "SpacetimeDB.SATS.AlgebraicType.I8TypeInfo",
SpecialType.System_Byte => "SpacetimeDB.SATS.AlgebraicType.U8TypeInfo",
SpecialType.System_Int16 => "SpacetimeDB.SATS.AlgebraicType.I16TypeInfo",
SpecialType.System_UInt16 => "SpacetimeDB.SATS.AlgebraicType.U16TypeInfo",
SpecialType.System_Int32 => "SpacetimeDB.SATS.AlgebraicType.I32TypeInfo",
SpecialType.System_UInt32 => "SpacetimeDB.SATS.AlgebraicType.U32TypeInfo",
SpecialType.System_Int64 => "SpacetimeDB.SATS.AlgebraicType.I64TypeInfo",
SpecialType.System_UInt64 => "SpacetimeDB.SATS.AlgebraicType.U64TypeInfo",
SpecialType.System_Single
=> "SpacetimeDB.SATS.BuiltinType.F32TypeInfo",
SpecialType.System_Double => "SpacetimeDB.SATS.BuiltinType.F64TypeInfo",
SpecialType.System_String => "SpacetimeDB.SATS.BuiltinType.StringTypeInfo",
=> "SpacetimeDB.SATS.AlgebraicType.F32TypeInfo",
SpecialType.System_Double => "SpacetimeDB.SATS.AlgebraicType.F64TypeInfo",
SpecialType.System_String => "SpacetimeDB.SATS.AlgebraicType.StringTypeInfo",
SpecialType.None when type.ToString() == "System.Int128"
=> "SpacetimeDB.SATS.BuiltinType.I128TypeInfo",
=> "SpacetimeDB.SATS.AlgebraicType.I128TypeInfo",
SpecialType.None when type.ToString() == "System.UInt128"
=> "SpacetimeDB.SATS.BuiltinType.U128TypeInfo",
=> "SpacetimeDB.SATS.AlgebraicType.U128TypeInfo",
SpecialType.None
when namedType.EnumUnderlyingType is not null
// check that enums also have [SpacetimeDB.Type]
Expand All @@ -88,12 +88,12 @@ when namedType.EnumUnderlyingType is not null
a.AttributeClass?.ToDisplayString()
== "SpacetimeDB.TypeAttribute"
)
=> $"SpacetimeDB.SATS.BuiltinType.MakeEnum<{type}>()",
=> $"SpacetimeDB.SATS.AlgebraicType.MakeEnum<{type}>()",
SpecialType.None
=> $"{type.OriginalDefinition.ToString() switch
{
"System.Collections.Generic.List<T>" => "SpacetimeDB.SATS.BuiltinType.MakeList",
"System.Collections.Generic.Dictionary<TKey, TValue>" => "SpacetimeDB.SATS.BuiltinType.MakeMap",
"System.Collections.Generic.List<T>" => "SpacetimeDB.SATS.AlgebraicType.MakeList",
"System.Collections.Generic.Dictionary<TKey, TValue>" => "SpacetimeDB.SATS.AlgebraicType.MakeMap",
// If we're here, then this is nullable value type like `int?`.
"System.Nullable<T>" => $"SpacetimeDB.SATS.SumType.MakeValueOption",
var name when name.StartsWith("System.") => throw new InvalidOperationException(
Expand All @@ -109,8 +109,8 @@ when namedType.EnumUnderlyingType is not null
IArrayTypeSymbol arrayType
=> arrayType.ElementType is INamedTypeSymbol namedType
&& namedType.SpecialType == SpecialType.System_Byte
? "SpacetimeDB.SATS.BuiltinType.BytesTypeInfo"
: $"SpacetimeDB.SATS.BuiltinType.MakeArray({GetTypeInfo(arrayType.ElementType)})",
? "SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo"
: $"SpacetimeDB.SATS.AlgebraicType.MakeArray({GetTypeInfo(arrayType.ElementType)})",
_ => throw new InvalidOperationException($"Unsupported type {type}")
};
}
Expand Down
90 changes: 39 additions & 51 deletions crates/bindings-csharp/Runtime/AlgebraicType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,13 @@ public MapType(AlgebraicType key, AlgebraicType value)
}

[SpacetimeDB.Type]
public partial struct BuiltinType
public partial struct AlgebraicType
: SpacetimeDB.TaggedEnum<(
AlgebraicTypeRef TypeRef,
SumType Sum,
ProductType Product,
AlgebraicType Array,
MapType Map,
Unit Bool,
Unit I8,
Unit U8,
Expand All @@ -208,68 +213,81 @@ public partial struct BuiltinType
Unit U128,
Unit F32,
Unit F64,
Unit String,
AlgebraicType Array,
MapType Map
Unit String
)>
{
public static implicit operator AlgebraicType(AlgebraicTypeRef typeRef)
{
return new AlgebraicType { TypeRef = typeRef };
}

public static implicit operator AlgebraicType(SumType sum)
{
return new AlgebraicType { Sum = sum };
}

public static implicit operator AlgebraicType(ProductType product)
{
return new AlgebraicType { Product = product };
}

public static readonly TypeInfo<bool> BoolTypeInfo = new TypeInfo<bool>(
new BuiltinType { Bool = default },
new AlgebraicType { Bool = default },
(reader) => reader.ReadBoolean(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<sbyte> I8TypeInfo = new TypeInfo<sbyte>(
new BuiltinType { I8 = default },
new AlgebraicType { I8 = default },
(reader) => reader.ReadSByte(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<byte> U8TypeInfo = new TypeInfo<byte>(
new BuiltinType { U8 = default },
new AlgebraicType { U8 = default },
(reader) => reader.ReadByte(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<short> I16TypeInfo = new TypeInfo<short>(
new BuiltinType { I16 = default },
new AlgebraicType { I16 = default },
(reader) => reader.ReadInt16(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<ushort> U16TypeInfo = new TypeInfo<ushort>(
new BuiltinType { U16 = default },
new AlgebraicType { U16 = default },
(reader) => reader.ReadUInt16(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<int> I32TypeInfo = new TypeInfo<int>(
new BuiltinType { I32 = default },
new AlgebraicType { I32 = default },
(reader) => reader.ReadInt32(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<uint> U32TypeInfo = new TypeInfo<uint>(
new BuiltinType { U32 = default },
new AlgebraicType { U32 = default },
(reader) => reader.ReadUInt32(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<long> I64TypeInfo = new TypeInfo<long>(
new BuiltinType { I64 = default },
new AlgebraicType { I64 = default },
(reader) => reader.ReadInt64(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<ulong> U64TypeInfo = new TypeInfo<ulong>(
new BuiltinType { U64 = default },
new AlgebraicType { U64 = default },
(reader) => reader.ReadUInt64(),
(writer, value) => writer.Write(value)
);

#if NET7_0_OR_GREATER
public static readonly TypeInfo<Int128> I128TypeInfo = new TypeInfo<Int128>(
new BuiltinType { I128 = default },
new AlgebraicType { I128 = default },
(reader) => new Int128(reader.ReadUInt64(), reader.ReadUInt64()),
(writer, value) =>
{
Expand All @@ -279,7 +297,7 @@ MapType Map
);

public static readonly TypeInfo<UInt128> U128TypeInfo = new TypeInfo<UInt128>(
new BuiltinType { U128 = default },
new AlgebraicType { U128 = default },
(reader) => new UInt128(reader.ReadUInt64(), reader.ReadUInt64()),
(writer, value) =>
{
Expand All @@ -290,19 +308,19 @@ MapType Map
#endif

public static readonly TypeInfo<float> F32TypeInfo = new TypeInfo<float>(
new BuiltinType { F32 = default },
new AlgebraicType { F32 = default },
(reader) => reader.ReadSingle(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<double> F64TypeInfo = new TypeInfo<double>(
new BuiltinType { F64 = default },
new AlgebraicType { F64 = default },
(reader) => reader.ReadDouble(),
(writer, value) => writer.Write(value)
);

public static readonly TypeInfo<byte[]> BytesTypeInfo = new TypeInfo<byte[]>(
new BuiltinType { Array = U8TypeInfo.AlgebraicType },
new AlgebraicType { Array = U8TypeInfo.AlgebraicType },
(reader) =>
{
var length = reader.ReadInt32();
Expand All @@ -316,7 +334,7 @@ MapType Map
);

public static readonly TypeInfo<string> StringTypeInfo = new TypeInfo<string>(
new BuiltinType { String = default },
new AlgebraicType { String = default },
(reader) => Encoding.UTF8.GetString(BytesTypeInfo.Read(reader)),
(writer, value) => BytesTypeInfo.Write(writer, Encoding.UTF8.GetBytes(value))
);
Expand Down Expand Up @@ -350,7 +368,7 @@ TypeInfo<T> elementTypeInfo
where A : ICollection<T>
{
return new TypeInfo<A>(
new BuiltinType { Array = elementTypeInfo.AlgebraicType },
new AlgebraicType { Array = elementTypeInfo.AlgebraicType },
(reader) => create(ReadEnumerable(reader, elementTypeInfo.Read)),
(writer, array) => WriteEnumerable(writer, array, elementTypeInfo.Write)
);
Expand All @@ -366,7 +384,7 @@ public static TypeInfo<Dictionary<K, V>> MakeMap<K, V>(TypeInfo<K> key, TypeInfo
where K : notnull
{
return new TypeInfo<Dictionary<K, V>>(
new BuiltinType { Map = new MapType(key.AlgebraicType, value.AlgebraicType) },
new AlgebraicType { Map = new MapType(key.AlgebraicType, value.AlgebraicType) },
(reader) =>
ReadEnumerable(
reader,
Expand Down Expand Up @@ -422,36 +440,6 @@ public static TypeInfo<T> MakeEnum<T>()
}
}

[SpacetimeDB.Type]
public partial struct AlgebraicType
: SpacetimeDB.TaggedEnum<(
SumType Sum,
ProductType Product,
BuiltinType Builtin,
AlgebraicTypeRef TypeRef
)>
{
public static implicit operator AlgebraicType(SumType sum)
{
return new AlgebraicType { Sum = sum };
}

public static implicit operator AlgebraicType(ProductType product)
{
return new AlgebraicType { Product = product };
}

public static implicit operator AlgebraicType(BuiltinType builtin)
{
return new AlgebraicType { Builtin = builtin };
}

public static implicit operator AlgebraicType(AlgebraicTypeRef typeRef)
{
return new AlgebraicType { TypeRef = typeRef };
}
}

[SpacetimeDB.Type]
public partial struct AlgebraicTypeRef
{
Expand Down
12 changes: 6 additions & 6 deletions crates/bindings-csharp/Runtime/Runtime.cs
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,11 @@ public override int GetHashCode() =>
// We need to set type info to inlined identity type as `generate` CLI currently can't recognise type references for built-ins.
new SpacetimeDB.SATS.ProductType
{
{ "__identity_bytes", SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.AlgebraicType }
{ "__identity_bytes", SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.AlgebraicType }
},
reader => new(SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Read(reader)),
reader => new(SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Read(reader)),
(writer, value) =>
SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Write(writer, value.bytes)
SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Write(writer, value.bytes)
);

public static SpacetimeDB.SATS.TypeInfo<Identity> GetSatsTypeInfo() => satsTypeInfo;
Expand Down Expand Up @@ -243,7 +243,7 @@ public override int GetHashCode() =>
// We need to set type info to inlined address type as `generate` CLI currently can't recognise type references for built-ins.
new SpacetimeDB.SATS.ProductType
{
{ "__address_bytes", SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.AlgebraicType }
{ "__address_bytes", SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.AlgebraicType }
},
// Concern: We use this "packed" representation (as Bytes)
// in the caller_id field of reducer arguments,
Expand All @@ -252,9 +252,9 @@ public override int GetHashCode() =>
// It's possible that these happen to be identical
// because BSATN is minimally self-describing,
// but that doesn't seem like something we should count on.
reader => new(SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Read(reader)),
reader => new(SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Read(reader)),
(writer, value) =>
SpacetimeDB.SATS.BuiltinType.BytesTypeInfo.Write(writer, value.bytes)
SpacetimeDB.SATS.AlgebraicType.BytesTypeInfo.Write(writer, value.bytes)
);

public static SpacetimeDB.SATS.TypeInfo<Address> GetSatsTypeInfo() => satsTypeInfo;
Expand Down

0 comments on commit 23e686c

Please sign in to comment.