lua-users home
lua-l archive

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


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