From c42af957b5002c1e55680f536500e7bbc874c98c Mon Sep 17 00:00:00 2001 From: ayende Date: Sun, 18 Nov 2007 03:23:07 +0000 Subject: [PATCH] Fixing a bug where ReferenceEquals was called on value types. Making sure that we can handle IntPtr values correctly, now, if there isn't a direct ldind_XYZ instruction, we will use an unbox.any instead of box & ldind git-svn-id: https://svn.castleproject.org/svn/castle/trunk@4492 73e77b4c-caa6-f847-a29a-24ab75ae54b6 --- .../RhinoMocksTestCase.cs | 22 ++++++++++++++++++++++ .../Generators/Emitters/OpCodeUtil.cs | 2 +- .../Emitters/SimpleAST/ConvertExpression.cs | 12 ++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy.Tests/RhinoMocksTestCase.cs b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy.Tests/RhinoMocksTestCase.cs index 1891caee1..7a463053f 100644 --- a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy.Tests/RhinoMocksTestCase.cs +++ b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy.Tests/RhinoMocksTestCase.cs @@ -133,6 +133,15 @@ namespace Castle.DynamicProxy.Tests Assert.IsNotNull(o); } + + [Test] + public void CanGetCorrectValuesFromIntPtr() + { + IHaveMethodThatReturnsIntPtr o = (IHaveMethodThatReturnsIntPtr)generator + .CreateInterfaceProxyWithoutTarget(typeof(IHaveMethodThatReturnsIntPtr), new IntPtrInterceptor()); + Assert.AreEqual(IntPtr.Zero, o.Foo()); + } + [Test] public void ProxyInternalMethod() { @@ -261,6 +270,11 @@ namespace Castle.DynamicProxy.Tests T Create() where T : List; } + public interface IHaveMethodThatReturnsIntPtr + { + IntPtr Foo(); + } + public abstract class SomeClassWithProtectedInternalAbstractClass { @@ -296,5 +310,13 @@ namespace Castle.DynamicProxy.Tests } } + public class IntPtrInterceptor : IInterceptor + { + public void Intercept(IInvocation invocation) + { + invocation.ReturnValue = IntPtr.Zero; + } + } + #endif } \ No newline at end of file diff --git a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/OpCodeUtil.cs b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/OpCodeUtil.cs index 1a1aaef77..fe18e7d50 100644 --- a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/OpCodeUtil.cs +++ b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/OpCodeUtil.cs @@ -86,7 +86,7 @@ namespace Castle.DynamicProxy.Generators.Emitters { OpCode opCode = LdindOpCodesDictionary.Instance[type]; - if (ReferenceEquals(opCode, LdindOpCodesDictionary.EmptyOpCode)) + if (opCode == LdindOpCodesDictionary.EmptyOpCode) { throw new ArgumentException("Type " + type + " could not be converted to a OpCode"); } diff --git a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/SimpleAST/ConvertExpression.cs b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/SimpleAST/ConvertExpression.cs index 6079ecb7c..0bceca057 100644 --- a/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/SimpleAST/ConvertExpression.cs +++ b/Tools/Castle.DynamicProxy2/Castle.DynamicProxy/Generators/Emitters/SimpleAST/ConvertExpression.cs @@ -64,8 +64,16 @@ namespace Castle.DynamicProxy.Generators.Emitters.SimpleAST { // Unbox conversion // Assumes fromType is a boxed value - gen.Emit(OpCodes.Unbox, target); - OpCodeUtil.EmitLoadIndirectOpCodeForType(gen, target); + // if we can, we emit a box and ldind, otherwise, we will use unbox.any + if (LdindOpCodesDictionary.Instance[target] != LdindOpCodesDictionary.EmptyOpCode) + { + gen.Emit(OpCodes.Unbox, target); + OpCodeUtil.EmitLoadIndirectOpCodeForType(gen, target); + } + else + { + gen.Emit(OpCodes.Unbox_Any, target); + } } } else -- 2.11.4.GIT