lua-users home
lua-l archive

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

I'm having trouble invoking a loaded chunk.

Here's a simple piece of Lua:

function print_loop()
  for i=1,10 do

This is kept in a text file called test.lua.

My client C++ code calls luaL_loadfile with this filename.  My stack diagnostics tell me a Lua function is on the top of the stack now.

Then it calls

lua_pushstring(lua, "print_loop");

(where lua is the lua_State*) which pushes "print_loop", the function name declared in the chunk, on the stack.  I then call

lua_gettable(lua, LUA_GLOBALSINDEX);

which exchanges the string "print_loop" for a function.  However, NIL is appearing at the top of the stack instead.  Now, my decision to call lua_gettable with LUA_GLOBALSINDEX is informed by the sample code in section 3.14 of the documentation.  I am assuming that, when the chunk is loaded, all its declared objects (functions and variables) are added to the LUA_GLOBALSINDEX table, using the identifier as the key.  I'm having trouble finding similar examples from the library code.

Any clues where I'm going wrong here?

While I'm waiting I'll write table diagnostics...

Guy Davidson