Lua 5.2 function call from C ++

I am very new to Lua. I was looking for sample code to call a Lua function from C ++, but the sample code uses 5.1, and I'm trying to get this to work with 5.2.

Here is an example of the code in question with my comments:

lua_State *luaState = luaL_newstate();
luaopen_io(luaState);
luaL_loadfile(luaState, "myLuaScript.lua");
lua_pcall(luaState, 0, LUA_MULTRET, 0);
//the code below needs to be rewritten i suppose
lua_pushstring(luaState, "myLuaFunction");
//the line of code below does not work in 5.2
lua_gettable(luaState, LUA_GLOBALSINDEX);
lua_pcall(luaState, 0, 0, 0);

I read in the manuel 5.2 reference ( http://www.lua.org/manual/5.2/manual.html#8.3 ) that you need to get the global environment from the registry (instead of the lua_gettable expression above), but I canโ€™t decide what changes I need to do to make this work. I tried for example:

lua_pushglobaltable(luaState);
lua_pushstring(luaState, "myLuaFunction");
lua_gettable(luaState, -2);
lua_pcall(luaState, 0, 0, 0);
+5
source share
1 answer

The code below should work in both 5.1 and 5.2.

lua_getglobal(luaState, "myLuaFunction");
lua_pcall(luaState, 0, 0, 0);

luaL_loadfile lua_pcall. , luaL_dofile.

+3

All Articles