New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Persist coroutines yielded from hook #30

wants to merge 1 commit into
base: master


None yet
1 participant
Copy link

evg-zhabotinsky commented Jan 11, 2019

Inspired by wanting preemptive multitasking in OpenComputers. See my other repo for explanation of changes to debug.sethook() that let Lua hooks yield. After posting to OC forum, it turned out that Eris cannot persist coroutines yielded from hooks (and even has a special error message for them), so here's my attempt to fix that.

The main problem seems to be that normal yields never happen from Lua code, there's no opcode for that. At the same time, hook yields look like such opcode was the last one, there's no enclosing C call. I assumed that this case should be treated the same way as a Lua stack frame, since it is one, but thread->ci->func no longer points to the closure and has to be restorestack()ed. I've additionally stored the value of restored ci->func for unpersisting, since restorestack() relies on ci->extra, which will only be unpersisted after the loop. This breaks serialization format compatibility, but is fine for testing, and a workaround can be implemented later. Actually, that only happens when persisting hook-yielded coroutines, so there's nothing to break.

This PR is a WIP, intended to be cleaned up and squashed when it's ready. The code currently seems to work, and I've added a simple test for it. However, I suspect more thorough testing is required, and I have no idea what kind of tests to make. I'm not very familiar with Lua and/or Eris internals. Need help producing more tests!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment