Fix calling convention gap in DynamicILGenerator.EmitCalli (#16546)
authorPetar Petrov <petarpetrovt@gmail.com>
Sat, 24 Feb 2018 18:52:01 +0000 (20:52 +0200)
committerJan Kotas <jkotas@microsoft.com>
Sat, 24 Feb 2018 18:52:01 +0000 (10:52 -0800)
src/mscorlib/src/System/Reflection/Emit/DynamicILGenerator.cs

index 5570233ff11a2b38a3e69b789c19d719b4137715..5ecf0bef8037e0f819fafe170062a393bc1ff0a7 100644 (file)
@@ -241,6 +241,41 @@ namespace System.Reflection.Emit
             PutInteger4(token);
         }
 
+        public override void EmitCalli(OpCode opcode, CallingConvention unmanagedCallConv, Type returnType, Type[] parameterTypes)
+        {
+            int stackchange = 0;
+            int cParams = 0;
+            int i;
+            SignatureHelper sig;
+
+            if (parameterTypes != null)
+                cParams = parameterTypes.Length;
+
+            sig = SignatureHelper.GetMethodSigHelper(unmanagedCallConv, returnType);
+
+            if (parameterTypes != null)
+                for (i = 0; i < cParams; i++)
+                    sig.AddArgument(parameterTypes[i]);
+
+            // If there is a non-void return type, push one. 
+            if (returnType != typeof(void))
+                stackchange++;
+
+            // Pop off arguments if any.
+            if (parameterTypes != null)
+                stackchange -= cParams;
+
+            // Pop the native function pointer.
+            stackchange--;
+            UpdateStackSize(OpCodes.Calli, stackchange);
+
+            EnsureCapacity(7);
+            Emit(OpCodes.Calli);
+
+            int token = GetTokenForSig(sig.GetSignature(true));
+            PutInteger4(token);
+        }
+
         public override void EmitCall(OpCode opcode, MethodInfo methodInfo, Type[] optionalParameterTypes)
         {
             if (methodInfo == null)
@@ -305,7 +340,7 @@ namespace System.Reflection.Emit
                 UpdateStackSize(opcode, stackchange);
             }
 
-            int token = GetTokenForSig(signature.GetSignature(true)); ;
+            int token = GetTokenForSig(signature.GetSignature(true));
             PutInteger4(token);
         }