summaryrefslogtreecommitdiff
path: root/source/3rd-party/Luax/luax_runtime.cpp
blob: dcf7b92e2eecca4e079f5203e5dcec4d86afafa4 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#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