Skip to content

Commit 968113c

Browse files
committed
Unify object and ref byte getters and setters metadata.
1 parent db6ac21 commit 968113c

6 files changed

+23
-63
lines changed

BTDB.SourceGenerator.Test/IOCTests.VerifyGenerateForOnAssemblyFromDifferentAssembly#BTDB.GenerateForAttribute.g.verified.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ static object Creator()
1414
}
1515
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_ConstructorParameters")]
1616
extern static global::System.Type[] Getter1(global::BTDB.GenerateForAttribute @this);
17-
static object GenGetter1(object @this)
17+
static void GenGetter1(object @this, ref byte value)
1818
{
19-
return Getter1(Unsafe.As<global::BTDB.GenerateForAttribute>(@this));
19+
Unsafe.As<byte, global::System.Type[]>(ref value) = Getter1(Unsafe.As<global::BTDB.GenerateForAttribute>(@this));
2020
}
2121
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_ConstructorParameters")]
2222
extern static void Setter1(global::BTDB.GenerateForAttribute @this, global::System.Type[] value);
23-
static void GenSetter1(object @this, object value)
23+
static void GenSetter1(object @this, ref byte value)
2424
{
25-
Setter1(Unsafe.As<global::BTDB.GenerateForAttribute>(@this), Unsafe.As<global::System.Type[]>(value));
25+
Setter1(Unsafe.As<global::BTDB.GenerateForAttribute>(@this), Unsafe.As<byte, global::System.Type[]>(ref value));
2626
}
2727
[ModuleInitializer]
2828
internal static unsafe void Register4BTDB()
@@ -50,8 +50,8 @@ internal static unsafe void Register4BTDB()
5050
{
5151
Name = "ConstructorParameters",
5252
Type = typeof(global::System.Type[]),
53-
PropObjGetter = &GenGetter1,
54-
PropObjSetter = &GenSetter1,
53+
PropRefGetter = &GenGetter1,
54+
PropRefSetter = &GenSetter1,
5555
},
5656
};
5757
global::BTDB.Serialization.ReflectionMetadata.Register(metadata);

