Skip to content

Commit

Permalink
JSS-113 Reset the execution context stack when stack limit is reached
Browse files Browse the repository at this point in the history
This prevents the execution context stack from being always filled when
a stack limit exception is triggered.
  • Loading branch information
PrestonLTaylor committed Jun 10, 2024
1 parent 7f100a3 commit a27f848
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion JSS.Lib/Execution/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ internal void PushExecutionContext(ExecutionContext context)
// FIXME: We currently have to rely on limiting our stack size as we would throw a stack overflow exception which is uncatchable.
// We should use a bytecode VM instead of an AST so that memory instead of stack size is the limiting factor
const int MAXIMUM_STACK_DEPTH = 250;
if (_executionContextStack.Count >= MAXIMUM_STACK_DEPTH) throw new InvalidOperationException($"Maximum stack depth ({MAXIMUM_STACK_DEPTH}) reached inside of JSS.");
if (_executionContextStack.Count >= MAXIMUM_STACK_DEPTH)
{
// NOTE: When executing there should always be one execution context on the stack (Step 15 in ScriptEvaluation).
var initializedStackContext = _executionContextStack.ToArray()[_executionContextStack.Count - 1];
_executionContextStack.Clear();
_executionContextStack.Push(initializedStackContext);
throw new InvalidOperationException($"Maximum stack depth ({MAXIMUM_STACK_DEPTH}) reached inside of JSS.");
}

_executionContextStack.Push(context);
}
Expand Down

0 comments on commit a27f848

Please sign in to comment.