using System; using System.Collections.Generic; using System.Runtime.InteropServices; namespace PorygonSharp.EvalValues { internal static class ObjectEvalValueHandler { private struct PtrObject { public readonly IntPtr Pointer; public readonly WeakReference WeakReference; public PtrObject(IntPtr pointer, WeakReference weakReference) { Pointer = pointer; WeakReference = weakReference; } } private static readonly Dictionary CreatedPointers = new Dictionary(); public static IntPtr GetObjectPtr(object o) { var hash = o.GetHashCode(); if (CreatedPointers.TryGetValue(hash, out var ptrObject)) { if (ptrObject.WeakReference.TryGetTarget(out _)) { return ptrObject.Pointer; } else { CreatedPointers.Remove(hash); } } var handle = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection); var ptr = GCHandle.ToIntPtr(handle); ptrObject = new PtrObject(ptr, new WeakReference(o)); CreatedPointers.Add(o.GetHashCode(), ptrObject); return ptrObject.Pointer; } } }