22 const std::string META_PREFIX =
"META_";
24 int panicCB(lua_State *L) {
25 ai_log_error(
"Lua panic. Error message: %s", (lua_isnil(L, -1) ?
"" : lua_tostring(L, -1)));
35 LUAType(lua_State* state,
const std::string& name) :
37 const std::string metaTable = META_PREFIX + name;
38 luaL_newmetatable(_state, metaTable.c_str());
39 lua_pushvalue(_state, -1);
40 lua_setfield(_state, -2,
"__index");
45 void addFunction(
const std::string& name, FUNC&& func) {
46 lua_pushcfunction(_state, func);
47 lua_setfield(_state, -2, name.c_str());
50 void addValue(
const char* name,
const char* value) {
51 lua_pushstring(_state, value);
52 lua_setfield(_state, -2, name);
72 explicit LUA(lua_State *state) :
73 _state(state), _destroy(
false) {
76 explicit LUA(
bool debug =
false) :
77 _state(luaL_newstate()), _destroy(
true) {
78 luaL_openlibs(_state);
79 lua_atpanic(_state, panicCB);
80 lua_gc(_state, LUA_GCSTOP, 0);
86 lua_gc(_state, LUA_GCRESTART, 0);
92 inline lua_State* getState ()
const {
97 inline T* newGlobalData (
const std::string& prefix, T *userData)
const {
98 lua_pushlightuserdata(_state, userData);
99 lua_setglobal(_state, prefix.c_str());
104 static T* getGlobalData(lua_State *L,
const std::string& prefix) {
105 lua_getglobal(L, prefix.c_str());
106 T* data = (T*) lua_touserdata(L, -1);
112 static T* newUserdata(lua_State *L,
const std::string& prefix, T* data) {
113 T ** udata = (T **) lua_newuserdata(L,
sizeof(T *));
114 const std::string name = META_PREFIX + prefix;
115 luaL_getmetatable(L, name.c_str());
116 lua_setmetatable(L, -2);
122 static T* getUserData (lua_State *L,
int n,
const std::string& prefix) {
123 const std::string name = META_PREFIX + prefix;
124 return *(T **) luaL_checkudata(L, n, name.c_str());
127 static int returnError (lua_State *L,
const std::string& error) {
129 return luaL_error(L,
"%s", error.c_str());
132 void reg (
const std::string& prefix, luaL_Reg* funcs) {
133 const std::string metaTableName = META_PREFIX + prefix;
134 luaL_newmetatable(_state, metaTableName.c_str());
135 luaL_setfuncs(_state, funcs, 0);
136 lua_pushvalue(_state, -1);
137 lua_setfield(_state, -1,
"__index");
138 lua_setglobal(_state, prefix.c_str());
141 LUAType registerType (
const std::string& name) {
145 void setError (
const std::string& errorStr) {
149 const std::string& error ()
const {
153 bool load (
const std::string &luaString) {
154 return load(luaString.c_str(), luaString.length());
157 bool load (
const char *luaString,
size_t len) {
158 if (luaL_loadbufferx(_state, luaString, len,
"",
nullptr) || lua_pcall(_state, 0, LUA_MULTRET, 0)) {
159 setError(lua_tostring(_state, -1));
170 bool execute (
const std::string &
function,
int returnValues = 0) {
171 lua_getglobal(_state,
function.c_str());
172 const int ret = lua_pcall(_state, 0, returnValues, 0);
174 setError(lua_tostring(_state, -1));
#define ai_log_error(...)
Logging macro to provide your own loggers.
Definition: Types.h:23
bool execute(const std::string &function, int returnValues=0)
Definition: LUA.h:170
Definition: NonCopyable.h:8