File: LuaUtil.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-3
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,740 kB
  • sloc: cpp: 595,005; ansic: 21,741; python: 1,174; sh: 457; makefile: 243; xml: 181
file content (66 lines) | stat: -rw-r--r-- 1,343 bytes parent folder | download | duplicates (2)
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

#include "LuaUtil.h"

namespace {
const char* mainStateRefName = "_lua_mainthread";
}

namespace luacpp {
namespace util {
const char* getValueName(ValueType type)
{
	switch (type) {
	case ValueType::NONE:
		return "none";
	case ValueType::NIL:
		return "nil";
	case ValueType::BOOLEAN:
		return "boolean";
		case ValueType::LIGHTUSERDATA:
			return "light userdata";
		case ValueType::STRING:
			return "string";
		case ValueType::NUMBER:
			return "number";
		case ValueType::TABLE:
			return "table";
		case ValueType::FUNCTION:
			return "function";
		case ValueType::USERDATA:
			return "userdata";
		case ValueType::THREAD:
			return "thread";
		default:
			return "unknown";
		}
}

void initializeLuaSupportLib(lua_State* L)
{
	auto mainThread = lua_pushthread(L);
	Assertion(mainThread, "Must be called with the main thread as the parameter!");

	lua_setfield(L, LUA_REGISTRYINDEX, mainStateRefName);
}

lua_State* getMainThread(lua_State* L)
{
	if (lua_pushthread(L)) {
		// State is the main thread, just return that
		lua_pop(L, 1);
		return L;
	}

	lua_pushstring(L, mainStateRefName);
	lua_rawget(L, LUA_REGISTRYINDEX);

	auto state = lua_tothread(L, -1);

	// Need to pop both the thread from L and the main thread to return the stack back to neutral
	lua_pop(L, 2);

	return state;
}

} // namespace util
} // namespace luacpp