Jump to content
  • Advertisement
Sign in to follow this  
hick18

Lua, code stack

This topic is 2689 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

According to the manual, when you call luaL_loadbuffer() it pushes onto the stack the compiled code, and then calling lua_pcall or lua_call runs that compiled code and pops it off the stack. Doesnt that mean that after calling lua_call or lua_pcall, any global variables from script are now destroyed, and that any functions should be inaccessable?

Yet Im still able to get globals after lua_pcall and lua_call.


lua_State* pState = luaL_newstate();
const char* pScript = " function MyFunc() end";
int error = luaL_loadbuffer( pState, pScript, strlen(pScript), pScript );
lua_pcall( pState, 0, 0, 0 );

lua_getglobal( pState, "MyFunc" ); // <-- why is this still valid? shouldn't the code have been popped off the stack with the last lua_pcall?
error = lua_pcall( pState, 0, 0, 0 );

Share this post


Link to post
Share on other sites
Advertisement

According to the manual, when you call luaL_loadbuffer() it pushes onto the stack the compiled code, and then calling lua_pcall or lua_call runs that compiled code and pops it off the stack. Doesnt that mean that after calling lua_call or lua_pcall, any global variables from script are now destroyed, and that any functions should be inaccessable?

No. Why would it? The whole point of global variables is that they aren't local to a function.

Share this post


Link to post
Share on other sites
I understood it as the chunk of code compiled and pushed on the stack by loadbuffer, is then run and popped off the stack by lua_call. Is this not correct?

Share this post


Link to post
Share on other sites

I understood it as the chunk of code compiled and pushed on the stack by loadbuffer, is then run and popped off the stack by lua_call. Is this not correct?

That is indeed what happens when you execute that code. The fact that a function is removed from the stack, though, has nothing to do with entries being removed from the globals table.

Share this post


Link to post
Share on other sites
I wasnt talking about a function though. I was talking about a chunk of code. The way its worded sounded to me like everying in the script loaded using loadbuffer (they call a code chunk) gets unloaded when its popped off the stack when lua_Call is used. Where that chunk of code would have included global variables defined in that script.

But apparently thats not the case.

So although its popped off the stack, the globals and functions defined in that code chunk still exist in the global table until the lua_state is closed?

Share this post


Link to post
Share on other sites

I wasnt talking about a function though. I was talking about a chunk of code.

A chunk of code IS a function (at least, once compiled).

The way its worded sounded to me like everying in the script loaded using loadbuffer (they call a code chunk) gets unloaded when its popped off the stack when lua_Call is used. Where that chunk of code would have included global variables defined in that script.[/quote]
Global variables aren't owned by anything except the global table. (If that weren't the case, they would be local variables.)

So although its popped off the stack, the globals and functions defined in that code chunk still exist in the global table until the lua_state is closed?[/quote]
Or until you manually remove them, yeah.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!