Skip to content

Commit

Permalink
Небольшой рефакторинг
Browse files Browse the repository at this point in the history
  • Loading branch information
akpaevj committed Aug 7, 2024
1 parent ea1bbdc commit f60c8fe
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 70 deletions.
104 changes: 38 additions & 66 deletions src/ScriptEngine/Machine/MachineInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public class MachineInstance
private ExecutionFrame _currentFrame;
private Action<int>[] _commands;
private Stack<ExceptionJumpInfo> _exceptionsStack;
private LruCache<string, StackRuntimeModule> _executeModuleCache = new LruCache<string, StackRuntimeModule>(64);
private readonly LruCache<string, StackRuntimeModule> _executeModuleCache = new LruCache<string, StackRuntimeModule>(64);

private StackRuntimeModule _module;
private ICodeStatCollector _codeStatCollector;
Expand Down Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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)
Expand Down Expand Up @@ -419,12 +418,10 @@ private bool ShouldRethrowException(ScriptException exc)
return true;
}

var callStackFrames = exc.RuntimeSpecificInfo as IList<ExecutionFrameInfo>;

if (callStackFrames == null)
if (!(exc.RuntimeSpecificInfo is IList<ExecutionFrameInfo>))
{
CreateFullCallstack();
callStackFrames = new List<ExecutionFrameInfo>(_fullCallstackCache);
IList<ExecutionFrameInfo> callStackFrames = new List<ExecutionFrameInfo>(_fullCallstackCache);
exc.RuntimeSpecificInfo = callStackFrames;
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -988,29 +978,22 @@ 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();
}

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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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)
{
Expand All @@ -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();
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}

Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -2369,14 +2344,14 @@ private void NewFunc(int argCount)
IValue[] argValues;

if (argCount == 0)
argValues = new IValue[0];
argValues = Array.Empty<IValue>();
else
{
var valueFromStack = _operationStack.Pop().GetRawValue();
if (valueFromStack is IValueArray array)
argValues = array.ToArray();
else
argValues = new IValue[0];
argValues = Array.Empty<IValue>();
}

var typeName = _operationStack.Pop().AsString();
Expand Down Expand Up @@ -2488,24 +2463,22 @@ public IList<ExecutionFrameInfo> GetExecutionFrames()

public IList<IVariable> 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<IVariable>();

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]
Expand All @@ -2518,8 +2491,7 @@ public static MachineInstance Current
{
get
{
if(_currentThreadWorker == null)
_currentThreadWorker = new MachineInstance();
_currentThreadWorker ??= new MachineInstance();

return _currentThreadWorker;
}
Expand Down
6 changes: 2 additions & 4 deletions src/ScriptEngine/Machine/MachineStopManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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();
Expand All @@ -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();
Expand Down

0 comments on commit f60c8fe

Please sign in to comment.