refactor: LuaState replaced with lua_engine
This commit is contained in:
@@ -27,6 +27,53 @@ namespace lua {
|
||||
inline void pop(lua_State* L, int n=1) {
|
||||
lua_pop(L, n);
|
||||
}
|
||||
inline int gettop(lua_State* L) {
|
||||
return lua_gettop(L);
|
||||
}
|
||||
inline size_t objlen(lua_State* L, int idx) {
|
||||
return lua_objlen(L, idx);
|
||||
}
|
||||
inline int type(lua_State* L, int idx) {
|
||||
return lua_type(L, idx);
|
||||
}
|
||||
inline const char* type_name(lua_State* L, int idx) {
|
||||
return lua_typename(L, idx);
|
||||
}
|
||||
inline int rawgeti(lua_State* L, int n, int idx=-1) {
|
||||
lua_rawgeti(L, idx, n);
|
||||
return 1;
|
||||
}
|
||||
inline void rawseti(lua_State* L, int n, int idx=-2) {
|
||||
lua_rawseti(L, idx, n);
|
||||
}
|
||||
|
||||
inline int createtable(lua_State* L, int narr, int nrec) {
|
||||
lua_createtable(L, narr, nrec);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline bool isnil(lua_State* L, int idx) {
|
||||
return lua_isnil(L, idx);
|
||||
}
|
||||
|
||||
inline bool getglobal(lua_State* L, const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (isnil(L, -1)) {
|
||||
pop(L);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool hasglobal(lua_State* L, const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (isnil(L, -1)) {
|
||||
pop(L);
|
||||
return false;
|
||||
}
|
||||
pop(L);
|
||||
return true;
|
||||
}
|
||||
|
||||
// function wrappers with number of pushed values as return value
|
||||
|
||||
@@ -46,88 +93,88 @@ namespace lua {
|
||||
}
|
||||
|
||||
inline int pushivec3(lua_State* L, lua_Integer x, lua_Integer y, lua_Integer z) {
|
||||
lua_pushinteger(L, x);
|
||||
lua_pushinteger(L, y);
|
||||
lua_pushinteger(L, z);
|
||||
pushinteger(L, x);
|
||||
pushinteger(L, y);
|
||||
pushinteger(L, z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushivec3(lua_State* L, glm::ivec3 vec) {
|
||||
lua_pushinteger(L, vec.x);
|
||||
lua_pushinteger(L, vec.y);
|
||||
lua_pushinteger(L, vec.z);
|
||||
pushinteger(L, vec.x);
|
||||
pushinteger(L, vec.y);
|
||||
pushinteger(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec3(lua_State* L, glm::vec3 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
pushnumber(L, vec.x);
|
||||
pushnumber(L, vec.y);
|
||||
pushnumber(L, vec.z);
|
||||
return 3;
|
||||
}
|
||||
|
||||
inline int pushvec4(lua_State* L, glm::vec4 vec) {
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_pushnumber(L, vec.w);
|
||||
pushnumber(L, vec.x);
|
||||
pushnumber(L, vec.y);
|
||||
pushnumber(L, vec.z);
|
||||
pushnumber(L, vec.w);
|
||||
return 4;
|
||||
}
|
||||
|
||||
inline int pushvec2_arr(lua_State* L, glm::vec2 vec) {
|
||||
lua_createtable(L, 2, 0);
|
||||
lua_getglobal(L, "vec2_mt");
|
||||
createtable(L, 2, 0);
|
||||
getglobal(L, "vec2_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
pushnumber(L, vec.x);
|
||||
rawseti(L, 1);
|
||||
pushnumber(L, vec.y);
|
||||
rawseti(L, 2);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec3_arr(lua_State* L, glm::vec3 vec) {
|
||||
lua_createtable(L, 3, 0);
|
||||
lua_getglobal(L, "vec3_mt");
|
||||
createtable(L, 3, 0);
|
||||
getglobal(L, "vec3_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
pushnumber(L, vec.x);
|
||||
rawseti(L, 1);
|
||||
pushnumber(L, vec.y);
|
||||
rawseti(L, 2);
|
||||
pushnumber(L, vec.z);
|
||||
rawseti(L, 3);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushvec4_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "vec4_mt");
|
||||
createtable(L, 4, 0);
|
||||
getglobal(L, "vec4_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushnumber(L, vec.x);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushnumber(L, vec.y);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushnumber(L, vec.z);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushnumber(L, vec.w);
|
||||
lua_rawseti(L, -2, 4);
|
||||
pushnumber(L, vec.x);
|
||||
rawseti(L, 1);
|
||||
pushnumber(L, vec.y);
|
||||
rawseti(L, 2);
|
||||
pushnumber(L, vec.z);
|
||||
rawseti(L, 3);
|
||||
pushnumber(L, vec.w);
|
||||
rawseti(L, 4);
|
||||
return 1;
|
||||
}
|
||||
inline int pushcolor_arr(lua_State* L, glm::vec4 vec) {
|
||||
lua_createtable(L, 4, 0);
|
||||
lua_getglobal(L, "color_mt");
|
||||
createtable(L, 4, 0);
|
||||
getglobal(L, "color_mt");
|
||||
lua_setmetatable(L, -2);
|
||||
|
||||
lua_pushinteger(L, vec.x*255);
|
||||
lua_rawseti(L, -2, 1);
|
||||
lua_pushinteger(L, vec.y*255);
|
||||
lua_rawseti(L, -2, 2);
|
||||
lua_pushinteger(L, vec.z*255);
|
||||
lua_rawseti(L, -2, 3);
|
||||
lua_pushinteger(L, vec.w*255);
|
||||
lua_rawseti(L, -2, 4);
|
||||
pushinteger(L, vec.x*255);
|
||||
rawseti(L, 1);
|
||||
pushinteger(L, vec.y*255);
|
||||
rawseti(L, 2);
|
||||
pushinteger(L, vec.z*255);
|
||||
rawseti(L, 3);
|
||||
pushinteger(L, vec.w*255);
|
||||
rawseti(L, 4);
|
||||
return 1;
|
||||
}
|
||||
inline int pushcfunction(lua_State* L, lua_CFunction func) {
|
||||
@@ -139,63 +186,84 @@ namespace lua {
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<typename... Args>
|
||||
inline int pushfstring(lua_State *L, const char * fmt, Args... args) {
|
||||
lua_pushfstring(L, fmt, args...);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int pushwstring(lua_State* L, const std::wstring& str);
|
||||
|
||||
inline int pushboolean(lua_State* L, bool value) {
|
||||
lua_pushboolean(L, value);
|
||||
return 1;
|
||||
}
|
||||
inline int pushglobals(lua_State* L) {
|
||||
lua_pushvalue(L, LUA_GLOBALSINDEX);
|
||||
return 1;
|
||||
}
|
||||
inline int pushvalue(lua_State* L, int idx) {
|
||||
lua_pushvalue(L, idx);
|
||||
return 1;
|
||||
}
|
||||
|
||||
inline int pushglobals(lua_State* L) {
|
||||
return pushvalue(L, LUA_GLOBALSINDEX);
|
||||
}
|
||||
inline bool isnoneornil(lua_State* L, int idx) {
|
||||
return lua_isnoneornil(L, idx);
|
||||
}
|
||||
inline bool isboolean(lua_State* L, int idx) {
|
||||
return lua_isboolean(L, idx);
|
||||
}
|
||||
inline bool isnumber(lua_State* L, int idx) {
|
||||
return lua_isnumber(L, idx);
|
||||
}
|
||||
inline bool isstring(lua_State* L, int idx) {
|
||||
return lua_isstring(L, idx);
|
||||
}
|
||||
inline bool istable(lua_State* L, int idx) {
|
||||
return lua_istable(L, idx);
|
||||
}
|
||||
inline bool isfunction(lua_State* L, int idx) {
|
||||
return lua_isfunction(L, idx);
|
||||
}
|
||||
inline bool toboolean(lua_State* L, int idx) {
|
||||
return lua_toboolean(L, idx);
|
||||
}
|
||||
|
||||
inline lua_Integer tointeger(lua_State* L, int idx) {
|
||||
return lua_tointeger(L, idx);
|
||||
}
|
||||
|
||||
inline lua_Number tonumber(lua_State* L, int idx) {
|
||||
return lua_tonumber(L, idx);
|
||||
}
|
||||
|
||||
inline const char* tostring(lua_State* L, int idx) {
|
||||
return lua_tostring(L, idx);
|
||||
}
|
||||
|
||||
inline const void* topointer(lua_State* L, int idx) {
|
||||
return lua_topointer(L, idx);
|
||||
}
|
||||
inline glm::vec2 tovec2(lua_State* L, int idx) {
|
||||
lua_pushvalue(L, idx);
|
||||
if (!lua_istable(L, idx) || lua_objlen(L, idx) < 2) {
|
||||
pushvalue(L, idx);
|
||||
if (!istable(L, idx) || objlen(L, idx) < 2) {
|
||||
throw std::runtime_error("value must be an array of two numbers");
|
||||
}
|
||||
lua_rawgeti(L, -1, 1);
|
||||
lua_Number x = lua_tonumber(L, -1); lua_pop(L, 1);
|
||||
lua_rawgeti(L, -1, 2);
|
||||
lua_Number y = lua_tonumber(L, -1); lua_pop(L, 1);
|
||||
rawgeti(L, 1);
|
||||
auto x = tonumber(L, -1); pop(L);
|
||||
rawgeti(L, 2);
|
||||
auto y = tonumber(L, -1); pop(L);
|
||||
pop(L);
|
||||
return glm::vec2(x, y);
|
||||
}
|
||||
|
||||
inline glm::vec4 tocolor(lua_State* L, int idx) {
|
||||
pushvalue(L, idx);
|
||||
if (!lua_istable(L, -1) || lua_objlen(L, idx) < 4) {
|
||||
if (!istable(L, -1) || objlen(L, idx) < 4) {
|
||||
throw std::runtime_error("RGBA array required");
|
||||
}
|
||||
lua_rawgeti(L, -1, 1);
|
||||
lua_Number r = tonumber(L, -1); pop(L);
|
||||
lua_rawgeti(L, -1, 2);
|
||||
lua_Number g = tonumber(L, -1); pop(L);
|
||||
lua_rawgeti(L, -1, 3);
|
||||
lua_Number b = tonumber(L, -1); pop(L);
|
||||
lua_rawgeti(L, -1, 4);
|
||||
lua_Number a = tonumber(L, -1); pop(L);
|
||||
rawgeti(L, 1);
|
||||
auto r = tonumber(L, -1); pop(L);
|
||||
rawgeti(L, 2);
|
||||
auto g = tonumber(L, -1); pop(L);
|
||||
rawgeti(L, 3);
|
||||
auto b = tonumber(L, -1); pop(L);
|
||||
rawgeti(L, 4);
|
||||
auto a = tonumber(L, -1); pop(L);
|
||||
pop(L);
|
||||
return glm::vec4(r/255, g/255, b/255, a/255);
|
||||
}
|
||||
@@ -205,8 +273,8 @@ namespace lua {
|
||||
|
||||
inline bool getfield(lua_State* L, const std::string& name, int idx=-1) {
|
||||
lua_getfield(L, idx, name.c_str());
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, -1);
|
||||
if (isnil(L, -1)) {
|
||||
pop(L);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
@@ -216,31 +284,12 @@ namespace lua {
|
||||
lua_setfield(L, idx, name.c_str());
|
||||
}
|
||||
|
||||
inline bool getglobal(lua_State* L, const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (lua_isnil(L, -1)) {
|
||||
pop(L);
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
inline bool hasglobal(lua_State* L, const std::string& name) {
|
||||
lua_getglobal(L, name.c_str());
|
||||
if (lua_isnil(L, -1)) {
|
||||
lua_pop(L, -1);
|
||||
return false;
|
||||
}
|
||||
pop(L);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void setglobal(lua_State* L, const std::string& name) {
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
|
||||
inline const char* require_string(lua_State* L, int idx) {
|
||||
if (!lua_isstring(L, idx)) {
|
||||
if (!isstring(L, idx)) {
|
||||
throw luaerror("string expected at "+std::to_string(idx));
|
||||
}
|
||||
return tostring(L, idx);
|
||||
@@ -249,28 +298,27 @@ namespace lua {
|
||||
std::wstring require_wstring(lua_State*, int idx);
|
||||
|
||||
inline bool rename(lua_State* L, const std::string& from, const std::string& to) {
|
||||
const char* src = from.c_str();
|
||||
lua_getglobal(L, src);
|
||||
if (lua_isnil(L, lua_gettop(L))) {
|
||||
lua_pop(L, lua_gettop(L));
|
||||
getglobal(L, from);
|
||||
if (lua_isnil(L, -1)) {
|
||||
pop(L, 1);
|
||||
return false;
|
||||
}
|
||||
lua_setglobal(L, to.c_str());
|
||||
setglobal(L, to);
|
||||
|
||||
// remove previous
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, src);
|
||||
pushnil(L);
|
||||
setglobal(L, from);
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void remove(lua_State* L, const std::string& name) {
|
||||
lua_pushnil(L);
|
||||
lua_setglobal(L, name.c_str());
|
||||
pushnil(L);
|
||||
setglobal(L, name);
|
||||
}
|
||||
|
||||
inline void loadbuffer(lua_State* L, int env, const std::string& src, const std::string& file) {
|
||||
if (luaL_loadbuffer(L, src.c_str(), src.length(), file.c_str())) {
|
||||
throw luaerror(lua_tostring(L, -1));
|
||||
throw luaerror(tostring(L, -1));
|
||||
}
|
||||
if (env && getglobal(L, env_name(env))) {
|
||||
lua_setfenv(L, -2);
|
||||
@@ -303,6 +351,17 @@ namespace lua {
|
||||
int createEnvironment(lua_State*, int parent);
|
||||
void removeEnvironment(lua_State*, int id);
|
||||
void dump_stack(lua_State*);
|
||||
|
||||
inline void addfunc(lua_State* L, const std::string& name, lua_CFunction func) {
|
||||
pushcfunction(L, func);
|
||||
setglobal(L, name);
|
||||
}
|
||||
|
||||
inline void openlib(lua_State* L, const std::string& name, const luaL_Reg* libfuncs) {
|
||||
createtable(L, 0, 0);
|
||||
luaL_setfuncs(L, libfuncs, 0);
|
||||
setglobal(L, name);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_
|
||||
|
||||
Reference in New Issue
Block a user