lua-users home
lua-l archive

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


Alex Bilyk escribió:

> Hey all,

> Correct me if I'm wrong, but one can not yeild from a hook
> (say a "line" or a "count") function in Lua 5? I wonder if
> the script below somehow violates expectations for 'yield'
> usage. I thought a while ago we were told Lua 5 release
> version will have this capability.

Here is my understanding:

You can yield from a line or count hook. However, you cannot yield from a
metamethod (except __call metamethods) or a C function. Consequently, if
you set a line or count hook in a Lua function which is called as a
callback from a C function, or is used as a metamethod, the yield will
fail. [see note below]

I think this renders the use of yield in line and count hooks impossible,
or at least incompatible with a number of standard features in Lua
(table.foreach and generators in general; string.gsub and friends;
metamethods) because sooner or later the yield will happen inside a
metamethod or callback.

In any event, I don't believe that you can do a yield from a hook written
in Lua; I believe you have to write the hook function in C for it to work.
I think that is because the Lua hook function is actually a reentry into
Lua.

As far as I can see, a call or return (C) hook function can try to yield if
it wants to; I don't believe it will generate an error but the yield won't
happen either.

I am curious as to whether anyone is seriously contemplating using yielding
hook functions (and if so, for what) because I have some (fairly technical)
ideas about how to implement these features in an alternative manner which
might be slightly more general. It seems to me that the main reason for
yielding count/line hooks is to try to implement non-cooperative
concurrency; a debugger would probably not require yielding hooks (although
it is possible to imagine a debugger which attempted to work in this
fashion). The problem with implementing concurrency with yielding hooks --
aside from any technical problem with the implementation of yield in a hook
function -- is that it would be incompatible with the actual use of
coroutines in the concurrent processes. But perhaps no-one thinks this is a
problem.

[note] I speculate that you could use call and return hooks to attempt to
suspend line or count hooks inside a callback; you would have to turn the
line/count hook off inside the call hook (and turn it back on inside the
return hook) if the function being called were a C-function. I don't think
this would capture metamethod calls, though.

I have some (fairly technical) ideas about how to implement some of these
features, if anyone cares.