Executing a Lua script
In some projects, you may have a file layer abstraction, or you may get a Lua script from a remote server. Then, you cannot pass a file path to the Lua library and ask it to load it for you. You may also want to load the file yourself as a string to do more auditing before executing it. In those situations, you can ask the Lua library to execute a string as a Lua script.
To do this, we will add a new capability to our Lua executor. In LuaExecutor.h
, add one more function:
class LuaExecutor { public: void execute(const std::string &script); };
This new function will accept the Lua code in a string directly and execute it.
In LuaExecutor.cc
, add this implementation:
void LuaExecutor::execute(const std::string &script) { if (luaL_loadstring(L, script.c_str())) { std::cerr << "Failed to prepare script: " &...