-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlua_wrapper.cc
369 lines (284 loc) · 8.45 KB
/
lua_wrapper.cc
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
#include "../../blowbox/lua/lua_wrapper.h"
#include "../../blowbox/console/console.h"
#include "../../blowbox/win32/file_watch.h"
namespace blowbox
{
//------------------------------------------------------------------------------------------------------
LuaWrapper::LuaWrapper()
{
}
//------------------------------------------------------------------------------------------------------
LuaWrapper::~LuaWrapper()
{
}
//------------------------------------------------------------------------------------------------------
LuaWrapper* LuaWrapper::Instance()
{
static SharedPtr<LuaWrapper> ptr(new LuaWrapper());
return ptr.get();
}
//------------------------------------------------------------------------------------------------------
void LuaWrapper::Dump(lua_State* L, const std::string& brief)
{
Console::Instance()->Log(brief);
std::string string;
int top = lua_gettop(L);
for (int i = 1; i <= top; ++i) {
int t = lua_type(L, i);
string = std::to_string(i) + ": ";
string += ToString(L, i);
Console::Instance()->Log(string);
}
}
//------------------------------------------------------------------------------------------------------
void LuaWrapper::CreateWeakTable(lua_State* L)
{
// Make sure the stack is big enough for this operation to be possible
lua_checkstack(L, 3);
// Create the actual weak table
lua_newtable(L);
// Copy the weak table which will be used as the metatable
lua_pushvalue(L, -1);
// Set the meta table to the actual weak table
lua_setmetatable(L, -2);
// Push the literal __mode
lua_pushliteral(L, "__mode");
// Push the actual mode value
lua_pushstring(L, "v");
// Set the value: -3[__mode] = "v"
lua_settable(L, -3);
}
//------------------------------------------------------------------------------------------------------
bool LuaWrapper::CompileFromFile(lua_State* L, const std::string& path, bool reloading)
{
if (luaL_dofile(L, path.c_str()) != 0)
{
Console::Instance()->Log(ToString(L, -1), LOG_COLOR_TYPES::LOG_COLOR_ERROR);
return false;
}
if (!reloading)
{
FileWatch::Instance()->Add(path, WATCH_FILE_TYPES::WATCH_FILE_SCRIPT);
}
Console::Instance()->Log("[LUA] Compiled file: " + path, LOG_COLOR_TYPES::LOG_COLOR_NOTICE);
return true;
}
//------------------------------------------------------------------------------------------------------
bool LuaWrapper::CompileFromString(lua_State* L, const std::string& code, const std::string& source)
{
int stacksize = lua_gettop(L);
if (luaL_dostring(L, code.c_str()))
{
Console::Instance()->Log(lua_tostring(L, -1), LOG_COLOR_TYPES::LOG_COLOR_ERROR);
return false;
}
if (stacksize != lua_gettop(L))
{
for (int i = 1; i <= lua_gettop(L) - stacksize; ++i)
{
Console::Instance()->Log(std::string("[OUTPUT] ") + ToString(L, i));
}
}
lua_settop(L, stacksize);
return true;
}
//------------------------------------------------------------------------------------------------------
int LuaWrapper::ToRelative(lua_State* L, const int& absolute)
{
if (absolute <= 0)
return absolute;
return -(lua_gettop(L) - absolute + 1);
}
//------------------------------------------------------------------------------------------------------
int LuaWrapper::ToAbsolute(lua_State* L, const int& relative)
{
if (relative >= 0)
return relative;
return lua_gettop(L) + 1 + relative;
}
//------------------------------------------------------------------------------------------------------
int LuaWrapper::ToArgument(lua_State* L, const int& index)
{
if (index < 0)
{
// relative index
return ToAbsolute(L, index);
}
else if (index > 0)
{
// absolute index
return index;
}
return 0;
}
//------------------------------------------------------------------------------------------------------
std::string LuaWrapper::ToString(lua_State* L, const int& index)
{
int top = lua_gettop(L);
std::string string;
switch (Typename(L, index))
{
case LUA_TYPE::LUA_TYPE_STRING: // strings
string = lua_tostring(L, index);
break;
case LUA_TYPE::LUA_TYPE_USERDATA: // userdata
case LUA_TYPE::LUA_TYPE_LIGHTUSERDATA:
lua_pushvalue(L, index);
lua_getglobal(L, "tostring");
lua_pushvalue(L, -2);
lua_pcall(L, 1, 1, 0);
string = lua_tostring(L, -1);
break;
case LUA_TYPE::LUA_TYPE_BOOLEAN:
string = lua_toboolean(L, index) ? "true" : "false";
break;
case LUA_TYPE::LUA_TYPE_NUMBER:
string = std::to_string(lua_tonumber(L, index));
break;
case LUA_TYPE::LUA_TYPE_FUNCTION:
string = "function";
break;
case LUA_TYPE::LUA_TYPE_NIL:
case LUA_TYPE::LUA_TYPE_NONE:
string = "nil";
break;
case LUA_TYPE::LUA_TYPE_TABLE:
string = "table";
break;
default:
string = lua_typename(L, index);
break;
}
lua_settop(L, top);
return string;
}
//------------------------------------------------------------------------------------------------------
std::map<std::string, LuaValue> LuaWrapper::ToTable(lua_State* L, const int& index)
{
int idx = ToAbsolute(L, index);
std::map<std::string, LuaValue> table;
if (lua_istable(L, idx))
{
lua_pushnil(L);
while (lua_next(L, idx))
{
std::map<std::string, LuaValue> element;
LuaValue value;
std::string identifier;
switch (lua_type(L, -1))
{
case LUA_TTABLE:
element = ToTable(L, -1);
value.type = LUA_TYPE::LUA_TYPE_TABLE;
value.location = LUA_LOCATION::LUA_LOCATION_FIELD;
if (Typename(L, -2) == LUA_TYPE::LUA_TYPE_NUMBER)
{
value.identifier = ToString(L, -2);
value.identifier = value.identifier.substr(0, value.identifier.find('.'));
}
else
{
value.identifier = ToString(L, -2);
}
value.value = ToString(L, -1);
table.emplace(value.identifier, value);
identifier = value.identifier;
for (auto it = element.begin(); it != element.end(); ++it)
{
value.type = it->second.type;
value.location = it->second.location;
value.identifier = it->second.identifier;
value.value = it->second.value;
table.find(identifier)->second.fields.emplace(value.identifier, value);
}
lua_pop(L, 1);
break;
default:
value.type = Typename(L, -1);
value.location = LUA_LOCATION::LUA_LOCATION_FIELD;
if (Typename(L, -2) == LUA_TYPE::LUA_TYPE_NUMBER)
{
value.identifier = ToString(L, -2);
value.identifier = value.identifier.substr(0, value.identifier.find('.'));
}
else
{
value.identifier = ToString(L, -2);
}
value.value = ToString(L, -1);
table.emplace(value.identifier, value);
lua_pop(L, 1);
break;
}
}
}
else
{
luaL_typerror(L, index, "table");
}
return table;
}
//------------------------------------------------------------------------------------------------------
LUA_TYPE LuaWrapper::Typename(lua_State* L, const int& index)
{
LUA_TYPE type;
switch (lua_type(L, index))
{
case LUA_TSTRING:
type = LUA_TYPE::LUA_TYPE_STRING;
break;
case LUA_TUSERDATA:
type = LUA_TYPE::LUA_TYPE_USERDATA;
break;
case LUA_TLIGHTUSERDATA:
type = LUA_TYPE::LUA_TYPE_LIGHTUSERDATA;
break;
case LUA_TBOOLEAN:
type = LUA_TYPE::LUA_TYPE_BOOLEAN;
break;
case LUA_TNUMBER:
type = LUA_TYPE::LUA_TYPE_NUMBER;
break;
case LUA_TFUNCTION:
type = LUA_TYPE::LUA_TYPE_FUNCTION;
break;
case LUA_TNIL:
type = LUA_TYPE::LUA_TYPE_NIL;
break;
case LUA_TNONE:
type = LUA_TYPE::LUA_TYPE_NONE;
break;
case LUA_TTABLE:
type = LUA_TYPE::LUA_TYPE_TABLE;
break;
default:
type = LUA_TYPE::LUA_TYPE_UNKNOWN;
break;
}
return type;
}
//------------------------------------------------------------------------------------------------------
int LuaWrapper::StackTrace(lua_State* L)
{
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getfield(L, -1, "traceback");
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
Console::Instance()->Log(lua_tostring(L, -1), LOG_COLOR_TYPES::LOG_COLOR_NOTICE);
lua_pop(L, 2);
return 0;
}
//------------------------------------------------------------------------------------------------------
int LuaWrapper::LuaStackTrace(lua_State* L)
{
lua_getfield(L, LUA_GLOBALSINDEX, "debug");
lua_getfield(L, -1, "traceback");
lua_pushvalue(L, 1);
lua_pushinteger(L, 2);
lua_call(L, 2, 1);
Console::Instance()->Log(lua_tostring(L, -1), LOG_COLOR_TYPES::LOG_COLOR_NOTICE);
lua_pop(L, 2);
return 0;
}
}