summaryrefslogtreecommitdiff
path: root/source/external/Luax/luax_vm.cpp
blob: 4dc7e0c15350ea277787188323d485a932a955c9 (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
76
#include "luax_internal.h"
#include "luax_vm.h"

namespace Luax
{

	LuaxVM::VMap LuaxVM::VMs;   // ̲ͨ߳Ϊ˷

	LuaxVM* LuaxVM::TryGetVM(global_State* gState)
	{
		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);
		mGlobalState = G(mMainThread);

		VMs.insert(std::pair<global_State*, LuaxVM*>(mGlobalState, this));
	}

	LuaxVM::~LuaxVM()
	{
		VMs.erase(mGlobalState);
		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);
		return thread;
	}

	lua_State* LuaxVM::GetMainThread()
	{
		return mMainThread;
	}

	LuaxState LuaxVM::GetMainState()
	{
		return mMainThread;
	}

	LuaxRefTable& LuaxVM::GetStrongRefTable()
	{
		return mStrongRefTable;
	}

	LuaxRefTable& LuaxVM::GetWeakRefTable()
	{
		return mWeakRefTable;
	}

}