-
Notifications
You must be signed in to change notification settings - Fork 445
/
Copy pathxlua.c
1245 lines (1089 loc) · 29.7 KB
/
xlua.c
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
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
*Tencent is pleased to support the open source community by making xLua available.
*Copyright (C) 2016 THL A29 Limited, a Tencent company. All rights reserved.
*Licensed under the MIT License (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at
*http://opensource.org/licenses/MIT
*Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.
*/
#define LUA_LIB
#include "lua.h"
#include "lualib.h"
#include "lauxlib.h"
#include <string.h>
#include <stdint.h>
#include "i64lib.h"
#if USING_LUAJIT
#include "lj_obj.h"
#else
#include "lstate.h"
#endif
/*
** stdcall C function support
*/
static int tag = 0;
static const char *const hooknames[] = {"call", "return", "line", "count", "tail return"};
static int hook_index = -1;
LUA_API void *xlua_tag ()
{
return &tag;
}
LUA_API int xlua_get_registry_index() {
return LUA_REGISTRYINDEX;
}
LUA_API int xlua_get_lib_version() {
return 105;
}
LUA_API int xlua_tocsobj_safe(lua_State *L,int index) {
int *udata = (int *)lua_touserdata (L,index);
if (udata != NULL) {
if (lua_getmetatable(L,index)) {
lua_pushlightuserdata(L, &tag);
lua_rawget(L,-2);
if (!lua_isnil (L,-1)) {
lua_pop (L, 2);
return *udata;
}
lua_pop (L, 2);
}
}
return -1;
}
LUA_API int xlua_tocsobj_fast (lua_State *L,int index) {
int *udata = (int *)lua_touserdata (L,index);
if(udata!=NULL)
return *udata;
return -1;
}
#if LUA_VERSION_NUM == 501
#undef lua_getglobal
LUA_API int lua_getglobal (lua_State *L, const char *name) {
lua_getfield(L, LUA_GLOBALSINDEX, name);
return 0;
}
#undef lua_setglobal
LUA_API void lua_setglobal (lua_State *L, const char *name) {
lua_setfield(L, LUA_GLOBALSINDEX, name);
}
LUA_API int lua_isinteger (lua_State *L, int idx) {
return 0;
}
LUA_API uint32_t xlua_objlen (lua_State *L, int idx) {
return lua_objlen (L, idx);
}
LUA_API uint32_t xlua_touint (lua_State *L, int idx) {
return (uint32_t)lua_tonumber(L, idx);
}
LUA_API void xlua_pushuint (lua_State *L, uint32_t n) {
lua_pushnumber(L, n);
}
#endif
#if LUA_VERSION_NUM ==503
LUA_API int lua_setfenv(lua_State *L, int idx)
{
int type = lua_type(L, idx);
if(type == LUA_TUSERDATA || type == LUA_TFUNCTION)
{
lua_setupvalue(L, idx, 1);
return 1;
}
else
{
return 0;
}
}
LUA_API uint32_t xlua_objlen (lua_State *L, int idx) {
return (uint32_t)lua_rawlen (L, idx);
}
LUA_API uint32_t xlua_touint (lua_State *L, int idx) {
return lua_isinteger(L, idx) ? (uint32_t)lua_tointeger(L, idx) : (uint32_t) lua_tonumber(L, idx);
}
LUA_API void xlua_pushuint (lua_State *L, uint32_t n) {
lua_pushinteger(L, n);
}
#undef lua_insert
LUA_API void lua_insert (lua_State *L, int idx) {
lua_rotate(L, idx, 1);
}
#undef lua_remove
LUA_API void lua_remove (lua_State *L, int idx) {
lua_rotate(L, idx, -1);
lua_pop(L, 1);
}
#undef lua_replace
LUA_API void lua_replace (lua_State *L, int idx) {
lua_copy(L, -1, idx);
lua_pop(L, 1);
}
#undef lua_pcall
LUA_API int lua_pcall (lua_State *L, int nargs, int nresults, int errfunc) {
return lua_pcallk(L, nargs, nresults, errfunc, 0, NULL);
}
#undef lua_tonumber
LUA_API lua_Number lua_tonumber (lua_State *L, int idx) {
return lua_tonumberx(L, idx, NULL);
}
#endif
#if LUA_VERSION_NUM < 503
#define lua_absindex(L, index) ((index > 0 || index <= LUA_REGISTRYINDEX) ? index : lua_gettop(L) + index + 1)
#endif
LUA_API void xlua_getloaders (lua_State *L) {
lua_getglobal(L, "package");
#if LUA_VERSION_NUM == 501
lua_getfield(L, -1, "loaders");
#else
lua_getfield(L, -1, "searchers");
#endif
lua_remove(L, -2);
}
LUA_API void xlua_rawgeti (lua_State *L, int idx, int64_t n) {
lua_rawgeti(L, idx, (lua_Integer)n);
}
LUA_API void xlua_rawseti (lua_State *L, int idx, int64_t n) {
lua_rawseti(L, idx, (lua_Integer)n);
}
LUA_API int xlua_ref_indirect(lua_State *L, int indirectRef) {
int ret = 0;
lua_rawgeti(L, LUA_REGISTRYINDEX, indirectRef);
lua_pushvalue(L, -2);
ret = luaL_ref(L, -2);
lua_pop(L, 2);
return ret;
}
LUA_API void xlua_getref_indirect(lua_State *L, int indirectRef, int reference) {
lua_rawgeti(L, LUA_REGISTRYINDEX, indirectRef);
lua_rawgeti(L, -1, reference);
lua_remove(L, -2);
}
LUA_API int xlua_tointeger (lua_State *L, int idx) {
return (int)lua_tointeger(L, idx);
}
LUA_API void xlua_pushinteger (lua_State *L, int n) {
lua_pushinteger(L, n);
}
LUA_API void xlua_pushlstring (lua_State *L, const char *s, int len) {
lua_pushlstring(L, s, len);
}
LUALIB_API int xluaL_loadbuffer (lua_State *L, const char *buff, int size,
const char *name) {
return luaL_loadbuffer(L, buff, size, name);
}
static int c_lua_gettable(lua_State* L) {
lua_gettable(L, 1);
return 1;
}
LUA_API int xlua_pgettable(lua_State* L, int idx) {
int top = lua_gettop(L);
idx = lua_absindex(L, idx);
lua_pushcfunction(L, c_lua_gettable);
lua_pushvalue(L, idx);
lua_pushvalue(L, top);
lua_remove(L, top);
return lua_pcall(L, 2, 1, 0);
}
static int c_lua_gettable_bypath(lua_State* L) {
size_t len = 0;
const char * pos = NULL;
const char * path = lua_tolstring(L, 2, &len);
lua_pushvalue(L, 1);
do {
pos = strchr(path, '.');
if (NULL == pos) {
lua_pushlstring(L, path, len);
} else {
lua_pushlstring(L, path, pos - path);
len = len - (pos - path + 1);
path = pos + 1;
}
lua_gettable(L, -2);
if (lua_type(L, -1) != LUA_TTABLE) {
if (NULL != pos) { // not found in path
lua_pushnil(L);
}
break;
}
lua_remove(L, -2);
} while(pos);
return 1;
}
LUA_API int xlua_pgettable_bypath(lua_State* L, int idx, const char *path) {
idx = lua_absindex(L, idx);
lua_pushcfunction(L, c_lua_gettable_bypath);
lua_pushvalue(L, idx);
lua_pushstring(L, path);
return lua_pcall(L, 2, 1, 0);
}
static int c_lua_settable(lua_State* L) {
lua_settable(L, 1);
return 0;
}
LUA_API int xlua_psettable(lua_State* L, int idx) {
int top = lua_gettop(L);
idx = lua_absindex(L, idx);
lua_pushcfunction(L, c_lua_settable);
lua_pushvalue(L, idx);
lua_pushvalue(L, top - 1);
lua_pushvalue(L, top);
lua_remove(L, top);
lua_remove(L, top - 1);
return lua_pcall(L, 3, 0, 0);
}
static int c_lua_settable_bypath(lua_State* L) {
size_t len = 0;
const char * pos = NULL;
const char * path = lua_tolstring(L, 2, &len);
lua_pushvalue(L, 1);
do {
pos = strchr(path, '.');
if (NULL == pos) { // last
lua_pushlstring(L, path, len);
lua_pushvalue(L, 3);
lua_settable(L, -3);
lua_pop(L, 1);
break;
} else {
lua_pushlstring(L, path, pos - path);
len = len - (pos - path + 1);
path = pos + 1;
}
lua_gettable(L, -2);
if (lua_type(L, -1) != LUA_TTABLE) {
return luaL_error(L, "can not set value to %s", lua_tostring(L, 2));
}
lua_remove(L, -2);
} while(pos);
return 0;
}
LUA_API int xlua_psettable_bypath(lua_State* L, int idx, const char *path) {
int top = lua_gettop(L);
idx = lua_absindex(L, idx);
lua_pushcfunction(L, c_lua_settable_bypath);
lua_pushvalue(L, idx);
lua_pushstring(L, path);
lua_pushvalue(L, top);
lua_remove(L, top);
return lua_pcall(L, 3, 0, 0);
}
static int c_lua_getglobal(lua_State* L) {
lua_getglobal(L, lua_tostring(L, 1));
return 1;
}
LUA_API int xlua_getglobal (lua_State *L, const char *name) {
lua_pushcfunction(L, c_lua_getglobal);
lua_pushstring(L, name);
return lua_pcall(L, 1, 1, 0);
}
static int c_lua_setglobal(lua_State* L) {
lua_setglobal(L, lua_tostring(L, 1));
return 0;
}
LUA_API int xlua_setglobal (lua_State *L, const char *name) {
int top = lua_gettop(L);
lua_pushcfunction(L, c_lua_setglobal);
lua_pushstring(L, name);
lua_pushvalue(L, top);
lua_remove(L, top);
return lua_pcall(L, 2, 0, 0);
}
LUA_API int xlua_tryget_cachedud(lua_State *L, int key, int cache_ref) {
lua_rawgeti(L, LUA_REGISTRYINDEX, cache_ref);
lua_rawgeti(L, -1, key);
if (!lua_isnil(L, -1))
{
lua_remove(L, -2);
return 1;
}
lua_pop(L, 2);
return 0;
}
static void cacheud(lua_State *L, int key, int cache_ref) {
lua_rawgeti(L, LUA_REGISTRYINDEX, cache_ref);
lua_pushvalue(L, -2);
lua_rawseti(L, -2, key);
lua_pop(L, 1);
}
LUA_API void xlua_pushcsobj(lua_State *L, int key, int meta_ref, int need_cache, int cache_ref) {
int* pointer = (int*)lua_newuserdata(L, sizeof(int));
*pointer = key;
if (need_cache) cacheud(L, key, cache_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);
lua_setmetatable(L, -2);
}
void print_top(lua_State *L) {
lua_getglobal(L, "print");
lua_pushvalue(L, -2);
lua_call(L, 1, 0);
}
void print_str(lua_State *L, char *str) {
lua_getglobal(L, "print");
lua_pushstring(L, str);
lua_call(L, 1, 0);
}
void print_value(lua_State *L, char *str, int idx) {
idx = lua_absindex(L, idx);
lua_getglobal(L, "print");
lua_pushstring(L, str);
lua_pushvalue(L, idx);
lua_call(L, 2, 0);
}
//upvalue --- [1]: methods, [2]:getters, [3]:csindexer, [4]:base, [5]:indexfuncs, [6]:arrayindexer, [7]:baseindex
//param --- [1]: obj, [2]: key
LUA_API int obj_indexer(lua_State *L) {
if (!lua_isnil(L, lua_upvalueindex(1))) {
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(1));
if (!lua_isnil(L, -1)) {//has method
return 1;
}
lua_pop(L, 1);
}
if (!lua_isnil(L, lua_upvalueindex(2))) {
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(2));
if (!lua_isnil(L, -1)) {//has getter
lua_pushvalue(L, 1);
lua_call(L, 1, 1);
return 1;
}
lua_pop(L, 1);
}
if (!lua_isnil(L, lua_upvalueindex(6)) && lua_type(L, 2) == LUA_TNUMBER) {
lua_pushvalue(L, lua_upvalueindex(6));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_call(L, 2, 1);
return 1;
}
if (!lua_isnil(L, lua_upvalueindex(3))) {
lua_pushvalue(L, lua_upvalueindex(3));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_call(L, 2, 2);
if (lua_toboolean(L, -2)) {
return 1;
}
lua_pop(L, 2);
}
if (!lua_isnil(L, lua_upvalueindex(4))) {
lua_pushvalue(L, lua_upvalueindex(4));
while(!lua_isnil(L, -1)) {
lua_pushvalue(L, -1);
lua_gettable(L, lua_upvalueindex(5));
if (!lua_isnil(L, -1)) // found
{
lua_replace(L, lua_upvalueindex(7)); //baseindex = indexfuncs[base]
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
lua_getfield(L, -1, "BaseType");
lua_remove(L, -2);
}
lua_pushnil(L);
lua_replace(L, lua_upvalueindex(4));//base = nil
}
if (!lua_isnil(L, lua_upvalueindex(7))) {
lua_settop(L, 2);
lua_pushvalue(L, lua_upvalueindex(7));
lua_insert(L, 1);
lua_call(L, 2, 1);
return 1;
} else {
return 0;
}
}
LUA_API int gen_obj_indexer(lua_State *L) {
lua_pushnil(L);
lua_pushcclosure(L, obj_indexer, 7);
return 0;
}
//upvalue --- [1]:setters, [2]:csnewindexer, [3]:base, [4]:newindexfuncs, [5]:arrayindexer, [6]:basenewindex
//param --- [1]: obj, [2]: key, [3]: value
LUA_API int obj_newindexer(lua_State *L) {
if (!lua_isnil(L, lua_upvalueindex(1))) {
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(1));
if (!lua_isnil(L, -1)) {//has setter
lua_pushvalue(L, 1);
lua_pushvalue(L, 3);
lua_call(L, 2, 0);
return 0;
}
lua_pop(L, 1);
}
if (!lua_isnil(L, lua_upvalueindex(2))) {
lua_pushvalue(L, lua_upvalueindex(2));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_call(L, 3, 1);
if (lua_toboolean(L, -1)) {
return 0;
}
}
if (!lua_isnil(L, lua_upvalueindex(5)) && lua_type(L, 2) == LUA_TNUMBER) {
lua_pushvalue(L, lua_upvalueindex(5));
lua_pushvalue(L, 1);
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_call(L, 3, 0);
return 0;
}
if (!lua_isnil(L, lua_upvalueindex(3))) {
lua_pushvalue(L, lua_upvalueindex(3));
while(!lua_isnil(L, -1)) {
lua_pushvalue(L, -1);
lua_gettable(L, lua_upvalueindex(4));
if (!lua_isnil(L, -1)) // found
{
lua_replace(L, lua_upvalueindex(6)); //basenewindex = newindexfuncs[base]
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
lua_getfield(L, -1, "BaseType");
lua_remove(L, -2);
}
lua_pushnil(L);
lua_replace(L, lua_upvalueindex(3));//base = nil
}
if (!lua_isnil(L, lua_upvalueindex(6))) {
lua_settop(L, 3);
lua_pushvalue(L, lua_upvalueindex(6));
lua_insert(L, 1);
lua_call(L, 3, 0);
return 0;
} else {
return luaL_error(L, "cannot set %s, no such field", lua_tostring(L, 2));
}
}
LUA_API int gen_obj_newindexer(lua_State *L) {
lua_pushnil(L);
lua_pushcclosure(L, obj_newindexer, 6);
return 0;
}
//upvalue --- [1]:getters, [2]:feilds, [3]:base, [4]:indexfuncs, [5]:baseindex
//param --- [1]: obj, [2]: key
LUA_API int cls_indexer(lua_State *L) {
if (!lua_isnil(L, lua_upvalueindex(1))) {
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(1));
if (!lua_isnil(L, -1)) {//has getter
lua_call(L, 0, 1);
return 1;
}
}
if (!lua_isnil(L, lua_upvalueindex(2))) {
lua_pushvalue(L, 2);
lua_rawget(L, lua_upvalueindex(2));
if (!lua_isnil(L, -1)) {//has feild
return 1;
}
lua_pop(L, 1);
}
if (!lua_isnil(L, lua_upvalueindex(3))) {
lua_pushvalue(L, lua_upvalueindex(3));
while(!lua_isnil(L, -1)) {
lua_pushvalue(L, -1);
lua_gettable(L, lua_upvalueindex(4));
if (!lua_isnil(L, -1)) // found
{
lua_replace(L, lua_upvalueindex(5)); //baseindex = indexfuncs[base]
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
lua_getfield(L, -1, "BaseType");
lua_remove(L, -2);
}
lua_pushnil(L);
lua_replace(L, lua_upvalueindex(3));//base = nil
}
if (!lua_isnil(L, lua_upvalueindex(5))) {
lua_settop(L, 2);
lua_pushvalue(L, lua_upvalueindex(5));
lua_insert(L, 1);
lua_call(L, 2, 1);
return 1;
} else {
lua_pushnil(L);
return 1;
}
}
LUA_API int gen_cls_indexer(lua_State *L) {
lua_pushnil(L);
lua_pushcclosure(L, cls_indexer, 5);
return 0;
}
//upvalue --- [1]:setters, [2]:base, [3]:indexfuncs, [4]:baseindex
//param --- [1]: obj, [2]: key, [3]: value
LUA_API int cls_newindexer(lua_State *L) {
if (!lua_isnil(L, lua_upvalueindex(1))) {
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(1));
if (!lua_isnil(L, -1)) {//has setter
lua_pushvalue(L, 3);
lua_call(L, 1, 0);
return 0;
}
}
if (!lua_isnil(L, lua_upvalueindex(2))) {
lua_pushvalue(L, lua_upvalueindex(2));
while(!lua_isnil(L, -1)) {
lua_pushvalue(L, -1);
lua_gettable(L, lua_upvalueindex(3));
if (!lua_isnil(L, -1)) // found
{
lua_replace(L, lua_upvalueindex(4)); //baseindex = indexfuncs[base]
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
lua_getfield(L, -1, "BaseType");
lua_remove(L, -2);
}
lua_pushnil(L);
lua_replace(L, lua_upvalueindex(2));//base = nil
}
if (!lua_isnil(L, lua_upvalueindex(4))) {
lua_settop(L, 3);
lua_pushvalue(L, lua_upvalueindex(4));
lua_insert(L, 1);
lua_call(L, 3, 0);
return 0;
} else {
return luaL_error(L, "no static field %s", lua_tostring(L, 2));
}
}
LUA_API int gen_cls_newindexer(lua_State *L) {
lua_pushnil(L);
lua_pushcclosure(L, cls_newindexer, 4);
return 0;
}
LUA_API int errorfunc(lua_State *L) {
lua_getglobal(L, "debug");
lua_getfield(L, -1, "traceback");
lua_remove(L, -2);
lua_pushvalue(L, 1);
lua_pushnumber(L, 2);
lua_call(L, 2, 1);
return 1;
}
LUA_API int get_error_func_ref(lua_State *L) {
lua_pushcclosure(L, errorfunc, 0);
return luaL_ref(L, LUA_REGISTRYINDEX);
}
LUA_API int load_error_func(lua_State *L, int ref) {
lua_rawgeti(L, LUA_REGISTRYINDEX, ref);
return lua_gettop(L);
}
LUA_API int pcall_prepare(lua_State *L, int error_func_ref, int func_ref) {
lua_rawgeti(L, LUA_REGISTRYINDEX, error_func_ref);
lua_rawgeti(L, LUA_REGISTRYINDEX, func_ref);
return lua_gettop(L) - 1;
}
static void hook(lua_State *L, lua_Debug *ar)
{
int event;
lua_pushlightuserdata(L, &hook_index);
lua_rawget(L, LUA_REGISTRYINDEX);
event = ar->event;
lua_pushstring(L, hooknames[event]);
lua_getinfo(L, "nS", ar);
if (*(ar->what) == 'C') {
lua_pushfstring(L, "[?%s]", ar->name);
} else {
lua_pushfstring(L, "%s:%d", ar->short_src, ar->linedefined > 0 ? ar->linedefined : 0);
}
lua_call(L, 2, 0);
}
static void call_ret_hook(lua_State *L) {
lua_Debug ar;
if (lua_gethook(L)) {
lua_getstack(L, 0, &ar);
lua_getinfo(L, "n", &ar);
lua_pushlightuserdata(L, &hook_index);
lua_rawget(L, LUA_REGISTRYINDEX);
if (lua_type(L, -1) != LUA_TFUNCTION){
lua_pop(L, 1);
return;
}
lua_pushliteral(L, "return");
lua_pushfstring(L, "[?%s]", ar.name);
lua_pushliteral(L, "[C#]");
lua_sethook(L, 0, 0, 0);
lua_call(L, 3, 0);
lua_sethook(L, hook, LUA_MASKCALL | LUA_MASKRET, 0);
}
}
static int profiler_set_hook(lua_State *L) {
if (lua_isnoneornil(L, 1)) {
lua_pushlightuserdata(L, &hook_index);
lua_pushnil(L);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_sethook(L, 0, 0, 0);
} else {
luaL_checktype(L, 1, LUA_TFUNCTION);
lua_pushlightuserdata(L, &hook_index);
lua_pushvalue(L, 1);
lua_rawset(L, LUA_REGISTRYINDEX);
lua_sethook(L, hook, LUA_MASKCALL | LUA_MASKRET, 0);
}
return 0;
}
static int csharp_function_wrap(lua_State *L) {
lua_CFunction fn = (lua_CFunction)lua_tocfunction(L, lua_upvalueindex(1));
int ret = fn(L);
if (lua_toboolean(L, lua_upvalueindex(2)))
{
lua_pushboolean(L, 0);
lua_replace(L, lua_upvalueindex(2));
return lua_error(L);
}
if (lua_gethook(L)) {
call_ret_hook(L);
}
return ret;
}
LUA_API void xlua_push_csharp_function(lua_State* L, lua_CFunction fn, int n)
{
lua_pushcfunction(L, fn);
if (n > 0) {
lua_insert(L, -1 - n);
}
lua_pushboolean(L, 0);
if (n > 0) {
lua_insert(L, -1 - n);
}
lua_pushcclosure(L, csharp_function_wrap, 2 + (n > 0 ? n : 0));
}
typedef int (*lua_CSWrapperCaller) (lua_State *L, int wrapperid, int top);
static lua_CSWrapperCaller g_csharp_wrapper_caller = NULL;
LUA_API void xlua_set_csharp_wrapper_caller(lua_CSWrapperCaller wrapper_caller)
{
g_csharp_wrapper_caller = wrapper_caller;
}
static int csharp_function_wrapper_wrapper(lua_State *L) {
int ret = 0;
if (g_csharp_wrapper_caller == NULL) {
return luaL_error(L, "g_csharp_wrapper_caller not set");
}
ret = g_csharp_wrapper_caller(L, xlua_tointeger(L, lua_upvalueindex(1)), lua_gettop(L));
if (lua_toboolean(L, lua_upvalueindex(2)))
{
lua_pushboolean(L, 0);
lua_replace(L, lua_upvalueindex(2));
return lua_error(L);
}
if (lua_gethook(L)) {
call_ret_hook(L);
}
return ret;
}
LUA_API void xlua_push_csharp_wrapper(lua_State* L, int wrapperid)
{
lua_pushinteger(L, wrapperid);
lua_pushboolean(L, 0);
lua_pushcclosure(L, csharp_function_wrapper_wrapper, 2);
}
LUALIB_API int xlua_upvalueindex(int n) {
return lua_upvalueindex(2 + n);
}
LUALIB_API int xlua_csharp_str_error(lua_State* L, const char* msg)
{
lua_pushboolean(L, 1);
lua_replace(L, lua_upvalueindex(2));
lua_pushstring(L, msg);
return 1;
}
LUALIB_API int xlua_csharp_error(lua_State* L)
{
lua_pushboolean(L, 1);
lua_replace(L, lua_upvalueindex(2));
return 1;
}
typedef struct {
int fake_id;
unsigned int len;
char data[1];
} CSharpStruct;
LUA_API void *xlua_pushstruct(lua_State *L, unsigned int size, int meta_ref) {
CSharpStruct *css = (CSharpStruct *)lua_newuserdata(L, size + sizeof(int) + sizeof(unsigned int));
css->fake_id = -1;
css->len = size;
lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);
lua_setmetatable(L, -2);
return css;
}
LUA_API void xlua_pushcstable(lua_State *L, unsigned int size, int meta_ref) {
lua_createtable(L, 0, size);
lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);
lua_setmetatable(L, -2);
}
LUA_API void *xlua_newstruct(lua_State *L, int size, int meta_ref) {
CSharpStruct *css = (CSharpStruct *)lua_newuserdata(L, size + sizeof(int) + sizeof(unsigned int));
css->fake_id = -1;
css->len = size;
lua_rawgeti(L, LUA_REGISTRYINDEX, meta_ref);
lua_setmetatable(L, -2);
return css->data;
}
LUA_API void *xlua_tostruct(lua_State *L, int idx, int meta_ref) {
CSharpStruct *css = (CSharpStruct *)lua_touserdata(L, idx);
if (NULL != css) {
if (lua_getmetatable (L, idx)) {
lua_rawgeti(L, -1, 1);
if (lua_type(L, -1) == LUA_TNUMBER && (int)lua_tointeger(L, -1) == meta_ref) {
lua_pop(L, 2);
return css->data;
}
lua_pop(L, 2);
}
}
return NULL;
}
LUA_API int xlua_gettypeid(lua_State *L, int idx) {
int type_id = -1;
if (lua_type(L, idx) == LUA_TUSERDATA) {
if (lua_getmetatable (L, idx)) {
lua_rawgeti(L, -1, 1);
if (lua_type(L, -1) == LUA_TNUMBER) {
type_id = (int)lua_tointeger(L, -1);
}
lua_pop(L, 2);
}
}
return type_id;
}
#define PACK_UNPACK_OF(type) \
LUALIB_API int xlua_pack_##type(void *p, int offset, type field) {\
CSharpStruct *css = (CSharpStruct *)p;\
if (css->fake_id != -1 || css->len < offset + sizeof(field)) {\
return 0;\
} else {\
memcpy((&(css->data[0]) + offset), &field, sizeof(field));\
return 1;\
}\
}\
\
LUALIB_API int xlua_unpack_##type(void *p, int offset, type *pfield) { \
CSharpStruct *css = (CSharpStruct *)p;\
if (css->fake_id != -1 || css->len < offset + sizeof(*pfield)) {\
return 0;\
} else {\
memcpy(pfield, (&(css->data[0]) + offset), sizeof(*pfield));\
return 1;\
}\
}\
PACK_UNPACK_OF(int8_t);
PACK_UNPACK_OF(int16_t);
PACK_UNPACK_OF(int32_t);
PACK_UNPACK_OF(int64_t);
PACK_UNPACK_OF(float);
PACK_UNPACK_OF(double);
LUALIB_API int xlua_pack_float2(void *p, int offset, float f1, float f2) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 2) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
pos[0] = f1;
pos[1] = f2;
return 1;
}
}
LUALIB_API int xlua_unpack_float2(void *p, int offset, float *f1, float *f2) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 2) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
*f1 = pos[0];
*f2 = pos[1];
return 1;
}
}
LUALIB_API int xlua_pack_float3(void *p, int offset, float f1, float f2, float f3) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
pos[0] = f1;
pos[1] = f2;
pos[2] = f3;
return 1;
}
}
LUALIB_API int xlua_unpack_float3(void *p, int offset, float *f1, float *f2, float *f3) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 3) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
*f1 = pos[0];
*f2 = pos[1];
*f3 = pos[2];
return 1;
}
}
LUALIB_API int xlua_pack_float4(void *p, int offset, float f1, float f2, float f3, float f4) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 4) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
pos[0] = f1;
pos[1] = f2;
pos[2] = f3;
pos[3] = f4;
return 1;
}
}
LUALIB_API int xlua_unpack_float4(void *p, int offset, float *f1, float *f2, float *f3, float *f4) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 4) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
*f1 = pos[0];
*f2 = pos[1];
*f3 = pos[2];
*f4 = pos[3];
return 1;
}
}
LUALIB_API int xlua_pack_float5(void *p, int offset, float f1, float f2, float f3, float f4, float f5) {
CSharpStruct *css = (CSharpStruct *)p;
if (css->fake_id != -1 || css->len < offset + sizeof(float) * 5) {
return 0;
} else {
float *pos = (float *)(&(css->data[0]) + offset);
pos[0] = f1;
pos[1] = f2;
pos[2] = f3;
pos[3] = f4;
pos[4] = f5;
return 1;