Rakudo has need of exit handlers for subroutines. These are events, such as cleanup tasks, that should be executed whenever a control flow leaves a block.

Exit Events

Here are some events that could cause control flow to leave a block:

  • Invoking the return continuation
  • Invoking any other continuation
  • Throwing an exception
  • Calling another function, which either invokes a continuation or throws an exception, and jumps somewhere else
  • Tailcalling another function

Strategies

  • We could attach exit handlers to the context, and execute the handlers when the return continuation is invoked. This does not work well with continuations or exceptions which bypass the return continuation
  • We could wrap the return continuation in a Sub that executes the exit handlers and then calls the return continuation. Same as above, this does not work with continuations or exceptions that bypass the wrapped retcontinuation