From fe9f1c7a921805f30185f23a6f5e844c0c102230 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Sat, 16 Sep 2017 09:35:05 +0200 Subject: [PATCH 1/3] Failing test for calling void method --- test/powershell/engine/ETS/Adapter.Tests.ps1 | 33 ++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/test/powershell/engine/ETS/Adapter.Tests.ps1 b/test/powershell/engine/ETS/Adapter.Tests.ps1 index 8ab621038f0..bb5b9f3215a 100644 --- a/test/powershell/engine/ETS/Adapter.Tests.ps1 +++ b/test/powershell/engine/ETS/Adapter.Tests.ps1 @@ -79,6 +79,39 @@ Describe "Adapter Tests" -tags "CI" { $val = $doc.ToString() $val | Should Be "book" } + + It "Calls CodeMethod with void result" { + + class TestCodeMethodInvokationWithVoidReturn { + [int] $CallCounter + + static [int] IntMethodCM([PSObject] $self) { + return $self.CallCounter + } + + static [void] VoidMethodCM([PSObject] $self) { + $self.CallCounter++ + } + + static [Reflection.MethodInfo] GetMethodInfo([string] $name) { + return [TestCodeMethodInvokationWithVoidReturn].GetMethod($name) + } + } + + Update-TypeData -Force -TypeName TestCodeMethodInvokationWithVoidReturn -MemberType CodeMethod -MemberName IntMethod -Value ([TestCodeMethodInvokationWithVoidReturn]::GetMethodInfo('IntMethodCM')) + Update-TypeData -Force -TypeName TestCodeMethodInvokationWithVoidReturn -MemberType CodeMethod -MemberName VoidMethod -Value ([TestCodeMethodInvokationWithVoidReturn]::GetMethodInfo('VoidMethodCM')) + try { + $o = [TestCodeMethodInvokationWithVoidReturn]::new() + $o.CallCounter | Should Be 0 + $o.VoidMethod() + $o.CallCounter | Should be 1 + + $o.IntMethod() | Should be 1 + } + finally { + Remove-TypeData TestCodeMethodInvokationWithVoidReturn + } + } } Describe "Adapter XML Tests" -tags "CI" { From 06e20fe8fccd112c267a9c0f34a3796c81e7972f Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Sat, 16 Sep 2017 09:05:49 +0200 Subject: [PATCH 2/3] Handling case in Invoking CodeMethod when return type is void. --- .../engine/runtime/Binding/Binders.cs | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index ca1bdb607dc..669d9e16f8f 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -6568,9 +6568,16 @@ public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, var codeMethod = methodInfo as PSCodeMethod; if (codeMethod != null) { - return new DynamicMetaObject( - InvokeMethod(codeMethod.CodeReference, null, args.Prepend(target).ToArray(), false, MethodInvocationType.Ordinary) - .Cast(typeof(object)), restrictions).WriteToDebugLog(this); + Expression expr = InvokeMethod(codeMethod.CodeReference, null, args.Prepend(target).ToArray(), false, MethodInvocationType.Ordinary); + if (codeMethod.CodeReference.ReturnType == typeof(void)) + { + expr = Expression.Block(expr, Expression.Default(typeof(object))); + } + else + { + expr = expr.Cast(typeof(object)); + } + return new DynamicMetaObject(expr, restrictions).WriteToDebugLog(this); } var parameterizedProperty = methodInfo as PSParameterizedProperty; From f0ad5f8bb55ba63468a9b0d4eb1bbb65cc880a41 Mon Sep 17 00:00:00 2001 From: Staffan Gustafsson Date: Mon, 18 Sep 2017 07:23:34 +0200 Subject: [PATCH 3/3] Addressing lzybkr review comments --- .../engine/runtime/Binding/Binders.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs index 669d9e16f8f..3436efee062 100644 --- a/src/System.Management.Automation/engine/runtime/Binding/Binders.cs +++ b/src/System.Management.Automation/engine/runtime/Binding/Binders.cs @@ -6571,7 +6571,7 @@ public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, Expression expr = InvokeMethod(codeMethod.CodeReference, null, args.Prepend(target).ToArray(), false, MethodInvocationType.Ordinary); if (codeMethod.CodeReference.ReturnType == typeof(void)) { - expr = Expression.Block(expr, Expression.Default(typeof(object))); + expr = Expression.Block(expr, ExpressionCache.AutomationNullConstant); } else {