add lua user types + bytearray
This commit is contained in:
@@ -2,9 +2,16 @@
|
||||
#define LOGIC_SCRIPTING_LUA_UTIL_HPP_
|
||||
|
||||
#include "lua_commons.hpp"
|
||||
#include "lua_custom_types.hpp"
|
||||
|
||||
#include <unordered_map>
|
||||
#include <typeindex>
|
||||
#include <typeinfo>
|
||||
|
||||
namespace lua {
|
||||
inline std::string LAMBDAS_TABLE = "$L";
|
||||
extern std::unordered_map<std::type_index, std::string> usertypeNames;
|
||||
int userdata_destructor(lua::State* L);
|
||||
|
||||
std::string env_name(int env);
|
||||
|
||||
@@ -230,6 +237,12 @@ namespace lua {
|
||||
inline bool isfunction(lua::State* L, int idx) {
|
||||
return lua_isfunction(L, idx);
|
||||
}
|
||||
inline bool isuserdata(lua::State* L, int idx) {
|
||||
return lua_isuserdata(L, idx);
|
||||
}
|
||||
inline void setfield(lua::State* L, const std::string& name, int idx=-2) {
|
||||
lua_setfield(L, idx, name.c_str());
|
||||
}
|
||||
inline bool toboolean(lua::State* L, int idx) {
|
||||
return lua_toboolean(L, idx);
|
||||
}
|
||||
@@ -245,7 +258,42 @@ namespace lua {
|
||||
inline const void* topointer(lua::State* L, int idx) {
|
||||
return lua_topointer(L, idx);
|
||||
}
|
||||
inline glm::vec2 tovec2(lua::State* L, int idx) {
|
||||
inline void setglobal(lua::State* L, const std::string& name) {
|
||||
lua_setglobal(L, name.c_str());
|
||||
}
|
||||
template<class T>
|
||||
inline T* touserdata(lua::State* L, int idx) {
|
||||
if (void* rawptr = lua_touserdata(L, idx)) {
|
||||
return static_cast<T*>(rawptr);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
template<class T, typename... Args>
|
||||
inline int newuserdata(lua::State* L, Args&&... args) {
|
||||
const auto& found = usertypeNames.find(typeid(T));
|
||||
void* ptr = lua_newuserdata(L, sizeof(T));
|
||||
new (ptr) T(args...);
|
||||
|
||||
if (found == usertypeNames.end()) {
|
||||
log_error("usertype is not registred: "+std::string(typeid(T).name()));
|
||||
} else if (getglobal(L, found->second)) {
|
||||
setmetatable(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
template<class T, lua_CFunction func>
|
||||
inline void newusertype(lua::State* L, const std::string& name) {
|
||||
usertypeNames[typeid(T)] = name;
|
||||
func(L);
|
||||
|
||||
pushcfunction(L, userdata_destructor);
|
||||
setfield(L, "__gc");
|
||||
|
||||
setglobal(L, name);
|
||||
}
|
||||
|
||||
inline glm::vec2 tovec2(lua::State* L, int idx) {
|
||||
pushvalue(L, idx);
|
||||
if (!istable(L, idx) || objlen(L, idx) < 2) {
|
||||
throw std::runtime_error("value must be an array of two numbers");
|
||||
@@ -287,14 +335,6 @@ namespace lua {
|
||||
return true;
|
||||
}
|
||||
|
||||
inline void setfield(lua::State* L, const std::string& name, int idx=-2) {
|
||||
lua_setfield(L, idx, name.c_str());
|
||||
}
|
||||
|
||||
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 (!isstring(L, idx)) {
|
||||
throw luaerror("string expected at "+std::to_string(idx));
|
||||
|
||||
Reference in New Issue
Block a user