|
|
|
|
|
by haberman
3707 days ago
|
|
> Say you want to resume continuation K, which has a stack of some size N. The size of the continuation's stack doesn't matter for the problem I described, it's the size of the stack "underneath" your continuation that matters (ie. C(1) and C(2) above). If C(2) > C(1) there is no way to shrink C(2) such that the continuation's stack can be copied into the right place. > I like doing unacceptable things in my programs. It's the best part of programming, really. What you do in your programs is up to you! But nobody else is going to use a C library that messes with the execution state of its callers (unless that is the point of the library, which it isn't with Lua). |
|
To create a continuation, we need to copy the entire stack, by definition. But "the stack" is just an array of bytes. It's all the bytes between the current stack pointer and the "root" stack frame. So to create a continuation, copy these bytes and stash them somewhere, then set up a longjmp target to the current instruction.
To apply a continuation, i.e. to restore the stack, we overwrite the current stack starting from the root frame. Then we longjmp to where the continuation was originally created.
It seems like this scheme should work in any situation, but perhaps I'm missing something?
loeg pointed out getcontext(3) / setcontext(3), which seems promising. It looks like a standard way to sidestep all of this bookkeeping. It appears to be a high-level interface to the operations described above.
Lua is just a language, though. It's not "for" anything in particular.