-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Propagate exceptions from parallel where possible (#2095)
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.
- Loading branch information
Showing
7 changed files
with
201 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...e/src/main/resources/data/computercraft/lua/rom/modules/main/cc/internal/tiny_require.lua
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
-- SPDX-FileCopyrightText: 2025 The CC: Tweaked Developers | ||
-- | ||
-- SPDX-License-Identifier: MPL-2.0 | ||
|
||
--[[- A minimal implementation of require. | ||
This is intended for use with APIs, and other internal code which is not run in | ||
the [`shell`] environment. This allows us to avoid some of the overhead of | ||
loading the full [`cc.require`] module. | ||
> [!DANGER] | ||
> This is an internal module and SHOULD NOT be used in your own code. It may | ||
> be removed or changed at any time. | ||
@local | ||
@tparam string name The module to require. | ||
@return The required module. | ||
]] | ||
|
||
local loaded = {} | ||
local env = setmetatable({}, { __index = _G }) | ||
local function require(name) | ||
local result = loaded[name] | ||
if result then return result end | ||
|
||
local path = "rom/modules/main/" .. name:gsub("%.", "/") | ||
if fs.exists(path .. ".lua") then | ||
result = assert(loadfile(path .. ".lua", nil, env))() | ||
else | ||
result = assert(loadfile(path .. "/init.lua", nil, env))() | ||
end | ||
loaded[name] = result | ||
return result | ||
end | ||
env.require = require | ||
return require |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters