MegaBites-AI/Windows-powershell
0372
1/* ****************************************************************************2 *3 * Copyright (c) Microsoft Corporation.4 *5 * This source code is subject to terms and conditions of the Apache License, Version 2.0. A6 * copy of the license can be found in the License.html file at the root of this distribution. If7 * you cannot locate the Apache License, Version 2.0, please send an email to8 * dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound9 * by the terms of the Apache License, Version 2.0.10 *11 * You must not remove this notice, or any other, from this software.12 *13 *14 * ***************************************************************************/15 16using System.Runtime.CompilerServices;17 18namespace System.Management.Automation.Interpreter19{20 internal sealed partial class DynamicInstructionN : Instruction21 {22 private readonly CallInstruction _target;23 private readonly object _targetDelegate;24 private readonly CallSite _site;25 private readonly int _argumentCount;26 private readonly bool _isVoid;27 28 public DynamicInstructionN(Type delegateType, CallSite site)29 {30 var methodInfo = delegateType.GetMethod("Invoke");31 var parameters = methodInfo.GetParameters();32 33 _target = CallInstruction.Create(methodInfo, parameters);34 _site = site;35 _argumentCount = parameters.Length - 1;36 _targetDelegate = site.GetType().GetField("Target").GetValue(site);37 }38 39 public DynamicInstructionN(Type delegateType, CallSite site, bool isVoid)40 : this(delegateType, site)41 {42 _isVoid = isVoid;43 }44 45 public override int ProducedStack { get { return _isVoid ? 0 : 1; } }46 47 public override int ConsumedStack { get { return _argumentCount; } }48 49 public override int Run(InterpretedFrame frame)50 {51 int first = frame.StackIndex - _argumentCount;52 object[] args = new object[1 + _argumentCount];53 args[0] = _site;54 for (int i = 0; i < _argumentCount; i++)55 {56 args[1 + i] = frame.Data[first + i];57 }58 59 object ret = _target.InvokeInstance(_targetDelegate, args);60 if (_isVoid)61 {62 frame.StackIndex = first;63 }64 else65 {66 frame.Data[first] = ret;67 frame.StackIndex = first + 1;68 }69 70 return 1;71 }72 73 public override string ToString()74 {75 return "DynamicInstructionN(" + _site + ")";76 }77 }78}79 