Commit 08599a5b authored by Hamish Milne's avatar Hamish Milne
Browse files

Fixed object aliasing bug

parent d0a17a61
...@@ -57,10 +57,26 @@ namespace NLua ...@@ -57,10 +57,26 @@ namespace NLua
*/ */
public class ObjectTranslator public class ObjectTranslator
{ {
// Compare cache entries by exact reference to avoid unwanted aliases
private class ReferenceComparer : IEqualityComparer<object>
{
public new bool Equals(object x, object y)
{
if (x != null && y != null && x.GetType() == y.GetType() && x.GetType().IsValueType && y.GetType().IsValueType)
return x.Equals(y); // Special case for boxed value types
return Object.ReferenceEquals(x, y);
}
public int GetHashCode(object obj)
{
return obj.GetHashCode();
}
}
LuaNativeFunction registerTableFunction, unregisterTableFunction, getMethodSigFunction, LuaNativeFunction registerTableFunction, unregisterTableFunction, getMethodSigFunction,
getConstructorSigFunction, importTypeFunction, loadAssemblyFunction, ctypeFunction, enumFromIntFunction; getConstructorSigFunction, importTypeFunction, loadAssemblyFunction, ctypeFunction, enumFromIntFunction;
// object to object # // object to object #
readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int> (); readonly Dictionary<object, int> objectsBackMap = new Dictionary<object, int> (new ReferenceComparer());
// object # to object (FIXME - it should be possible to get object address as an object #) // object # to object (FIXME - it should be possible to get object address as an object #)
readonly Dictionary<int, object> objects = new Dictionary<int, object> (); readonly Dictionary<int, object> objects = new Dictionary<int, object> ();
internal EventHandlerContainer pendingEvents = new EventHandlerContainer (); internal EventHandlerContainer pendingEvents = new EventHandlerContainer ();
...@@ -777,9 +793,9 @@ namespace NLua ...@@ -777,9 +793,9 @@ namespace NLua
int index = nextObj++; int index = nextObj++;
objects [index] = obj; objects [index] = obj;
#if NETFX_CORE #if NETFX_CORE
if (!obj.GetType ().GetTypeInfo().IsValueType || obj.GetType ().GetTypeInfo().IsValueType) if (!obj.GetType ().GetTypeInfo().IsValueType || obj.GetType ().GetTypeInfo().IsEnum)
#else #else
if (!obj.GetType().IsValueType || obj.GetType().IsValueType) if (!obj.GetType().IsValueType || obj.GetType().IsEnum)
#endif #endif
objectsBackMap [obj] = index; objectsBackMap [obj] = index;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment