lua-users home
lua-l archive

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


"Björklund, Peter" wrote:
> 
> How do I make latent functions in Lua?
> 
> When I am inside a c-function (called by Lua) I want to "pause" execution
> and resume that at a later time. The function that called lua in the first
> place should return (lua_dostring, lua_dofile etc)

For that you need coroutines.  Lua has no build in coroutines; they need
system dependant code.  But it has all to implement them.  I.e. with Sol's
coroutine lib you can write:

function copause()
    return Coroutine.main(Coroutine.current)
end

function costring(str)
    local docoro = function(str)
        while true do
            copause()
            dostring(str)
        end
    end

    return Coroutine.create(docoro)(str)
end

x = costring[[ print"a"  copause()  print"b" ]]
x() --> a
x() --> b
x() --> a (restarted)
x() --> b

Of course, you could call costring and copause from C code, too.

Ciao, ET.