lua-users home
lua-l archive

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


Guy Davidson wrote:
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.

Right. The chunk is at the top of the stack. You will have to call it, so it will be able to register print_loop in the globals table.

// Load the code
luaL_loadfile(L, "test.lua");

// Execute the chunk
lua_call(L, 0, 0);

// You know what to do from now on :)
lua_pushstring(L, "print_loop");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_call(L, 0, 0);



The Lua version of this code would be:

> local chunk = loadfile("test.lua")
> print(type(chunk))
function: ...
> print(print_loop)
nil
> chunk()
> print(print_loop)
function: ...

Hope it helps

--rb