-
Notifications
You must be signed in to change notification settings - Fork 9
Implement object_isinst and object_get_class #129
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
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 |
---|---|---|
@@ -0,0 +1,69 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
namespace UnityEmbedHost.Tests; | ||
|
||
class Mammal : Anaimal, IMammal | ||
{ | ||
} | ||
|
||
class Cat : Mammal, ICat | ||
{ | ||
} | ||
|
||
class CatOnlyInterface : ICat | ||
{ | ||
} | ||
|
||
class Rock : IRock | ||
{ | ||
} | ||
|
||
class Anaimal : IAnimal | ||
{ | ||
} | ||
|
||
class NoInterfaces | ||
{ | ||
} | ||
|
||
interface IAnimal | ||
{ | ||
} | ||
|
||
interface IMammal : IAnimal | ||
{ | ||
} | ||
|
||
interface ICat : IMammal | ||
{ | ||
} | ||
|
||
interface IRock | ||
{ | ||
} | ||
|
||
struct MyStruct | ||
{ | ||
|
||
} | ||
|
||
struct ValueMammal : IMammal | ||
{ | ||
} | ||
|
||
struct ValueCat : ICat | ||
{ | ||
} | ||
|
||
struct ValueRock : IRock | ||
{ | ||
} | ||
|
||
struct ValueAnimal : IAnimal | ||
{ | ||
} | ||
|
||
struct ValueNoInterfaces | ||
{ | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
|
||
using System; | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
namespace Unity.CoreCLRHelpers; | ||
|
||
static class Extensions | ||
{ | ||
public static Type TypeFromHandleIntPtr(this nint intPtrToTypeHandle) | ||
=> Type.GetTypeFromHandle(RuntimeTypeHandle.FromIntPtr(intPtrToTypeHandle)); | ||
|
||
public static nint TypeHandleIntPtr(this object obj) | ||
=> obj.GetType().TypeHandleIntPtr(); | ||
|
||
public static nint TypeHandleIntPtr(this Type type) | ||
=> RuntimeTypeHandle.ToIntPtr(type.TypeHandle); | ||
|
||
public static T UnsafeAs<T>(this nint intPtr) | ||
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. Nit, the extension below is clearly to convert an object to an IntPtr/nint representation in the embedding API. I'm not sure of the best way of organizing it, but we want to make sure we keep all object<->nint and nint<->object conversions in one place (and only use methods for that) because at some point that will switch from just an unsafe cast to allocating/retrieving GC Handles. 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. Basically, any objects (reference types) being passes as parameters or as a return value on the embedding interface need special handling. 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. Are you suggesting I rename / refactor the methods to be more like ToNativeRepresentation ? 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. That's my initial thought. I don't think it's required, just want to ensure we keep those specific operations isolated to we can change them easily without having to re-audit all the code. |
||
=> Unsafe.As<nint, T>(ref intPtr); | ||
|
||
public static nint UnsafeAsIntPtr(this object obj) | ||
=> obj.UnsafeAs<nint>(); | ||
|
||
public static T UnsafeAs<T>(this object obj) | ||
=> Unsafe.As<object, T>(ref obj); | ||
|
||
public static GCHandle ToGCHandle(this nint intPtr) | ||
=> GCHandle.FromIntPtr(intPtr); | ||
} |
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.
What runtime are these tests running on? Mono? Our CoreCLR with our GC?
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.
From Rider they are going to run on whatever coreclr rider is using.
From the CI scripts, I'm not sure. Maybe the system installed, maybe our fork. @UnityAlex do you know?
Uh oh!
There was an error while loading. Please reload this page.
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.
The Unsafe calls making objects into IntPtr's are actually unsafe ;-) on stock CoreCLR as objects may be moved by the GC.
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.
Yes the way it's setup currently it's just running
dotnet
so whatever is on the PATH env var.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.
Makes sense. I didn't think about that. I guess we'll see how long we get lucky for 😄