refactor: 'lua' namespace expansion

This commit is contained in:
MihailRis
2024-06-11 03:37:35 +03:00
parent 7973a9c32b
commit 0647bc6f90
26 changed files with 613 additions and 723 deletions
+112 -37
View File
@@ -1,19 +1,33 @@
#ifndef LOGIC_SCRIPTING_LUA_UTIL_HPP_
#define LOGIC_SCRIPTING_LUA_UTIL_HPP_
#include "LuaState.hpp"
#ifdef __linux__
#include <luajit-2.1/luaconf.h>
#include <luajit-2.1/lua.hpp>
#else
#include <lua.hpp>
#endif
#include <glm/glm.hpp>
#include <stdexcept>
#include "lua_commons.hpp"
namespace lua {
inline std::string LAMBDAS_TABLE = "$L";
std::string env_name(int env);
template <lua_CFunction func> int wrap(lua_State *L) {
int result = 0;
try {
result = func(L);
}
// transform exception with description into lua_error
catch (std::exception &e) {
luaL_error(L, e.what());
}
// Rethrow any other exception (lua error for example)
catch (...) {
throw;
}
return result;
}
inline void pop(lua_State* L, int n=1) {
lua_pop(L, n);
}
// function wrappers with number of pushed values as return value
inline int pushnil(lua_State* L) {
@@ -144,6 +158,18 @@ namespace lua {
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 glm::vec2 tovec2(lua_State* L, int idx) {
lua_pushvalue(L, idx);
if (!lua_istable(L, idx) || lua_objlen(L, idx) < 2) {
@@ -153,42 +179,30 @@ namespace lua {
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);
lua_pop(L, 1);
pop(L);
return glm::vec2(x, y);
}
inline glm::vec4 tocolor(lua_State* L, int idx) {
lua_pushvalue(L, idx);
pushvalue(L, idx);
if (!lua_istable(L, -1) || lua_objlen(L, idx) < 4) {
throw std::runtime_error("RGBA array required");
}
lua_rawgeti(L, -1, 1);
lua_Number r = lua_tonumber(L, -1); lua_pop(L, 1);
lua_Number r = tonumber(L, -1); pop(L);
lua_rawgeti(L, -1, 2);
lua_Number g = lua_tonumber(L, -1); lua_pop(L, 1);
lua_Number g = tonumber(L, -1); pop(L);
lua_rawgeti(L, -1, 3);
lua_Number b = lua_tonumber(L, -1); lua_pop(L, 1);
lua_Number b = tonumber(L, -1); pop(L);
lua_rawgeti(L, -1, 4);
lua_Number a = lua_tonumber(L, -1); lua_pop(L, 1);
lua_pop(L, 1);
lua_Number a = tonumber(L, -1); pop(L);
pop(L);
return glm::vec4(r/255, g/255, b/255, a/255);
}
inline const char* require_string(lua_State* L, int idx) {
if (!lua_isstring(L, idx)) {
throw luaerror("string expected at "+std::to_string(idx));
}
return lua_tostring(L, idx);
}
std::wstring require_wstring(lua_State*, int idx);
int pushvalue(lua_State*, const dynamic::Value& value);
dynamic::Value tovalue(lua_State*, int idx);
inline void pop(lua_State* L, int n=1) {
lua_pop(L, n);
}
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)) {
@@ -204,8 +218,8 @@ namespace lua {
inline bool getglobal(lua_State* L, const std::string& name) {
lua_getglobal(L, name.c_str());
if (lua_isnil(L, lua_gettop(L))) {
lua_pop(L, lua_gettop(L));
if (lua_isnil(L, -1)) {
pop(L);
return false;
}
return true;
@@ -213,11 +227,11 @@ namespace lua {
inline bool hasglobal(lua_State* L, const std::string& name) {
lua_getglobal(L, name.c_str());
if (lua_isnil(L, lua_gettop(L))) {
lua_pop(L, lua_gettop(L));
if (lua_isnil(L, -1)) {
lua_pop(L, -1);
return false;
}
lua_pop(L, lua_gettop(L));
pop(L);
return true;
}
@@ -225,9 +239,70 @@ namespace lua {
lua_setglobal(L, name.c_str());
}
void logError(const std::string& text);
inline const char* require_string(lua_State* L, int idx) {
if (!lua_isstring(L, idx)) {
throw luaerror("string expected at "+std::to_string(idx));
}
return tostring(L, idx);
}
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));
return false;
}
lua_setglobal(L, to.c_str());
// remove previous
lua_pushnil(L);
lua_setglobal(L, src);
return true;
}
inline void remove(lua_State* L, const std::string& name) {
lua_pushnil(L);
lua_setglobal(L, name.c_str());
}
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));
}
if (env && getglobal(L, env_name(env))) {
lua_setfenv(L, -2);
}
}
int call(lua_State*, int argc, int nresults=-1);
int callNoThrow(lua_State*, int argc);
int call_nothrow(lua_State*, int argc);
inline int eval(lua_State* L, int env, const std::string& src, const std::string& file="<eval>") {
auto srcText = "return "+src;
loadbuffer(L, env, srcText, file);
return call(L, 0);
}
inline int execute(lua_State* L, int env, const std::string& src, const std::string& file="<eval>") {
loadbuffer(L, env, src, file);
return call_nothrow(L, 0);
}
runnable create_runnable(lua_State*);
scripting::common_func create_lambda(lua_State* );
inline int pushenv(lua_State* L, int env) {
if (getglobal(L, env_name(env))) {
return 1;
}
return 0;
}
int createEnvironment(lua_State*, int parent);
void removeEnvironment(lua_State*, int id);
void dump_stack(lua_State*);
}
#endif // LOGIC_SCRIPTING_LUA_UTIL_HPP_