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 16#if !CLR217#else18using Microsoft.Scripting.Ast;19#endif20using System.Reflection;21using System.Runtime.CompilerServices;22using System.Collections.Generic;23 24namespace System.Management.Automation.Interpreter25{26 27#nullable enable28 internal interface IBoxableInstruction29 {30 Instruction? BoxIfIndexMatches(int index);31 }32#nullable restore33 34 internal abstract class LocalAccessInstruction : Instruction35 {36 internal readonly int _index;37 38 protected LocalAccessInstruction(int index)39 {40 _index = index;41 }42 43 public override string ToDebugString(int instructionIndex, object cookie, Func<int, int> labelIndexer, IList<object> objects)44 {45 return cookie == null ?46 InstructionName + "(" + _index + ")" :47 InstructionName + "(" + cookie + ": " + _index + ")";48 }49 }50 51 #region Load52 53 internal sealed class LoadLocalInstruction : LocalAccessInstruction, IBoxableInstruction54 {55 internal LoadLocalInstruction(int index)56 : base(index)57 {58 }59 60 public override int ProducedStack { get { return 1; } }61 62 public override int Run(InterpretedFrame frame)63 {64 frame.Data[frame.StackIndex++] = frame.Data[_index];65 // frame.Push(frame.Data[_index]);66 return +1;67 }68 69 public Instruction BoxIfIndexMatches(int index)70 {71 return (index == _index) ? InstructionList.LoadLocalBoxed(index) : null;72 }73 }74 75 internal sealed class LoadLocalBoxedInstruction : LocalAccessInstruction76 {77 internal LoadLocalBoxedInstruction(int index)78 : base(index)79 {80 }81 82 public override int ProducedStack { get { return 1; } }83 84 public override int Run(InterpretedFrame frame)85 {86 var box = (StrongBox<object>)frame.Data[_index];87 frame.Data[frame.StackIndex++] = box.Value;88 return +1;89 }90 }91 92 internal sealed class LoadLocalFromClosureInstruction : LocalAccessInstruction93 {94 internal LoadLocalFromClosureInstruction(int index)95 : base(index)96 {97 }98 99 public override int ProducedStack { get { return 1; } }100 101 public override int Run(InterpretedFrame frame)102 {103 var box = frame.Closure[_index];104 frame.Data[frame.StackIndex++] = box.Value;105 return +1;106 }107 }108 109 internal sealed class LoadLocalFromClosureBoxedInstruction : LocalAccessInstruction110 {111 internal LoadLocalFromClosureBoxedInstruction(int index)112 : base(index)113 {114 }115 116 public override int ProducedStack { get { return 1; } }117 118 public override int Run(InterpretedFrame frame)119 {120 var box = frame.Closure[_index];121 frame.Data[frame.StackIndex++] = box;122 return +1;123 }124 }125 126 #endregion127 128 #region Store, Assign129 130 internal sealed class AssignLocalInstruction : LocalAccessInstruction, IBoxableInstruction131 {132 internal AssignLocalInstruction(int index)133 : base(index)134 {135 }136 137 public override int ConsumedStack { get { return 1; } }138 139 public override int ProducedStack { get { return 1; } }140 141 public override int Run(InterpretedFrame frame)142 {143 frame.Data[_index] = frame.Peek();144 return +1;145 }146 147 public Instruction BoxIfIndexMatches(int index)148 {149 return (index == _index) ? InstructionList.AssignLocalBoxed(index) : null;150 }151 }152 153 internal sealed class StoreLocalInstruction : LocalAccessInstruction, IBoxableInstruction154 {155 internal StoreLocalInstruction(int index)156 : base(index)157 {158 }159 160 public override int ConsumedStack { get { return 1; } }161 162 public override int Run(InterpretedFrame frame)163 {164 frame.Data[_index] = frame.Data[--frame.StackIndex];165 // frame.Data[_index] = frame.Pop();166 return +1;167 }168 169 public Instruction BoxIfIndexMatches(int index)170 {171 return (index == _index) ? InstructionList.StoreLocalBoxed(index) : null;172 }173 }174 175 internal sealed class AssignLocalBoxedInstruction : LocalAccessInstruction176 {177 internal AssignLocalBoxedInstruction(int index)178 : base(index)179 {180 }181 182 public override int ConsumedStack { get { return 1; } }183 184 public override int ProducedStack { get { return 1; } }185 186 public override int Run(InterpretedFrame frame)187 {188 var box = (StrongBox<object>)frame.Data[_index];189 box.Value = frame.Peek();190 return +1;191 }192 }193 194 internal sealed class StoreLocalBoxedInstruction : LocalAccessInstruction195 {196 internal StoreLocalBoxedInstruction(int index)197 : base(index)198 {199 }200 201 public override int ConsumedStack { get { return 1; } }202 203 public override int ProducedStack { get { return 0; } }204 205 public override int Run(InterpretedFrame frame)206 {207 var box = (StrongBox<object>)frame.Data[_index];208 box.Value = frame.Data[--frame.StackIndex];209 return +1;210 }211 }212 213 internal sealed class AssignLocalToClosureInstruction : LocalAccessInstruction214 {215 internal AssignLocalToClosureInstruction(int index)216 : base(index)217 {218 }219 220 public override int ConsumedStack { get { return 1; } }221 222 public override int ProducedStack { get { return 1; } }223 224 public override int Run(InterpretedFrame frame)225 {226 var box = frame.Closure[_index];227 box.Value = frame.Peek();228 return +1;229 }230 }231 232 #endregion233 234 #region Initialize235 236 [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1012:AbstractTypesShouldNotHaveConstructors")]237 internal abstract class InitializeLocalInstruction : LocalAccessInstruction238 {239 internal InitializeLocalInstruction(int index)240 : base(index)241 {242 }243 244 internal sealed class Reference : InitializeLocalInstruction, IBoxableInstruction245 {246 internal Reference(int index)247 : base(index)248 {249 }250 251 public override int Run(InterpretedFrame frame)252 {253 frame.Data[_index] = null;254 return 1;255 }256 257 public Instruction BoxIfIndexMatches(int index)258 {259 return (index == _index) ? InstructionList.InitImmutableRefBox(index) : null;260 }261 262 public override string InstructionName263 {264 get { return "InitRef"; }265 }266 }267 268 internal sealed class ImmutableValue : InitializeLocalInstruction, IBoxableInstruction269 {270 private readonly object _defaultValue;271 272 internal ImmutableValue(int index, object defaultValue)273 : base(index)274 {275 _defaultValue = defaultValue;276 }277 278 public override int Run(InterpretedFrame frame)279 {280 frame.Data[_index] = _defaultValue;281 return 1;282 }283 284 public Instruction BoxIfIndexMatches(int index)285 {286 return (index == _index) ? new ImmutableBox(index, _defaultValue) : null;287 }288 289 public override string InstructionName290 {291 get { return "InitImmutableValue"; }292 }293 }294 295 internal sealed class ImmutableBox : InitializeLocalInstruction296 {297 // immutable value:298 private readonly object _defaultValue;299 300 internal ImmutableBox(int index, object defaultValue)301 : base(index)302 {303 _defaultValue = defaultValue;304 }305 306 public override int Run(InterpretedFrame frame)307 {308 frame.Data[_index] = new StrongBox<object>(_defaultValue);309 return 1;310 }311 312 public override string InstructionName313 {314 get { return "InitImmutableBox"; }315 }316 }317 318 internal sealed class ParameterBox : InitializeLocalInstruction319 {320 public ParameterBox(int index)321 : base(index)322 {323 }324 325 public override int Run(InterpretedFrame frame)326 {327 frame.Data[_index] = new StrongBox<object>(frame.Data[_index]);328 return 1;329 }330 }331 332 internal sealed class Parameter : InitializeLocalInstruction, IBoxableInstruction333 {334 internal Parameter(int index)335 : base(index)336 {337 }338 339 public override int Run(InterpretedFrame frame)340 {341 // nop342 return 1;343 }344 345 public Instruction BoxIfIndexMatches(int index)346 {347 if (index == _index)348 {349 return InstructionList.ParameterBox(index);350 }351 352 return null;353 }354 355 public override string InstructionName356 {357 get { return "InitParameter"; }358 }359 }360 361 internal sealed class MutableValue : InitializeLocalInstruction, IBoxableInstruction362 {363 private readonly Type _type;364 365 internal MutableValue(int index, Type type)366 : base(index)367 {368 _type = type;369 }370 371 public override int Run(InterpretedFrame frame)372 {373 try374 {375 frame.Data[_index] = Activator.CreateInstance(_type);376 }377 catch (TargetInvocationException e)378 {379 ExceptionHelpers.UpdateForRethrow(e.InnerException);380 throw e.InnerException;381 }382 383 return 1;384 }385 386 public Instruction BoxIfIndexMatches(int index)387 {388 return (index == _index) ? new MutableBox(index, _type) : null;389 }390 391 public override string InstructionName392 {393 get { return "InitMutableValue"; }394 }395 }396 397 internal sealed class MutableBox : InitializeLocalInstruction398 {399 private readonly Type _type;400 401 internal MutableBox(int index, Type type)402 : base(index)403 {404 _type = type;405 }406 407 public override int Run(InterpretedFrame frame)408 {409 frame.Data[_index] = new StrongBox<object>(Activator.CreateInstance(_type));410 return 1;411 }412 413 public override string InstructionName414 {415 get { return "InitMutableBox"; }416 }417 }418 }419 420 #endregion421 422 #region RuntimeVariables423 424 internal sealed class RuntimeVariablesInstruction : Instruction425 {426 private readonly int _count;427 428 public RuntimeVariablesInstruction(int count)429 {430 _count = count;431 }432 433 public override int ProducedStack { get { return 1; } }434 435 public override int ConsumedStack { get { return _count; } }436 437 public override int Run(InterpretedFrame frame)438 {439 var ret = new IStrongBox[_count];440 for (int i = ret.Length - 1; i >= 0; i--)441 {442 ret[i] = (IStrongBox)frame.Pop();443 }444 445 frame.Push(RuntimeVariables.Create(ret));446 return +1;447 }448 449 public override string ToString()450 {451 return "GetRuntimeVariables()";452 }453 }454 455 #endregion456}457 