-
Notifications
You must be signed in to change notification settings - Fork 0
Code execution methods
The LuaInstance class has 3 methods for executing code: DoFunction, DoFile and DoString. All 3 can return Lua values, which will be placed into the lua_return_values vector of the LuaInstance object used. These will remain accessable until another Lua code execution is performed, at which point the vector will be cleared to make room for the new return values.
All 3 methods use protected calls, meaning that errors will not result in the entire program crashing, but merely in the return of control to C++. A stack traceback system from the Lua standard library is also used, generating error messages that contain information about the state of the Lua code at the time of the error. These are printed to cerr, but the code can be modified to do something else with the error string.
The int value returned by all of these functions is the status code returned by Lua, which is one of a few constants defined in luainclude/lua.h.
Takes a string as input, which is the path to the file to be executed. The file name must include the .lua extension. The details of how the file path is used by the underlying Lua API is not explained in the Lua reference manual, and so this function may have some inconsistent behaviour across systems.
Takes a string containing Lua code to be executed. Lua syntax does not require that any number of newlines or semicolons are used, but they can sometimes be needed to resolve ambiguous statements as intended, as detailed by the Lua documentation.
Takes in a Lua function to execute alongside a vector of arguments to pass to the function. Arguments are passed in the order they are stored, so element 0 of the vector is the first argument, element 1 is the second and so on. Functions are stored in an opaque bytecode format rather than text, so Lua function literals cannot be passed as an argument.