summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_state.cpp
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-12 00:39:26 +0800
committerchai <chaifix@163.com>2019-03-12 00:39:26 +0800
commit70b82d1981c0de3c7b77670ff8abcfeb26815142 (patch)
treef69c05bcd204cc3f9bf745be37a2ba5911e52436 /Source/3rdParty/Luax/luax_state.cpp
parentc19a282e10f51ddd50d198b903f8fbd5a2238b62 (diff)
*misc
Diffstat (limited to 'Source/3rdParty/Luax/luax_state.cpp')
-rw-r--r--Source/3rdParty/Luax/luax_state.cpp606
1 files changed, 605 insertions, 1 deletions
diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp
index 3fc1645..a92d697 100644
--- a/Source/3rdParty/Luax/luax_state.cpp
+++ b/Source/3rdParty/Luax/luax_state.cpp
@@ -1,8 +1,612 @@
-//#include "luax_state.h"
+#include "luax_state.h"
namespace Luax
{
+
+#define L mState
+ LuaxState::LuaxState(lua_State* state)
+ : L(state)
+ {
+ assert(state);
+ }
+ LuaxState::~LuaxState()
+ {
+ }
+
+ LuaxState::operator lua_State*()
+ {
+ return L;
+ };
+
+ LuaxState::operator bool()
+ {
+ return L != nullptr;
+ }
+
+ lua_State* LuaxState::operator ->()
+ {
+ return L;
+ }
+
+ lua_State& LuaxState::operator*()
+ {
+ return *L;
+ }
+
+ void LuaxState::OpenLibs()
+ {
+ luaL_openlibs(L);
+ }
+
+ void LuaxState::PushNamespace(cc8* name)
+ {
+ bool isG = !lua_istable(L, -1);
+ int idx = isG ? LUA_GLOBALSINDEX : -1;
+ lua_getfield(L, idx, name);
+ if (lua_isnil(L, -1))
+ {
+ lua_pop(L, 1);
+ lua_newtable(L);
+ assert(lua_istable(L, -1));
+ lua_pushvalue(L, -1);
+ int t = isG ? LUA_GLOBALSINDEX : -3;
+ lua_setfield(L, t, name);
+ }
+
+ // stack:
+ // -1 namespace
+ }
+
+ void LuaxState::PopNamespace()
+ {
+ assert(lua_istable(L, -1));
+ lua_pop(L, 1);
+ }
+
+ void LuaxState::DoString(const std::string& code)
+ {
+ luaL_dostring(L, code.c_str());
+ }
+
+ int LuaxState::AbsIndex(int idx)
+ {
+ if (idx < 0) {
+ return lua_gettop(L) + idx + 1;
+ }
+ return idx;
+ }
+
+ int LuaxState::Call(int nArgs, int nResults)
+ {
+ return 0;
+ }
+
+ void LuaxState::Push()
+ {
+ lua_pushnil(L);
+ }
+
+ void LuaxState::Push(bool value)
+ {
+ lua_pushboolean(L, value ? 1 : 0);
+ }
+
+ void LuaxState::Push(cc8* value)
+ {
+ lua_pushstring(L, value);
+ }
+
+ void LuaxState::Push(double value)
+ {
+ lua_pushnumber(L, value);
+ }
+
+ void LuaxState::Push(float value)
+ {
+ lua_pushnumber(L, value);
+ }
+
+ void LuaxState::Push(int value)
+ {
+ lua_pushnumber(L, value);
+ }
+
+ void LuaxState::Push(u16 value)
+ {
+ lua_pushnumber(L, value);
+ }
+
+ void LuaxState::Push(u32 value)
+ {
+ lua_pushnumber(L, value);
+ }
+
+ void LuaxState::Push(u64 value)
+ {
+ lua_pushnumber(L, (double)value);
+ }
+
+ void LuaxState::Push(uintptr value)
+ {
+ lua_pushlightuserdata(L, (void*)value);
+ }
+
+ void LuaxState::Push(lua_CFunction value)
+ {
+ lua_pushcfunction(L, value);
+ }
+
+ void LuaxState::Push(void* data, size_t size)
+ {
+ lua_pushlstring(L, (cc8*)data, size);
+ }
+
+ void LuaxState::Push(const void* value)
+ {
+ lua_pushlightuserdata(L, (void*)value);
+ }
+
+ void LuaxState::Pop(int n /* = 1 */)
+ {
+ lua_pop(L, n);
+ }
+
+ bool LuaxState::IsNil(int idx)
+ {
+ return lua_isnil(L, idx);
+ }
+
+ bool LuaxState::IsNilOrNone(int idx)
+ {
+ int t = lua_type(L, idx);
+ return ((t == LUA_TNONE) || (t == LUA_TNIL));
+ }
+
+ bool LuaxState::IsTableOrUserdata(int idx)
+ {
+ int check = lua_type(L, idx);
+ return ((check == LUA_TTABLE) || (check == LUA_TUSERDATA));
+ }
+
+ bool LuaxState::IsTrueOrNotNil(int idx)
+ {
+ if (lua_isboolean(L, idx)) {
+ return lua_toboolean(L, idx) ? true : false;
+ }
+ return !lua_isnil(L, idx);
+ }
+
+ bool LuaxState::IsType(int idx, int type)
+ {
+ return (lua_type(L, idx) == type);
+ }
+
+ bool LuaxState::IsType(int idx, cc8* name, int type)
+ {
+ return this->HasField(idx, name, type);
+ }
+
+ bool LuaxState::IsValid()
+ {
+ return (L != 0);
+ }
+
+ int LuaxState::GetTop()
+ {
+ return lua_gettop(mState);
+ }
+
+ bool LuaxState::HasField(int idx, cc8* name) {
+
+ lua_getfield(L, idx, name);
+ bool hasField = (lua_isnil(L, -1) == false);
+ lua_pop(L, 1);
+
+ return hasField;
+ }
+
+ bool LuaxState::HasField(int idx, int key) {
+
+ this->GetField(idx, key);
+ bool hasField = (lua_isnil(L, -1) == false);
+ lua_pop(L, 1);
+
+ return hasField;
+ }
+
+ bool LuaxState::HasField(int idx, cc8* name, int type) {
+
+ lua_getfield(L, idx, name);
+ bool hasField = (lua_type(L, -1) == type);
+ lua_pop(L, 1);
+
+ return hasField;
+ }
+
+ bool LuaxState::HasField(int idx, int key, int type) {
+
+ this->GetField(idx, key);
+ bool hasField = (lua_type(L, -1) == type);
+ lua_pop(L, 1);
+
+ return hasField;
+ }
+
+ bool LuaxState::HasKeys(int idx) {
+
+ idx = this->AbsIndex(idx);
+
+ lua_pushnil(L); /* first key */
+ if (lua_next(L, idx) != 0) {
+ lua_pop(L, 2);
+ return true;
+ }
+ return false;
+ }
+
+ void LuaxState::Register(const luaL_Reg *l)
+ {
+ luaL_register(L, 0, l);
+ }
+
+ void LuaxState::GetField(int idx, cc8* name)
+ {
+ lua_getfield(L, idx, name);
+ }
+
+ void LuaxState::GetField(int idx, int key)
+ {
+ idx = this->AbsIndex(idx);
+
+ lua_pushinteger(L, key);
+ lua_gettable(L, idx);
+ }
+
+ std::string LuaxState::GetField(int idx, cc8* key, cc8* value)
+ {
+ std::string str;
+ if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
+ str = lua_tostring(L, -1);
+ lua_pop(L, 1);
+ }
+ else {
+ str = value;
+ }
+ return str;
+ }
+
+ std::string LuaxState::GetField(int idx, int key, cc8* value)
+ {
+ std::string str;
+ if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
+ str = lua_tostring(L, -1);
+ lua_pop(L, 1);
+ }
+ else {
+ str = value;
+ }
+ return str;
+ }
+
+ std::string LuaxState::GetField(int idx, cc8* key, const std::string& value)
+ {
+ std::string str;
+ if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
+ str = lua_tostring(L, -1);
+ lua_pop(L, 1);
+ }
+ else {
+ str = value;
+ }
+ return str;
+ }
+
+ std::string LuaxState::GetField(int idx, int key, const std::string& value)
+ {
+ std::string str;
+ if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
+ str = lua_tostring(L, -1);
+ lua_pop(L, 1);
+ }
+ else {
+ str = value;
+ }
+ return str;
+ }
+
+ bool LuaxState::GetFieldWithType(int idx, cc8* name, int type)
+ {
+ lua_getfield(L, idx, name);
+ if (lua_type(L, -1) != type) {
+ lua_pop(L, 1);
+ return false;
+ }
+ return true;
+ }
+
+ bool LuaxState::GetFieldWithType(int idx, int key, int type)
+ {
+ this->GetField(idx, key);
+ if (lua_type(L, -1) != type) {
+ lua_pop(L, 1);
+ return false;
+ }
+ return true;
+ }
+
+ void LuaxState::SetField(int idx, cc8* key)
+ {
+ if (IsTableOrUserdata(idx))
+ {
+ idx = AbsIndex(idx);
+ lua_setfield(L, idx, key);
+ }
+ }
+
+ cc8* LuaxState::GetLuaTypeName(int type)
+ {
+ switch (type) {
+ case LUA_TNONE: return "none";
+ case LUA_TNIL: return "nil";
+ case LUA_TBOOLEAN: return "boolean";
+ case LUA_TLIGHTUSERDATA: return "lightuserdata";
+ case LUA_TNUMBER: return "number";
+ case LUA_TSTRING: return "string";
+ case LUA_TTABLE: return "table";
+ case LUA_TFUNCTION: return "function";
+ case LUA_TUSERDATA: return "userdata";
+ case LUA_TTHREAD: return "thread";
+ }
+ return "unknown";
+ }
+
+
+ bool LuaxState::GetSubfieldWithType(int idx, cc8* format, int type, ...)
+ {
+ va_list args;
+ va_start(args, type);
+
+ idx = this->AbsIndex(idx);
+ lua_pushvalue(this->mState, idx);
+
+ for (cc8* c = format; *c; ++c) {
+ switch (*c) {
+ // number
+ case 'N':
+ lua_pushnumber(this->mState, va_arg(args, int));
+ lua_gettable(this->mState, -1);
+ break;
+
+ // string
+ case 'S':
+ lua_getfield(this->mState, -1, va_arg(args, char*));
+ break;
+
+ default:
+ lua_pushnil(this->mState);
+ }
+
+ if (lua_isnil(this->mState, -1)) break;
+ lua_replace(this->mState, -2);
+ }
+ va_end(args);
+ if (lua_type(this->mState, -1) != type) {
+ lua_pop(this->mState, 1);
+ return false;
+ }
+ return true;
+ }
+
+ bool LuaxState::CheckParams(int idx, cc8* format)
+ {
+ idx = AbsIndex(idx);
+
+ for (int i = 0; format[i]; ++i) {
+
+ int pos = idx + i;
+ int type = LUA_TNIL;
+ int expected = LUA_TNONE;
+
+ if (pos <= GetTop()) {
+ type = lua_type(mState, pos);
+ }
+
+ switch (format[i]) {
+
+ // boolean
+ case 'B':
+ if (type != LUA_TBOOLEAN) expected = LUA_TBOOLEAN;
+ break;
+
+ // coroutine
+ case 'C':
+ if (type != LUA_TTHREAD) expected = LUA_TTHREAD;
+ break;
+
+ // function
+ case 'F':
+ if (type != LUA_TFUNCTION) expected = LUA_TFUNCTION;
+ break;
+
+ // light userdata
+ case 'L':
+ if (type != LUA_TLIGHTUSERDATA) expected = LUA_TLIGHTUSERDATA;
+ break;
+
+ // number
+ case 'N':
+ if (type != LUA_TNUMBER) expected = LUA_TNUMBER;
+ break;
+
+ // string
+ case 'S':
+ if (type != LUA_TSTRING) expected = LUA_TSTRING;
+ break;
+
+ // table
+ case 'T':
+ if (type != LUA_TTABLE) expected = LUA_TTABLE;
+ break;
+
+ // userdata
+ case 'U':
+ if (type != LUA_TUSERDATA) expected = LUA_TUSERDATA;
+ break;
+
+ // any type
+ case '*':
+ case '.':
+ break;
+ }
+
+ if (expected != LUA_TNONE) {
+ return false;
+ }
+ }
+
+ return true;
+ }
+
+ template <>
+ bool LuaxState::GetValue < bool >(int idx, const bool value) {
+
+ if (this->IsType(idx, LUA_TBOOLEAN)) {
+ return (lua_toboolean(this->mState, idx) != 0);
+ }
+ return value;
+ }
+
+
+ template <>
+ cc8* LuaxState::GetValue < cc8* >(int idx, const cc8* value) {
+
+ if (this->IsType(idx, LUA_TSTRING)) {
+ return lua_tostring(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ std::string LuaxState::GetValue<std::string>(int idx, const std::string value)
+ {
+ std::string str;
+ if (lua_type(this->mState, idx) == LUA_TSTRING) {
+ str = lua_tostring(this->mState, idx);
+ }
+ else {
+ str = value;
+ }
+ return str;
+ }
+
+ template <>
+ double LuaxState::GetValue < double >(int idx, const double value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ float LuaxState::GetValue < float >(int idx, const float value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (float)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ s8 LuaxState::GetValue < s8 >(int idx, const s8 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (s8)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+
+ template <>
+ s16 LuaxState::GetValue < s16 >(int idx, const s16 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (s16)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+
+ template <>
+ s32 LuaxState::GetValue < s32 >(int idx, const s32 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (s32)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ s64 LuaxState::GetValue < s64 >(int idx, const s64 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (s64)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ u8 LuaxState::GetValue < u8 >(int idx, const u8 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (u8)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ u16 LuaxState::GetValue < u16 >(int idx, const u16 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (u16)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ u32 LuaxState::GetValue < u32 >(int idx, const u32 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (u32)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ u64 LuaxState::GetValue < u64 >(int idx, const u64 value)
+ {
+ if (this->IsType(idx, LUA_TNUMBER)) {
+ return (u64)lua_tonumber(this->mState, idx);
+ }
+ return value;
+ }
+
+ template <>
+ const void* LuaxState::GetValue < const void* >(int idx, const void* value)
+ {
+ if (this->IsType(idx, LUA_TLIGHTUSERDATA)) {
+ return (void*)lua_touserdata(this->mState, idx);
+ }
+ return value;
+ }
+
+ void LuaxState::PushPtrUserData(void* ptr) {
+
+ void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*));
+ assert(handle);
+ (*handle) = ptr;
+ }
} \ No newline at end of file