-
Notifications
You must be signed in to change notification settings - Fork 34
/
lbind.h
1703 lines (1480 loc) · 46.7 KB
/
lbind.h
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
#ifndef LBIND_H
#define LBIND_H
#include <lua.h>
#include <lauxlib.h>
#if LUA_VERSION_NUM >= 503
# define lua53_getuservalue lua_getuservalue
# define lua53_gettable lua_gettable
# define lua53_getfield lua_getfield
# define lua53_rawget lua_rawget
# define lua53_rawgeti lua_rawgeti
# define lua53_rawgetp lua_rawgetp
# define lua53_rotate lua_rotate
#else
LUA_API int lua53_getuservalue(lua_State *L, int idx);
LUA_API int lua53_gettable(lua_State *L, int idx);
LUA_API int lua53_getfield(lua_State *L, int idx, const char *field);
LUA_API int lua53_rawget(lua_State *L, int idx);
LUA_API int lua53_rawgeti(lua_State *L, int idx, lua_Integer n);
LUA_API int lua53_rawgetp(lua_State *L, int idx, const void *p);
LUA_API void lua53_rotate(lua_State *L, int idx, int n);
#endif
#if LUA_VERSION_NUM < 502
# define LUA_OK 0
# define lua_getuservalue lua_getfenv
# define lua_setuservalue lua_setfenv
# define lua_rawlen lua_objlen
# define luaL_newlibtable(L,l) \
lua_createtable(L, 0, sizeof(l)/sizeof((l)[0]) - 1)
# define luaL_newlib(L,l) \
(luaL_newlibtable(L,l), luaL_setfuncs(L,l,0))
LUA_API lua_Integer (lua_tointegerx) (lua_State *L, int idx, int *valid);
LUA_API void (lua_rawsetp) (lua_State *L, int idx, const void *p);
LUA_API void (lua_rawgetp) (lua_State *L, int idx, const void *p);
LUALIB_API const char *(luaL_tolstring) (lua_State *L, int idx, size_t *len);
LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
#endif /* LUA_VERSION_NUM */
#ifdef __cplusplus
# define LB_NS_BEGIN extern "C" {
# define LB_NS_END }
#else
# define LB_NS_BEGIN
# define LB_NS_END
#endif
#ifdef LBIND_STATIC_API
# ifndef LBIND_IMPLEMENTATION
# define LBIND_IMPLEMENTATION
# endif
# if __GNUC__
# define LB_API static __attribute((unused))
# else
# define LB_API static
# endif
#endif
#if !defined(LB_API) && defined(_WIN32)
# ifdef LBIND_IMPLEMENTATION
# define LB_API __declspec(dllexport)
# else
# define LB_API __declspec(dllimport)
# endif
#endif
#ifndef LB_API
# define LB_API extern
#endif
#if defined(_WIN32)
# define LBLIB_API __declspec(dllexport)
#else
# define LBLIB_API extern
#endif
/* lbind internal max alignment */
#ifndef LBIND_MAXALIGN
# define LBIND_MAXALIGN union { double u; void *s; long l; }
#endif
LB_NS_BEGIN
typedef LBIND_MAXALIGN lbind_MaxAlign;
/* lbind runtime */
#ifndef LBIND_NO_RUNTIME
LBLIB_API int luaopen_lbind (lua_State *L);
#endif /* LBIND_NO_RUNTIME */
/* lbind utils functions */
LB_API int lbind_relindex (int idx, int onstack);
LB_API int lbind_argferror (lua_State *L, int idx, const char *fmt, ...);
LB_API int lbind_typeerror (lua_State *L, int idx, const char *tname);
LB_API int lbind_matcherror (lua_State *L, const char *extramsg);
LB_API int lbind_copystack (lua_State *from, lua_State *to, int nargs);
LB_API int lbind_hasfield (lua_State *L, int idx, const char *field);
LB_API int lbind_self (lua_State *L, const void *p, const char *method, int nargs, int *ptraceback);
LB_API int lbind_pcall (lua_State *L, int nargs, int nrets);
LB_API const char *lbind_dumpstack (lua_State *L, const char *extramsg);
#define lbind_returnself(L) do { lua_settop((L), 1); return 1; } while (0)
#define lbind_printstack(L, msg) ( printf("%s\n", lbind_dumpstack((L), (msg))), lua_pop((L), 1) )
/* lbind lua module install */
typedef struct lbind_Reg {
const char *name; /* name of library */
lua_CFunction open_func; /* luaopen_ function of library */
} lbind_Reg;
LB_API void lbind_install (lua_State *L, lbind_Reg *reg);
LB_API int lbind_requiref (lua_State *L, const char *name, lua_CFunction loader);
LB_API void lbind_requirelibs (lua_State *L, lbind_Reg *reg);
LB_API void lbind_requireinto (lua_State *L, const char *prefix, lbind_Reg *reg);
/* metatable maintain */
LB_API int lbind_setmetatable (lua_State *L, const void *t);
LB_API int lbind_getmetatable (lua_State *L, const void *t);
LB_API int lbind_setmetafield (lua_State *L, int idx, const char *field);
LB_API int lbind_setlibcall (lua_State *L, const char *method);
#define LBIND_INDEX 0x01
#define LBIND_NEWINDEX 0x02
LB_API void lbind_setaccessors (lua_State *L, int ntables, int field);
LB_API void lbind_setarrayf (lua_State *L, lua_CFunction f, int field);
LB_API void lbind_sethashf (lua_State *L, lua_CFunction f, int field);
LB_API void lbind_setmaptable (lua_State *L, luaL_Reg libs[], int field);
#define lbind_checkreadonly(L) ((void)( \
lua_gettop(L)!=2 && \
luaL_error((L), "field %s is read-only", \
lbind_tostring((L), 2))))
#define lbind_checkwriteonly(L) ((void)( \
lua_gettop(L)!=3 && \
luaL_error((L), "field %s is write-only", \
lbind_tostring((L), 2))))
/* light userdata utils */
LB_API int lbind_getudtypebox (lua_State *L);
LB_API int lbind_getlightuservalue (lua_State *L, const void *p);
LB_API void lbind_setlightuservalue (lua_State *L, const void *p);
/* lbind peer table support, define LBIND_NO_PEER to disable this.
*
* peer table is a normal table that has a field "__peer", t.__peer is
* the real userdata, in this case, lbind use this table as it is
* t.__peer. i.e. lbind treat that table as a native object.
*/
LB_API void *lbind_touserdata (lua_State *L, int idx);
/* lbind class runtime */
/*
* NOTE: all types must have a global Type structure. its address used
* to find the right informations about type. So all const lbind_Type *t
* argument must be &var, where var is declared by LB_API, e.g.
* LB_API lbind_Type basetype;
*/
typedef struct lbind_Type lbind_Type;
typedef void *lbind_Cast(lua_State *L, int idx, const lbind_Type *to_type);
struct lbind_Type {
const char *name;
int flags;
lbind_Cast *cast;
lbind_Type **bases;
};
/* lbind type registry
*
* a lbind object can tracked and interned.
*
* If a object is tracked, when it collected (i.e. not used any more)
* it's delete function in metatable will called. otherwise it won't
* deleted by Lua, i.e. it's life-time is not associate with Lua.
*
* When a object is interned, You can use it's pointer to find the
* object itself.
*
* if a type has flags LBIND_ACCESSOR, it has a non-trival __index and
* __newindex, that means the object of this type has the ability to
* save any value into it's uservalue and can have custom accessors.
* if a type has base type, it also have LBIND_ACCESSOR flag.
*/
#define LBIND_TRACK 0x01
#define LBIND_INTERN 0x02
#define LBIND_ACCESSOR 0x04
#ifndef LBIND_DEFAULT_FLAG
# define LBIND_DEFAULT_FLAG (LBIND_TRACK)
#endif
#define LBIND_INIT(name, flags) { name, flags, NULL, NULL }
#define LBIND_TYPE_FLAGS(var, name, flags) LB_API lbind_Type var = LBIND_INIT(name, flags)
#define LBIND_TYPE(var, name) LB_API lbind_Type var = LBIND_INIT(name, LBIND_DEFAULT_FLAG)
LB_API void lbind_inittype (lbind_Type *t, const char *name);
LB_API void lbind_setbase (lbind_Type *t, lbind_Type **bases, lbind_Cast *cast);
LB_API int lbind_settrack (lbind_Type *t, int autotrack);
LB_API int lbind_setintern (lbind_Type *t, int autointern);
/* lbind type metatable */
LB_API int lbind_newmetatable (lua_State *L, luaL_Reg *libs, const lbind_Type *t);
LB_API void lbind_setagency (lua_State *L);
/* get lbind_Type* from metatable */
LB_API lbind_Type *lbind_typeobject (lua_State *L, int idx);
/* lbind type system */
LB_API const char *lbind_tolstring (lua_State *L, int idx, size_t *plen);
LB_API const char *lbind_type (lua_State *L, int idx);
LB_API int lbind_isa (lua_State *L, int idx, const lbind_Type *t);
LB_API int lbind_copy (lua_State *L, const void *p, const lbind_Type *t);
LB_API void *lbind_cast (lua_State *L, int idx, const lbind_Type *t);
LB_API void *lbind_check (lua_State *L, int idx, const lbind_Type *t);
LB_API void *lbind_test (lua_State *L, int idx, const lbind_Type *t);
#define lbind_opt(L,idx,defs,t) \
(lua_isnoneornil((L),(idx)) ? (defs) : lbind_check((L),(idx),(t)))
#define lbind_tostring(L,idx) lbind_tolstring((L),(idx),NULL)
/* lbind object creation
* `lbind_raw` create a raw lbind object, not associate with a
* lbind_Type, if `intern` is non-zero, intern it.
* `lbind_new` create a lbind object associated with a lbind_Type,
* this type decide whether the object is signed up.
* `lbind_wrapraw` same as `lbind_raw`, but wrap a pointer instead.
* `lbind_wrap` wrap a pointer to lbind object associated with
* lbind_Type, the type decide the signing.
*/
LB_API void *lbind_newraw (lua_State *L, size_t objsize, int intern);
LB_API void *lbind_new (lua_State *L, size_t objsize, const lbind_Type *t);
LB_API void *lbind_wrapraw (lua_State *L, void *p, int intern);
LB_API void *lbind_wrap (lua_State *L, void *p, const lbind_Type *t);
/* delete a lbind object. unsign, clear and remove metatable of it. */
LB_API void *lbind_delete (lua_State *L, int idx);
/* get pointer from a lbind object, or NULL. */
LB_API void *lbind_object (lua_State *L, int idx);
/* intern a object with a pointer p, object is on stack. */
LB_API void lbind_intern (lua_State *L, const void *p);
/* get lbind object userdata from object pointer.
* require interned before */
LB_API int lbind_retrieve (lua_State *L, const void *p);
/* track/untrack object */
LB_API void lbind_track (lua_State *L, int idx);
LB_API void lbind_untrack (lua_State *L, int idx);
LB_API int lbind_hastrack (lua_State *L, int idx);
/* lbind enum runtime */
#ifndef LBIND_NO_ENUM
typedef struct lbind_EnumItem {
const char *name;
int value;
} lbind_EnumItem;
typedef struct lbind_Enum {
const char *name;
size_t nitem;
lbind_EnumItem *items;
} lbind_Enum;
#define LBIND_INITENUM(name, es) { name, sizeof(es)/sizeof((es)[0]), es }
#define LBIND_ENUM(var, name, es) LB_API lbind_Enum var = LBIND_INITENUM(name, es)
LB_API void lbind_initenum (lbind_Enum *et, const char *name);
LB_API lbind_EnumItem *lbind_findenum (lbind_Enum *et, const char *s, size_t len);
LB_API int lbind_pushenum (lua_State *L, const char *name, lbind_Enum *et);
LB_API int lbind_testenum (lua_State *L, int idx, lbind_Enum *et);
LB_API int lbind_checkenum (lua_State *L, int idx, lbind_Enum *et);
LB_API int lbind_pushmask (lua_State *L, int evalue, lbind_Enum *et);
LB_API int lbind_testmask (lua_State *L, int idx, lbind_Enum *et);
LB_API int lbind_checkmask (lua_State *L, int idx, lbind_Enum *et);
#define lbind_optenum(L,idx,defs,t) \
(lua_isnoneornil((L),(idx)) ? (defs) : lbind_checkenum((L),(idx),(t)))
#define lbind_optmask(L,idx,defs,t) \
(lua_isnoneornil((L),(idx)) ? (defs) : lbind_checkmask((L),(idx),(t)))
#endif /* LBIND_NO_ENUM */
LB_NS_END
#endif /* LBIND_H */
#if defined(LBIND_IMPLEMENTATION) && !defined(lbind_implemented)
#define lbind_implemented
#include <string.h>
LB_NS_BEGIN
#if LUA_VERSION_NUM < 503
LUA_API int lua53_getuservalue(lua_State *L, int idx)
{ lua_getuservalue(L, idx); return lua_type(L, -1); }
LUA_API int lua53_gettable(lua_State *L, int idx)
{ lua_gettable(L, idx); return lua_type(L, -1); }
LUA_API int lua53_getfield(lua_State *L, int idx, const char *field)
{ lua_getfield(L, idx, field); return lua_type(L, -1); }
LUA_API int lua53_rawget(lua_State *L, int idx)
{ lua_rawget(L, idx); return lua_type(L, -1); }
LUA_API int lua53_rawgeti(lua_State *L, int idx, lua_Integer n)
{ lua_rawgeti(L, idx, n); return lua_type(L, -1); }
LUA_API int lua53_rawgetp(lua_State *L, int idx, const void *p)
{ lua_rawgetp(L, idx, p); return lua_type(L, -1); }
LUA_API void lua53_rotate(lua_State *L, int idx, int n) {
int i;
if (n < 0)
n += (idx < 0) ? -idx : (lua_gettop(L)-idx-1);
for (i = 0; i < n; ++i)
lua_insert(L, -idx);
}
#endif
/* 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, const 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);
}
/* LuaJIT has its own luaL_traceback(),
* so we do not export this, use static instead. */
#ifdef LUA_BITSINT /* not LuaJIT */
#define LEVELS1 12 /* size of the first part of the stack */
#define LEVELS2 10 /* size of the second part of the stack */
static void luaL_traceback(lua_State *L, lua_State *L1, const char *msg, int level) {
int top = lua_gettop(L);
int firstpart = 1; /* still before eventual `...' */
lua_Debug ar;
if (msg) lua_pushfstring(L, "%s\n", msg);
lua_pushliteral(L, "stack traceback:");
while (lua_getstack(L1, level++, &ar)) {
if (level > LEVELS1 && firstpart) {
/* no more than `LEVELS2' more levels? */
if (!lua_getstack(L1, level+LEVELS2, &ar))
level--; /* keep going */
else {
lua_pushliteral(L, "\n\t..."); /* too many levels */
while (lua_getstack(L1, level+LEVELS2, &ar)) /* find last levels */
level++;
}
firstpart = 0;
continue;
}
lua_pushliteral(L, "\n\t");
lua_getinfo(L1, "Snl", &ar);
lua_pushfstring(L, "%s:", ar.short_src);
if (ar.currentline > 0)
lua_pushfstring(L, "%d:", ar.currentline);
if (*ar.namewhat != '\0') /* is there a name? */
lua_pushfstring(L, " in function " LUA_QS, ar.name);
else {
if (*ar.what == 'm') /* main? */
lua_pushfstring(L, " in main chunk");
else if (*ar.what == 'C' || *ar.what == 't')
lua_pushliteral(L, " ?"); /* C function or tail call */
else
lua_pushfstring(L, " in function <%s:%d>",
ar.short_src, ar.linedefined);
}
lua_concat(L, lua_gettop(L) - top);
}
lua_concat(L, lua_gettop(L) - top);
}
#endif /* LUA_BITSINT */
#endif /* LUA_VERSION_NUM < 502 */
/* lbind information hash routine */
#define LBIND_PTRBOX 0x90127B07
#define LBIND_TYPEBOX 0x799E0B07
#define LBIND_UDBOX 0xC5E7DB07
static int lbB_retrieve(lua_State *L, unsigned id) {
if (lua53_rawgetp(L, LUA_REGISTRYINDEX, (void*)(ptrdiff_t)id) == LUA_TNIL) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
lua_rawsetp(L, LUA_REGISTRYINDEX, (void*)(ptrdiff_t)id);
return 1;
}
return 0;
}
static void lbB_internbox(lua_State *L) {
if (lbB_retrieve(L, LBIND_PTRBOX)) {
lua_pushliteral(L, "v");
lbind_setmetafield(L, -2, "__mode");
}
}
static void lbB_typebox(lua_State *L) {
lbB_retrieve(L, LBIND_TYPEBOX);
}
/* light userdata utils */
LB_API int lbind_getudtypebox(lua_State *L) {
return lbB_retrieve(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);
if (lua53_rawgetp(L, -1, p) == LUA_TNIL) {
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 */
if (lua53_getfield(L, -1, name) != LUA_TNIL) { /* 2 */
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 */
if (lua53_rawget(L, -3) != LUA_TNIL) { /* 3->3 */
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 */
if (lua53_rawget(L, -4) == LUA_TNIL) { /* 4->4 */
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 */
static int lbL_traceback(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_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 = lua53_getfield(L, idx, field) != LUA_TNIL;
lua_pop(L, 1);
return hasfield;
}
LB_API int lbind_self(lua_State *L, const void *p, const char *method, int nargs, int *ptraceback) {
int top = lua_gettop(L);
luaL_checkstack(L, nargs+3, "too many arguments to self call");
if (!lbind_retrieve(L, p) ||
lua53_getuservalue(L, -1) == LUA_TNIL ||
lua53_getfield(L, -1, method) == LUA_TNIL)
{ lua_settop(L, top); return 0; }
lua_remove(L, -2);
if (ptraceback) {
lua_pushcfunction(L, lbL_traceback);
lua_insert(L, -3);
*ptraceback = top;
}
lua_insert(L, -2);
/* stack: traceback? method object */
return ptraceback ? 3 : 2;
}
LB_API int lbind_pcall(lua_State *L, int nargs, int nrets) {
int res, tb_idx = lua_gettop(L)-nargs;
lua_pushcfunction(L, lbL_traceback);
lua_insert(L, tb_idx);
res = lua_pcall(L, nargs, nrets, tb_idx);
lua_remove(L, tb_idx);
return res;
}
/* metatable utils */
static int lbL_libcall(lua_State *L) {
lua_pushvalue(L, lua_upvalueindex(1));
if (lua53_rawget(L, 1) == LUA_TNIL) {
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);
}
static int lbM_callacc(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 lbM_calllut(lua_State *L, int idx, int nargs) {
lua_CFunction f;
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);
}
lua_pop(L, 1);
return -1;
}
static int lbL_newindex(lua_State *L) {
int nret;
/* upvalue: seti, seth
* order:
* - lut
* - accessor
* - normaltable
* - uservalue
*/
if (!lua_isnone(L, lua_upvalueindex(3)) &&
lua_type(L, 2) == LUA_TNUMBER &&
(nret = lbM_callacc(L, lua_upvalueindex(3), 3)) >= 0)
return nret;
if (!lua_isnone(L, lua_upvalueindex(1)) &&
(nret = lbM_calllut(L, lua_upvalueindex(1), 3)) >= 0)
return nret;
if (!lua_isnone(L, lua_upvalueindex(2)) &&
(nret = lbM_callacc(L, lua_upvalueindex(2), 3)) >= 0)
return nret;
if (!lua_isuserdata(L, 1)) {
lua_settop(L, 3);
lua_rawset(L, 1);
return 0;
}
if (lua53_getuservalue(L, 1) == LUA_TNIL) {
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 lbL_index(lua_State *L) {
int i, nret;
/* upvalue: geti, geth, tables
* order:
* - uservalue
* - metatable
* - lut
* - accessor
* - upvalue tables
*/
if (lua_isuserdata(L, 1)) {
if (lua53_getuservalue(L, 1) != LUA_TNIL) {
lua_pushvalue(L, 2);
if (lua53_rawget(L, -2) != LUA_TNIL)
return 1;
}
}
if (lua_getmetatable(L, 1)) {
lua_pushvalue(L, 2);
if (lua53_rawget(L, -2) != LUA_TNIL)
return 1;
}
if (!lua_isnoneornil(L, lua_upvalueindex(3)) &&
lua_type(L, 2) == LUA_TNUMBER &&
(nret = lbM_callacc(L, lua_upvalueindex(3), 2)) >= 0)
return nret;
if (!lua_isnoneornil(L, lua_upvalueindex(1)) &&
(nret = lbM_calllut(L, lua_upvalueindex(1), 2)) >= 0)
return nret;
if (!lua_isnoneornil(L, lua_upvalueindex(2)) &&
(nret = lbM_callacc(L, lua_upvalueindex(2), 2)) >= 0)
return nret;
/* find in libtable/superlibtable */
for (i = 4; !lua_isnoneornil(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);
if (lua53_gettable(L, lua_upvalueindex(i)) != LUA_TNIL)
return 1;
}
return 0;
}
static void lbM_newindex(lua_State *L) {
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
lua_pushcclosure(L, lbL_newindex, 3);
}
static void lbM_index(lua_State *L, int ntables) {
lua_pushnil(L);
lua_pushnil(L);
lua_pushnil(L);
if (ntables != 0)
lua53_rotate(L, -ntables-3, 3);
lua_pushcclosure(L, lbL_index, ntables+3);
}
static void get_default_metafield(lua_State *L, int idx, int field) {
if (field == LBIND_INDEX) {
if (lua53_getfield(L, idx, "__index") == LUA_TNIL
|| lua_tocfunction(L, -1) != lbL_index) {
lua_pop(L, 1);
lbM_index(L, 0);
lua_pushvalue(L, -1);
lua_setfield(L, lbind_relindex(idx, 2), "__index");
}
}
else if (field == LBIND_NEWINDEX) {
if (lua53_getfield(L, idx, "__newindex") == LUA_TNIL
|| lua_tocfunction(L, -1) != lbL_newindex) {
lua_pop(L, 1);
lbM_newindex(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 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) {
if (lua53_rawgetp(L, LUA_REGISTRYINDEX, t) == LUA_TNIL) {
lua_pop(L, 1);
return 0;
}
return 1;
}
LB_API int lbind_setmetafield(lua_State *L, int idx, const char *field) {
if (!lua_getmetatable(L, idx)) {
lua_createtable(L, 0, 1);
lua_pushvalue(L, -2);
lua_setfield(L, -2, field);
lua_setmetatable(L, lbind_relindex(idx, 1));
lua_pop(L, 1);
return 1;
}
lua_pushvalue(L, -2);
lua_setfield(L, -2, field);
lua_pop(L, 2);
return 0;
}
LB_API int lbind_setlibcall(lua_State *L, const char *method) {
if (method == NULL) method = "new";
lua_pushstring(L, method);
lua_pushcclosure(L, lbL_libcall, 1);
return lbind_setmetafield(L, -2, "__call");
}
LB_API void lbind_setaccessors(lua_State *L, int ntables, int field) {
if ((field & LBIND_INDEX) != 0) {
lbM_index(L, ntables);
lua_setfield(L, -2, "__index");
}
if ((field & LBIND_NEWINDEX) != 0) {
lbM_newindex(L);
lua_setfield(L, -2, "__newindex");
}
}
LB_API void lbind_sethashf(lua_State *L, lua_CFunction f, int field) {
set_cfuncupvalue(L, f, field, 2);
}
LB_API void lbind_setarrayf(lua_State *L, lua_CFunction f, int field) {
set_cfuncupvalue(L, f, field, 3);
}
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 *lbO_new(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 *lbO_test(lua_State *L, int idx) {
lbind_Object *obj = (lbind_Object*)lbind_touserdata(L, idx);
if (obj != NULL) {
if (!check_size(L, idx) || obj->o.instance == NULL)
obj = NULL;
#if 0
else {
lbB_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_touserdata(lua_State *L, int idx) {
#ifndef LBIND_NO_PEER
if (lua_istable(L, idx)) {
if (lua53_getfield(L, idx, "__peer") == LUA_TNIL) {
lua_pop(L, 1);
return NULL;
}
lua_replace(L, idx);
}
#endif
return lua_touserdata(L, idx);
}
LB_API void *lbind_newraw(lua_State *L, size_t objsize, int intern) {
return lbO_new(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 = lbO_new(L, objsize, t->flags);
if (lbind_getmetatable(L, t))
lua_setmetatable(L, -2);