mirror of
https://github.com/SquidDev-CC/CC-Tweaked
synced 2025-04-25 12:13:18 +00:00

In the original implementation of our prettier runtime errors (#1320), we wrapped the errors thrown within parallel functions into an exception object. This means the call-stack is available to the catching-code, and so is able to report a pretty exception message. Unfortunately, this was a breaking change, and so we had to roll that back. Some people were pcalling the parallel function, and matching on the result of the error. This is a second attempt at this, using a technique I've affectionately dubbed "magic throws". The parallel API is now aware of whether it is being pcalled or not, and thus able to decide whether to wrap the error into an exception or not: - Add a new `cc.internal.tiny_require` module. This is a tiny reimplementation of require, for use in our global APIs. - Add a new (global, in the debug registry) `cc_try_barrier` function. This acts as a marker function, and is used to store additional information about the current coroutine. Currently this stores the parent coroutine (used to walk the full call stack) and a cache of whether any `pcall`-like function is on the stack. Both `parallel` and `cc.internal.exception.try` add this function to the root of the call stack. - When an error occurs within `parallel`, we walk up the call stack, using `cc_try_barrier` to traverse up the parent coroutine's stack too. If we do not find any `pcall`-like functions, then we know the error is never intercepted by user code, and so its safe to throw a full exception.