Support IL2CPP and fix stale handle

Summary:
- Unity IL2CPP (ENABLE_IL2CPP) requires the same code path with AOT compile.
- Clean up unneeded code in YGConfigHandle.
- YogaNode.Reset makes YGNodeHandle stale. Unmanaged side was reset by YGNodeReset, but YGNodeHandle keeps to retain the old managed handle. Release it.
Closes https://github.com/facebook/yoga/pull/491

Reviewed By: astreet

Differential Revision: D4765683

Pulled By: splhack

fbshipit-source-id: 83bfe19feb0e6ec45dc504e42b0c6c34e10af8e2
This commit is contained in:
Kazuki Sakamoto
2017-03-27 07:57:54 -07:00
committed by Facebook Github Bot
parent d342fb1879
commit 91a34bb875
3 changed files with 39 additions and 30 deletions

View File

@@ -28,7 +28,7 @@ namespace Facebook.Yoga
internal class YGNodeHandle : SafeHandle internal class YGNodeHandle : SafeHandle
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
private GCHandle _managed; private GCHandle _managed;
#endif #endif
@@ -46,27 +46,37 @@ namespace Facebook.Yoga
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
if (_managed.IsAllocated) ReleaseManaged();
{
_managed.Free();
}
#endif #endif
Native.YGNodeFree(this.handle); Native.YGNodeFree(this.handle);
GC.KeepAlive(this); GC.KeepAlive(this);
return true; return true;
} }
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
public void SetContext(YogaNode node) public void SetContext(YogaNode node)
{ {
if (!_managed.IsAllocated) if (!_managed.IsAllocated)
{ {
#if ENABLE_IL2CPP
// Weak causes 'GCHandle value belongs to a different domain' error
_managed = GCHandle.Alloc(node);
#else
_managed = GCHandle.Alloc(node, GCHandleType.Weak); _managed = GCHandle.Alloc(node, GCHandleType.Weak);
#endif
Native.YGNodeSetContext(this.handle, GCHandle.ToIntPtr(_managed)); Native.YGNodeSetContext(this.handle, GCHandle.ToIntPtr(_managed));
} }
} }
public void ReleaseManaged()
{
if (_managed.IsAllocated)
{
_managed.Free();
}
}
public static YogaNode GetManaged(IntPtr ygNodePtr) public static YogaNode GetManaged(IntPtr ygNodePtr)
{ {
var node = var node =
@@ -83,10 +93,6 @@ namespace Facebook.Yoga
internal class YGConfigHandle : SafeHandle internal class YGConfigHandle : SafeHandle
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
private GCHandle _managed;
#endif
private YGConfigHandle() : base(IntPtr.Zero, true) private YGConfigHandle() : base(IntPtr.Zero, true)
{ {
} }
@@ -101,12 +107,6 @@ namespace Facebook.Yoga
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__
if (_managed.IsAllocated)
{
_managed.Free();
}
#endif
Native.YGConfigFree(this.handle); Native.YGConfigFree(this.handle);
GC.KeepAlive(this); GC.KeepAlive(this);
return true; return true;
@@ -429,9 +429,9 @@ namespace Facebook.Yoga
#endregion #endregion
#region IOS #region AOT
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
[DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)] [DllImport(DllName, ExactSpelling = true, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr YGNodeGetContext(IntPtr node); public static extern IntPtr YGNodeGetContext(IntPtr node);

View File

@@ -13,6 +13,9 @@ using System.Runtime.InteropServices;
#if __IOS__ #if __IOS__
using ObjCRuntime; using ObjCRuntime;
#endif #endif
#if ENABLE_IL2CPP
using AOT;
#endif
namespace Facebook.Yoga namespace Facebook.Yoga
{ {
@@ -26,7 +29,7 @@ namespace Facebook.Yoga
public static Func Logger = null; public static Func Logger = null;
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
[MonoPInvokeCallback(typeof(Func))] [MonoPInvokeCallback(typeof(Func))]
#endif #endif
public static void LoggerInternal(YogaLogLevel level, string message) public static void LoggerInternal(YogaLogLevel level, string message)

View File

@@ -12,12 +12,15 @@ using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Text; using System.Text;
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
#endif #endif
#if __IOS__ #if __IOS__
using ObjCRuntime; using ObjCRuntime;
#endif #endif
#if ENABLE_IL2CPP
using AOT;
#endif
namespace Facebook.Yoga namespace Facebook.Yoga
{ {
@@ -63,7 +66,7 @@ namespace Facebook.Yoga
private MeasureFunction _measureFunction; private MeasureFunction _measureFunction;
private BaselineFunction _baselineFunction; private BaselineFunction _baselineFunction;
private object _data; private object _data;
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
private static YogaMeasureFunc _managedMeasure; private static YogaMeasureFunc _managedMeasure;
private static YogaBaselineFunc _managedBaseline; private static YogaBaselineFunc _managedBaseline;
#else #else
@@ -106,6 +109,9 @@ namespace Facebook.Yoga
_data = null; _data = null;
Native.YGNodeReset(_ygNode); Native.YGNodeReset(_ygNode);
#if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
_ygNode.ReleaseManaged();
#endif
} }
public bool IsDirty public bool IsDirty
@@ -616,8 +622,8 @@ namespace Facebook.Yoga
_measureFunction = measureFunction; _measureFunction = measureFunction;
if (measureFunction != null) if (measureFunction != null)
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
_managedMeasure = MeasureInternalIOS; _managedMeasure = MeasureInternalAOT;
_ygNode.SetContext(this); _ygNode.SetContext(this);
#else #else
_managedMeasure = MeasureInternal; _managedMeasure = MeasureInternal;
@@ -635,8 +641,8 @@ namespace Facebook.Yoga
_baselineFunction = baselineFunction; _baselineFunction = baselineFunction;
if (baselineFunction != null) if (baselineFunction != null)
{ {
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
_managedBaseline = BaselineInternalIOS; _managedBaseline = BaselineInternalAOT;
_ygNode.SetContext(this); _ygNode.SetContext(this);
#else #else
_managedBaseline = BaselineInternal; _managedBaseline = BaselineInternal;
@@ -658,9 +664,9 @@ namespace Facebook.Yoga
Native.YGNodeStyleGetDirection(_ygNode)); Native.YGNodeStyleGetDirection(_ygNode));
} }
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
[MonoPInvokeCallback(typeof(YogaMeasureFunc))] [MonoPInvokeCallback(typeof(YogaMeasureFunc))]
private static YogaSize MeasureInternalIOS( private static YogaSize MeasureInternalAOT(
IntPtr ygNodePtr, IntPtr ygNodePtr,
float width, float width,
YogaMeasureMode widthMode, YogaMeasureMode widthMode,
@@ -687,9 +693,9 @@ namespace Facebook.Yoga
return _measureFunction(this, width, widthMode, height, heightMode); return _measureFunction(this, width, widthMode, height, heightMode);
} }
#if (UNITY_IOS && !UNITY_EDITOR) || __IOS__ #if (UNITY_IOS && !UNITY_EDITOR) || ENABLE_IL2CPP || __IOS__
[MonoPInvokeCallback(typeof(YogaBaselineFunc))] [MonoPInvokeCallback(typeof(YogaBaselineFunc))]
private static float BaselineInternalIOS( private static float BaselineInternalAOT(
IntPtr ygNodePtr, IntPtr ygNodePtr,
float width, float width,
float height) float height)