File: LuaConvert.cpp

package info (click to toggle)
freespace2 24.2.0%2Brepack-1
  • links: PTS, VCS
  • area: non-free
  • in suites: forky, sid
  • size: 43,716 kB
  • sloc: cpp: 595,001; ansic: 21,741; python: 1,174; sh: 457; makefile: 248; xml: 181
file content (186 lines) | stat: -rw-r--r-- 4,477 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
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186

#include "LuaConvert.h"

namespace luacpp {
namespace convert {
namespace internal {

bool isValidIndex(lua_State* state, int index) {
	if (1 <= std::abs(index) && std::abs(index) <= lua_gettop(state)) {
		return true;
	} else {
		return false;
	}
}

bool ade_odata_helper(lua_State* L, int stackposition, size_t idx) {
	if (!ade_odata_is_userdata_type(L, stackposition, idx, false)) {
		// Issue the LuaError here since this is the only place where we have all relevant information
		LuaError(L, "Argument %d is the wrong type of userdata; '%s' given, but '%s' expected", stackposition,
			::scripting::internal::getTableEntry((size_t)lua_tointeger(L, -2)).Name,
			::scripting::internal::getTableEntry(idx).GetName());
		return false;
	}
	else {
		return true;
	}
}

}

bool ade_odata_is_userdata_type(lua_State* L, int stackposition, size_t typeIdx, bool cleanup) {
	// it needs to be userdata before we narrow down the specific type of userdata
	if (lua_type(L, stackposition) != LUA_TUSERDATA) {
		return false;
	}

	// WMC - Get metatable
	lua_getmetatable(L, stackposition);
	int mtb_ldx = lua_gettop(L);
	Assert(!lua_isnil(L, -1));

	// Get ID
	lua_pushstring(L, "__adeid");
	lua_rawget(L, mtb_ldx);

	if (lua_tonumber(L, -1) != typeIdx) {
		lua_pushstring(L, "__adederivid");
		lua_rawget(L, mtb_ldx);
		if ((size_t)lua_tointeger(L, -1) != typeIdx) {
			if(cleanup)
				lua_pop(L, 3);
			return false;
		}
		lua_pop(L, 1);
	}
	lua_pop(L, 2);
	return true;
}

void pushValue(lua_State* luaState, const double& value) {
	lua_pushnumber(luaState, value);
}
void pushValue(lua_State* luaState, const float& value) {
	lua_pushnumber(luaState, value);
}
void pushValue(lua_State* luaState, const int& value) {
	lua_pushnumber(luaState, value);
}
void pushValue(lua_State* luaState, const size_t& value) {
	lua_pushnumber(luaState, (lua_Number)value);
}
void pushValue(lua_State* luaState, const std::string& value) {
	lua_pushlstring(luaState, value.c_str(), value.size());
}
void pushValue(lua_State* luaState, const char* value) {
	lua_pushstring(luaState, value);
}
void pushValue(lua_State* luaState, const bool& value) {
	lua_pushboolean(luaState, value);
}
void pushValue(lua_State* luaState, const lua_CFunction& value) {
	lua_pushcfunction(luaState, value);
}

bool popValue(lua_State* luaState, int& target, int stackposition, bool remove) {
	double temp;

	auto ret = popValue(luaState, temp, stackposition, remove);
	if (ret) {
		target = (int)temp;
	}
	return ret;
}
bool popValue(lua_State* luaState, size_t& target, int stackposition, bool remove) {
	double temp;

	auto ret = popValue(luaState, temp, stackposition, remove);
	if (ret) {
		target = (size_t)temp;
	}
	return ret;
}
bool popValue(lua_State* luaState, float& target, int stackposition, bool remove) {
	double temp;

	auto ret = popValue(luaState, temp, stackposition, remove);
	if (ret) {
		target = (float)temp;
	}
	return ret;
}
bool popValue(lua_State* luaState, double& target, int stackposition, bool remove) {
	if (!internal::isValidIndex(luaState, stackposition)) {
		return false;
	}

	if (!lua_isnumber(luaState, stackposition)) {
		return false;
	} else {
		target = lua_tonumber(luaState, stackposition);

		if (remove) {
			lua_remove(luaState, stackposition);
		}

		return true;
	}
}
bool popValue(lua_State* luaState, std::string& target, int stackposition, bool remove) {
	if (!internal::isValidIndex(luaState, stackposition)) {
		return false;
	}

	if (!lua_isstring(luaState, stackposition)) {
		return false;
	} else {
		size_t size;
		const char* string = lua_tolstring(luaState, stackposition, &size);
		target.assign(string, size);

		if (remove) {
			lua_remove(luaState, stackposition);
		}

		return true;
	}
}

bool popValue(lua_State* luaState, bool& target, int stackposition, bool remove) {
	if (!internal::isValidIndex(luaState, stackposition)) {
		return false;
	}

	if (!lua_isboolean(luaState, stackposition)) {
		return false;
	} else {
		target = lua_toboolean(luaState, stackposition) != 0;

		if (remove) {
			lua_remove(luaState, stackposition);
		}

		return true;
	}
}

bool popValue(lua_State* luaState, lua_CFunction& target, int stackposition, bool remove) {
	if (!internal::isValidIndex(luaState, stackposition)) {
		return false;
	}

	if (!lua_iscfunction(luaState, stackposition)) {
		return false;
	} else {
		target = lua_tocfunction(luaState, stackposition);

		if (remove) {
			lua_remove(luaState, stackposition);
		}

		return true;
	}
}

}
}