-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbase.cpp
More file actions
523 lines (458 loc) · 10.1 KB
/
base.cpp
File metadata and controls
523 lines (458 loc) · 10.1 KB
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
#include "base.hpp"
#include <lua.hpp>
#include <iostream>
#include <sstream>
#include <vector>
#include <fmt/core.h>
#include <fmt/format.h>
#include <fmt/chrono.h>
#include <fmt/args.h>
#include <boost/version.hpp>
#include <Eigen/Core>
#include "textmodule.hpp"
#include "textmodule_string.hpp"
#include "textmodule_exception.hpp"
#include "textmodule_option.hpp"
#include "textmodule_lua.hpp"
static const int sentinel_ = 0;
#define sentinel ((void *)&sentinel_)
#define abs_index(L, i) ((i) > 0 || (i) <= LUA_REGISTRYINDEX ? (i) : lua_gettop(L) + (i) + 1)
int pairsaux(lua_State* L, const char* method, int iszero, lua_CFunction iter) {
if (!luaL_getmetafield(L, 1, method)) { /* no metamethod? */
luaL_checktype(L, 1, LUA_TTABLE); /* argument must be a table */
lua_pushcfunction(L, iter); /* will return generator, */
lua_pushvalue(L, 1); /* state, */
if (iszero) lua_pushinteger(L, 0); /* and initial value */
else lua_pushnil(L);
}
else {
lua_pushvalue(L, 1); /* argument 'self' to metamethod */
lua_call(L, 1, 3); /* get 3 values from metamethod */
}
return 3;
}
int nextaux(lua_State* L) {
luaL_checktype(L, 1, LUA_TTABLE);
lua_settop(L, 2); /* create a 2nd argument if there isn't one */
if (lua_next(L, 1))
return 2;
else {
lua_pushnil(L);
return 1;
}
}
int ipairsaux(lua_State* L) {
int i = luaL_checkint(L, 2);
luaL_checktype(L, 1, LUA_TTABLE);
i++; /* next value */
lua_pushinteger(L, i);
lua_rawgeti(L, 1, i);
return (lua_isnil(L, -1)) ? 1 : 2;
}
int base_getinfo(lua_State* L) {
try {
lua_Wstring t = tm_towstring(L, 1);
lua_Integer i = tm_tointeger_s(L, 2, 1);
if (t == L"version") {
if (i == 1)
lua_pushwstring(L, getVersionNum(MODULE_VERSION));
else if (i == 2)
lua_pushinteger(L, MODULE_VERSION);
else
return 0;
return 1;
}
else if (t == L"versions") {
lua_Wstring res = getVersionNum(i);
if (res != L"")
lua_pushwstring(L, res);
else
return 0;
return 1;
}
else if (t == L"name") {
lua_pushstring(L, MODULE_NAME);
return 1;
}
else if (t == L"lua") {
lua_getglobal(L, "_VERSION");
return 1;
}
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
void base_showtable_aux(lua_State* L, std::string* ret, int idx, int depth, bool tree) {
int p = lua_gettop(L);
lua_pushnil(L);
lua_Sstring key;
lua_Sstring value;
while (lua_next(L, idx) != 0) {
if (ret->length() != 0)
*ret += "\n";
if (lua_type(L, -2) == LUA_TNUMBER)
key = WstrToStr(tostring_n(lua_tonumber(L, -2)));
else
key = tm_convtostring(L, -2);
if (lua_type(L, -1) == LUA_TTABLE && tree) {
value = tm_convtostring(L, -1);
*ret += std::string(depth, '\t') + std::string("[" + key + "] ") + value;
base_showtable_aux(L, ret, abs_index(L, -1), depth + 1, tree);
}
else {
value = tm_convtostring(L, -1);
*ret += std::string(depth, '\t') + std::string("[" + key + "] ") + value;
}
lua_pop(L, 1);
}
lua_settop(L, p);
}
int base_showtable(lua_State* L) {
try {
if (!lua_istable(L, 1))
return luaL_argerror(L, 1, "table expected");
bool tree = tm_toboolean_s(L, 2, false);
lua_settop(L, 1);
std::string ret = "";
base_showtable_aux(L, &ret, 1, 0, tree);
lua_pushsstring(L, ret);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_showmetatable(lua_State* L) {
try {
luaL_checkany(L, 1);
if (!lua_getmetatable(L, 1)) {
lua_pushnil(L);
return 1; /* no metatable */
}
luaL_getmetafield(L, 1, "__metatable");
lua_remove(L, 1);
lua_pushnil(L);
lua_Sstring key;
lua_Sstring value;
lua_Sstring ret = "";
while (lua_next(L, 1) != 0) {
if (ret.length() != 0)
ret += "\n";
if (lua_type(L, -2) == LUA_TNUMBER)
key = WstrToStr(tostring_n(lua_tonumber(L, -2)));
else
key = tm_convtostring(L, -2);
value = tm_convtostring(L, -1);
ret += std::string("[" + key + "] " + value);
lua_pop(L, 1);
}
lua_pushsstring(L, ret);
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_maketable(lua_State* L) {
try {
int n = lua_gettop(L);
if (n == 1) {
if (tm_callmetan(L, 1, "__table"))
return 1;
}
lua_newtable(L);
for (int i = 0; i < n/2; i++) {
lua_pushvalue(L, i*2 + 2);
lua_pushvalue(L, i*2 + 1);
lua_settable(L, -3);
}
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_makelist(lua_State* L) {
try {
int n = lua_gettop(L);
lua_newtable(L);
for (int i = 0; i < n; i++) {
lua_pushinteger(L, i + 1);
lua_pushvalue(L, i + 1);
lua_settable(L, -3);
}
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_debug_print(lua_State* L) {
try {
int i = 1;
std::wstring ret = L"";
if (lua_type(L, i) == LUA_TNONE) {
debug_string(ret);
return 0;
}
for (int i = 1; i <= lua_gettop(L); i++) {
ret += StrToWstr(tm_convtostring(L, i)) + L"\t";
}
ret = ret.substr(0, ret.length() - 1);
debug_string(ret);
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_type(lua_State* L) {
try {
int n= lua_gettop(L);
if (n <= 0) {
lua_pushstring(L, tm_typename(L, 1));
return 1;
}
for (int i = 0; i < n; i++)
lua_pushstring(L, tm_typename(L, i+1));
return n;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_print(lua_State* L) {
try {
for (int i = 0; i < lua_gettop(L); i++) {
std::cout << tm_convtostring(L, i + 1) << "\t";
}
std::cout << std::endl;
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_printf(lua_State* L) {
try {
lua_Sstring str = tm_tosstring(L, 1);
auto store = fmt::dynamic_format_arg_store<fmt::format_context>();
lua_formatargs_store(L, &store, 2, lua_gettop(L));
std::cout << fmt::vformat(str, store) << std::endl;
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_exception(lua_State* L) {
if (lua_type(L, 1) == LUA_TSTRING)
throw std::exception(lua_tostring(L, 1));
else if (lua_type(L, 1) == LUA_TNONE)
throw std::exception();
return 0;
}
int base_error(lua_State* L) {
try {
int n = lua_gettop(L);
switch (n) {
case 0:
return lua_error(L);
case 1:
return luaL_error(L, tm_tostring(L, 1));
case 2:
return luaL_argerror(L, tm_tointeger(L, 1), tm_tostring(L, 2));
default:
return 0;
}
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_versioncheck(lua_State* L) {
try {
int res = versionCheck();
switch (res)
{
case VERSION_CHECK_LATEST:
lua_pushboolean(L, true);
break;
case VERSION_CHECK_OUTDATED:
lua_pushboolean(L, false);
break;
case VERSION_CHECK_ERROR:
default:
lua_pushnil(L);
break;
}
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_tostring(lua_State* L) {
try {
int s = lua_gettop(L);
if (s < 1) {
lua_pushsstring(L, tm_convtostring(L, 1));
return 1;
}
for (int i = 0; i < s; i++) {
lua_pushsstring(L, tm_convtostring(L, i+1));
}
return s;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_tonumber(lua_State* L) {
try {
luaL_checkany(L, 1);
int base = luaL_optint(L, 2, 10);
if (base == 10) { /* standard conversion */
if (luaL_callmeta(L, 1, "__tonumber"))
return 1;
if (lua_isnumber(L, 1)) {
lua_pushnumber(L, lua_tonumber(L, 1));
return 1;
}
}
else {
const char* s1 = "";
if (luaL_callmeta(L, 1, "__tonumber")) {
s1 = luaL_checkstring(L, -1);
}
else {
s1 = luaL_checkstring(L, 1);
}
char* s2;
unsigned long n;
luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
n = strtoul(s1, &s2, base);
if (s1 != s2) { /* at least one valid digit? */
while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
if (*s2 == '\0') { /* no invalid trailing characters? */
lua_pushnumber(L, (lua_Number)n);
return 1;
}
}
}
lua_pushnil(L); /* else not a number */
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_pointer(lua_State* L) {
try {
int s = lua_gettop(L);
for (int i = 1; i <= s; i++) {
std::string s = fmt::format("{0:p}", lua_topointer(L, i));
lua_pushsstring(L, s);
}
return s;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_getglobal(lua_State* L) {
try {
lua_getglobal(L, tm_tostring(L, 1));
return 1;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_setglobal(lua_State* L) {
try {
lua_pushvalue(L, 1);
lua_setglobal(L, tm_tostring(L, 2));
return 0;
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_pairs(lua_State* L) {
try {
return pairsaux(L, "__pairs", 0, nextaux);
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_ipairs(lua_State* L) {
try {
return pairsaux(L, "__ipairs", 1, ipairsaux);
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
int base_next(lua_State* L){
try {
return nextaux(L);
}
catch (std::exception& e) {
luaL_error(L, e.what());
return 1;
}
}
static luaL_Reg TEXTMODULE_BASE_REG[] = {
{"getinfo", base_getinfo},
{"showtable", base_showtable},
{"showmetatable", base_showmetatable},
{"maketable", base_maketable},
{"makelist", base_makelist},
{"debug_print", base_debug_print},
{"type", base_type},
{"print", base_print},
{"printf", base_printf},
{"exception", base_exception},
{"error", base_error},
{"versioncheck", base_versioncheck},
{"tostring", base_tostring},
{"tonumber", base_tonumber},
{"pointer", base_pointer},
{"getglobal", base_getglobal},
{"setglobal", base_setglobal},
{"pairs", base_pairs},
{"ipairs", base_ipairs},
{"next", base_next},
{ nullptr, nullptr }
};
void luaReg_base(lua_State* L, lua_Option opt) {
if (opt["api"]["base"]) {
tm_debuglog_apiloaded(opt, "base");
luaL_register(L, NULL, TEXTMODULE_BASE_REG);
}
else {
tm_debuglog_apinoloaded(opt, "base");
}
}