File size: 10,383 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | /* ****************************************************************************
*
* Copyright (c) Microsoft Corporation.
*
* This source code is subject to terms and conditions of the Apache License, Version 2.0. A
* copy of the license can be found in the License.html file at the root of this distribution. If
* you cannot locate the Apache License, Version 2.0, please send an email to
* dlr@microsoft.com. By using this source code in any fashion, you are agreeing to be bound
* by the terms of the Apache License, Version 2.0.
*
* You must not remove this notice, or any other, from this software.
*
*
* ***************************************************************************/
#if !CLR2
#else
using Microsoft.Scripting.Ast;
#endif
using System.Collections.Generic;
using System.Diagnostics;
using System.Management.Automation.Language;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace System.Management.Automation.Interpreter
{
internal sealed class InterpretedFrame
{
public static readonly ThreadLocal<InterpretedFrame> CurrentFrame = new ThreadLocal<InterpretedFrame>();
internal readonly Interpreter Interpreter;
internal InterpretedFrame _parent;
private readonly int[] _continuations;
private int _continuationIndex;
private int _pendingContinuation;
private object _pendingValue;
public readonly object[] Data;
public readonly StrongBox<object>[] Closure;
public int StackIndex;
public int InstructionIndex;
internal InterpretedFrame(Interpreter interpreter, StrongBox<object>[] closure)
{
Interpreter = interpreter;
StackIndex = interpreter.LocalCount;
Data = new object[StackIndex + interpreter.Instructions.MaxStackDepth];
int c = interpreter.Instructions.MaxContinuationDepth;
if (c > 0)
{
_continuations = new int[c];
}
Closure = closure;
_pendingContinuation = -1;
_pendingValue = Interpreter.NoValue;
}
public DebugInfo GetDebugInfo(int instructionIndex)
{
return DebugInfo.GetMatchingDebugInfo(Interpreter._debugInfos, instructionIndex);
}
public string Name
{
get { return Interpreter._name; }
}
#region Data Stack Operations
public void Push(object value)
{
Data[StackIndex++] = value;
}
public void Push(bool value)
{
Data[StackIndex++] = value ? ScriptingRuntimeHelpers.True : ScriptingRuntimeHelpers.False;
}
public void Push(int value)
{
Data[StackIndex++] = ScriptingRuntimeHelpers.Int32ToObject(value);
}
public object Pop()
{
return Data[--StackIndex];
}
internal void SetStackDepth(int depth)
{
StackIndex = Interpreter.LocalCount + depth;
}
public object Peek()
{
return Data[StackIndex - 1];
}
public void Dup()
{
int i = StackIndex;
Data[i] = Data[i - 1];
StackIndex = i + 1;
}
public ExecutionContext ExecutionContext
{
get { return (ExecutionContext)Data[1]; }
}
public FunctionContext FunctionContext
{
get { return (FunctionContext)Data[0]; }
}
#endregion
#region Stack Trace
public InterpretedFrame Parent
{
get { return _parent; }
}
public static bool IsInterpretedFrame(MethodBase method)
{
// ContractUtils.RequiresNotNull(method, "method");
return method.DeclaringType == typeof(Interpreter) && method.Name == "Run";
}
/// <summary>
/// A single interpreted frame might be represented by multiple subsequent Interpreter.Run CLR frames.
/// This method filters out the duplicate CLR frames.
/// </summary>
public static IEnumerable<StackFrame> GroupStackFrames(IEnumerable<StackFrame> stackTrace)
{
bool inInterpretedFrame = false;
foreach (StackFrame frame in stackTrace)
{
if (InterpretedFrame.IsInterpretedFrame(frame.GetMethod()))
{
if (inInterpretedFrame)
{
continue;
}
inInterpretedFrame = true;
}
else
{
inInterpretedFrame = false;
}
yield return frame;
}
}
public IEnumerable<InterpretedFrameInfo> GetStackTraceDebugInfo()
{
var frame = this;
do
{
yield return new InterpretedFrameInfo(frame.Name, frame.GetDebugInfo(frame.InstructionIndex));
frame = frame.Parent;
} while (frame != null);
}
internal void SaveTraceToException(Exception exception)
{
if (exception.Data[typeof(InterpretedFrameInfo)] == null)
{
exception.Data[typeof(InterpretedFrameInfo)] = new List<InterpretedFrameInfo>(GetStackTraceDebugInfo()).ToArray();
}
}
public static InterpretedFrameInfo[] GetExceptionStackTrace(Exception exception)
{
return exception.Data[typeof(InterpretedFrameInfo)] as InterpretedFrameInfo[];
}
#if DEBUG
internal string[] Trace
{
get
{
var trace = new List<string>();
var frame = this;
do
{
trace.Add(frame.Name);
frame = frame.Parent;
} while (frame != null);
return trace.ToArray();
}
}
#endif
internal ThreadLocal<InterpretedFrame>.StorageInfo Enter()
{
var currentFrame = InterpretedFrame.CurrentFrame.GetStorageInfo();
_parent = currentFrame.Value;
currentFrame.Value = this;
return currentFrame;
}
internal void Leave(ThreadLocal<InterpretedFrame>.StorageInfo currentFrame)
{
currentFrame.Value = _parent;
}
#endregion
#region Continuations
internal bool IsJumpHappened()
{
return _pendingContinuation >= 0;
}
public void RemoveContinuation()
{
_continuationIndex--;
}
public void PushContinuation(int continuation)
{
_continuations[_continuationIndex++] = continuation;
}
public int YieldToCurrentContinuation()
{
var target = Interpreter._labels[_continuations[_continuationIndex - 1]];
SetStackDepth(target.StackDepth);
return target.Index - InstructionIndex;
}
/// <summary>
/// Get called from the LeaveFinallyInstruction.
/// </summary>
public int YieldToPendingContinuation()
{
Debug.Assert(_pendingContinuation >= 0);
RuntimeLabel pendingTarget = Interpreter._labels[_pendingContinuation];
// the current continuation might have higher priority (continuationIndex is the depth of the current continuation):
if (pendingTarget.ContinuationStackDepth < _continuationIndex)
{
RuntimeLabel currentTarget = Interpreter._labels[_continuations[_continuationIndex - 1]];
SetStackDepth(currentTarget.StackDepth);
return currentTarget.Index - InstructionIndex;
}
SetStackDepth(pendingTarget.StackDepth);
if (_pendingValue != Interpreter.NoValue)
{
Data[StackIndex - 1] = _pendingValue;
}
// Set the _pendingContinuation and _pendingValue to the default values if we finally gets to the Goto target
_pendingContinuation = -1;
_pendingValue = Interpreter.NoValue;
return pendingTarget.Index - InstructionIndex;
}
internal void PushPendingContinuation()
{
Push(_pendingContinuation);
Push(_pendingValue);
_pendingContinuation = -1;
_pendingValue = Interpreter.NoValue;
}
internal void PopPendingContinuation()
{
_pendingValue = Pop();
_pendingContinuation = (int)Pop();
}
private static MethodInfo s_goto;
private static MethodInfo s_voidGoto;
internal static MethodInfo GotoMethod
{
get { return s_goto ??= typeof(InterpretedFrame).GetMethod("Goto"); }
}
internal static MethodInfo VoidGotoMethod
{
get { return s_voidGoto ??= typeof(InterpretedFrame).GetMethod("VoidGoto"); }
}
public int VoidGoto(int labelIndex)
{
return Goto(labelIndex, Interpreter.NoValue, gotoExceptionHandler: false);
}
public int Goto(int labelIndex, object value, bool gotoExceptionHandler)
{
// TODO: we know this at compile time (except for compiled loop):
RuntimeLabel target = Interpreter._labels[labelIndex];
Debug.Assert(!gotoExceptionHandler || _continuationIndex == target.ContinuationStackDepth,
"When it's time to jump to the exception handler, all previous finally blocks should already be processed");
if (_continuationIndex == target.ContinuationStackDepth)
{
SetStackDepth(target.StackDepth);
if (value != Interpreter.NoValue)
{
Data[StackIndex - 1] = value;
}
return target.Index - InstructionIndex;
}
// if we are in the middle of executing jump we forget the previous target and replace it by a new one:
_pendingContinuation = labelIndex;
_pendingValue = value;
return YieldToCurrentContinuation();
}
#endregion
}
}
|