forked from starwing/nui
/
lbind.c
1299 lines (1161 loc) · 32.6 KB
/
lbind.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
/* vim: set sw=2: */
#define LUA_LIB
#include "lbind.h"
#include <assert.h>
#include <string.h>
/* compatible apis */
#if LUA_VERSION_NUM < 502
LUA_API lua_Integer lua_tointegerx(lua_State *L, int idx, int *valid) {
lua_Integer n;
*valid = (n = lua_tointeger(L, idx)) != 0 || lua_isnumber(L, idx);
return n;
}
LUA_API void lua_rawgetp(lua_State *L, int idx, const void *p) {
lua_pushlightuserdata(L, (void*)p);
lua_rawget(L, lbind_relindex(idx, 1));
}
LUA_API void lua_rawsetp(lua_State *L, int idx, const void *p) {
lua_pushlightuserdata(L, (void*)p);
lua_insert(L, -2);
lua_rawset(L, lbind_relindex(idx, 1));
}
LUALIB_API void luaL_setfuncs(lua_State *L, luaL_Reg *l, int nup) {
luaL_checkstack(L, nup, "too many upvalues");
for (; l->name != NULL; l++) { /* fill the table with given functions */
int i;
for (i = 0; i < nup; i++) /* copy upvalues to the top */
lua_pushvalue(L, -nup);
lua_pushcclosure(L, l->func, nup); /* closure with those upvalues */
lua_setfield(L, -(nup + 2), l->name);
}
lua_pop(L, nup); /* remove upvalues */
}
LUALIB_API const char *luaL_tolstring(lua_State *L, int idx, size_t *plen) {
if (!luaL_callmeta(L, idx, "__tostring")) { /* no metafield? */
switch (lua_type(L, idx)) {
case LUA_TNUMBER:
case LUA_TSTRING:
lua_pushvalue(L, idx);
break;
case LUA_TBOOLEAN:
lua_pushstring(L, (lua_toboolean(L, idx) ? "true" : "false"));
break;
case LUA_TNIL:
lua_pushliteral(L, "nil");
break;
default:
lua_pushfstring(L, "%s: %p", luaL_typename(L, idx),
lua_topointer(L, idx));
break;
}
}
return lua_tolstring(L, -1, plen);
}
LUALIB_API void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level) {
lua_Debug ar;
int top = lua_gettop(L);
int numlevels = countlevels(L1);
int mark = (numlevels > LEVELS1 + LEVELS2) ? LEVELS1 : 0;
if (msg) lua_pushfstring(L, "%s\n", msg);
lua_pushliteral(L, "stack traceback:");
while (lua_getstack(L1, level++, &ar)) {
if (level == mark) { /* too many levels? */
lua_pushliteral(L, "\n\t..."); /* add a '...' */
level = numlevels - LEVELS2; /* and skip to last ones */
}
else {
lua_getinfo(L1, "Slnt", &ar);
lua_pushfstring(L, "\n\t%s:", ar.short_src);
if (ar.currentline > 0)
lua_pushfstring(L, "%d:", ar.currentline);
lua_pushliteral(L, " in ");
pushfuncname(L, &ar);
if (ar.istailcall)
lua_pushliteral(L, "\n\t(...tail calls...)");
lua_concat(L, lua_gettop(L) - top);
}
}
lua_concat(L, lua_gettop(L) - top);
}
#endif /* LUA_VERSION_NUM < 502 */
/* lbind information hash routine */
#define LBIND_PTRBOX 0x90127B07
#define LBIND_TYPEBOX 0x799E0B07
#define LBIND_UDBOX 0xC5E7DB07
static int get_box(lua_State *L, unsigned id) {
lua_rawgetp(L, LUA_REGISTRYINDEX, (void*)id);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)id);
return 1;
}
return 0;
}
static void get_internbox(lua_State *L) {
if (get_box(L, LBIND_PTRBOX)) {
lua_pushliteral(L, "v");
lbind_setmetafield(L, -2, "__mode");
}
}
static void get_typebox(lua_State *L) {
get_box(L, LBIND_TYPEBOX);
}
/* light userdata utils */
LB_API int lbind_getudtypebox(lua_State *L) {
return get_box(L, LBIND_UDBOX);
}
LB_API void lbind_setlightuservalue(lua_State *L, const void *p) {
lbind_getudtypebox(L);
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, p);
lua_pop(L, 1);
}
LB_API int lbind_getlightuservalue(lua_State *L, const void *p) {
lbind_getudtypebox(L);
lua_rawgetp(L, -1, p);
if (lua_isnil(L, -1)) {
lua_pop(L, 2);
return 0;
}
lua_remove(L, 2);
return 1;
}
/* lbind class register */
LB_API void lbind_install(lua_State *L, lbind_Reg *libs) {
lua_getfield(L, LUA_REGISTRYINDEX, "_PRELOAD"); /* 1 */
if (libs != NULL) {
for (; libs->name != NULL; ++libs) {
lua_pushstring(L, libs->name); /* 2 */
lua_pushcfunction(L, libs->open_func); /* 3 */
lua_rawset(L, -3); /* 2,3->1 */
}
}
#ifndef LBIND_NO_RUNTIME
lua_pushstring(L, "lbind"); /* 2 */
lua_pushcfunction(L, luaopen_lbind); /* 3 */
lua_rawset(L, -3); /* 2,3->1 */
#endif
lua_pop(L, 1); /* (1) */
}
LB_API int lbind_requiref(lua_State *L, const char *name, lua_CFunction loader) {
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); /* 1 */
lua_getfield(L, -1, name); /* 2 */
if (!lua_isnil(L, -1)) {
lua_remove(L, -2); /* (1) */
return 0;
}
lua_pop(L, 1);
lua_pushstring(L, name); /* 2 */
lua_pushcfunction(L, loader); /* 3 */
lua_pushvalue(L, -2); /* 2->4 */
lua_call(L, 1, 1); /* 3,4->3 */
lua_pushvalue(L, -1); /* 3->4 */
lua_insert(L, -4); /* 4->1 */
/* stack: lib _LOADED name lib */
lua_rawset(L, -3); /* 3,4->2 */
lua_pop(L, 1); /* (2) */
return 1;
}
LB_API void lbind_requirelibs(lua_State *L, lbind_Reg *libs) {
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); /* 1 */
for (; libs->name != NULL; ++libs) {
lua_pushstring(L, libs->name); /* 2 */
lua_pushvalue(L, -1); /* 3 */
lua_rawget(L, -3); /* 3->3 */
if (!lua_isnil(L, -1)) {
lua_pop(L, 2);
continue;
}
lua_pop(L, 1);
lua_pushcfunction(L, libs->open_func); /* 3 */
lua_pushvalue(L, -2); /* 2->4 */
lua_call(L, 1, 1); /* 3,4->3 */
lua_rawset(L, -5); /* 2,3->1 */
}
lua_pop(L, 1);
}
LB_API void lbind_requireinto(lua_State *L, const char *prefix, lbind_Reg *libs) {
/* stack: table */
lua_getfield(L, LUA_REGISTRYINDEX, "_LOADED"); /* 1 */
for (; libs->name != NULL; ++libs) {
lua_pushstring(L, libs->name); /* 2 */
if (prefix == NULL)
lua_pushvalue(L, -1); /* 3 */
else
lua_pushfstring(L, "%s.%s", prefix, libs->name); /* 3 */
lua_pushvalue(L, -1); /* 4 */
lua_rawget(L, -4); /* 4->4 */
if (lua_isnil(L, -1)) {
lua_pop(L, 1); /* (4) */
lua_pushcfunction(L, libs->open_func); /* 4 */
lua_pushvalue(L, -2); /* 3->5 */
lua_call(L, 1, 1); /* 4,5->4 */
lua_pushvalue(L, -2); /* 3->5 */
lua_pushvalue(L, -2); /* 4->6 */
/* stack: table [_LOADED name prefix.name ret prefix.name ret] */
lua_rawset(L, -6); /* 4,5->1 */
}
lua_remove(L, -2); /* (3) */
lua_rawset(L, -4); /* 2,3->table */
}
lua_pop(L, 1);
}
/* lbind utils functions */
LB_API int lbind_relindex(int idx, int onstack) {
return (idx > 0 || idx <= LUA_REGISTRYINDEX)
? idx
: idx - onstack;
}
LB_API int lbind_argferror(lua_State *L, int idx, const char *fmt, ...) {
const char *errmsg;
va_list argp;
va_start(argp, fmt);
errmsg = lua_pushvfstring(L, fmt, argp);
va_end(argp);
return luaL_argerror(L, idx, errmsg);
}
LB_API int lbind_typeerror(lua_State *L, int idx, const char *tname) {
const char *real_type = lbind_type(L, idx);
return lbind_argferror(L, idx, "%s expected, got %s", tname,
real_type != NULL ? real_type : luaL_typename(L, idx));
}
LB_API int lbind_matcherror(lua_State *L, const char *extramsg) {
lua_Debug ar;
lua_getinfo(L, "n", &ar);
if (ar.name == NULL)
ar.name = "?";
return luaL_error(L, "no matching functions for call to %s\n"
"candidates are:\n%s", ar.name, extramsg);
}
LB_API int lbind_copystack(lua_State *from, lua_State *to, int n) {
int i;
luaL_checkstack(from, n, "too many args");
for (i = 0; i < n; ++i)
lua_pushvalue(from, -n);
lua_xmove(from, to, n);
return n;
}
LB_API const char *lbind_dumpstack(lua_State *L, const char *msg) {
int i, top = lua_gettop(L);
luaL_Buffer b;
luaL_buffinit(L, &b);
luaL_addstring(&b, "dump stack: ");
luaL_addstring(&b, msg != NULL ? msg : "");
luaL_addstring(&b, "\n---------------------------\n");
for (i = 1; i <= top; ++i) {
lua_pushfstring(L, "%d: ", i);
luaL_addvalue(&b);
lbind_tolstring(L, i, NULL);
luaL_addvalue(&b);
luaL_addstring(&b, "\n");
}
luaL_addstring(&b, "---------------------------\n");
luaL_pushresult(&b);
return lua_tostring(L, -1);
}
LB_API int lbind_hasfield(lua_State *L, int idx, const char *field) {
int hasfield;
lua_getfield(L, idx, field);
hasfield = !lua_isnil(L, -1);
lua_pop(L, 1);
return hasfield;
}
static int Ltraceback(lua_State *L) {
const char *msg = lua_tostring(L, 1);
if (msg)
luaL_traceback(L, L, msg, 1);
else if (!lua_isnoneornil(L, 1)) { /* is there an error object? */
if (!luaL_callmeta(L, 1, "__tostring")) /* try its 'tostring' metamethod */
lua_pushliteral(L, "(no error message)");
}
return 1;
}
LB_API int lbind_self(lua_State *L, const void *p, const char *method, int nargs, int *ptraceback) {
luaL_checkstack(L, nargs+3, "too many arguments to self call");
if (!lbind_retrieve(L, p)) return 0; /* 1 */
lua_getfield(L, -1, method); /* 2 */
if (lua_isnil(L, -1)) {
lua_pop(L, 2);
return 0;
}
if (ptraceback) {
lua_pushcfunction(L, Ltraceback);
lua_insert(L, -3);
*ptraceback = lua_gettop(L) - 3;
}
lua_insert(L, -2);
/* stack: traceback method object */
return 1;
}
LB_API int lbind_pcall(lua_State *L, int nargs, int nrets) {
int res, tb_idx;
lua_pushcfunction(L, Ltraceback);
lua_insert(L, -nargs-2);
tb_idx = lua_gettop(L)-nargs-1;
res = lua_pcall(L, nargs, nrets, tb_idx);
lua_remove(L, tb_idx);
return res;
}
/* metatable utils */
LB_API int lbind_setmetatable(lua_State *L, const void *t) {
if (lbind_getmetatable(L, t)) {
lua_setmetatable(L, -2);
return 1;
}
return 0;
}
LB_API int lbind_getmetatable(lua_State *L, const void *t) {
lua_rawgetp(L, LUA_REGISTRYINDEX, t);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
return 0;
}
return 1;
}
LB_API int lbind_setmetafield(lua_State *L, int idx, const char *field) {
int newmt = 0;
if (!lua_getmetatable(L, idx)) {
lua_createtable(L, 0, 1);
lua_pushvalue(L, -1);
lua_setmetatable(L, lbind_relindex(idx, 2));
newmt = 1;
}
lua_pushvalue(L, -2);
lua_setfield(L, -2, field);
lua_pop(L, 2);
return newmt;
}
static int Llibcall(lua_State *L) {
lua_pushvalue(L, lua_upvalueindex(1));
lua_rawget(L, 1);
if (lua_isnil(L, -1)) {
lua_pushfstring(L, "no such method (%s)", lua_tostring(L, lua_upvalueindex(1)));
return luaL_argerror(L, 1, lua_tostring(L, -1));
}
lua_replace(L, 1);
lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
return lua_gettop(L);
}
LB_API int lbind_setlibcall(lua_State *L, const char *method) {
if (method == NULL) method = "new";
lua_pushstring(L, method);
lua_pushcclosure(L, Llibcall, 1);
return lbind_setmetafield(L, -2, "__call");
}
static int call_accessor(lua_State *L, int idx, int nargs) {
lua_CFunction f = lua_tocfunction(L, idx);
if (f != NULL) {
lua_settop(L, nargs);
return f(L);
}
return -1;
}
static int call_lut(lua_State *L, int idx, int nargs) {
lua_CFunction f = lua_tocfunction(L, idx);
/* look up table */
if (f == NULL) {
lua_pushvalue(L, 2);
lua_rawget(L, lbind_relindex(idx, 1));
f = lua_tocfunction(L, -1);
}
if (f != NULL) {
lua_settop(L, nargs);
return f(L);
}
return -1;
}
static int Lnewindex(lua_State *L) {
int nret;
/* upvalue: seti, seth */
/* order:
* - lut
* - accessor
* - normaltable
* - uservalue
*/
if (!lua_isnone(L, lua_upvalueindex(1)) &&
(nret = call_lut(L, lua_upvalueindex(1), 3)) >= 0)
return nret;
if (!lua_isnone(L, lua_upvalueindex(2)) &&
(nret = call_accessor(L, lua_upvalueindex(2), 3)) >= 0)
return nret;
if (!lua_isuserdata(L, 1)) {
lua_settop(L, 3);
lua_rawset(L, 1);
return 0;
}
lua_getuservalue(L, 1);
if (lua_isnil(L, -1)) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_setuservalue(L, 1);
}
lua_pushvalue(L, 2);
lua_pushvalue(L, 3);
lua_rawset(L, -3);
return 0;
}
static int Lindex(lua_State *L) {
int i, nret;
/* upvalue: seti, seth, tables */
/* order:
* - uservalue
* - metatable
* - lut
* - accessor
* - upvalue tables
*/
if (lua_isuserdata(L, 1)) {
lua_getuservalue(L, 1);
if (!lua_isnil(L, -1)) {
lua_pushvalue(L, 2);
lua_rawget(L, -2);
if (!lua_isnil(L, -1))
return 1;
}
}
if (lua_getmetatable(L, 1)) {
lua_pushvalue(L, 2);
lua_rawget(L, -2);
if (!lua_isnil(L, -1))
return 1;
}
if (!lua_isnone(L, lua_upvalueindex(1)) &&
(nret = call_lut(L, lua_upvalueindex(1), 2)) >= 0)
return nret;
if (!lua_isnone(L, lua_upvalueindex(2)) &&
(nret = call_accessor(L, lua_upvalueindex(2), 2)) >= 0)
return nret;
/* find in libtable/superlibtable */
for (i = 3; !lua_isnone(L, lua_upvalueindex(i)); ++i) {
lua_settop(L, 2);
if (lua_islightuserdata(L, lua_upvalueindex(i))) {
if (!lbind_getmetatable(L, lua_touserdata(L, lua_upvalueindex(i))))
continue;
lua_replace(L, lua_upvalueindex(i));
}
lua_pushvalue(L, 2);
lua_gettable(L, lua_upvalueindex(i));
if (!lua_isnil(L, -1))
return 1;
}
return 0;
}
static void push_indexf(lua_State *L, int ntables) {
lua_pushnil(L);
lua_insert(L, -ntables-1);
lua_pushnil(L);
lua_insert(L, -ntables-1);
lua_pushcclosure(L, Lindex, ntables+2);
}
static void push_newindexf(lua_State *L) {
lua_pushnil(L);
lua_pushnil(L);
lua_pushcclosure(L, Lnewindex, 2);
}
LB_API void lbind_setindexf(lua_State *L, int ntables) {
push_indexf(L, ntables);
lua_setfield(L, -2, "__index");
}
static void get_default_metafield(lua_State *L, int idx, int field) {
if (field == LBIND_INDEX) {
lua_getfield(L, idx, "__index");
if (lua_isnil(L, -1) || lua_tocfunction(L, -1) != Lindex) {
lua_pop(L, 1);
push_indexf(L, 0);
lua_pushvalue(L, -1);
lua_setfield(L, lbind_relindex(idx, 2), "__index");
}
}
else if (field == LBIND_NEWINDEX) {
lua_getfield(L, idx, "__newindex");
if (lua_isnil(L, -1) || lua_tocfunction(L, -1) != Lnewindex) {
lua_pop(L, 1);
push_newindexf(L);
lua_pushvalue(L, -1);
lua_setfield(L, lbind_relindex(idx, 2), "__newindex");
}
}
}
static void set_cfuncupvalue(lua_State *L, lua_CFunction f, int field, int idx) {
if ((field & LBIND_INDEX) != 0) {
get_default_metafield(L, -1, LBIND_INDEX);
lua_pushcfunction(L, f);
lua_setupvalue(L, -2, idx);
lua_pop(L, 1);
}
if ((field & LBIND_NEWINDEX) != 0) {
get_default_metafield(L, -1, LBIND_NEWINDEX);
lua_pushcfunction(L, f);
lua_setupvalue(L, -2, idx);
lua_pop(L, 1);
}
}
LB_API void lbind_sethashf(lua_State *L, lua_CFunction f, int field) {
set_cfuncupvalue(L, f, field, 1);
}
LB_API void lbind_setarrayf(lua_State *L, lua_CFunction f, int field) {
set_cfuncupvalue(L, f, field, 2);
}
LB_API void lbind_setmaptable(lua_State *L, luaL_Reg libs[], int field) {
lua_newtable(L);
luaL_setfuncs(L, libs, 0);
if ((field & LBIND_INDEX) != 0) {
get_default_metafield(L, -2, LBIND_INDEX);
lua_pushvalue(L, -2);
lua_setupvalue(L, -2, 1);
lua_pop(L, 1);
}
if ((field & LBIND_NEWINDEX) != 0) {
get_default_metafield(L, -2, LBIND_NEWINDEX);
lua_pushvalue(L, -2);
lua_setupvalue(L, -2, 1);
lua_pop(L, 1);
}
lua_pop(L, 1);
}
/* lbind userdata maintain */
typedef union {
lbind_MaxAlign dummy; /* ensures maximum alignment for `intern' object */
struct {
void *instance;
int flags;
} o;
} lbind_Object;
#define check_size(L,n) (lua_rawlen((L),(n)) >= sizeof(lbind_Object))
static lbind_Object *newobj(lua_State *L, size_t objsize, int flags) {
lbind_Object *obj;
obj = (lbind_Object*)lua_newuserdata(L, sizeof(lbind_Object) + objsize);
obj->o.flags = flags;
obj->o.instance = (void*)(obj+1);
if (objsize != 0 && (flags & LBIND_INTERN) != 0)
lbind_intern(L, obj->o.instance);
return obj;
}
static lbind_Object *testobj(lua_State *L, int idx) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
if (obj != NULL) {
if (!check_size(L, idx) || obj->o.instance == NULL)
obj = NULL;
#if 0
else {
get_internbox(L); /* 1 */
lua_rawgetp(L, -1, obj->o.instance); /* 2 */
if (!lua_rawequal(L, lbind_relindex(idx, 2), -1))
obj = NULL;
lua_pop(L, 2); /* (2)(1) */
}
#endif
}
return obj;
}
LB_API void *lbind_raw(lua_State *L, size_t objsize, int intern) {
return newobj(L, objsize, intern ? LBIND_INTERN : 0)->o.instance;
}
LB_API void *lbind_new(lua_State *L, size_t objsize, const lbind_Type *t) {
lbind_Object *obj = newobj(L, objsize, t->flags);
if (lbind_getmetatable(L, t))
lua_setmetatable(L, -2);
return obj->o.instance;
}
LB_API void *lbind_wrap(lua_State *L, void *p, const lbind_Type *t) {
lbind_Object *obj = newobj(L, 0, t->flags);
obj->o.instance = p;
if ((obj->o.flags & LBIND_INTERN) != 0)
lbind_intern(L, p);
if (lbind_getmetatable(L, t))
lua_setmetatable(L, -2);
return p;
}
LB_API void *lbind_delete(lua_State *L, int idx) {
void *u = NULL;
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
if (obj != NULL) {
if (!check_size(L, idx))
return NULL;
if ((u = obj->o.instance) != NULL) {
obj->o.instance = NULL;
obj->o.flags &= ~LBIND_TRACK;
#if LUA_VERSION_NUM < 502
get_internbox(L); /* 1 */
lua_pushnil(L); /* 2 */
lua_rawsetp(L, -3, u); /* 2->1 */
lua_pop(L, 1); /* (1) */
#endif
}
}
return u;
}
LB_API void *lbind_object(lua_State *L, int idx) {
lbind_Object *obj = testobj(L, idx);
return obj == NULL ? NULL : obj->o.instance;
}
LB_API void lbind_intern(lua_State *L, const void *p) {
/* stack: object */
get_internbox(L);
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, p);
lua_pop(L, 1);
}
LB_API int lbind_retrieve(lua_State *L, const void *p) {
get_internbox(L); /* 1 */
lua_rawgetp(L, -1, p); /* 2 */
if (lua_isnil(L, -1)) {
lua_pop(L, 2);
return 0;
}
lua_remove(L, -2);
return 1;
}
LB_API void lbind_track(lua_State *L, int idx) {
lbind_Object *obj = testobj(L, idx);
if (obj != NULL)
obj->o.flags |= LBIND_TRACK;
}
LB_API void lbind_untrack(lua_State *L, int idx) {
lbind_Object *obj = testobj(L, idx);
if (obj != NULL)
obj->o.flags &= ~LBIND_TRACK;
}
LB_API int lbind_hastrack(lua_State *L, int idx) {
lbind_Object *obj = testobj(L, idx);
return obj != NULL && (obj->o.flags & LBIND_TRACK) != 0;
}
/* lbind type registry */
LB_API void lbind_inittype(lbind_Type *t, const char *name) {
t->name = name;
t->flags = LBIND_DEFAULT_FLAG;
t->cast = NULL;
t->bases = NULL;
}
LB_API void lbind_setbase(lbind_Type *t, lbind_Type **bases, lbind_Cast *cast) {
t->bases = bases;
t->cast = cast;
}
LB_API int lbind_settrack(lbind_Type *t, int autotrack) {
int old_flag = t->flags&LBIND_TRACK ? 1 : 0;
if (autotrack)
t->flags |= LBIND_TRACK;
else
t->flags &= ~LBIND_TRACK;
return old_flag;
}
LB_API int lbind_setintern(lbind_Type *t, int autointern) {
int old_flag = t->flags&LBIND_INTERN ? 1 : 0;
if (autointern)
t->flags |= LBIND_INTERN;
else
t->flags &= ~LBIND_INTERN;
return old_flag;
}
LB_API lbind_Type *lbind_typeobject(lua_State *L, int idx) {
lbind_Type *t = NULL;
if (lua_getmetatable(L, idx)) {
lua_getfield(L, -1, "__type");
t = (lbind_Type*)lua_touserdata(L, -1);
lua_pop(L, 2);
if (t != NULL)
return t;
}
if (lua_istable(L, idx)) {
lua_getfield(L, idx, "__type");
t = (lbind_Type*)lua_touserdata(L, -1);
lua_pop(L, 1);
}
return t;
}
/* lbind type metatable */
static int Ltostring(lua_State *L) {
lbind_tolstring(L, 1, NULL);
return 1;
}
static int Lgc(lua_State *L) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, 1);
if (obj != NULL && check_size(L, 1)) {
if ((obj->o.flags & LBIND_TRACK) != 0) {
lua_getfield(L, 1, "delete");
if (!lua_isnil(L, -1)) {
lua_pushvalue(L, 1);
lua_call(L, 1, 0);
}
if ((obj->o.flags & LBIND_TRACK) != 0)
lbind_delete(L, 1);
}
}
return 0;
}
static void register_type(lua_State *L, const char *name, const void *t) {
/* stack: metatable */
lua_pushvalue(L, -1);
lua_setfield(L, LUA_REGISTRYINDEX, name);
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, t);
get_typebox(L);
lua_pushvalue(L, -2);
lua_setfield(L, -2, name);
lua_pushvalue(L, -2);
lua_rawsetp(L, -2, t);
lua_pop(L, 1);
}
static int type_exists(lua_State *L, const lbind_Type *t) {
lua_rawgetp(L, LUA_REGISTRYINDEX, (const void*)t);
if (!lua_isnil(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_getfield(L, LUA_REGISTRYINDEX, t->name);
if (!lua_isnil(L, -1)) {
lua_pop(L, 1);
return 1;
}
lua_pop(L, 2);
return 0;
}
LB_API int lbind_newmetatable(lua_State *L, luaL_Reg *libs, const lbind_Type *t) {
if (type_exists(L, t)) return 0;
lua_createtable(L, 0, 8);
if (libs != NULL)
luaL_setfuncs(L, libs, 0);
/* init type metatable */
lua_pushlightuserdata(L, (void*)t);
lua_setfield(L, -2, "__type");
if (!lbind_hasfield(L, -1, "__gc")) {
lua_pushcfunction(L, Lgc);
lua_setfield(L, -2, "__gc");
}
if (!lbind_hasfield(L, -1, "__tostring")) {
lua_pushcfunction(L, Ltostring);
lua_setfield(L, -2, "__tostring");
}
if (t->bases != NULL && t->bases[0] != NULL) {
int nups = 0;
int freeslots = 0;
lbind_Type **bases = t->bases;
for (; *bases != NULL; ++nups, ++bases) {
if (nups > freeslots) {
luaL_checkstack(L, 10, "no space for base types");
freeslots += 10;
}
if (!lbind_getmetatable(L, *bases))
lua_pushlightuserdata(L, *bases);
}
lbind_setindexf(L, nups);
}
else if (!lbind_hasfield(L, -1, "__index")) {
lua_pushvalue(L, -1);
lua_setfield(L, -2, "__index");
}
register_type(L, t->name, (const void*)t);
return 1;
}
static int Lagency(lua_State *L) {
lua_pushvalue(L, lua_upvalueindex(1));
lua_gettable(L, 1);
lua_insert(L, 1);
lua_call(L, lua_gettop(L)-1, LUA_MULTRET);
return lua_gettop(L);
}
static void set_meta_agency(lua_State *L, const char *key) {
/* stack: libtable mt */
lua_pushstring(L, key);
lua_rawget(L, -3); /* do not track __index */
if (!lua_isnil(L, -1)) {
lua_pushfstring(L, "__%s", key);
lua_pushstring(L, key);
lua_pushcclosure(L, Lagency, 1);
lua_rawset(L, -4);
}
lua_pop(L, 1);
}
LB_API void lbind_setagency(lua_State *L) {
set_meta_agency(L, "len");
#if LUA_VERSION_NUM >= 502
set_meta_agency(L, "pairs");
set_meta_agency(L, "ipairs");
#endif /* LUA_VERSION_NUM >= 502 */
}
/* lbind type system */
LB_API const char *lbind_tolstring(lua_State *L, int idx, size_t *plen) {
const char *tname = lbind_type(L, idx);
lbind_Object *obj = testobj(L, idx);
if (obj != NULL && tname)
lua_pushfstring(L, "%s: %p", tname, obj->o.instance);
else if (obj == NULL) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
if (obj == NULL)
return luaL_tolstring(L, idx, plen);
if (tname && check_size(L, idx))
lua_pushfstring(L, "%s[N]: %p", tname, obj->o.instance);
else
lua_pushfstring(L, "userdata: %p", (void*)obj);
}
return lua_tolstring(L, -1, plen);
}
LB_API const char *lbind_type(lua_State *L, int idx) {
lbind_Type *t = lbind_typeobject(L, idx);
if (t != NULL) return t->name;
return NULL;
}
static int testtypemeta(lua_State *L, int idx, const lbind_Type *t) {
if (lua_getmetatable(L, idx)) { /* does it have a metatable? */
int res = 1;
if (!lbind_getmetatable(L, t)) { /* get correct metatable */
lua_pop(L, 1); /* no such metatable? fail */
return 0;
}
if (!lua_rawequal(L, -1, -2)) /* not the same? */
res = 0; /* value is a userdata with wrong metatable */
lua_pop(L, 2);
return res;
}
return 0;
}
LB_API void *try_cast(lua_State *L, int idx, const lbind_Type *t) {
lbind_Type *from_type = lbind_typeobject(L, idx);
if (from_type != NULL && from_type->cast != NULL)
return from_type->cast(L, idx, t);
return NULL;
}
LB_API int lbind_isa(lua_State *L, int idx, const lbind_Type *t) {
return testtypemeta(L, idx, t) || try_cast(L, idx, t) != NULL;
}
LB_API void *lbind_cast(lua_State *L, int idx, const lbind_Type *t) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
if (!check_size(L, idx) || obj == NULL || obj->o.instance == NULL)
return NULL;
return testtypemeta(L, idx, t) ? obj->o.instance : try_cast(L, idx, t);
}
LB_API int lbind_copy(lua_State *L, const void *obj, const lbind_Type *t) {
if (!lbind_getmetatable(L, t)) /* 1 */
return 0;
lua_pushliteral(L, "new"); /* 2 */
lua_rawget(L, -2); /* 2->2 */
if (lua_isnil(L, -1)) {
lua_pop(L, 2); /* (2)(1) */
return 0;
}
lua_remove(L, -2); /* (1) */
if (!lbind_retrieve(L, obj))
lbind_wrap(L, (void*)obj, t);
if (lua_pcall(L, 1, 1, 0) != LUA_OK) {
lua_pop(L, 1);
return 0;
}
lbind_track(L, -1); /* enable autodeletion for copied stuff */
return 1;
}
LB_API void *lbind_check(lua_State *L, int idx, const lbind_Type *t) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
void *u = NULL;
if (!check_size(L, idx))
luaL_argerror(L, idx, "invalid lbind userdata");
if (obj != NULL && obj->o.instance == NULL)
luaL_argerror(L, idx, "null lbind object");
u = testtypemeta(L, idx, t) ? obj->o.instance : try_cast(L, idx, t);
if (u == NULL)
lbind_typeerror(L, idx, t->name);
return u;
}
LB_API void *lbind_test(lua_State *L, int idx, const lbind_Type *t) {
lbind_Object *obj = (lbind_Object*)lua_touserdata(L, idx);
return testtypemeta(L, idx, t) ? obj->o.instance : try_cast(L, idx, t);
}
/* lbind enum/mask support */
#ifndef LBIND_NO_ENUM
static const char *skip_white(const char *s) {
while (*s == '\t' || *s == '\n' || *s == '\r'
|| *s == ' ' || *s == '+' || *s == '|' || *s == ',')
++s;
return s;
}
static const char *skip_ident(const char *s) {
while ((*s >= 'A' && *s <= 'Z') ||
(*s >= 'a' && *s <= 'z') ||
(*s == '-' || *s == '_'))
++s;
return s;
}
static int parse_mask(lbind_Enum *et, const char *s, int *penum, lua_State *L) {
*penum = 0;
while (*s != '\0') {
const char *e;
int inversion = 0;
lbind_EnumItem *item;
s = skip_white(s);