-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add support for UnsafeAccessorKind.FieldOffset (#45152) #93946
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ class UserDataClass | |
{ | ||
public const string StaticFieldName = nameof(_F); | ||
public const string FieldName = nameof(_f); | ||
public const string FieldSecondName = nameof(_i); | ||
public const string StaticMethodName = nameof(_M); | ||
public const string MethodName = nameof(_m); | ||
public const string StaticMethodVoidName = nameof(_MVV); | ||
|
@@ -30,6 +31,7 @@ class UserDataClass | |
|
||
private static string _F = PrivateStatic; | ||
private string _f; | ||
private int _i; | ||
|
||
public string Value => _f; | ||
|
||
|
@@ -215,6 +217,18 @@ public static void Verify_AccessFieldClass() | |
extern static ref string GetPrivateField(UserDataClass d); | ||
} | ||
|
||
[Fact] | ||
public static void Verify_AccessFieldOffsetClass() | ||
{ | ||
Console.WriteLine($"Running {nameof(Verify_AccessFieldOffsetClass)}"); | ||
|
||
// Offset = method table pointer + first string pointer | ||
Assert.Equal(IntPtr.Size + IntPtr.Size, GetPrivateFieldOffset()); | ||
|
||
[UnsafeAccessor(UnsafeAccessorKind.FieldOffset, Name=UserDataClass.FieldSecondName)] | ||
extern static nint GetPrivateFieldOffset(UserDataClass? d = null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure what others' opinions on this are, but I like to be explicit with these sorts of APIs - I'd like it if we could also specify a second parameter which is the type of the field - it would first check for fields with same type (including modifiers), and then same type (ignoring modifiers) (same as other checks for the |
||
} | ||
|
||
[Fact] | ||
[ActiveIssue("https://github.com/dotnet/runtime/issues/92633")] | ||
public static void Verify_AccessStaticFieldGenericClass() | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should just be
IntPtr.Size
I think, since you cannot legitimately obtain a ref before the first field iirc.Presumably the
GetRawData
API will give the offset of the 0th field.Also, should we have tests for structs and for ref fields?
(edit: I'm editing this because my edge crashed after posting my comments and undid all my changes & deleted a comment?!)