lua-users home
lua-l archive

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


Hello Jonathan, hello Duncan,

Thank you so much. That's it. Now all works as expected. I think I should read 
again the chapter about the C API and Lua stack ;-)

Anyway - thank you both for your fast response and helping to solve my 
problem. Now I can continue my work and learn more about the Lua language.

Best regards

Joachim


On Saturday 21 August 2010 22:51, Jonathan Castello wrote:
> The call to lua_getglobal pushes the global you wanted onto the stack,
> so the stack index -1 refers to the result, not the chunk (which would
> now probably be -2).
>
> It helps to add comments next to each lua_* line showing what the
> current status of the stack is, so you can always tell what you need
> to do. For example:
>
> ----
> /* L: chunk */
> lua_pushvalue(L, -1); /* L: chunk, chunk */
> lua_pcall( L, 0, 0, 0 ); /* L: chunk */
> lua_getglobal( L, "result" ); /* L: chunk, result */
> ---
>
> Simplest thing to do would be to add a lua_pop(L, 1) after you're done
> with each result, to remove it from the stack..
>
> ~Jonathan
>
> On Sat, Aug 21, 2010 at 1:44 PM, Joachim Buermann <jbuermann@iftools.com> 
wrote:
> > Hello Duncan,
> >
> > WOW!!! Thank you very much for your fast response. Unfortunatelly I did
> > forget some detail in my example. For it your hint works great, but the
> > little detail is:
> >
> > I need something like this in a loop or triggered by some user action:
> >
> > while(...) {
> >
> >        lua_pushvalue(L, -1);
> >
> >        lua_pcall( L, 0, 0, 0 );
> >
> >        lua_getglobal( L, "result" );
> >
> > }
> >
> > So the query of the global result is always execute after running the
> > chunk. In my example it was:
> >
> > ...
> > lua_pushvalue(L, -1);
> > lua_pcall( L, 0, 0, 0 );
> > lua_getglobal( L, "result" );
> > ...
> > lua_pushvalue(L, -1);
> > lua_pcall( L, 0, 0, 0 );
> > lua_getglobal( L, "result" );
> >
> > In this case the output is just again:
> >
> > l_counter()
> > 0
> >
> > Does the call lua_getglobal(L, "result") also some stack cleaning?
> >
> > Thanks again and best regards
> >
> > Joachim
> >
> > On Saturday 21 August 2010 22:28, Duncan Cross wrote:
> >> On Sat, Aug 21, 2010 at 9:17 PM, Joachim Buermann
> >> <jbuermann@iftools.com>
> >
> > wrote:
> >> >    if( luaL_loadstring( L, "result=counter()" ) != 0 ) {
> >> >
> >> >           std::cout << "Error" << std::endl;
> >> >
> >> >    }
> >> >
> >> >    lua_pcall( L, 0, 0, 0 );
> >> >
> >> >    lua_pcall( L, 0, 0, 0 );
> >> >
> >> >    lua_getglobal( L, "result" );
> >>
> >> As well as running the compiled chunk, lua_pcall() removes it from the
> >> stack, so the second call will not be trying to run the same compiled
> >> chunk.
> >>
> >> To counteract this, try making a copy of the compiled chunk value by
> >> using lua_pushvalue(), so the copy is removed instead:
> >>
> >>   lua_pushvalue(L, -1);
> >>   lua_pcall(L, 0, 0, 0);
> >>   lua_pushvalue(L, -1);
> >>   lua_pcall(L, 0, 0, 0);
> >>
> >> -Duncan