summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2019-03-29 08:56:55 +0800
committerchai <chaifix@163.com>2019-03-29 08:56:55 +0800
commit62085e1b49ce2d8a630373e410812d5c4a9eecc2 (patch)
tree5b221e36ff35348aaedc041a2a0513f1d0390ecf
parentf4c338c63f3456a8eccd56c35e233843687d55be (diff)
*luax
-rw-r--r--source/3rd-party/Luax/luax.h2
-rw-r--r--source/3rd-party/Luax/luax_class.cpp2
-rw-r--r--source/3rd-party/Luax/luax_config.h2
-rw-r--r--source/3rd-party/Luax/luax_enum.cpp2
-rw-r--r--source/3rd-party/Luax/luax_ref.cpp17
-rw-r--r--source/3rd-party/Luax/luax_runtime.cpp97
-rw-r--r--source/3rd-party/Luax/luax_runtime.h64
-rw-r--r--source/3rd-party/Luax/luax_state.cpp2
-rw-r--r--source/3rd-party/Luax/luax_state.h17
-rw-r--r--source/3rd-party/Luax/luax_vm.cpp62
-rw-r--r--source/3rd-party/Luax/luax_vm.h34
11 files changed, 104 insertions, 197 deletions
diff --git a/source/3rd-party/Luax/luax.h b/source/3rd-party/Luax/luax.h
index d05c258..45fa514 100644
--- a/source/3rd-party/Luax/luax.h
+++ b/source/3rd-party/Luax/luax.h
@@ -2,7 +2,7 @@
#define __LUAX_H__
#include "luax_state.h"
-#include "luax_runtime.h"
+#include "luax_vm.h"
#include "luax_ref.h"
#include "luax_reftable.h"
#include "luax_enum.h"
diff --git a/source/3rd-party/Luax/luax_class.cpp b/source/3rd-party/Luax/luax_class.cpp
index 9f316c1..960fd47 100644
--- a/source/3rd-party/Luax/luax_class.cpp
+++ b/source/3rd-party/Luax/luax_class.cpp
@@ -1,5 +1,5 @@
#include "luax_class.hpp"
-#include "luax_runtime.h"
+#include "luax_vm.h"
#include "luax_cfunctions.h"
namespace Luax
diff --git a/source/3rd-party/Luax/luax_config.h b/source/3rd-party/Luax/luax_config.h
index 0ace00b..c251c6f 100644
--- a/source/3rd-party/Luax/luax_config.h
+++ b/source/3rd-party/Luax/luax_config.h
@@ -55,6 +55,8 @@ namespace Luax
#define LUAX_ENABLE_PLAIN_CLASS 0
#define LUAX_ENABLE_PLAIN_ENUM 0
+#define LUAX_PROFILER 0
+
}
#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_enum.cpp b/source/3rd-party/Luax/luax_enum.cpp
index 60dd552..59d8161 100644
--- a/source/3rd-party/Luax/luax_enum.cpp
+++ b/source/3rd-party/Luax/luax_enum.cpp
@@ -1,6 +1,6 @@
#include "luax_enum.h"
#include "luax_state.h"
-#include "luax_runtime.h"
+#include "luax_vm.h"
namespace Luax
{
diff --git a/source/3rd-party/Luax/luax_ref.cpp b/source/3rd-party/Luax/luax_ref.cpp
index 544861d..78596e3 100644
--- a/source/3rd-party/Luax/luax_ref.cpp
+++ b/source/3rd-party/Luax/luax_ref.cpp
@@ -1,4 +1,4 @@
-#include "luax_runtime.h"
+#include "luax_vm.h"
#include "luax_ref.h"
namespace Luax
@@ -23,16 +23,16 @@ namespace Luax
{
assert(mRefID != LUA_NOREF);
- LuaxRuntime& runtime = LuaxRuntime::Get();
-
+ LuaxVM* vm = LuaxVM::TryGetVM(state);
+ if (!vm) return false;
if (mMode == STRONG_REF)
{
- LuaxRefTable& table = runtime[state.GetHandle()].strongRefTable;
+ LuaxRefTable& table = vm->GetStrongRefTable();
table.PushRef(state, mRefID);
}
else if (mMode == WEAK_REF)
{
- LuaxRefTable& table = runtime[state.GetHandle()].weakRefTable;
+ LuaxRefTable& table = vm->GetWeakRefTable();
table.PushRef(state, mRefID);
}
else
@@ -43,15 +43,16 @@ namespace Luax
void LuaxRef::SetRef(LuaxState& state, int idx)
{
- LuaxRuntime& runtime = LuaxRuntime::Get();
+ LuaxVM* vm = LuaxVM::TryGetVM(state);
+ if (!vm) return;
if (mMode == STRONG_REF)
{
- LuaxRefTable& table = runtime[state.GetHandle()].strongRefTable;
+ LuaxRefTable& table = vm->GetStrongRefTable();
mRefID = table.Ref(state, idx);
}
else if (mMode == WEAK_REF)
{
- LuaxRefTable& table = runtime[state.GetHandle()].weakRefTable;
+ LuaxRefTable& table = vm->GetWeakRefTable();
mRefID = table.Ref(state, idx);
}
}
diff --git a/source/3rd-party/Luax/luax_runtime.cpp b/source/3rd-party/Luax/luax_runtime.cpp
deleted file mode 100644
index dcf7b92..0000000
--- a/source/3rd-party/Luax/luax_runtime.cpp
+++ /dev/null
@@ -1,97 +0,0 @@
-#ifndef __LUAX_RUNTIME_H_
-#define __LUAX_RUNTIME_H_
-
-#include "luax_runtime.h"
-
-using namespace std;
-
-namespace Luax
-{
-
- LuaxRuntime* LuaxRuntime::mRuntime = nullptr;
-
- LuaxRuntime::LuaxRuntime() {};
- LuaxRuntime::~LuaxRuntime() {};
-
- LuaxRuntime& LuaxRuntime::Get()
- {
- if (mRuntime == nullptr)
- mRuntime = new LuaxRuntime();
-
- return *mRuntime;
- }
-
- lua_State* LuaxRuntime::Open()
- {
- lua_State* L = lua_open();
- assert(L);
-
- // 1)
- mContexts.insert(pair<lua_State*, LuaxVM*>(L, new LuaxVM(L)));
- // 2) ʼcontext
- (*this)[L].Setup();
-
- return L;
- }
-/*
- lua_State* LuaxRuntime::CreateThread(lua_State* main)
- {
- lua_State* thread = lua_newthread(main);
- mContexts.insert(pair<lua_State*, LuaxVM*>(thread, mContexts[main]));
- return thread;
- }
-*/
- void LuaxRuntime::Close(lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- if (it != mContexts.end())
- {
- lua_close(it->second->state);
- mContexts.erase(it);
- }
- }
-
- bool LuaxRuntime::HasLuaxState(lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- return it != mContexts.end();
- }
-
- LuaxState& LuaxRuntime::GetLuaxState(lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- if (it != mContexts.end())
- {
- return it->second->state;
- }
- }
-
- LuaxRefTable& LuaxRuntime::GetStrongRefTable(lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- if (it != mContexts.end())
- {
- return it->second->strongRefTable;
- }
- }
-
- LuaxRefTable& LuaxRuntime::GetWeaksRefTable(lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- if (it != mContexts.end())
- {
- return it->second->weakRefTable;
- }
- }
-
- LuaxVM& LuaxRuntime::operator[](lua_State* L)
- {
- map<lua_State*, LuaxVM*>::iterator it = mContexts.find(L);
- assert(it != mContexts.end());
-
- return *it->second;
- }
-
-}
-
-#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_runtime.h b/source/3rd-party/Luax/luax_runtime.h
deleted file mode 100644
index 27c9af4..0000000
--- a/source/3rd-party/Luax/luax_runtime.h
+++ /dev/null
@@ -1,64 +0,0 @@
-#ifndef __LUAX_RUNTIME_H__
-#define __LUAX_RUNTIME_H__
-
-#include <map>
-
-#include "luax_ref.h"
-#include "luax_config.h"
-#include "luax_state.h"
-#include "luax_vm.h"
-
-namespace Luax
-{
-
- ///
- /// ͳһеlua states
- ///
- class LuaxRuntime
- {
- public:
-
- static LuaxRuntime& Get();
-
- ///
- /// һµluaʹ̲߳صlua_State*һ8\4ֽڵkey
- ///
- lua_State* Open();
-/*
- ///
- /// ̴߳һ̡߳ӵcontextӳ䡣ҪΪ˷context߳ʡ
- ///
- lua_State* CreateThread(lua_State* mainThread);
-*/
- ///
- /// رlua_Stateruntimeɾ
- ///
- void Close(lua_State* L);
-
- bool HasLuaxState(lua_State* L);
- LuaxState& GetLuaxState(lua_State* L);
- LuaxRefTable& GetStrongRefTable(lua_State* L);
- LuaxRefTable& GetWeaksRefTable(lua_State* L);
-
- LuaxVM& operator[](lua_State* L);
-
- private:
-
- LuaxRuntime();
- ~LuaxRuntime();
-
- static LuaxRuntime* mRuntime;
-
- ///
- /// lua_State handlecontextӳ
- ///
- std::map<lua_State*, LuaxVM*> mContexts;
-
- };
-
-#define LUAX_RUNTIME() \
- LuaxRuntime& runtime = LuaxRuntime::Get()
-
-}
-
-#endif \ No newline at end of file
diff --git a/source/3rd-party/Luax/luax_state.cpp b/source/3rd-party/Luax/luax_state.cpp
index f453be7..b4d989a 100644
--- a/source/3rd-party/Luax/luax_state.cpp
+++ b/source/3rd-party/Luax/luax_state.cpp
@@ -1,7 +1,7 @@
//#include "luax_class.h"
#include "luax_enum.h"
#include "luax_state.h"
-#include "luax_runtime.h"
+#include "luax_vm.h"
#include "luax_class.hpp"
namespace Luax
diff --git a/source/3rd-party/Luax/luax_state.h b/source/3rd-party/Luax/luax_state.h
index b6d1ff0..dc3b79f 100644
--- a/source/3rd-party/Luax/luax_state.h
+++ b/source/3rd-party/Luax/luax_state.h
@@ -27,6 +27,10 @@ namespace Luax
{
public:
+ LuaxState(lua_State* state);
+ LuaxState(const LuaxState& state);
+ virtual ~LuaxState();
+
inline lua_State* operator ->() { return mState; };
inline lua_State& operator *() { return *mState; };
inline operator lua_State*() { return mState; }
@@ -192,22 +196,18 @@ namespace Luax
void RegisterPlainEnumRegistry(cc8* name);
#endif
- //------------------------------------------------------------------------------//
-
protected:
friend class LuaxVM;
- LuaxState(lua_State* state);
- LuaxState(const LuaxState& state);
- virtual ~LuaxState();
-
///
/// ζLuaxStateĵַز
///
void* operator &();
void* operator new(size_t size);
+ //------------------------------------------------------------------------------//
+
lua_State* const mState;
};
@@ -252,12 +252,11 @@ namespace Luax
/// ڳԱﴴLuaxStateԲм顣
///
#define LUAX_SETUP(L, params) \
- LuaxRuntime& runtime = LuaxRuntime::Get(); \
- LuaxState& state = runtime[L].state; \
+ Luax::LuaxState state(L); \
if(!state.CheckParams(1, params)) return 0
#define LUAX_STATE(L) \
- Luax::LuaxState& state = Luax::LuaxRuntime::Get().GetLuaxState(L)
+ Luax::LuaxState state(L)
//--------------------------------------------------------------------------------//
diff --git a/source/3rd-party/Luax/luax_vm.cpp b/source/3rd-party/Luax/luax_vm.cpp
index 4257b4d..549e20b 100644
--- a/source/3rd-party/Luax/luax_vm.cpp
+++ b/source/3rd-party/Luax/luax_vm.cpp
@@ -3,27 +3,73 @@
namespace Luax
{
- LuaxVM::LuaxVM(lua_State* L)
- : state(L)
+ LuaxVM::ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+
+ LuaxVM* LuaxVM::TryGetVM(lua_State* L)
{
- assert(state);
+ auto it = threadMap.find(L);
+ if (it != threadMap.end())
+ return it->second;
+ else
+ return nullptr;
+ }
+
+ LuaxVM::LuaxVM()
+ : mStrongRefTable()
+ , mWeakRefTable()
+ {
+ mMainThread = luaL_newstate();
+ assert(mMainThread);
+ mThreads.insert(mMainThread);
+
+ threadMap.insert(std::pair<lua_State*, LuaxVM*>(mMainThread, this));
}
LuaxVM::~LuaxVM()
{
+ lua_close(mMainThread);
}
// ʼcontext
void LuaxVM::Setup()
{
- SetupRefTables();
+ LUAX_STATE(mMainThread);
+ // ȫñ
+ mStrongRefTable.Init(state, "_LUAX_STRONGREF_TABLE");
+ mWeakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v");
+ }
+
+ lua_State* LuaxVM::CreateThread()
+ {
+ lua_State* thread = lua_newthread(mMainThread);
+ assert(thread);
+ mThreads.insert(thread);
+ return thread;
+ }
+
+ lua_State* LuaxVM::GetMainThread()
+ {
+ return mMainThread;
+ }
+
+ LuaxRefTable& LuaxVM::GetStrongRefTable()
+ {
+ return mStrongRefTable;
+ }
+
+ LuaxRefTable& LuaxVM::GetWeakRefTable()
+ {
+ return mWeakRefTable;
+ }
+
+ bool LuaxVM::HasThread(lua_State* L)
+ {
+ return mThreads.find(L) != mThreads.end();
}
- void LuaxVM::SetupRefTables()
+ int LuaxVM::GetThreadCount()
{
- // strong ref weak ref
- strongRefTable.Init(state, "_LUAX_STRONGREF_TABLE");
- weakRefTable.Init(state, "_LUAX_WEAKREF_TABLE", "v");
+ 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 e99d5b5..9039a04 100644
--- a/source/3rd-party/Luax/luax_vm.h
+++ b/source/3rd-party/Luax/luax_vm.h
@@ -1,6 +1,9 @@
#ifndef __LUAX_CONTEXT_H__
#define __LUAX_CONTEXT_H__
+#include <map>
+#include <unordered_set>
+
#include "luax_ref.h"
#include "luax_config.h"
#include "luax_state.h"
@@ -14,22 +17,39 @@ namespace Luax
class LuaxVM
{
public:
- LuaxVM(lua_State* L);
+ LuaxVM();
~LuaxVM();
void Setup();
- LuaxState state; // lua main state
- LuaxRefTable strongRefTable; // strong ref table
- LuaxRefTable weakRefTable; // weak ref table
+ lua_State* CreateThread();
+ lua_State* GetMainThread();
+
+ int GetThreadCount();
+
+ LuaxRefTable& GetStrongRefTable();
+ LuaxRefTable& GetWeakRefTable();
+
+ bool HasThread(lua_State* L);
- size_t objectCount; // ͳڴstateдʵ
+ static LuaxVM* TryGetVM(lua_State* L);
+
+ typedef std::map<lua_State*, LuaxVM*> ThreadMap;
private:
- void SetupRefTables();
+ static ThreadMap threadMap; // ̲ͨ߳Ϊ˷
+
+ LuaxRefTable mStrongRefTable; // _LUAX_STRONGREF_TABLE
+ LuaxRefTable mWeakRefTable; // _LUAX_WEAKREF_TABLE
+
+ lua_State* mMainThread; // ߳
+
+ std::unordered_set<lua_State*> mThreads; // ߳
- int mThreadsCount; // context߳
+#if LUAX_PROFILER
+ size_t mObjectCount; // ͳڴдʵ
+#endif
};