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_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 );
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.
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?
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.
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?
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.