forked from zupet/LuaTinker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lua_tinker.cpp
745 lines (637 loc) · 16.5 KB
/
lua_tinker.cpp
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
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
// lua_tinker.cpp
//
// LuaTinker - Simple and light C++ wrapper for Lua.
//
// Copyright (c) 2005-2007 Kwon-il Lee ([email protected])
//
// please check Licence.txt file for licence and legal issues.
#include <iostream>
extern "C"
{
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
};
#include "lua_tinker.h"
/*---------------------------------------------------------------------------*/
/* init */
/*---------------------------------------------------------------------------*/
void lua_tinker::init(lua_State *L)
{
init_s64(L);
init_u64(L);
}
/*---------------------------------------------------------------------------*/
/* __s64 */
/*---------------------------------------------------------------------------*/
static int tostring_s64(lua_State *L)
{
char temp[64];
sprintf_s(temp, "%I64d", *(long long*)lua_topointer(L, 1));
lua_pushstring(L, temp);
return 1;
}
/*---------------------------------------------------------------------------*/
static int eq_s64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(long long)) == 0);
return 1;
}
/*---------------------------------------------------------------------------*/
static int lt_s64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(long long)) < 0);
return 1;
}
/*---------------------------------------------------------------------------*/
static int le_s64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(long long)) <= 0);
return 1;
}
/*---------------------------------------------------------------------------*/
void lua_tinker::init_s64(lua_State *L)
{
const char* name = "__s64";
lua_pushstring(L, name);
lua_newtable(L);
lua_pushstring(L, "__name");
lua_pushstring(L, name);
lua_rawset(L, -3);
lua_pushstring(L, "__tostring");
lua_pushcclosure(L, tostring_s64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__eq");
lua_pushcclosure(L, eq_s64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__lt");
lua_pushcclosure(L, lt_s64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__le");
lua_pushcclosure(L, le_s64, 0);
lua_rawset(L, -3);
lua_settable(L, LUA_GLOBALSINDEX);
}
/*---------------------------------------------------------------------------*/
/* __u64 */
/*---------------------------------------------------------------------------*/
static int tostring_u64(lua_State *L)
{
char temp[64];
sprintf_s(temp, "%I64u", *(unsigned long long*)lua_topointer(L, 1));
lua_pushstring(L, temp);
return 1;
}
/*---------------------------------------------------------------------------*/
static int eq_u64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(unsigned long long)) == 0);
return 1;
}
/*---------------------------------------------------------------------------*/
static int lt_u64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(unsigned long long)) < 0);
return 1;
}
/*---------------------------------------------------------------------------*/
static int le_u64(lua_State *L)
{
lua_pushboolean(L, memcmp(lua_topointer(L, 1), lua_topointer(L, 2), sizeof(unsigned long long)) <= 0);
return 1;
}
/*---------------------------------------------------------------------------*/
void lua_tinker::init_u64(lua_State *L)
{
const char* name = "__u64";
lua_pushstring(L, name);
lua_newtable(L);
lua_pushstring(L, "__name");
lua_pushstring(L, name);
lua_rawset(L, -3);
lua_pushstring(L, "__tostring");
lua_pushcclosure(L, tostring_u64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__eq");
lua_pushcclosure(L, eq_u64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__lt");
lua_pushcclosure(L, lt_u64, 0);
lua_rawset(L, -3);
lua_pushstring(L, "__le");
lua_pushcclosure(L, le_u64, 0);
lua_rawset(L, -3);
lua_settable(L, LUA_GLOBALSINDEX);
}
/*---------------------------------------------------------------------------*/
/* excution */
/*---------------------------------------------------------------------------*/
void lua_tinker::dofile(lua_State *L, const char *filename)
{
lua_pushcclosure(L, on_error, 0);
int errfunc = lua_gettop(L);
if(luaL_loadfile(L, filename) == 0)
{
lua_pcall(L, 0, 1, errfunc);
}
else
{
print_error(L, "%s", lua_tostring(L, -1));
}
lua_remove(L, errfunc);
lua_pop(L, 1);
}
/*---------------------------------------------------------------------------*/
void lua_tinker::dostring(lua_State *L, const char* buff)
{
lua_tinker::dobuffer(L, buff, strlen(buff));
}
/*---------------------------------------------------------------------------*/
void lua_tinker::dobuffer(lua_State *L, const char* buff, size_t len)
{
lua_pushcclosure(L, on_error, 0);
int errfunc = lua_gettop(L);
if(luaL_loadbuffer(L, buff, len, "lua_tinker::dobuffer()") == 0)
{
lua_pcall(L, 0, 1, errfunc);
}
else
{
print_error(L, "%s", lua_tostring(L, -1));
}
lua_remove(L, errfunc);
lua_pop(L, 1);
}
/*---------------------------------------------------------------------------*/
/* debug helpers */
/*---------------------------------------------------------------------------*/
static void call_stack(lua_State* L, int n)
{
lua_Debug ar;
if(lua_getstack(L, n, &ar) == 1)
{
lua_getinfo(L, "nSlu", &ar);
const char* indent;
if(n == 0)
{
indent = "->\t";
lua_tinker::print_error(L, "\t<call stack>");
}
else
{
indent = "\t";
}
if(ar.name)
lua_tinker::print_error(L, "%s%s() : line %d [%s : line %d]", indent, ar.name, ar.currentline, ar.source, ar.linedefined);
else
lua_tinker::print_error(L, "%sunknown : line %d [%s : line %d]", indent, ar.currentline, ar.source, ar.linedefined);
call_stack(L, n+1);
}
}
/*---------------------------------------------------------------------------*/
int lua_tinker::on_error(lua_State *L)
{
print_error(L, "%s", lua_tostring(L, -1));
call_stack(L, 0);
return 0;
}
/*---------------------------------------------------------------------------*/
void lua_tinker::print_error(lua_State *L, const char* fmt, ...)
{
char text[4096];
va_list args;
va_start(args, fmt);
vsprintf_s(text, fmt, args);
va_end(args);
lua_pushstring(L, "_ALERT");
lua_gettable(L, LUA_GLOBALSINDEX);
if(lua_isfunction(L, -1))
{
lua_pushstring(L, text);
lua_call(L, 1, 0);
}
else
{
printf("%s\n", text);
lua_pop(L, 1);
}
}
/*---------------------------------------------------------------------------*/
void lua_tinker::enum_stack(lua_State *L)
{
int top = lua_gettop(L);
print_error(L, "Type:%d", top);
for(int i=1; i<=lua_gettop(L); ++i)
{
switch(lua_type(L, i))
{
case LUA_TNIL:
print_error(L, "\t%s", lua_typename(L, lua_type(L, i)));
break;
case LUA_TBOOLEAN:
print_error(L, "\t%s %s", lua_typename(L, lua_type(L, i)), lua_toboolean(L, i)?"true":"false");
break;
case LUA_TLIGHTUSERDATA:
print_error(L, "\t%s 0x%08p", lua_typename(L, lua_type(L, i)), lua_topointer(L, i));
break;
case LUA_TNUMBER:
print_error(L, "\t%s %f", lua_typename(L, lua_type(L, i)), lua_tonumber(L, i));
break;
case LUA_TSTRING:
print_error(L, "\t%s %s", lua_typename(L, lua_type(L, i)), lua_tostring(L, i));
break;
case LUA_TTABLE:
print_error(L, "\t%s 0x%08p", lua_typename(L, lua_type(L, i)), lua_topointer(L, i));
break;
case LUA_TFUNCTION:
print_error(L, "\t%s() 0x%08p", lua_typename(L, lua_type(L, i)), lua_topointer(L, i));
break;
case LUA_TUSERDATA:
print_error(L, "\t%s 0x%08p", lua_typename(L, lua_type(L, i)), lua_topointer(L, i));
break;
case LUA_TTHREAD:
print_error(L, "\t%s", lua_typename(L, lua_type(L, i)));
break;
}
}
}
/*---------------------------------------------------------------------------*/
/* read */
/*---------------------------------------------------------------------------*/
template<>
char* lua_tinker::read(lua_State *L, int index)
{
return (char*)lua_tostring(L, index);
}
template<>
const char* lua_tinker::read(lua_State *L, int index)
{
return (const char*)lua_tostring(L, index);
}
template<>
char lua_tinker::read(lua_State *L, int index)
{
return (char)lua_tonumber(L, index);
}
template<>
unsigned char lua_tinker::read(lua_State *L, int index)
{
return (unsigned char)lua_tonumber(L, index);
}
template<>
short lua_tinker::read(lua_State *L, int index)
{
return (short)lua_tonumber(L, index);
}
template<>
unsigned short lua_tinker::read(lua_State *L, int index)
{
return (unsigned short)lua_tonumber(L, index);
}
template<>
long lua_tinker::read(lua_State *L, int index)
{
return (long)lua_tonumber(L, index);
}
template<>
unsigned long lua_tinker::read(lua_State *L, int index)
{
return (unsigned long)lua_tonumber(L, index);
}
template<>
int lua_tinker::read(lua_State *L, int index)
{
return (int)lua_tonumber(L, index);
}
template<>
unsigned int lua_tinker::read(lua_State *L, int index)
{
return (unsigned int)lua_tonumber(L, index);
}
template<>
float lua_tinker::read(lua_State *L, int index)
{
return (float)lua_tonumber(L, index);
}
template<>
double lua_tinker::read(lua_State *L, int index)
{
return (double)lua_tonumber(L, index);
}
template<>
bool lua_tinker::read(lua_State *L, int index)
{
if(lua_isboolean(L, index))
return lua_toboolean(L, index) != 0;
else
return lua_tonumber(L, index) != 0;
}
template<>
void lua_tinker::read(lua_State *L, int index)
{
return;
}
template<>
long long lua_tinker::read(lua_State *L, int index)
{
if(lua_isnumber(L,index))
return (long long)lua_tonumber(L, index);
else
return *(long long*)lua_touserdata(L, index);
}
template<>
unsigned long long lua_tinker::read(lua_State *L, int index)
{
if(lua_isnumber(L,index))
return (unsigned long long)lua_tonumber(L, index);
else
return *(unsigned long long*)lua_touserdata(L, index);
}
template<>
lua_tinker::table lua_tinker::read(lua_State *L, int index)
{
return table(L, index);
}
/*---------------------------------------------------------------------------*/
/* push */
/*---------------------------------------------------------------------------*/
template<>
void lua_tinker::push(lua_State *L, char ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, unsigned char ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, short ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, unsigned short ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, long ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, unsigned long ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, int ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, unsigned int ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, float ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, double ret)
{
lua_pushnumber(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, char* ret)
{
lua_pushstring(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, const char* ret)
{
lua_pushstring(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, bool ret)
{
lua_pushboolean(L, ret);
}
template<>
void lua_tinker::push(lua_State *L, lua_value* ret)
{
if(ret) ret->to_lua(L); else lua_pushnil(L);
}
template<>
void lua_tinker::push(lua_State *L, long long ret)
{
*(long long*)lua_newuserdata(L, sizeof(long long)) = ret;
lua_pushstring(L, "__s64");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_setmetatable(L, -2);
}
template<>
void lua_tinker::push(lua_State *L, unsigned long long ret)
{
*(unsigned long long*)lua_newuserdata(L, sizeof(unsigned long long)) = ret;
lua_pushstring(L, "__u64");
lua_gettable(L, LUA_GLOBALSINDEX);
lua_setmetatable(L, -2);
}
template<>
void lua_tinker::push(lua_State *L, lua_tinker::table ret)
{
lua_pushvalue(L, ret.m_obj->m_index);
}
/*---------------------------------------------------------------------------*/
/* pop */
/*---------------------------------------------------------------------------*/
template<>
void lua_tinker::pop(lua_State *L)
{
lua_pop(L, 1);
}
template<>
lua_tinker::table lua_tinker::pop(lua_State *L)
{
return table(L, lua_gettop(L));
}
/*---------------------------------------------------------------------------*/
/* Tinker Class Helper */
/*---------------------------------------------------------------------------*/
static void invoke_parent(lua_State *L)
{
lua_pushstring(L, "__parent");
lua_rawget(L, -2);
if(lua_istable(L,-1))
{
lua_pushvalue(L,2);
lua_rawget(L, -2);
if(!lua_isnil(L,-1))
{
lua_remove(L,-2);
}
else
{
lua_remove(L, -1);
invoke_parent(L);
lua_remove(L,-2);
}
}
}
/*---------------------------------------------------------------------------*/
int lua_tinker::meta_get(lua_State *L)
{
lua_getmetatable(L,1);
lua_pushvalue(L,2);
lua_rawget(L,-2);
if(lua_isuserdata(L,-1))
{
user2type<var_base*>::invoke(L,-1)->get(L);
lua_remove(L, -2);
}
else if(lua_isnil(L,-1))
{
lua_remove(L,-1);
invoke_parent(L);
if(lua_isnil(L,-1))
{
lua_pushfstring(L, "can't find '%s' class variable. (forgot registering class variable ?)", lua_tostring(L, 2));
lua_error(L);
}
}
lua_remove(L,-2);
return 1;
}
/*---------------------------------------------------------------------------*/
int lua_tinker::meta_set(lua_State *L)
{
lua_getmetatable(L,1);
lua_pushvalue(L,2);
lua_rawget(L,-2);
if(lua_isuserdata(L,-1))
{
user2type<var_base*>::invoke(L,-1)->set(L);
}
else if(lua_isnil(L, -1))
{
lua_pushvalue(L,2);
lua_pushvalue(L,3);
lua_rawset(L, -4);
}
lua_settop(L, 3);
return 0;
}
/*---------------------------------------------------------------------------*/
void lua_tinker::push_meta(lua_State *L, const char* name)
{
lua_pushstring(L, name);
lua_gettable(L, LUA_GLOBALSINDEX);
}
/*---------------------------------------------------------------------------*/
/* table object on stack */
/*---------------------------------------------------------------------------*/
lua_tinker::table_obj::table_obj(lua_State* L, int index)
:m_L(L)
,m_index(index)
,m_ref(0)
{
if(lua_isnil(m_L, m_index))
{
m_pointer = NULL;
lua_remove(m_L, m_index);
}
else
{
m_pointer = lua_topointer(m_L, m_index);
}
}
lua_tinker::table_obj::~table_obj()
{
if(validate())
{
lua_remove(m_L, m_index);
}
}
void lua_tinker::table_obj::inc_ref()
{
++m_ref;
}
void lua_tinker::table_obj::dec_ref()
{
if(--m_ref == 0)
delete this;
}
bool lua_tinker::table_obj::validate()
{
if(m_pointer != NULL)
{
if(m_pointer == lua_topointer(m_L, m_index))
{
return true;
}
else
{
int top = lua_gettop(m_L);
for(int i=1; i<=top; ++i)
{
if(m_pointer == lua_topointer(m_L, i))
{
m_index = i;
return true;
}
}
m_pointer = NULL;
return false;
}
}
else
{
return false;
}
}
/*---------------------------------------------------------------------------*/
/* Table Object Holder */
/*---------------------------------------------------------------------------*/
lua_tinker::table::table(lua_State* L)
{
lua_newtable(L);
m_obj = new table_obj(L, lua_gettop(L));
m_obj->inc_ref();
}
lua_tinker::table::table(lua_State* L, const char* name)
{
lua_pushstring(L, name);
lua_gettable(L, LUA_GLOBALSINDEX);
if(lua_istable(L, -1) == 0)
{
lua_pop(L, 1);
lua_newtable(L);
lua_pushstring(L, name);
lua_pushvalue(L, -2);
lua_settable(L, LUA_GLOBALSINDEX);
}
m_obj = new table_obj(L, lua_gettop(L));
}
lua_tinker::table::table(lua_State* L, int index)
{
if(index < 0)
{
index = lua_gettop(L) + index + 1;
}
m_obj = new table_obj(L, index);
m_obj->inc_ref();
}
lua_tinker::table::table(const table& input)
{
m_obj = input.m_obj;
m_obj->inc_ref();
}
lua_tinker::table::~table()
{
m_obj->dec_ref();
}
/*---------------------------------------------------------------------------*/