Add resume_error to Thread for luau #513
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This adds support for the Luau specific
lua_resumeerror
to mlua dev branch as Thread::resume_error which allows directly resuming a thread with a bubbled up error which is at the top of the threads stack. This can be useful for many situations including resuming coroutine yields but as a bubbled up error with working pcall etc.This C API is increasingly used by luau runtimes for error handling in threads which is how I found out about it:
Hence why I made a pr for mlua to add it @khvzak
The API in question takes in a single argument of type IntoLua which is pushed to top of thread stack. Resume error is called and gettop is used to get the results and then IntoLuaMulti is used to convert to desired value.
For sample usage of this API:
Without lua_resumeerror, doing the above requires monkeypatching coroutine.yield to get errors to correctly bubble which then breaks pcall. With lua_resumeerror, the above just works with fully working error propagation and pcall fully working as desired (and is also more performant).
Also, merging this PR would solve the original issue of #500 regarding erroring yielded threads. It turns out
lua_resumeerror
is the Luau solution for this.