Skip to content

Commit

Permalink
Allow setting the default crash handler in the constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
vddCore committed May 26, 2024
1 parent 2431bfe commit 4f99b83
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 18 deletions.
8 changes: 3 additions & 5 deletions FrontEnd/EVIL.evil/EvmFrontEnd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ namespace EVIL.evil
{
public partial class EvmFrontEnd
{
private static CeresVM _vm = new();
private static CeresVM _vm = new(CrashHandler);
private static Compiler _compiler = new();
private static EvilRuntime _runtime = new(_vm);
private static RuntimeModuleLoader _runtimeModuleLoader = new(_runtime);
Expand Down Expand Up @@ -190,8 +190,6 @@ public async Task Run(string[] args)
Terminate(msg);
}

_vm.Scheduler.SetDefaultCrashHandler(CrashHandler);
_vm.MainFiber.SetCrashHandler(CrashHandler);
_vm.Start();

_vm.MainFiber.Schedule(rootChunk, scriptArgs);
Expand Down Expand Up @@ -315,7 +313,7 @@ private void SetStandardGlobals(string scriptPath)
}
}

private void CrashHandler(Fiber fiber, Exception exception)
private static void CrashHandler(Fiber fiber, Exception exception)
{
var fiberArray = _vm.Scheduler.Fibers.ToArray();
var fiberIndex = Array.IndexOf(fiberArray, fiber);
Expand All @@ -334,7 +332,7 @@ private void CrashHandler(Fiber fiber, Exception exception)
TerminateWithError(fiberIndex, callStack, exception);
}

private void TerminateWithError(int fiberIndex, StackFrame[] callStack, Exception exception)
private static void TerminateWithError(int fiberIndex, StackFrame[] callStack, Exception exception)
{
var top = callStack[0];
ScriptStackFrame? scriptTop = null;
Expand Down
9 changes: 5 additions & 4 deletions VirtualMachine/EVIL.Ceres/ExecutionEngine/CeresVM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Threading.Tasks;
using EVIL.Ceres.ExecutionEngine.Collections;
using EVIL.Ceres.ExecutionEngine.Concurrency;
using EVIL.Ceres.ExecutionEngine.Diagnostics.Debugging;

namespace EVIL.Ceres.ExecutionEngine
{
Expand All @@ -13,15 +14,15 @@ public class CeresVM : IDisposable
public FiberScheduler Scheduler { get; }
public Fiber MainFiber { get; }

public CeresVM()
: this(new Table())
public CeresVM(FiberCrashHandler? crashHandler = null)
: this(new Table(), crashHandler)
{
}

public CeresVM(Table global)
public CeresVM(Table global, FiberCrashHandler? crashHandler = null)
{
Global = global;
Scheduler = new FiberScheduler(this, DefaultCrashHandler);
Scheduler = new FiberScheduler(this, crashHandler ?? DefaultCrashHandler);
MainFiber = Scheduler.CreateFiber(true);
}

Expand Down
10 changes: 9 additions & 1 deletion VirtualMachine/EVIL.Ceres/ExecutionEngine/Concurrency/Fiber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public sealed class Fiber

private ExecutionUnit _executionUnit;
private FiberCrashHandler? _crashHandler;
private FiberState _state;
internal FiberState _state;

internal ChunkInvokeHandler? OnChunkInvoke { get; private set; }
internal NativeFunctionInvokeHandler? OnNativeFunctionInvoke { get; private set; }
Expand Down Expand Up @@ -81,6 +81,14 @@ internal Fiber(CeresVM virtualMachine, Dictionary<string, ClosureContext>? closu
_state = FiberState.Fresh;
}

internal Fiber(
CeresVM virtualMachine,
FiberCrashHandler fiberCrashHandler,
Dictionary<string, ClosureContext>? closureContexts = null) : this(virtualMachine, closureContexts)
{
SetCrashHandler(fiberCrashHandler);
}

public void Schedule(Chunk chunk, params DynamicValue[] args)
=> Schedule(chunk, true, args);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,22 @@ public void Run()
{
var fiber = _fibers[i];

if (fiber.State == FiberState.Awaiting)
if (fiber._state == FiberState.Awaiting)
{
fiber.RemoveFinishedAwaitees();
fiber.Resume();
}
else if (fiber.State == FiberState.Fresh)
else if (fiber._state == FiberState.Fresh)
{
fiber.Resume();
}
else if (fiber.State == FiberState.Paused)
else if (fiber._state == FiberState.Paused)
{
continue;
}
else
{
if (fiber.State == FiberState.Finished || fiber.State == FiberState.Crashed)
if (fiber._state == FiberState.Finished || fiber._state == FiberState.Crashed)
{
if (!fiber.ImmuneToCollection)
{
Expand All @@ -67,7 +67,7 @@ public void Run()
fiber.Resume();
}

if (fiber.State != FiberState.Running)
if (fiber._state != FiberState.Running)
{
continue;
}
Expand Down Expand Up @@ -99,8 +99,11 @@ public Fiber CreateFiber(
FiberCrashHandler? crashHandler = null,
Dictionary<string, ClosureContext>? closureContexts = null)
{
var fiber = new Fiber(_vm, closureContexts);
fiber.SetCrashHandler(crashHandler ?? _defaultCrashHandler);
var fiber = new Fiber(
_vm,
crashHandler ?? _defaultCrashHandler,
closureContexts
);

if (immunized)
{
Expand All @@ -119,7 +122,7 @@ public void RemoveCrashedFibers()
{
for (var i = 0; i < _fibers.Count; i++)
{
if (_fibers[i].State == FiberState.Crashed)
if (_fibers[i]._state == FiberState.Crashed)
{
_dueForRemoval.Add(i);
}
Expand Down

0 comments on commit 4f99b83

Please sign in to comment.