Executing a Lua file
In Chapter 1, we used the Lua library to load a file and run the script. We will do the same here but in a more proper C++ way. In LuaExecutor.h
, add the following new code:
#include <string> class LuaExecutor { public: void executeFile(const std::string &path); private: void pcall(int nargs = 0, int nresults = 0); std::string popString(); };
You can of course make all those member functions const
, for example, std::string popString() const
, because in LuaExecutor
we only keep the Lua state L
transparently and do not change its value. Here, we are omitting it to prevent too many line breaks in code listings.
executeFile
is our public function and the other two are internal helper functions. In LuaExecutor.cc
, let us implement executeFile
first:
#include <iostream> void LuaExecutor::executeFile(const std::string &path) { if (luaL_loadfile...