lua-users home
lua-l archive

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


This is awesome. Lua is awesome!

Ok, now that I've done my reading into all this that you told me, i got one question. When I install the hook, what lua_Hook func do i pass it?

Because from what i understand, it never hooks into one of my functions, but an internal lua function that then yields and returns from my lua_resume. Or am i missing something?


Mike Pall wrote:

Hi,

David Morris-Oliveros wrote:
It would be similar to the hooks in lua, but rather lua calling the hook every 5 instructions, i let it execute those 5 instructions, then it comes back to me.

That's inversion of control. Lua already supports that with the
help of coroutines. And lucky you, you can yield from a count hook
(or line hook).

So from C do a lua_newthread() and then lua_sethook() to install
your count hook. When the hook decides that the timeout is reached
it forces a yield for the coroutine with 'return lua_yield(L, 0)'.

Resuming (starting) the new coroutine will return with LUA_YIELD,
(at least in Lua 5.1) so you know it's not done yet. To continue,
just resume it again. You can of course have many different
coroutines at the same time or just recycle a few of them.

Alas, there is no (easy) way to differentiate a yield from a hook
with a yield from the Lua function itself. So you have to decide
for one feature or the other.

Note that the count hook slows things down. Also note that
coroutine.resume() and lua_resume() work subtly different.
Have a look at the manual first and then check lbaselib.c.

Oh, and BTW: do not mix coroutine.resume() (the Lua function)
with yielding from the line or count hook. It rips apart the
stack frame of the running Lua function. You really have to use
lua_resume() from C and carefully avoid touching the coroutine
stack.

Bye,
    Mike



--
// David Morris-Oliveros
// Camera & Lua Coder
// Team Bondi


------------------------------------------------------------------------
Contact:
Team Bondi Pty Ltd
Level 2, 608 Harris Street
Ultimo, NSW 2007
Australia
Tel: +61 (0)2 8218 1500
Fax: +61 (0)2 8218 1507
Web: http://www.teambondi.com
------------------------------------------------------------------------
This email may contain confidential information.  If you are not
the intended recipient, you may not copy or deliver this message to
anyone. In such case, you should destroy this message and kindly
notify the sender by reply email. Opinions, conclusions and other
information in this message that do not relate to the official business
of our firm shall be understood as neither given nor endorsed by it.
------------------------------------------------------------------------