lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


It was thus said that the Great Marcus Mason once stated:
> Suppose you have a coroutine which is suspended at some point in its
> execution. Would it be possible via a C API trick or extension of the c API
> to duplicate it so that I have two independently runnable coroutines in the
> same state? Said coroutines are always pure.

  Define "pure".

  To duplicate a coroutine, at the very least, you'll need to duplicate the
code (easy enough---string.dump() and load() can do that), possible upvalues
(again, easy to get with debug.getupvalue() and debug.setupvalue())) and
maybe uservalues (debug.getuservalue() and debug.setuservalue()).  The hard
part will be obtaining the current stack for the coroutine as currently
there is no API for that, nor for creating the new call stack.  Also
difficult would be getting the current execution location (IP or PC if you
know assembly) of the coroutine being duplicated.

  Another complication are references to tables, userdata and coroutines
created by the suspended coroutine.  as they are just that---references and
not copies.  Do they need to be copied as well?  Or are references okay? 
Tables can be copied but that can get quite expensive, but you are out of
luck for userdata values as they represent state outside of Lua.

  -spc