10000 Void codemethod call should not crash by powercode · Pull Request #4850 · PowerShell/PowerShell · GitHub
[go: up one dir, main page]

Skip to content

Void codemethod call should not crash #4850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions src/System.Management.Automation/engine/runtime/Binding/Binders.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ExpressionCache.AutomationNullConstant);
}
else
{
expr = expr.Cast(typeof(object));
}
return new DynamicMetaObject(expr, restrictions).WriteToDebugLog(this);
}

var parameterizedProperty = methodInfo as PSParameterizedProperty;
Expand Down
33 changes: 33 additions & 0 deletions test/powershell/engine/ETS/Adapter.Tests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -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" {
Expand Down
0