Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
TypeOperations.cs198 linesDownload Raw Back to interpreter
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.Reflection;17using System.Runtime.CompilerServices;18 19namespace System.Management.Automation.Interpreter20{21    internal sealed class CreateDelegateInstruction : Instruction22    {23        private readonly LightDelegateCreator _creator;24 25        internal CreateDelegateInstruction(LightDelegateCreator delegateCreator)26        {27            _creator = delegateCreator;28        }29 30        public override int ConsumedStack { get { return _creator.Interpreter.ClosureSize; } }31 32        public override int ProducedStack { get { return 1; } }33 34        public override int Run(InterpretedFrame frame)35        {36            StrongBox<object>[] closure;37            if (ConsumedStack > 0)38            {39                closure = new StrongBox<object>[ConsumedStack];40                for (int i = closure.Length - 1; i >= 0; i--)41                {42                    closure[i] = (StrongBox<object>)frame.Pop();43                }44            }45            else46            {47                closure = null;48            }49 50            Delegate d = _creator.CreateDelegate(closure);51 52            frame.Push(d);53            return +1;54        }55    }56 57    internal sealed class NewInstruction : Instruction58    {59        private readonly ConstructorInfo _constructor;60        private readonly int _argCount;61 62        public NewInstruction(ConstructorInfo constructor)63        {64            _constructor = constructor;65            _argCount = constructor.GetParameters().Length;66        }67 68        public override int ConsumedStack { get { return _argCount; } }69 70        public override int ProducedStack { get { return 1; } }71 72        public override int Run(InterpretedFrame frame)73        {74            object[] args = new object[_argCount];75            for (int i = _argCount - 1; i >= 0; i--)76            {77                args[i] = frame.Pop();78            }79 80            object ret;81            try82            {83                ret = _constructor.Invoke(args);84            }85            catch (TargetInvocationException e)86            {87                ExceptionHelpers.UpdateForRethrow(e.InnerException);88                throw e.InnerException;89            }90 91            frame.Push(ret);92            return +1;93        }94 95        public override string ToString()96        {97            return "New " + _constructor.DeclaringType.Name + "(" + _constructor + ")";98        }99    }100 101    internal sealed class DefaultValueInstruction<T> : Instruction102    {103        internal DefaultValueInstruction() { }104 105        public override int ConsumedStack { get { return 0; } }106 107        public override int ProducedStack { get { return 1; } }108 109        public override int Run(InterpretedFrame frame)110        {111            frame.Push(default(T));112            return +1;113        }114 115        public override string ToString()116        {117            return "New " + typeof(T);118        }119    }120 121    internal sealed class TypeIsInstruction<T> : Instruction122    {123        internal TypeIsInstruction() { }124 125        public override int ConsumedStack { get { return 1; } }126 127        public override int ProducedStack { get { return 1; } }128 129        public override int Run(InterpretedFrame frame)130        {131            // unfortunately Type.IsInstanceOfType() is 35-times slower than "is T" so we use generic code:132            frame.Push(ScriptingRuntimeHelpers.BooleanToObject(frame.Pop() is T));133            return +1;134        }135 136        public override string ToString()137        {138            return "TypeIs " + typeof(T).Name;139        }140    }141 142    internal sealed class TypeAsInstruction<T> : Instruction143    {144        internal TypeAsInstruction() { }145 146        public override int ConsumedStack { get { return 1; } }147 148        public override int ProducedStack { get { return 1; } }149 150        public override int Run(InterpretedFrame frame)151        {152            // can't use as w/o generic constraint153            object value = frame.Pop();154            if (value is T)155            {156                frame.Push(value);157            }158            else159            {160                frame.Push(null);161            }162 163            return +1;164        }165 166        public override string ToString()167        {168            return "TypeAs " + typeof(T).Name;169        }170    }171 172    internal sealed class TypeEqualsInstruction : Instruction173    {174        public static readonly TypeEqualsInstruction Instance = new TypeEqualsInstruction();175 176        public override int ConsumedStack { get { return 2; } }177 178        public override int ProducedStack { get { return 1; } }179 180        private TypeEqualsInstruction()181        {182        }183 184        public override int Run(InterpretedFrame frame)185        {186            object type = frame.Pop();187            object obj = frame.Pop();188            frame.Push(ScriptingRuntimeHelpers.BooleanToObject(obj != null && (object)obj.GetType() == type));189            return +1;190        }191 192        public override string InstructionName193        {194            get { return "TypeEquals()"; }195        }196    }197}198