#ifndef LUA_VM_H
#define LUA_VM_H
struct lua_State;
// Details needed for CallLua impl
#include "lua-5.3.2/src/lua.h"
#include "lua-5.3.2/src/lauxlib.h"
#include <vector>
class LuaVM
{
public:
LuaVM();
~LuaVM();
void LoadFile(const char* file);
// Unfortunately because this is a template some of the implementation is bleeding out in to the header/interface
template <typename... Ts>
void CallLua(const char *func, const Ts... values) {
lua_getglobal(L, func); /* get function */
int argc = 0;
int retc = 0; /* number of expected results */
PushArgs(argc, values...);
if (lua_pcall(L, argc, retc, 0) != 0) /* do the call */
printf("error running function `%s': %s", func, lua_tostring(L, -1));
/* TODO: retrieve results */
}
void BindCFunction(const char* name, lua_CFunction fn) {
lua_pushcclosure(L, fn, 0);
lua_setglobal(L, name);
}
private:
lua_State* L;
// Helpers
void Push(void);
void Push(bool b);
void Push(const char *str);
void Push(std::vector<char> buf);
void Push(void *data);
void Push(double value);
void Push(int value);
void Push(lua_CFunction fn);
void PushArgs(int &argc);
template <typename T, typename... Ts>
void PushArgs(int &argc, const T value, const Ts... values) {
argc++;
Push(value);
PushArgs(argc, values...);
}
};
#endif // LUA_VM_H