blob: 549e20b4d1be449a44cdec312ebda7ee54f06542 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
|
#include "luax_vm.h"
namespace Luax
{
LuaxVM::ThreadMap threadMap; // ̲ͨ߳Ϊ˷
LuaxVM* LuaxVM::TryGetVM(lua_State* L)
{
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()
{
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();
}
int LuaxVM::GetThreadCount()
{
return mThreads.size();
}
}
|