From 425decdf7e9284d15aa726e3ae96b9942fb0e3ea Mon Sep 17 00:00:00 2001 From: IronClawTrem Date: Sun, 16 Feb 2020 03:40:06 +0000 Subject: create tremded branch --- src/script/rapidjson/luax.hpp | 82 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 src/script/rapidjson/luax.hpp (limited to 'src/script/rapidjson/luax.hpp') diff --git a/src/script/rapidjson/luax.hpp b/src/script/rapidjson/luax.hpp new file mode 100644 index 0000000..e8ea7c6 --- /dev/null +++ b/src/script/rapidjson/luax.hpp @@ -0,0 +1,82 @@ +#ifndef __LUA_RAPIDJSION_LUACOMPAT_H__ +#define __LUA_RAPIDJSION_LUACOMPAT_H__ + +#include +#include +#include + +#include "qcommon/q3_lauxlib.h" + +#include "lua.hpp" + +namespace luax +{ + inline void setfuncs(lua_State* L, const luaL_Reg* funcs) + { luaL_setfuncs(L, funcs, 0); } + + inline size_t rawlen(lua_State* L, int idx) + { return lua_rawlen(L, idx); } + + inline bool isinteger(lua_State* L, int idx, int64_t* out = nullptr) + { + if (lua_isinteger(L, idx)) // but it maybe not detect all integers. + { + if (out) + *out = lua_tointeger(L, idx); + return true; + } + + double intpart; + if (std::modf(lua_tonumber(L, idx), &intpart) == 0.0) + { + if (std::numeric_limits::min() <= intpart + && intpart <= std::numeric_limits::max()) + { + if (out) + *out = static_cast(intpart); + return true; + } + } + return false; + } + + inline int typerror(lua_State* L, int narg, const char* tname) + { + const char *msg = lua_pushfstring(L, "%s expected, got %s", tname, luaL_typename(L, narg)); + return luaL_argerror(L, narg, msg); + } + + inline bool optboolfield(lua_State* L, int idx, const char* name, bool def) + { + auto v = def; + auto t = lua_type(L, idx); + + if (t != LUA_TTABLE && t != LUA_TNONE) + luax::typerror(L, idx, "table"); + + if (t != LUA_TNONE) + { + lua_getfield(L, idx, name); + if (!lua_isnoneornil(L, -1)) + v = lua_toboolean(L, -1) != 0; + lua_pop(L, 1); + } + + return v; + } + + inline int optintfield(lua_State* L, int idx, const char* name, int def) + { + auto v = def; + lua_getfield(L, idx, name); + + if (lua_isnumber(L, -1)) + v = static_cast(lua_tointeger(L, -1)); + + lua_pop(L, 1); + return v; + } + +} + +#endif -- cgit