lua-users home
lua-l archive

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


Benoit Germain wrote:
> 
> > I don't think this functionality is readily available in Lua
> > out of the box.
> 
> I fear that also.

Fear not! "I'll save ya!" ^_~
A simple lua wrapper will probably allow you to
do what you want to do. Here goes:

chocoro = {} -- Cho is japanese for Ultra ;)

function chocoro.create(fun)
local result
   result = {}
   result.act = coroutine.create(fun)
   result.fun = fun
return result
end

function chocoro.resume(coro, ...)
return coroutine.resume(coro.act, unpack(arg))
end

function chocoro.yield(...)
return coroutine.yield(unpack(arg))
end

function chocoro.reset(coro)
coro.act = coroutine.create(coro.fun); 
end

function AAA() 
local i
i = 1
while(true) do 
   print("AAA Step ", i); i = i + 1; chocoro.yield();
end
end

coro1 = chocoro.create(AAA)
chocoro.resume(coro1) --AAA Step 1
chocoro.resume(coro1) --AAA Step 2
chocoro.reset(coro1)  -- Reset the coroutine.
chocoro.resume(coro1) --AAA Step 1 
chocoro.resume(coro1) --AAA Step 2

Note that I'm using coroutine.xxx syntax. 
How to write a chocoro.wrap(), aswel as how
to do some sanity-checking is left as an exercice 
for the reader. ^_^


-- 
"No one knows true heroes, for they speak not of their greatness." -- 
Daniel Remar.
Björn De Meyer 
bjorn.demeyer@pandora.be