BTDB.SourceGenerator.Test/MetadataTests.VerifyMetadataWithComplexGetter#TestNamespace.Person.g.verified.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ static file class PersonRegistration
1212
extern static global::TestNamespace.Person Creator();
1313
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Name")]
1414
extern static string Getter1(global::TestNamespace.Person @this);
15-
static object GenGetter1(object @this)
15+
static void GenGetter1(object @this, ref byte value)
1616
{
17-
return Getter1(Unsafe.As<global::TestNamespace.Person>(@this));
17+
Unsafe.As<byte, string>(ref value) = Getter1(Unsafe.As<global::TestNamespace.Person>(@this));
1818
}
1919
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Name")]
2020
extern static void Setter1(global::TestNamespace.Person @this, string value);
21-
static void GenSetter1(object @this, object value)
21+
static void GenSetter1(object @this, ref byte value)
2222
{
23-
Setter1(Unsafe.As<global::TestNamespace.Person>(@this), Unsafe.As<string>(value));
23+
Setter1(Unsafe.As<global::TestNamespace.Person>(@this), Unsafe.As<byte, string>(ref value));
2424
}
2525
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "get_Age")]
2626
extern static int Getter2(global::TestNamespace.Person @this);
@@ -58,8 +58,8 @@ internal static unsafe void Register4BTDB()
5858
{
5959
Name = "Name",
6060
Type = typeof(string),
61-
PropObjGetter = &GenGetter1,
62-
PropObjSetter = &GenSetter1,
61+
PropRefGetter = &GenGetter1,
62+
PropRefSetter = &GenSetter1,
6363
},
6464
new global::BTDB.Serialization.FieldMetadata
6565
{

BTDB.SourceGenerator.Test/MetadataTests.VerifyMetadataWithComplexSetter#TestNamespace.Person.g.verified.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ static file class PersonRegistration
1414
extern static ref string Field1(global::TestNamespace.Person @this);
1515
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "set_Name")]
1616
extern static void Setter1(global::TestNamespace.Person @this, string value);
17-
static void GenSetter1(object @this, object value)
17+
static void GenSetter1(object @this, ref byte value)
1818
{
19-
Setter1(Unsafe.As<global::TestNamespace.Person>(@this), Unsafe.As<string>(value));
19+
Setter1(Unsafe.As<global::TestNamespace.Person>(@this), Unsafe.As<byte, string>(ref value));
2020
}
2121
[UnsafeAccessor(UnsafeAccessorKind.Field, Name = "_age")]
2222
extern static ref int Field2(global::TestNamespace.Person @this);
@@ -51,7 +51,7 @@ internal static unsafe void Register4BTDB()
5151
Name = "Name",
5252
Type = typeof(string),
5353
ByteOffset = global::BTDB.Serialization.RawData.CalcOffset(dummy, ref Field1(dummy)),
54-
PropObjSetter = &GenSetter1,
54+
PropRefSetter = &GenSetter1,
5555
},
5656
new global::BTDB.Serialization.FieldMetadata
5757
{

BTDB.SourceGenerator/Main.cs

+2-40
Original file line numberDiff line numberDiff line change
@@ -857,26 +857,7 @@ static object Creator()
857857
""");
858858
}
859859

860-
if (field is { GetterName: not null, IsReference: true })
861-
{
862-
// language=c#
863-
declarations.Append($$"""
864-
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "{{field.GetterName}}")]
865-
extern static {{normalizedType}} Getter{{fieldIndex}}({{generationInfo.FullName}} @this);
866-
static object GenGetter{{fieldIndex}}(object @this)
867-
{
868-
return Getter{{fieldIndex}}(Unsafe.As<{{generationInfo.FullName}}>(@this));
869-
}
870-
871-
""");
872-
// language=c#
873-
metadataCode.Append($$"""
874-
PropObjGetter = &GenGetter{{fieldIndex}},
875-
876-
""");
877-
}
878-
879-
if (field is { GetterName: not null, IsReference: false })
860+
if (field is { GetterName: not null})
880861
{
881862
// language=c#
882863
declarations.Append($$"""
@@ -895,26 +876,7 @@ static object Creator()
895876
""");
896877
}
897878

898-
if (field is { SetterName: not null, IsReference: true })
899-
{
900-
// language=c#
901-
declarations.Append($$"""
902-
[UnsafeAccessor(UnsafeAccessorKind.Method, Name = "{{field.SetterName}}")]
903-
extern static void Setter{{fieldIndex}}({{generationInfo.FullName}} @this, {{normalizedType}} value);
904-
static void GenSetter{{fieldIndex}}(object @this, object value)
905-
{
906-
Setter{{fieldIndex}}(Unsafe.As<{{generationInfo.FullName}}>(@this), Unsafe.As<{{normalizedType}}>(value));
907-
}
908-
909-
""");
910-
// language=c#
911-
metadataCode.Append($"""
912-
PropObjSetter = &GenSetter{fieldIndex},
913-
914-
""");
915-
}
916-
917-
if (field is { SetterName: not null, IsReference: false })
879+
if (field is { SetterName: not null })
918880
{
919881
// language=c#
920882
declarations.Append($$"""

BTDB/Serialization/FieldMetadata.cs

-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ public class FieldMetadata
77
public string Name;
88
public Type Type;
99
public uint? ByteOffset;
10-
public unsafe delegate*<object, object> PropObjGetter;
1110
public unsafe delegate*<object, ref byte, void> PropRefGetter;
12-
public unsafe delegate*<object, object, void> PropObjSetter;
1311
public unsafe delegate*<object, ref byte, void> PropRefSetter;
1412
}

BTDBTest/SerializationTests/SandboxTest.cs

+6-6
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@ public unsafe void Sandbox()
4343
{
4444
Name = "Name",
4545
Type = typeof(string),
46-
PropObjGetter = &GetName,
47-
PropObjSetter = &SetName
46+
PropRefGetter = &GetName,
47+
PropRefSetter = &SetName
4848
},
4949
new FieldMetadata
5050
{
@@ -56,14 +56,14 @@ public unsafe void Sandbox()
5656

5757
Assert.Equal(2 * Unsafe.SizeOf<object>(), (int)metadata.Fields[1].ByteOffset.GetValueOrDefault());
5858

59-
static object GetName(object obj)
59+
static void GetName(object obj, ref byte value)
6060
{
61-
return Unsafe.As<Person>(obj).Name;
61+
Unsafe.As<byte, string>(ref value) = Unsafe.As<Person>(obj).Name;
6262
}
6363

64-
static void SetName(object obj, object value)
64+
static void SetName(object obj, ref byte value)
6565
{
66-
Unsafe.As<Person>(obj).Name = Unsafe.As<string>(value);
66+
Unsafe.As<Person>(obj).Name = Unsafe.As<byte, string>(ref value);
6767
}
6868
}
6969
}

0 commit comments

Comments
 (0)