summaryrefslogtreecommitdiff
path: root/source/3rd-party/Luax
diff options
context:
space:
mode:
Diffstat (limited to 'source/3rd-party/Luax')
-rw-r--r--source/3rd-party/Luax/luax_class.cpp6
-rw-r--r--source/3rd-party/Luax/luax_class.hpp6
-rw-r--r--source/3rd-party/Luax/luax_class.inl44
-rw-r--r--source/3rd-party/Luax/luax_config.h6
-rw-r--r--source/3rd-party/Luax/luax_globalstate.h7
-rw-r--r--source/3rd-party/Luax/luax_internal.h12
-rw-r--r--source/3rd-party/Luax/luax_ref.cpp6
-rw-r--r--source/3rd-party/Luax/luax_state.cpp16
-rw-r--r--source/3rd-party/Luax/luax_state.h8
-rw-r--r--source/3rd-party/Luax/luax_vm.cpp32
-rw-r--r--source/3rd-party/Luax/luax_vm.h34
11 files changed, 117 insertions, 60 deletions
diff --git a/source/3rd-party/Luax/luax_class.cpp b/source/3rd-party/Luax/luax_class.cpp
index 960fd47..bb1609d 100644
--- a/source/3rd-party/Luax/luax_class.cpp
+++ b/source/3rd-party/Luax/luax_class.cpp
@@ -46,13 +46,13 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, l___tostring, 1);
+ lua_pushcclosure(L, _Tostring, 1);
lua_setfield(L, -2, "__tostring");
return 1;
}
- int LuaxPlainClass::l___tostring(lua_State* L)
+ int LuaxPlainClass::_Tostring(lua_State* L)
{
// upvalues:
// 1: class name
@@ -184,7 +184,7 @@ namespace Luax
lua_setfield(L, -2, "__index");
lua_pushstring(L, type);
- lua_pushcclosure(L, l___tostring, 1);
+ lua_pushcclosure(L, _Tostring, 1);
lua_setfield(L, -2, "__tostring");
// classmetatableΪbaseClass
diff --git a/source/3rd-party/Luax/luax_class.hpp b/source/3rd-party/Luax/luax_class.hpp
index 99ac381..257e1ca 100644
--- a/source/3rd-party/Luax/luax_class.hpp
+++ b/source/3rd-party/Luax/luax_class.hpp
@@ -180,12 +180,12 @@ namespace Luax
public:
//
- LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_GetClass );
LUAX_DECL_METHOD( l_GetClassName );
//
- LUAX_DECL_METHOD( l___gc );
+ LUAX_DECL_METHOD( _GC );
#if LUAX_ENABLE_NATIVE_EXTEND
LUAX_DECL_METHOD( l_ExtendFactory );
#endif
@@ -213,7 +213,7 @@ namespace Luax
///
static int registry(lua_State* L);
- LUAX_DECL_METHOD( l___tostring );
+ LUAX_DECL_METHOD( _Tostring );
LUAX_DECL_METHOD( l_Extend );
LUAX_DECL_METHOD( l_New );
LUAX_DECL_METHOD( l_TypeOf );
diff --git a/source/3rd-party/Luax/luax_class.inl b/source/3rd-party/Luax/luax_class.inl
index d1c8c4b..45e6552 100644
--- a/source/3rd-party/Luax/luax_class.inl
+++ b/source/3rd-party/Luax/luax_class.inl
@@ -116,7 +116,12 @@ namespace Luax
u32 count = state.GetValue<u32>(-1, 0); // get the count (or 0)
lua_pop(state, 1); // pop the old count
- if (count == 0) return; // nothing to do
+ // no such reference
+ if (count == 0)
+ {
+ state.Pop(2); // userdata, reftable
+ return; // nothing to do
+ }
if (count > 1) {
lua_pushnumber(state, count - 1); // push the new count
@@ -125,7 +130,12 @@ namespace Luax
lua_pushnil(state); // maybe cause gc
}
lua_settable(state, -3); // save it in the table
+
+ state.Pop(1); // reftable
+ return;
}
+ state.Pop(2); // nil, reftable
+ return;
}
}
@@ -194,9 +204,10 @@ namespace Luax
///
/// userdataԴref tablemember tableclass table
- /// ref table kvǿtableuserdataüͨuserdataΪkeyΪvalueԼԱ
+ /// ref table kvǿtableuserdataüͨuserdataΪkey
+ /// ΪvalueԼԱ
/// member table luaʵijԱ
- /// class table б͵ʵеĺ
+ /// class table б͵ʵеĺ
///
template<typename T>
void LuaxNativeClass<T>::BindToLua(LuaxState& state)
@@ -205,8 +216,13 @@ namespace Luax
assert(!T::IsLuaxClassSingleton());
assert(!mUserdata);
- // userdataջעַҪתΪT*ֱthisܻᵼ¶ؼ̳еɥʧ̬
- state.PushPtrUserdata(static_cast<T*>(this));
+ ///
+ /// userdataջעַҪתΪT*ֱthisܻᵼ¶ؼ̳еɥʧ̬
+ /// ֱӴthisȥڶؼ̳£òһͷ麯ġҪthis
+ /// תΪĵ͵ַõһ麯ͨһʵֶ̬
+ ///
+ T* p = static_cast<T*>(this);
+ state.PushPtrUserdata(p);
lua_newtable(state); // ref table޷luaʣC
lua_newtable(state); // member tableluaдĶԱ
@@ -222,10 +238,13 @@ namespace Luax
int memberTable = top - 1;
int refTable = top - 2;
- // ref table ע __tostring
- lua_pushcfunction(state, l___tostring);
+ // ref table ע __tostring __gc
+ lua_pushcfunction(state, _Tostring);
lua_setfield(state, refTable, "__tostring");
+ lua_pushcfunction(state, _GC);
+ lua_setfield(state, refTable, "__gc");
+
// ref table __index __newindex Ϊ member table
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__index");
@@ -233,9 +252,6 @@ namespace Luax
lua_pushvalue(state, memberTable);
lua_setfield(state, refTable, "__newindex");
- lua_pushcfunction(state, l___gc);
- lua_setfield(state, refTable, "__gc");
-
// Ԫ
lua_setmetatable(state, -2); // class is meta of member
lua_setmetatable(state, -2); // member is meta of ref
@@ -321,8 +337,12 @@ namespace Luax
/// ͷŹʵ
///
template<typename T>
- int LuaxNativeClass<T>::l___gc(lua_State* L)
+ int LuaxNativeClass<T>::_GC(lua_State* L)
{
+#if LUAX_PROFILER
+ std::cout << "Luax: GC<" << T::GetLuaxClassName() << ">\n";
+#endif
+
LUAX_SETUP(L, "U");
T* self = state.GetUserdata<T>(1);
delete self;
@@ -334,7 +354,7 @@ namespace Luax
/// ַ
///
template<typename T>
- int LuaxNativeClass<T>::l___tostring(lua_State* L)
+ int LuaxNativeClass<T>::_Tostring(lua_State* L)
{
// params:
// 1: userdata
diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h
index c251c6f..2a8ed3e 100644
--- a/source/3rd-party/Luax/luax_config.h
+++ b/source/3rd-party/Luax/luax_config.h
@@ -55,7 +55,11 @@ namespace Luax
#define LUAX_ENABLE_PLAIN_CLASS 0
#define LUAX_ENABLE_PLAIN_ENUM 0
-#define LUAX_PROFILER 0
+#define LUAX_PROFILER 1
+
+#if LUAX_PROFILER
+#include <iostream>
+#endif
}
diff --git a/source/3rd-party/Luax/luax_globalstate.h b/source/3rd-party/Luax/luax_globalstate.h
new file mode 100644
index 0000000..91be51f
--- /dev/null
+++ b/source/3rd-party/Luax/luax_globalstate.h
@@ -0,0 +1,7 @@
+#ifndef __LUAX_GLOBAL_STATE_H__
+#define __LUAX_GLOBAL_STATE_H__
+
+// luaglobal_State
+typedef struct global_State global_State;
+
+#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_internal.h b/source/3rd-party/Luax/luax_internal.h
new file mode 100644
index 0000000..5904008
--- /dev/null
+++ b/source/3rd-party/Luax/luax_internal.h
@@ -0,0 +1,12 @@
+#ifndef __LUAX_INTERNAL_H__
+#define __LUAX_INTERNAL_H__
+
+///
+/// luaԴʹ
+///
+extern "C"
+{
+#include "lua51/lstate.h"
+}
+
+#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_ref.cpp b/source/3rd-party/Luax/luax_ref.cpp
index 78596e3..d4be775 100644
--- a/source/3rd-party/Luax/luax_ref.cpp
+++ b/source/3rd-party/Luax/luax_ref.cpp
@@ -23,7 +23,7 @@ namespace Luax
{
assert(mRefID != LUA_NOREF);
- LuaxVM* vm = LuaxVM::TryGetVM(state);
+ LuaxVM* vm = state.GetVM();
if (!vm) return false;
if (mMode == STRONG_REF)
{
@@ -37,13 +37,15 @@ namespace Luax
}
else
{
+ state.PushNil();
return false;
}
+ return true;
}
void LuaxRef::SetRef(LuaxState& state, int idx)
{
- LuaxVM* vm = LuaxVM::TryGetVM(state);
+ LuaxVM* vm = state.GetVM();
if (!vm) return;
if (mMode == STRONG_REF)
{
diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp
index b4d989a..3eae1df 100644
--- a/source/3rd-party/Luax/luax_state.cpp
+++ b/source/3rd-party/Luax/luax_state.cpp
@@ -1,8 +1,8 @@
-//#include "luax_class.h"
#include "luax_enum.h"
#include "luax_state.h"
#include "luax_vm.h"
#include "luax_class.hpp"
+#include "luax_internal.h"
namespace Luax
{
@@ -28,6 +28,16 @@ namespace Luax
luaL_openlibs(mState);
}
+ global_State* LuaxState::GetGlobalState()
+ {
+ return G(mState);
+ }
+
+ LuaxVM* LuaxState::GetVM()
+ {
+ return LuaxVM::TryGetVM(G(mState));
+ }
+
void LuaxState::PushGlobalNamespace()
{
int top = GetTop();
@@ -642,8 +652,8 @@ namespace Luax
return value;
}
- void LuaxState::PushPtrUserdata(void* ptr) {
-
+ void LuaxState::PushPtrUserdata(void* ptr)
+ {
void** handle = (void**)lua_newuserdata(this->mState, sizeof(void*));
assert(handle);
(*handle) = ptr;
diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h
index dc3b79f..7c7d813 100644
--- a/source/3rd-party/Luax/luax_state.h
+++ b/source/3rd-party/Luax/luax_state.h
@@ -5,6 +5,7 @@
#include "luax_config.h"
#include "luax_reftable.h"
+#include "luax_globalstate.h"
namespace Luax
{
@@ -41,6 +42,10 @@ namespace Luax
///
inline lua_State* GetHandle() { return mState; };
+ global_State* GetGlobalState();
+
+ LuaxVM* GetVM();
+
//------------------------------------------------------------------------------//
void OpenLibs();
@@ -206,10 +211,7 @@ namespace Luax
void* operator &();
void* operator new(size_t size);
- //------------------------------------------------------------------------------//
-
lua_State* const mState;
-
};
//--------------------------------------------------------------------------------//
diff --git a/source/3rd-party/Luax/luax_vm.cpp b/source/3rd-party/Luax/luax_vm.cpp
index 549e20b..5e9d1f5 100644
--- a/source/3rd-party/Luax/luax_vm.cpp
+++ b/source/3rd-party/Luax/luax_vm.cpp
@@ -1,32 +1,39 @@
+#include "luax_internal.h"
#include "luax_vm.h"
namespace Luax
{
- LuaxVM::ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+ LuaxVM::VMap LuaxVM::VMs; // ̲ͨ߳Ϊ˷
- LuaxVM* LuaxVM::TryGetVM(lua_State* L)
+ LuaxVM* LuaxVM::TryGetVM(global_State* gState)
{
- auto it = threadMap.find(L);
- if (it != threadMap.end())
+ auto it = VMs.find(gState);
+ if (it != VMs.end())
return it->second;
else
return nullptr;
}
+ LuaxVM* LuaxVM::TryGetVM(lua_State* state)
+ {
+ return TryGetVM(G(state));
+ }
+
LuaxVM::LuaxVM()
: mStrongRefTable()
, mWeakRefTable()
{
mMainThread = luaL_newstate();
assert(mMainThread);
- mThreads.insert(mMainThread);
+ mGlobalState = G(mMainThread);
- threadMap.insert(std::pair<lua_State*, LuaxVM*>(mMainThread, this));
+ VMs.insert(std::pair<global_State*, LuaxVM*>(mGlobalState, this));
}
LuaxVM::~LuaxVM()
{
+ VMs.erase(mGlobalState);
lua_close(mMainThread);
}
@@ -34,7 +41,7 @@ namespace Luax
void LuaxVM::Setup()
{
LUAX_STATE(mMainThread);
- // ȫñ
+
mStrongRefTable.Init(state, "_LUAX_STRONGREF_TABLE");
mWeakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v");
}
@@ -43,7 +50,6 @@ namespace Luax
{
lua_State* thread = lua_newthread(mMainThread);
assert(thread);
- mThreads.insert(thread);
return thread;
}
@@ -62,14 +68,4 @@ namespace Luax
return mWeakRefTable;
}
- bool LuaxVM::HasThread(lua_State* L)
- {
- return mThreads.find(L) != mThreads.end();
- }
-
- int LuaxVM::GetThreadCount()
- {
- return mThreads.size();
- }
-
} \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_vm.h b/source/3rd-party/Luax/luax_vm.h
index 9039a04..1bac71e 100644
--- a/source/3rd-party/Luax/luax_vm.h
+++ b/source/3rd-party/Luax/luax_vm.h
@@ -7,6 +7,7 @@
#include "luax_ref.h"
#include "luax_config.h"
#include "luax_state.h"
+#include "luax_globalstate.h"
namespace Luax
{
@@ -17,38 +18,41 @@ namespace Luax
class LuaxVM
{
public:
+
+ ///
+ /// global_Stateõ
+ ///
+ static LuaxVM* TryGetVM(global_State* gState);
+ static LuaxVM* TryGetVM(lua_State* state);
+
LuaxVM();
~LuaxVM();
+ ///
+ /// ҪֶSetupʼһЩ״̬
+ ///
void Setup();
- lua_State* CreateThread();
lua_State* GetMainThread();
-
- int GetThreadCount();
+ lua_State* CreateThread();
LuaxRefTable& GetStrongRefTable();
LuaxRefTable& GetWeakRefTable();
- bool HasThread(lua_State* L);
-
- static LuaxVM* TryGetVM(lua_State* L);
-
- typedef std::map<lua_State*, LuaxVM*> ThreadMap;
-
private:
- static ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+ typedef std::map<global_State*, LuaxVM*> VMap;
- LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
- LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
+ static VMap VMs; // ͨglobal_StateΪ˷
- lua_State* mMainThread; // ߳
+ LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
+ LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
- std::unordered_set<lua_State*> mThreads; // ߳
+ global_State* mGlobalState; // global_Stateɵǰ̹߳
+ lua_State* mMainThread; // ߳
#if LUAX_PROFILER
- size_t mObjectCount; // ͳڴдʵ
+ size_t mObjectCount; // ͳڴдʵ
#endif
};