Calling Lua from C++
To call Lua code from C++, we can use lua_pcall
, which is declared as follows:
int lua_pcall( lua_State *L, int nargs, int nresults, int msgh);
This will call a Lua callable, which can be a function or a chunk. You can push the Lua function to be called onto the stack, or compile a file or a string into a chunk and then place it onto the stack. nargs
is the number of arguments for the callable. The arguments are pushed onto the stack above the callable. nresults
is the count of return values the callable would return. Use LUA_MULTRET
to indicate that you expect a variable count of return values. msgh
is the stack index for an error message handler.
lua_pcall
calls the callable in protected mode, which means that any error that may have occurred in the call chain is not propagated. Instead, an error status code is returned from lua_pcall
.
In the LuaExecutor
class that we have implemented, you can find many examples of calling...