lua-users home
lua-l archive

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


Hi,

thank you for your comments. I think we may be misunderstanding each other:

Jamie Webb wrote:
> On Monday 31 May 2004 19:06, Mike Pall wrote:            
> > So this is no problem for C code, since lua_State is passed to every
> > call (and this just happens to be the thread handle). But I think there
> > is no way to push this handle to the stack, so you cannot operate on
> > it with lua functions (e.g. to store it in a table). I guess we need to add
> > lua_pushthread()?
> 
> You could save the Lua thread value in the registry when you first create it, 
> keyed on the state pointer.

This sentence does not make sense to me. The state pointer *IS* the thread
value, so the key would be the same as the value. What would that accomplish?

And upon retrieval you can only index a table (e.g. the registry) with a
key that is on the stack. But you cannot put 'L' on the stack -- there is
just no public API for that. There are lua_isthread() and lua_tothread(),
but these are only accessors.

Please look at these declarations:

lua_State *lua_newthread (lua_State *L);
=== new ===               == parent ==

typedef void (*lua_Hook) (lua_State *L, lua_Debug *ar);
                          == current ==

So when a hook is called you certainly get the thread object. And of
course you can do many useful things in C with it (like printf("%p\n", L)).
But you cannot do much with it when working with Lua objects (such as
tables).

> > I see no way to get at the current thread from within Lua code. I've
> > proposed coroutine.current() and an extension to debug.getinfo() to
> > allow for that. See my previous message on this list.
> 
> And given the above you could easily implement this yourself.

Ok, maybe I'm missing something obvious. So, please show me:

function hook(event, line)
  thread = *** place magic code here to get the current thread object ***
  print(event, line, thread)
end

debug.sethook(hook, "l")

for i=1,10 do 
  a=1
  b=2
end

Accomplishing this with coroutines is identical under the assumption that
we only have a single global hook (because storing the thread id in
a closure does not work anymore). And the problem is not limited to hooks.

Bye,
     Mike