diff --git a/src/ScriptEngine/Machine/MachineInstance.cs b/src/ScriptEngine/Machine/MachineInstance.cs index 73c930e95..e61b6953a 100644 --- a/src/ScriptEngine/Machine/MachineInstance.cs +++ b/src/ScriptEngine/Machine/MachineInstance.cs @@ -32,7 +32,7 @@ public class MachineInstance private ExecutionFrame _currentFrame; private Action[] _commands; private Stack _exceptionsStack; - private LruCache _executeModuleCache = new LruCache(64); + private readonly LruCache _executeModuleCache = new LruCache(64); private StackRuntimeModule _module; private ICodeStatCollector _codeStatCollector; @@ -167,8 +167,7 @@ private void SetExecutionFrame(ExecutionFrame frame, MachineMethodInfo methodInf public void SetDebugMode(IBreakpointManager breakpointManager) { - if (_stopManager == null) - _stopManager = new MachineStopManager(this, breakpointManager); + _stopManager ??= new MachineStopManager(this, breakpointManager); } public void UnsetDebugMode() @@ -181,7 +180,7 @@ public void StepOver() if (_stopManager == null) throw new InvalidOperationException("Machine is not in debug mode"); - _stopManager.StepOver(_currentFrame); + _stopManager.StepOver(); } public void StepIn() @@ -197,7 +196,7 @@ public void StepOut() if (_stopManager == null) throw new InvalidOperationException("Machine is not in debug mode"); - _stopManager.StepOut(_currentFrame); + _stopManager.StepOut(); } public IValue Evaluate(string expression) @@ -419,12 +418,10 @@ private bool ShouldRethrowException(ScriptException exc) return true; } - var callStackFrames = exc.RuntimeSpecificInfo as IList; - - if (callStackFrames == null) + if (!(exc.RuntimeSpecificInfo is IList)) { CreateFullCallstack(); - callStackFrames = new List(_fullCallstackCache); + IList callStackFrames = new List(_fullCallstackCache); exc.RuntimeSpecificInfo = callStackFrames; } @@ -468,16 +465,6 @@ private void CodeStat_LineReached() _codeStatCollector.MarkEntryReached(CurrentCodeEntry()); } - private void CodeStat_StopFrameStatistics() - { - _codeStatCollector?.StopWatch(CurrentCodeEntry()); - } - - private void CodeStat_ResumeFrameStatistics() - { - _codeStatCollector?.ResumeWatch(CurrentCodeEntry()); - } - private void MainCommandLoop() { try @@ -509,8 +496,11 @@ private void MainCommandLoop() private ErrorPositionInfo GetPositionInfo() { - var epi = new ErrorPositionInfo(); - epi.LineNumber = _currentFrame.LineNumber; + var epi = new ErrorPositionInfo + { + LineNumber = _currentFrame.LineNumber + }; + if (_module.Source != null && epi.LineNumber > 0) { epi.ModuleName = _module.Source.Name; @@ -988,10 +978,7 @@ private void ResolveProp(int arg) private void ResolveMethodProc(int arg) { - IRuntimeContextInstance context; - int methodId; - IValue[] argValues; - PrepareContextCallArguments(arg, out context, out methodId, out argValues); + PrepareContextCallArguments(arg, out IRuntimeContextInstance context, out int methodId, out IValue[] argValues); context.CallAsProcedure(methodId, argValues); NextInstruction(); @@ -999,18 +986,14 @@ private void ResolveMethodProc(int arg) private void ResolveMethodFunc(int arg) { - IRuntimeContextInstance context; - int methodId; - IValue[] argValues; - PrepareContextCallArguments(arg, out context, out methodId, out argValues); + PrepareContextCallArguments(arg, out IRuntimeContextInstance context, out int methodId, out IValue[] argValues); if (!context.DynamicMethodSignatures && context.GetMethodInfo(methodId).ReturnType == typeof(void)) { throw RuntimeException.UseProcAsAFunction(); } - IValue retVal; - context.CallAsFunction(methodId, argValues, out retVal); + context.CallAsFunction(methodId, argValues, out IValue retVal); _operationStack.Push(retVal); NextInstruction(); } @@ -1148,7 +1131,7 @@ private void JmpCounter(int arg) private void Inc(int arg) { var operand = _operationStack.Pop().AsNumber(); - operand = operand + 1; + operand++; _operationStack.Push(ValueFactory.Create(operand)); NextInstruction(); } @@ -1207,12 +1190,7 @@ private void PushIterator(int arg) private void IteratorNext(int arg) { - var iterator = _currentFrame.LocalFrameStack.Peek() as CollectionEnumerator; - if (iterator == null) - { - throw new WrongStackConditionException(); - } - + var iterator = _currentFrame.LocalFrameStack.Peek() as CollectionEnumerator ?? throw new WrongStackConditionException(); var hasNext = iterator.MoveNext(); if (hasNext) { @@ -1224,22 +1202,19 @@ private void IteratorNext(int arg) private void StopIterator(int arg) { - var iterator = _currentFrame.LocalFrameStack.Pop() as CollectionEnumerator; - if (iterator == null) - { - throw new WrongStackConditionException(); - } - + var iterator = _currentFrame.LocalFrameStack.Pop() as CollectionEnumerator ?? throw new WrongStackConditionException(); iterator.Dispose(); NextInstruction(); } private void BeginTry(int exceptBlockAddress) { - var info = new ExceptionJumpInfo(); - info.HandlerAddress = exceptBlockAddress; - info.HandlerFrame = _currentFrame; - info.StackSize = _operationStack.Count; + var info = new ExceptionJumpInfo + { + HandlerAddress = exceptBlockAddress, + HandlerFrame = _currentFrame, + StackSize = _operationStack.Count + }; _exceptionsStack.Push(info); NextInstruction(); @@ -1548,9 +1523,9 @@ private void TrimL(int arg) for (int i = 0; i < str.Length; i++) { - if(!Char.IsWhiteSpace(str[i])) + if(!char.IsWhiteSpace(str[i])) { - var trimmed = str.Substring(i); + var trimmed = str[i..]; _operationStack.Push(ValueFactory.Create(trimmed)); NextInstruction(); return; @@ -1570,7 +1545,7 @@ private void TrimR(int arg) { if (!Char.IsWhiteSpace(str[i])) { - var trimmed = str.Substring(0, i+1); + var trimmed = str[..(i + 1)]; _operationStack.Push(ValueFactory.Create(trimmed)); NextInstruction(); return; @@ -1602,7 +1577,7 @@ private void Left(int arg) return; } - _operationStack.Push(ValueFactory.Create(str.Substring(0, len))); + _operationStack.Push(ValueFactory.Create(str[..len])); NextInstruction(); } @@ -1881,7 +1856,7 @@ private void Second(int arg) NextInstruction(); } - private DateTime DropTimeFraction(in DateTime date) + private static DateTime DropTimeFraction(in DateTime date) { return new DateTime(date.Year, date.Month, date.Day); } @@ -1945,7 +1920,7 @@ private void BegOfQuarter(int arg) { //1,4,7,10 var date = _operationStack.Pop().AsDate(); - var month = date.Month; + int quarterMonth; if (date.Month >= 1 && date.Month <= 3) { @@ -2011,7 +1986,7 @@ private void EndOfQuarter(int arg) { //1,4,7,10 var date = _operationStack.Pop().AsDate(); - var month = date.Month; + int quarterMonth; if (date.Month >= 1 && date.Month <= 3) { @@ -2247,7 +2222,7 @@ private void Pow(int arg) NextInstruction(); } - private decimal PowInt(decimal bas, uint exp) + private static decimal PowInt(decimal bas, uint exp) { decimal pow = 1; @@ -2369,14 +2344,14 @@ private void NewFunc(int argCount) IValue[] argValues; if (argCount == 0) - argValues = new IValue[0]; + argValues = Array.Empty(); else { var valueFromStack = _operationStack.Pop().GetRawValue(); if (valueFromStack is IValueArray array) argValues = array.ToArray(); else - argValues = new IValue[0]; + argValues = Array.Empty(); } var typeName = _operationStack.Pop().AsString(); @@ -2488,24 +2463,22 @@ public IList GetExecutionFrames() public IList GetFrameLocals(int frameId) { - System.Diagnostics.Debug.Assert(_fullCallstackCache != null); + Debug.Assert(_fullCallstackCache != null); if (frameId < 0 || frameId >= _fullCallstackCache.Count) - return new IVariable[0]; + return Array.Empty(); var frame = _fullCallstackCache[frameId]; return frame.FrameObject.Locals; } - private ExecutionFrameInfo FrameInfo(StackRuntimeModule module, ExecutionFrame frame) - { - return new ExecutionFrameInfo() + private static ExecutionFrameInfo FrameInfo(StackRuntimeModule module, ExecutionFrame frame) + => new ExecutionFrameInfo() { LineNumber = frame.LineNumber, MethodName = frame.MethodName, Source = module.Source.Location, FrameObject = frame }; - } // multithreaded instance [ThreadStatic] @@ -2518,8 +2491,7 @@ public static MachineInstance Current { get { - if(_currentThreadWorker == null) - _currentThreadWorker = new MachineInstance(); + _currentThreadWorker ??= new MachineInstance(); return _currentThreadWorker; } diff --git a/src/ScriptEngine/Machine/MachineStopManager.cs b/src/ScriptEngine/Machine/MachineStopManager.cs index 55adf791b..9ed0c7883 100644 --- a/src/ScriptEngine/Machine/MachineStopManager.cs +++ b/src/ScriptEngine/Machine/MachineStopManager.cs @@ -31,8 +31,6 @@ private struct StopPoint private readonly IBreakpointManager _breakpoints; private readonly MachineInstance _machine; private ExecutionFrame[] _stopFrames; - - private StopPoint _lastStopPoint; public MachineStopManager(MachineInstance runner, IBreakpointManager breakpoints) { @@ -113,7 +111,7 @@ private bool FrameIsInStopList(ExecutionFrame currentFrame) return _stopFrames != null && _stopFrames.Contains(currentFrame); } - public void StepOver(ExecutionFrame currentFrame) + public void StepOver() { _currentState = DebugState.SteppingOver; _stopFrames = _machine.GetExecutionFrames().Select(x => x.FrameObject).ToArray(); @@ -125,7 +123,7 @@ public void StepIn() _currentState = DebugState.SteppingIn; } - internal void StepOut(ExecutionFrame currentFrame) + internal void StepOut() { _currentState = DebugState.SteppingOut; _stopFrames = _machine.GetExecutionFrames().Select(x => x.FrameObject).Skip(1).ToArray();