summaryrefslogtreecommitdiff
path: root/Source/3rdParty/Luax/luax_state.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'Source/3rdParty/Luax/luax_state.cpp')
-rw-r--r--Source/3rdParty/Luax/luax_state.cpp215
1 files changed, 115 insertions, 100 deletions
diff --git a/Source/3rdParty/Luax/luax_state.cpp b/Source/3rdParty/Luax/luax_state.cpp
index 87c3bcd..5253a2c 100644
--- a/Source/3rdParty/Luax/luax_state.cpp
+++ b/Source/3rdParty/Luax/luax_state.cpp
@@ -1,18 +1,19 @@
+//#include "luax_class.h"
+#include "luax_enum.h"
#include "luax_state.h"
+#include "luax_runtime.h"
namespace Luax
{
-#define L mState
-
LuaxState::LuaxState(lua_State* state)
- : L(state)
+ : mState(state)
{
assert(state);
}
LuaxState::LuaxState(const LuaxState& state)
- : L(state.mState)
+ : mState(state.mState)
{
assert(state.mState);
}
@@ -21,49 +22,24 @@ namespace Luax
{
}
- LuaxState::operator lua_State*()
- {
- return L;
- };
-
- LuaxState::operator bool()
- {
- return L != nullptr;
- }
-
- lua_State* LuaxState::operator ->()
- {
- return L;
- }
-
- lua_State& LuaxState::operator*()
- {
- return *L;
- }
-
- lua_State* LuaxState::GetHandle()
- {
- return L;
- }
-
void LuaxState::OpenLibs()
{
- luaL_openlibs(L);
+ luaL_openlibs(mState);
}
void LuaxState::PushNamespace(cc8* name)
{
- bool isG = !lua_istable(L, -1);
+ bool isG = !lua_istable(mState, -1);
int idx = isG ? LUA_GLOBALSINDEX : -1;
- lua_getfield(L, idx, name);
- if (lua_isnil(L, -1))
+ lua_getfield(mState, idx, name);
+ if (lua_isnil(mState, -1))
{
- lua_pop(L, 1);
- lua_newtable(L);
- assert(lua_istable(L, -1));
- lua_pushvalue(L, -1);
+ lua_pop(mState, 1);
+ lua_newtable(mState);
+ assert(lua_istable(mState, -1));
+ lua_pushvalue(mState, -1);
int t = isG ? LUA_GLOBALSINDEX : -3;
- lua_setfield(L, t, name);
+ lua_setfield(mState, t, name);
}
// stack:
@@ -72,25 +48,25 @@ namespace Luax
void LuaxState::PopNamespace()
{
- assert(lua_istable(L, -1));
- lua_pop(L, 1);
+ assert(lua_istable(mState, -1));
+ lua_pop(mState, 1);
}
void LuaxState::DoString(const std::string& code)
{
- luaL_dostring(L, code.c_str());
+ luaL_dostring(mState, code.c_str());
}
int LuaxState::AbsIndex(int idx)
{
/*
-#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
- lua_gettop(L) + (i) + 1)
+#define abs_index(mState, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : \
+ lua_gettop(mState) + (i) + 1)
*/
if (idx < 0) {
- //return lua_gettop(L) + idx + 1;
+ //return lua_gettop(mState) + idx + 1;
return ((idx) > 0 || (idx) <= LUA_REGISTRYINDEX ? (idx) : \
- lua_gettop(L) + (idx)+1);
+ lua_gettop(mState) + (idx)+1);
}
return idx;
}
@@ -102,107 +78,107 @@ namespace Luax
void LuaxState::PushNil()
{
- lua_pushnil(L);
+ lua_pushnil(mState);
}
void LuaxState::Push(bool value)
{
- lua_pushboolean(L, value ? 1 : 0);
+ lua_pushboolean(mState, value ? 1 : 0);
}
void LuaxState::Push(cc8* value)
{
- lua_pushstring(L, value);
+ lua_pushstring(mState, value);
}
void LuaxState::Push(double value)
{
- lua_pushnumber(L, value);
+ lua_pushnumber(mState, value);
}
void LuaxState::Push(float value)
{
- lua_pushnumber(L, value);
+ lua_pushnumber(mState, value);
}
void LuaxState::Push(int value)
{
- lua_pushnumber(L, value);
+ lua_pushnumber(mState, value);
}
void LuaxState::Push(u16 value)
{
- lua_pushnumber(L, value);
+ lua_pushnumber(mState, value);
}
void LuaxState::Push(u32 value)
{
- lua_pushnumber(L, value);
+ lua_pushnumber(mState, value);
}
void LuaxState::Push(u64 value)
{
- lua_pushnumber(L, (double)value);
+ lua_pushnumber(mState, (double)value);
}
void LuaxState::Push(uintptr value)
{
- lua_pushlightuserdata(L, (void*)value);
+ lua_pushlightuserdata(mState, (void*)value);
}
void LuaxState::Push(lua_CFunction value)
{
- lua_pushcfunction(L, value);
+ lua_pushcfunction(mState, value);
}
void LuaxState::Push(void* data, size_t size)
{
- lua_pushlstring(L, (cc8*)data, size);
+ lua_pushlstring(mState, (cc8*)data, size);
}
void LuaxState::Push(const void* value)
{
- lua_pushlightuserdata(L, (void*)value);
+ lua_pushlightuserdata(mState, (void*)value);
}
void LuaxState::Pop(int n /* = 1 */)
{
- lua_pop(L, n);
+ lua_pop(mState, n);
}
void LuaxState::Settop(int idx)
{
- lua_settop(L, idx);
+ lua_settop(mState, idx);
}
bool LuaxState::IsNil(int idx)
{
- return lua_isnil(L, idx);
+ return lua_isnil(mState, idx);
}
bool LuaxState::IsNilOrNone(int idx)
{
- int t = lua_type(L, idx);
+ int t = lua_type(mState, idx);
return ((t == LUA_TNONE) || (t == LUA_TNIL));
}
bool LuaxState::IsTableOrUserdata(int idx)
{
- int check = lua_type(L, idx);
+ int check = lua_type(mState, 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;
+ if (lua_isboolean(mState, idx)) {
+ return lua_toboolean(mState, idx) ? true : false;
}
- return !lua_isnil(L, idx);
+ return !lua_isnil(mState, idx);
}
bool LuaxState::IsType(int idx, int type)
{
- return (lua_type(L, idx) == type);
+ return (lua_type(mState, idx) == type);
}
bool LuaxState::IsType(int idx, cc8* name, int type)
@@ -212,7 +188,7 @@ namespace Luax
bool LuaxState::IsValid()
{
- return (L != 0);
+ return (mState != 0);
}
int LuaxState::GetTop()
@@ -222,9 +198,9 @@ namespace Luax
bool LuaxState::HasField(int idx, cc8* name) {
- lua_getfield(L, idx, name);
- bool hasField = (lua_isnil(L, -1) == false);
- lua_pop(L, 1);
+ lua_getfield(mState, idx, name);
+ bool hasField = (lua_isnil(mState, -1) == false);
+ lua_pop(mState, 1);
return hasField;
}
@@ -232,17 +208,17 @@ namespace Luax
bool LuaxState::HasField(int idx, int key) {
this->GetField(idx, key);
- bool hasField = (lua_isnil(L, -1) == false);
- lua_pop(L, 1);
+ bool hasField = (lua_isnil(mState, -1) == false);
+ lua_pop(mState, 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);
+ lua_getfield(mState, idx, name);
+ bool hasField = (lua_type(mState, -1) == type);
+ lua_pop(mState, 1);
return hasField;
}
@@ -250,8 +226,8 @@ namespace Luax
bool LuaxState::HasField(int idx, int key, int type) {
this->GetField(idx, key);
- bool hasField = (lua_type(L, -1) == type);
- lua_pop(L, 1);
+ bool hasField = (lua_type(mState, -1) == type);
+ lua_pop(mState, 1);
return hasField;
}
@@ -260,38 +236,38 @@ namespace Luax
idx = this->AbsIndex(idx);
- lua_pushnil(L); /* first key */
- if (lua_next(L, idx) != 0) {
- lua_pop(L, 2);
+ lua_pushnil(mState); /* first key */
+ if (lua_next(mState, idx) != 0) {
+ lua_pop(mState, 2);
return true;
}
return false;
}
- void LuaxState::Register(const luaL_Reg *l)
+ void LuaxState::RegisterMethods(const luaL_Reg *l)
{
- luaL_register(L, 0, l);
+ luaL_register(mState, 0, l);
}
void LuaxState::GetField(int idx, cc8* name)
{
- lua_getfield(L, idx, name);
+ lua_getfield(mState, idx, name);
}
void LuaxState::GetField(int idx, int key)
{
idx = this->AbsIndex(idx);
- lua_pushinteger(L, key);
- lua_gettable(L, idx);
+ lua_pushinteger(mState, key);
+ lua_gettable(mState, 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);
+ str = lua_tostring(mState, -1);
+ lua_pop(mState, 1);
}
else {
str = value;
@@ -303,8 +279,8 @@ namespace Luax
{
std::string str;
if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
- str = lua_tostring(L, -1);
- lua_pop(L, 1);
+ str = lua_tostring(mState, -1);
+ lua_pop(mState, 1);
}
else {
str = value;
@@ -316,8 +292,8 @@ namespace Luax
{
std::string str;
if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
- str = lua_tostring(L, -1);
- lua_pop(L, 1);
+ str = lua_tostring(mState, -1);
+ lua_pop(mState, 1);
}
else {
str = value;
@@ -329,8 +305,8 @@ namespace Luax
{
std::string str;
if (this->GetFieldWithType(idx, key, LUA_TSTRING)) {
- str = lua_tostring(L, -1);
- lua_pop(L, 1);
+ str = lua_tostring(mState, -1);
+ lua_pop(mState, 1);
}
else {
str = value;
@@ -340,9 +316,9 @@ namespace Luax
bool LuaxState::GetFieldWithType(int idx, cc8* name, int type)
{
- lua_getfield(L, idx, name);
- if (lua_type(L, -1) != type) {
- lua_pop(L, 1);
+ lua_getfield(mState, idx, name);
+ if (lua_type(mState, -1) != type) {
+ lua_pop(mState, 1);
return false;
}
return true;
@@ -351,8 +327,8 @@ namespace Luax
bool LuaxState::GetFieldWithType(int idx, int key, int type)
{
this->GetField(idx, key);
- if (lua_type(L, -1) != type) {
- lua_pop(L, 1);
+ if (lua_type(mState, -1) != type) {
+ lua_pop(mState, 1);
return false;
}
return true;
@@ -363,7 +339,7 @@ namespace Luax
if (IsTableOrUserdata(idx))
{
idx = AbsIndex(idx);
- lua_setfield(L, idx, key);
+ lua_setfield(mState, idx, key);
}
}
@@ -631,4 +607,43 @@ namespace Luax
(*handle) = ptr;
}
+ void LuaxState::RegisterEnum(cc8* name, LuaxEnum* en)
+ {
+ assert(name);
+ assert(en);
+
+ // short name
+ lua_State* L = mState;
+
+ int top = GetTop();
+
+ lua_newtable(L); // enum table
+
+ int et = GetTop();
+
+ lua_newtable(L); // matatable
+
+ // öٶmetatable£޸ʱ__newindex
+ for (; en->name; ++en)
+ {
+ lua_pushinteger(L, en->value);
+ lua_setfield(L, -2, en->name);
+ }
+
+ // __index
+ //lua_pushvalue(L, -1); // metatable
+ //lua_pushcclosure(L, l_rmt__index, 1);
+ lua_pushvalue(L, -1);
+ lua_setfield(L, -2, "__index");
+
+ // __newinedx
+ lua_pushstring(L, name); // enum name
+ lua_pushcclosure(L, l_rmt__newindex, 1);
+ lua_setfield(L, -2, "__newindex");
+
+ lua_setmetatable(L, et);
+
+ lua_setfield(L, top, name);
+ }
+
} \ No newline at end of file