-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patholua.c
1982 lines (1799 loc) · 56.3 KB
/
olua.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
/**
* The MIT License (MIT)
*
* Copyright (c) 2019-2024 codetypes@gmail.com
*
* https://github.com/zhongfq/olua
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include "olua.h"
#define OLUA_CIDX_ISA (lua_upvalueindex(1))
#define OLUA_CIDX_FUNC (lua_upvalueindex(2))
#define OLUA_CIDX_GET (lua_upvalueindex(3))
#define OLUA_CIDX_SET (lua_upvalueindex(4))
#define OLUA_CIDX_USER (lua_upvalueindex(5))
#define OLUA_CIDX_TOTAL 5
#define OLUA_CKEY_ISA ".isa"
#define OLUA_CKEY_FUNC ".func"
#define OLUA_CKEY_GET ".get"
#define OLUA_CKEY_SET ".set"
#define OLUA_CKEY_CLSOBJ ".classobj"
#define OLUA_CKEY_CLASS ".class"
#define OLUA_OBJSTUB ".olua.objstub"
#define OLUA_OBJTABLE ".olua.objtable"
#define OLUA_POOLTABLE ".olua.pooltable"
#define OLUA_LUAREFTABLE ".olua.luareftable"
#define OLUA_VMENV ".olua.vmenv"
#define OLUA_REF_FMT ".olua.ref.%s"
#define OLUA_GET_NAME ".olua.getname"
#define registry_rawgeti(L, f) olua_rawgeti(L, LUA_REGISTRYINDEX, (f))
#define registry_rawgetf(L, f) olua_rawgetf(L, LUA_REGISTRYINDEX, (f))
#define registry_rawsetf(L, f) olua_rawsetf(L, LUA_REGISTRYINDEX, (f))
#define const_value(L, k, v) (lua_pushvalue(L, (v)), oluacls_const(L, (k)))
#define const_string(L, k, v) (olua_pushstring(L, (v)), oluacls_const(L, (k)))
#define const_from(L, k, mt) (olua_rawgetf(L, (mt), (k)), oluacls_const(L, k))
#define OLUA_VMBUFF_SIZE 128
#define OLUA_FLAGBITS 32
typedef struct {
int64_t ctxid;
size_t poolsize;
int64_t refcount;
int64_t objcount;
bool poolenabled;
bool debug;
char buff[OLUA_VMBUFF_SIZE];
} olua_VMEnv;
typedef struct {
void *self;
uintptr_t flags;
} olua_Object;
static int luaopen_olua(lua_State *L);
static int luaopen_voidp(lua_State *L);
static olua_Object *olua_toluaobj(lua_State *L, int idx);
static inline int64_t obtainref(olua_VMEnv *env)
{
return ++env->refcount <= 0 ? 1 : env->refcount;
}
static int errfunc(lua_State *L)
{
if (olua_isthread(L, 1)) {
luaL_traceback(L, lua_tothread(L, 1), lua_tostring(L, 2), 0);
lua_insert(L, 1);
}
luaL_traceback(L, L, lua_tostring(L, 1), 1);
printf("%s\n", lua_tostring(L, -1));
return 0;
}
char *tobitstr(uint16_t x, char buf[OLUA_FLAGBITS])
{
int bits = sizeof(uint16_t) * 8;
char *s = buf;
*s++ = '0';
*s++ = 'b';
for (int i = bits - 1; i >= 0; i--) {
*s++ = '0' + ((x >> i) & 1);
}
*s++ = 0;
return buf;
}
static olua_VMEnv *olua_getvmenv(lua_State *L)
{
static int64_t s_ctxid = 0;
olua_VMEnv *env;
if (olua_unlikely(registry_rawgetf(L, OLUA_VMENV) != LUA_TUSERDATA)) {
env = (olua_VMEnv *)olua_newrawobj(L, NULL, sizeof(*env));
env->ctxid = ++s_ctxid;
env->debug = false;
env->poolenabled = false;
env->poolsize = 0;
env->refcount = 0;
env->objcount = 0;
registry_rawsetf(L, OLUA_VMENV);
#if LUA_VERSION_NUM == 501
// detect main thread
olua_getglobal(L, "coroutine");
olua_getfield(L, -1, "running");
lua_call(L, 0, 1);
if (olua_isnil(L, -1)) {
olua_initcompat(L);
}
lua_pop(L, 2); // pop coroutine table and return value
if (registry_rawgeti(L, LUA_RIDX_MAINTHREAD) != LUA_TTHREAD) {
luaL_error(L, "main thread not set");
}
if (registry_rawgeti(L, LUA_RIDX_GLOBALS) != LUA_TTABLE) {
luaL_error(L, "global table not set");
}
if (!lua_rawequal(L, -1, LUA_GLOBALSINDEX)) {
luaL_error(L, "global table not match");
}
lua_pop(L, 2);
#endif
} else {
env = (olua_VMEnv *)olua_torawobj(L, -1);
}
lua_pop(L, 1);
return env;
}
OLUA_API bool olua_isdebug(lua_State *L)
{
return olua_getvmenv(L)->debug;
}
bool olua_isinteger(lua_State *L, int idx)
{
#if LUA_VERSION_NUM >= 503
if (lua_isinteger(L, idx)) {
return true;
}
#endif
if (olua_isnumber(L, idx)) {
lua_Number n = lua_tonumber(L, idx);
return n == (lua_Number)(floor(n));
}
return false;
}
OLUA_API lua_Integer olua_checkinteger(lua_State *L, int idx)
{
luaL_checktype(L, idx, LUA_TNUMBER);
return luaL_checkinteger(L, idx);
}
OLUA_API lua_Number olua_checknumber(lua_State *L, int idx)
{
luaL_checktype(L, idx, LUA_TNUMBER);
return olua_tonumber(L, idx);
}
OLUA_API const char *olua_checklstring(lua_State *L, int idx, size_t *len)
{
luaL_checktype(L, idx, LUA_TSTRING);
return olua_tolstring(L, idx, len);
}
OLUA_API bool olua_checkbool(lua_State *L, int idx)
{
luaL_checktype(L, idx, LUA_TBOOLEAN);
return olua_tobool(L, idx);
}
OLUA_API int olua_rawgetf(lua_State *L, int idx, const char *field)
{
idx = lua_absindex(L, idx);
olua_pushstring(L, field);
return olua_rawget(L, idx);
}
OLUA_API void olua_rawsetf(lua_State *L, int idx, const char *field)
{
idx = lua_absindex(L, idx);
olua_pushstring(L, field);
lua_insert(L, -2);
olua_rawset(L, idx);
}
OLUA_API void olua_pusherrorfunc(lua_State *L)
{
if (olua_unlikely(olua_getglobal(L, "__TRACEBACK__") != LUA_TFUNCTION)) {
lua_pop(L, 1);
lua_pushcfunction(L, errfunc);
}
}
OLUA_API int olua_pcall(lua_State *L, int nargs, int nresults)
{
int status;
int errfunc = lua_absindex(L, -(nargs + 1));
olua_pusherrorfunc(L);
lua_insert(L, errfunc); // insert error handle at func position
status = lua_pcall(L, nargs, nresults, errfunc);
lua_remove(L, errfunc);
return status;
}
OLUA_API void olua_require(lua_State *L, const char *name, lua_CFunction func)
{
luaL_getsubtable(L, LUA_REGISTRYINDEX, LUA_LOADED_TABLE);
olua_getfield(L, -1, name);
if (!lua_toboolean(L, -1)) {
lua_pushcfunction(L, func);
olua_pushstring(L, name);
lua_call(L, 1, 1);
if (!lua_toboolean(L, -1)) {
lua_pop(L, 1);
lua_pushboolean(L, true);
}
olua_setfield(L, -3, name);
}
lua_pop(L, 2);
}
OLUA_API void olua_import(lua_State *L, lua_CFunction m)
{
lua_pushcfunction(L, m);
lua_call(L, 0, 0);
}
OLUA_API const char *olua_typename(lua_State *L, int idx)
{
const char *tn = NULL;
if (olua_isuserdata(L, idx) && lua_getmetatable(L, idx)) {
if (olua_rawgetf(L, -1, "__name") == LUA_TSTRING) {
tn = olua_tostring(L, -1);
}
lua_pop(L, 2); // pop mt and value
}
return tn ? tn : lua_typename(L, lua_type(L, idx));
}
OLUA_API bool olua_isa(lua_State *L, int idx, const char *cls)
{
bool isa = false;
int top = lua_gettop(L);
if (olua_likely(olua_isuserdata(L, idx) && lua_getmetatable(L, idx))) {
if (olua_likely(olua_rawgetf(L, -1, OLUA_CKEY_ISA) == LUA_TTABLE)) {
olua_rawgetf(L, -1, cls);
isa = olua_tobool(L, -1);
}
}
lua_settop(L, top);
return isa;
}
static void olua_pushobjtable(lua_State *L)
{
if (olua_unlikely(registry_rawgetf(L, OLUA_OBJTABLE) != LUA_TTABLE)) {
lua_pop(L, 1);
lua_newtable(L);
olua_setfieldstring(L, -1, "__mode", "v"); // value weak table
lua_pushvalue(L, -1);
lua_setmetatable(L, -2);
lua_pushvalue(L, -1);
registry_rawsetf(L, OLUA_OBJTABLE);
}
}
OLUA_API void *olua_newobjstub(lua_State *L, const char *cls)
{
void *ptr;
olua_VMEnv *env = olua_getvmenv(L);
olua_pushobjtable(L);
olua_newrawobj(L, NULL, 0); // create obj stub
ptr = (void *)lua_topointer(L, -1);
env->objcount++;
lua_pushvalue(L, -1);
olua_rawsetp(L, -3, ptr); // objtable[ptr] = stub
lua_replace(L, -2); // pop objtable
olua_setmetatable(L, cls);
return ptr;
}
OLUA_API int olua_pushobjstub(lua_State *L, void *obj, void *stub, const char *cls)
{
int status = OLUA_OBJ_EXIST;
olua_pushobjtable(L);
if (olua_rawgetp(L, -1, obj) == LUA_TUSERDATA) {
// ref stub in obj
olua_pushstring(L, OLUA_OBJSTUB);
olua_rawgetp(L, -3, stub);
olua_setvariable(L, -3); // obj[.stub] = stub
// stub point to obj
lua_pushvalue(L, -1);
olua_rawsetp(L, -3, stub); // objtable[stub] = obj
} else if (olua_rawgetp(L, -2, stub) == LUA_TUSERDATA) {
// L: nil stub
lua_remove(L, -2); // remove nil
olua_setmetatable(L, cls);
olua_setrawobj(L, -1, obj);
lua_pushvalue(L, -1);
olua_rawsetp(L, -3, obj); // objtable[obj] = stub
status = OLUA_OBJ_NEW;
} else {
luaL_error(L, "stub object not found for '%s'", cls);
}
lua_replace(L, -2);
return status;
}
static void olua_pushpooltable(lua_State *L)
{
if (olua_unlikely(registry_rawgetf(L, OLUA_POOLTABLE) != LUA_TTABLE)) {
lua_pop(L, 1);
lua_createtable(L, 16, 0);
lua_pushvalue(L, -1);
registry_rawsetf(L, OLUA_POOLTABLE);
}
}
static void olua_pushpoolobj(lua_State *L, olua_VMEnv *env, void *obj)
{
olua_Object *luaobj;
olua_pushpooltable(L);
if (olua_unlikely(olua_rawgeti(L, -1, ++env->poolsize) != LUA_TUSERDATA)) {
lua_pop(L, 1);
olua_newrawobj(L, NULL, 0);
lua_pushvalue(L, -1);
olua_rawseti(L, -3, env->poolsize);
}
lua_replace(L, -2); // rm pool table
luaobj = olua_toluaobj(L, -1);
luaobj->self = obj;
luaobj->flags = OLUA_FLAG_SKIP_GC | OLUA_FLAG_IN_POOL;
}
OLUA_API void olua_emplaceobj(lua_State *L, int idx, void *obj, const char *cls)
{
idx = lua_absindex(L, idx);
if (olua_unlikely(!cls || olua_getmetatable(L, cls) != LUA_TTABLE)) {
luaL_error(L, "class metatable '%s' not found", cls ? cls : "NULL");
return;
}
lua_setmetatable(L, -2);
olua_setrawobj(L, idx, obj);
olua_pushobjtable(L);
if (olua_rawgetp(L, -1, obj) == LUA_TNIL) {
olua_getvmenv(L)->objcount++;
lua_pushvalue(L, -3);
olua_rawsetp(L, -3, obj);
}
lua_pop(L, 2);
}
OLUA_API int olua_pushobj(lua_State *L, void *obj, const char *cls)
{
int status = OLUA_OBJ_EXIST;
if (olua_unlikely(!obj)) {
lua_pushnil(L);
return status;
}
if (olua_unlikely(!cls || olua_getmetatable(L, cls) != LUA_TTABLE)) {
luaL_error(L, "class metatable '%s' not found", cls ? cls : "NULL");
return status;
}
olua_pushobjtable(L);
if (olua_likely(olua_rawgetp(L, -1, obj) == LUA_TNIL)) {
olua_VMEnv *env = olua_getvmenv(L);
lua_pop(L, 1);
if (olua_unlikely(env->poolenabled)) {
olua_pushpoolobj(L, env, obj);
status = OLUA_OBJ_EXIST;
} else {
olua_newrawobj(L, obj, 0);
}
if (!olua_unlikely(env->poolenabled)) {
lua_pushvalue(L, -1);
// L: metaclass objtable obj obj
olua_rawsetp(L, -3, obj); // objtable[obj] = obj
env->objcount++;
status = OLUA_OBJ_NEW;
}
// L: metaclass objtable obj
lua_pushvalue(L, -3);
lua_setmetatable(L, -2); // obj.metatable = metaclass
} else if (olua_unlikely(!olua_strequal(cls, OLUA_VOIDCLS))) {
// L: metaclass objtable obj
if (luaL_testudata(L, -1, OLUA_VOIDCLS)) {
lua_pushvalue(L, -3);
lua_setmetatable(L, -2); // obj.metatable = metaclass
status = OLUA_OBJ_UPDATE;
} else if (!olua_isa(L, -1, cls)) {
lua_pushnil(L);
olua_rawsetp(L, -3, obj); // objtable[obj] = nil
lua_pop(L, 3); // pop metaclass, objtable and obj
return olua_pushobj(L, obj, cls);
}
}
lua_copy(L, -1, -3);
// L: obj objtable obj
lua_pop(L, 2);
return status;
}
olua_Object *olua_toluaobj(lua_State *L, int idx)
{
#ifdef OLUA_DEBUG
size_t len = lua_rawlen(L, idx);
if (len < sizeof(olua_Object)) {
luaL_error(L, "'%s: %p' is not a valid olua_Object",
lua_typename(L, lua_type(L, idx)), lua_topointer(L, idx));
}
#endif
return (olua_Object *)lua_touserdata(L, idx);
}
OLUA_API void *olua_newrawobj(lua_State *L, void *obj, size_t extra)
{
olua_Object *luaobj;
void *ptr;
lua_newuserdata(L, sizeof(olua_Object) + extra);
luaobj = olua_toluaobj(L, -1);
luaobj->flags = 0;
ptr = extra > 0 ? ((char *)luaobj) + sizeof(olua_Object) : NULL;
luaobj->self = obj ? obj : ptr;
return ptr;
}
OLUA_API void olua_setrawobj(lua_State *L, int idx, void *obj)
{
olua_toluaobj(L, idx)->self = obj;
}
OLUA_API void *olua_torawobj(lua_State *L, int idx)
{
return olua_toluaobj(L, idx)->self;
}
OLUA_API bool olua_getrawobj(lua_State *L, void *obj)
{
if (!obj) {
return false;
}
olua_pushobjtable(L);
if (olua_rawgetp(L, -1, obj) == LUA_TUSERDATA) {
lua_replace(L, -2);
return true;
} else {
lua_pop(L, 2);
return false;
}
}
OLUA_API void olua_setobjflag(lua_State *L, int idx, unsigned flag)
{
olua_Object *luaobj = olua_toluaobj(L, idx);
luaobj->flags |= flag;
}
OLUA_API bool olua_hasobjflag(lua_State *L, int idx, unsigned flag)
{
olua_Object *luaobj = olua_toluaobj(L, idx);
return (luaobj->flags & flag) == flag;
}
static void *aux_toobj(lua_State *L, int idx, const char *cls, bool checked)
{
if (olua_likely(checked ? olua_isa(L, idx, cls) : olua_isuserdata(L, idx))) {
void *obj = olua_torawobj(L, idx);
if (olua_unlikely(!obj)) {
idx = lua_absindex(L, idx);
olua_printobj(L, "access dead object", idx);
luaL_error(L, "object '%s %p' survive from gc",
olua_typename(L, idx), lua_topointer(L, idx));
}
return obj;
} else {
luaL_error(L, "#%d argument error, expect: '%s', got '%s'", idx,
cls, olua_typename(L, idx));
}
return NULL;
}
OLUA_API void *olua_checkobj(lua_State *L, int idx, const char *cls)
{
return aux_toobj(L, idx, cls, true);
}
OLUA_API void *olua_toobj(lua_State *L, int idx, const char *cls)
{
#if OLUA_DEBUG
return aux_toobj(L, idx, cls, true);
#else
return aux_toobj(L, idx, cls, false);
#endif
}
OLUA_API void olua_delobj(lua_State *L, void *obj)
{
if (!obj) {
return;
}
olua_pushobjtable(L);
if (olua_rawgetp(L, -1, obj) == LUA_TUSERDATA) {
olua_setobjflag(L, -1, OLUA_FLAG_DEL);
olua_setrawobj(L, -1, NULL);
}
lua_pushnil(L); // L: objtable ud nil
olua_rawsetp(L, -3, obj);
lua_pop(L, 2);
}
OLUA_API const char *olua_objstring(lua_State *L, int idx)
{
olua_VMEnv *env = olua_getvmenv(L);
const void *ud = lua_topointer(L, idx);
const void *obj = ud;
if (olua_isuserdata(L, idx)) {
obj = olua_torawobj(L, idx);
}
if (olua_isdebug(L)) {
snprintf(env->buff, OLUA_VMBUFF_SIZE, "%s: %p %p", olua_typename(L, idx), obj, ud);
} else {
snprintf(env->buff, OLUA_VMBUFF_SIZE, "%s: %p", olua_typename(L, idx), obj);
}
return env->buff;
}
OLUA_API void olua_print(lua_State *L, const char *str)
{
olua_getglobal(L, "print");
olua_pushstring(L, str);
lua_pcall(L, 1, 0, 0);
}
OLUA_API void olua_printobj(lua_State *L, const char *tag, int idx)
{
char buf[256];
char bitstr[OLUA_FLAGBITS];
olua_Object *luaobj = olua_toluaobj(L, idx);
olua_VMEnv *env = olua_getvmenv(L);
snprintf(buf, sizeof(buf), "%s:%05"PRId64": %s {luaobj=%p,%s,%s}",
tag,
env->objcount,
olua_objstring(L, idx),
luaobj,
luaobj->self ? olua_optfieldstring(L, idx, OLUA_GET_NAME, "") : "",
tobitstr(luaobj->flags, bitstr));
olua_print(L, buf);
}
OLUA_API int olua_indexerror(lua_State *L)
{
const char *cls = olua_checkfieldstring(L, 1, "classname");
const char *field = olua_tostring(L, 2);
luaL_error(L, "index '%s.%s' error", cls, field);
return 0;
}
OLUA_API int olua_newindexerror(lua_State *L)
{
const char *cls = olua_checkfieldstring(L, 1, "classname");
const char *field = olua_tostring(L, 2);
luaL_error(L, "newindex '%s.%s' error", cls, field);
return 0;
}
OLUA_API void olua_enable_objpool(lua_State *L)
{
olua_getvmenv(L)->poolenabled = true;
}
OLUA_API void olua_disable_objpool(lua_State *L)
{
olua_getvmenv(L)->poolenabled = false;
}
OLUA_API size_t olua_push_objpool(lua_State *L)
{
return olua_getvmenv(L)->poolsize;
}
OLUA_API void olua_pop_objpool(lua_State *L, size_t position)
{
if (olua_likely(registry_rawgetf(L, OLUA_POOLTABLE) == LUA_TTABLE)) {
size_t len = (size_t)lua_rawlen(L, -1);
olua_assert(position <= len, "invalid position");
olua_getvmenv(L)->poolsize = position;
for (size_t i = position + 1; i <= len; i++) {
olua_rawgeti(L, -1, (lua_Integer)i);
olua_Object *luaobj = olua_toluaobj(L, -1);
lua_pushnil(L);
lua_setmetatable(L, -2); // remove metatable
lua_pushnil(L);
lua_setuservalue(L, -2); // remove user value
lua_pop(L, 1);
if (olua_likely(luaobj->self != NULL)) {
luaobj->self = NULL;
} else {
break;
}
}
}
lua_pop(L, 1);
}
static void olua_pushusertable(lua_State *L, int idx)
{
if (olua_unlikely(lua_getuservalue(L, idx) != LUA_TTABLE)) {
olua_assert(olua_isnil(L, -1), "expect nil");
lua_pop(L, 1);
idx = lua_absindex(L, idx);
lua_createtable(L, 0, 4);
lua_pushvalue(L, -1);
lua_setuservalue(L, idx);
}
}
static bool test_tag_mode(lua_State *L, int idx, const char *tag, int mode)
{
if (olua_isstring(L, idx)) {
const char *field = olua_tostring(L, idx);
if ((field = strchr(field, '@')) != NULL) {
++field; // skip '@'
if (mode == OLUA_TAG_STARTWITH) {
return olua_strstartwith(field, tag);
} else if (mode == OLUA_TAG_EQUAL || mode == OLUA_TAG_REPLACE) {
return olua_strequal(field, tag);
}
}
}
return false;
}
OLUA_API const char *olua_setcallback(lua_State *L, void *obj, int func, const char *tag, int tagmode)
{
const char *cb = NULL;
func = lua_absindex(L, func);
luaL_checktype(L, func, LUA_TFUNCTION);
if (!olua_getrawobj(L, obj)) {
luaL_error(L, "obj userdata not found, it maybe in the obj pool!");
}
olua_pushusertable(L, -1);
if (tagmode == OLUA_TAG_REPLACE) {
lua_pushnil(L);
while (lua_next(L, -2)) {
// L: obj usertable k v
if (test_tag_mode(L, -2, tag, tagmode)) {
cb = olua_tostring(L, -2);
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
}
}
if (cb == NULL) {
const char *cls = olua_checkfieldstring(L, -2, "classname");
olua_VMEnv *env = olua_getvmenv(L);
while (true) {
char ref[64];
snprintf(ref, sizeof(ref), "%"PRId64, obtainref(env));
cb = lua_pushfstring(L, ".olua.cb#%s$%s@%s", ref, cls, tag);
lua_pushvalue(L, -1);
// L: obj usertable fn fn
if (olua_rawget(L, -3) == LUA_TNIL) {
lua_pop(L, 1);
break;
} else {
lua_pop(L, 2);
}
}
}
lua_pushvalue(L, func);
// L: obj usertable fn func
olua_rawset(L, -3); // usertable[fn] = func
// L: obj usertable
lua_pop(L, 2);
return cb;
}
OLUA_API int olua_getcallback(lua_State *L, void *obj, const char *tag, int tagmode)
{
if (!olua_getrawobj(L, obj)) {
lua_pushnil(L);
return LUA_TNIL;
}
olua_pushusertable(L, -1);
if (tagmode == OLUA_TAG_WHOLE) {
olua_rawgetf(L, -1, tag);
} else {
lua_pushnil(L);
lua_pushnil(L);
while (lua_next(L, -3)) {
// L: obj usertable nil fn func
if (test_tag_mode(L, -2, tag, tagmode)) {
lua_replace(L, -3);
lua_pop(L, 1);
break;
}
lua_pop(L, 1);
}
}
// L: obj usertable func
lua_insert(L, -3);
lua_pop(L, 2);
return lua_type(L, -1);
}
OLUA_API void olua_removecallback(lua_State *L, void *obj, const char *tag, int tagmode)
{
if (!olua_getrawobj(L, obj)) {
return;
}
olua_pushusertable(L, -1);
if (tagmode == OLUA_TAG_WHOLE) {
lua_pushnil(L);
olua_rawsetf(L, -2, tag); // usertable[tag] = nil
} else {
lua_pushnil(L);
while (lua_next(L, -2)) {
if (test_tag_mode(L, -2, tag, tagmode)) {
lua_pushvalue(L, -2);
lua_pushnil(L);
// L: obj usertable fn func fn nil
olua_rawset(L, -5); // usertable[fn] = nil
}
// L: obj usertable fn fun
lua_pop(L, 1);
}
}
lua_pop(L, 2); // pop obj usertable
}
OLUA_API int olua_callback(lua_State *L, void *obj, const char *func, int argc)
{
int top = lua_gettop(L) - argc + 1;
int status = LUA_ERRRUN;
olua_pusherrorfunc(L);
if (olua_getcallback(L, obj, func, OLUA_TAG_WHOLE) == LUA_TFUNCTION) {
for (int i = 0; i < argc; i++) {
// L: ...arg errfunc func ...arg
lua_pushvalue(L, -(2 + argc));
}
status = lua_pcall(L, argc, 1, -(2 + argc));
} else {
lua_pop(L, 1);
if (olua_getrawobj(L, obj)) {
lua_pop(L, 1);
lua_pushfstring(L, "callback missed: %s", func);
} else {
lua_pushfstring(L, "object missed: %s", func);
}
lua_pcall(L, 1, 0, 0); // call error func
}
if (status != LUA_OK) {
lua_pushnil(L);
}
lua_replace(L, top);
lua_settop(L, top);
return status;
}
OLUA_API void *olua_pushclassobj(lua_State *L, const char *cls)
{
olua_getmetatable(L, cls);
olua_rawgetf(L, -1, OLUA_CKEY_CLSOBJ); // metaclass[.classobj]
lua_replace(L, -2); // pop metaclass
olua_assert(olua_isuserdata(L, -1), "expect userdata");
return olua_torawobj(L, -1);
}
OLUA_API bool olua_getclass(lua_State *L, const char *cls)
{
if (olua_getmetatable(L, cls) == LUA_TTABLE) {
olua_rawgetf(L, -1, OLUA_CKEY_CLASS); // metaclass[.class]
lua_replace(L, -2); // pop metaclass
return true;
} else {
lua_pop(L, 1);
return false;
}
}
OLUA_API int olua_getvariable(lua_State *L, int idx)
{
int type = LUA_TNIL;
olua_assert(olua_isuserdata(L, idx), "expect userdata");
if (lua_getuservalue(L, idx) == LUA_TTABLE) {
lua_insert(L, -2);
type = olua_gettable(L, -2); // uservalue[k]
lua_replace(L, -2); // pop uservalue
} else {
lua_pop(L, 2); // pop k uservalue
lua_pushnil(L);
}
return type;
}
OLUA_API void olua_setvariable(lua_State *L, int idx)
{
olua_assert(olua_isuserdata(L, idx), "expect userdata");
olua_pushusertable(L, idx);
lua_insert(L, -3);
// L: obj uservalue k v
olua_settable(L, -3); // uservalue[k] = v
lua_pop(L, 1); // pop uservalue
}
static void olua_pushluareftable(lua_State *L)
{
if (registry_rawgetf(L, OLUA_LUAREFTABLE) != LUA_TTABLE) {
lua_pop(L, 1);
lua_newtable(L);
lua_pushvalue(L, -1);
registry_rawsetf(L, OLUA_LUAREFTABLE);
}
}
OLUA_API olua_Ref olua_ref(lua_State *L, int idx, int type)
{
if (type != LUA_TANY) {
luaL_checktype(L, idx, type);
}
if (!olua_isnil(L, idx)) {
olua_VMEnv *env = olua_getvmenv(L);
int64_t ref = obtainref(env);
idx = lua_absindex(L, idx);
olua_pushluareftable(L);
// find a valid ref slot
while (olua_rawgeti(L, -1, ref) != LUA_TNIL) {
lua_pop(L, 1);
ref = obtainref(env);
}
lua_pushvalue(L, idx);
olua_rawseti(L, -3, ref); // refable[ref] = value
lua_pop(L, 2); // pop reftable nil
return ref;
}
return LUA_REFNIL;
}
OLUA_API void olua_unref(lua_State *L, olua_Ref ref)
{
olua_pushluareftable(L);
lua_pushnil(L);
olua_rawseti(L, -2, ref); // reftable[ref] = nil
lua_pop(L, 1);
}
OLUA_API void olua_getref(lua_State *L, olua_Ref ref)
{
olua_pushluareftable(L);
olua_rawgeti(L, -1, ref);
lua_replace(L, -2);
}
OLUA_API int olua_loadref(lua_State *L, int idx, const char *name)
{
int type;
olua_assert(olua_isuserdata(L, idx), "expect userdata");
olua_pushusertable(L, idx);
lua_pushfstring(L, OLUA_REF_FMT, name);
type = olua_rawget(L, -2);
lua_replace(L, -2); // replace usertable
return type;
}
static void olua_getreftable(lua_State *L, int idx, const char *name)
{
olua_assert(olua_isuserdata(L, idx), "expect userdata");
olua_pushusertable(L, idx);
name = lua_pushfstring(L, OLUA_REF_FMT, name);;
luaL_getsubtable(L, -2, name);
// L: usertable name reftable
lua_insert(L, -3);
// L: reftable name usertable
lua_pop(L, 2); // pop name usertable
}
static void aux_changeref(lua_State *L, int idx, const char *name, int obj, int flags)
{
int top = lua_gettop(L);
if (idx == OLUA_NOREFSTORE) {
return;
}
idx = lua_absindex(L, idx);
obj = lua_absindex(L, obj);
olua_assert(olua_isuserdata(L, idx), "expect userdata");
if (flags & OLUA_REF_REMOVE) {
lua_pushnil(L);
} else if (flags & OLUA_REF_ALONE) {
olua_assert(olua_isuserdata(L, obj) || olua_isnil(L, obj), "expect userdata or nil");
lua_pushvalue(L, obj);
} else {
olua_pushbool(L, true);
}
if (flags & OLUA_REF_ALONE) {
olua_pushusertable(L, idx);
lua_pushfstring(L, OLUA_REF_FMT, name);
lua_pushvalue(L, top + 1);
// L: usertable name obj|nil
olua_rawset(L, -3); // L: usertable[name] = obj|nil
} else if (flags & OLUA_REF_MULTI) {
if (olua_isnil(L, obj)) {
lua_settop(L, top);
return;
}
olua_getreftable(L, idx, name);
if (flags & OLUA_REF_TABLE) {
olua_assert(olua_istable(L, obj), "expect table");
lua_pushnil(L);
while (lua_next(L, obj)) {
if (olua_isuserdata(L, -2)) {
lua_pushvalue(L, -2);
lua_pushvalue(L, top + 1);
// L: reftable obj value obj true|nil
olua_rawset(L, -5); // L: reftable[obj] = true|nil
}
if (olua_isuserdata(L, -1)) {
lua_pushvalue(L, -1);
lua_pushvalue(L, top + 1);
// L: reftable name obj obj true|nil
olua_rawset(L, -5); // L: reftable[obj] = true|nil
}
lua_pop(L, 1);
}
} else {
olua_assert(olua_isuserdata(L, obj), "expect userdata");
lua_pushvalue(L, obj);
lua_pushvalue(L, top + 1);
// L: reftable obj obj|nil
olua_rawset(L, -3); // L: reftable[obj] = true|nil
}
}
lua_settop(L, top);
}
OLUA_API void olua_addref(lua_State *L, int idx, const char *name, int obj, int flags)
{
aux_changeref(L, idx, name, obj, flags);
}
OLUA_API void olua_delref(lua_State *L, int idx, const char *name, int obj, int flags)
{
aux_changeref(L, idx, name, obj, flags | OLUA_REF_REMOVE);
}
OLUA_API void olua_delallrefs(lua_State *L, int idx, const char *name)
{
aux_changeref(L, idx, name, 0, OLUA_REF_ALONE | OLUA_REF_REMOVE);
}
OLUA_API void olua_visitrefs(lua_State *L, int idx, const char *name, olua_RefVisitor walk)
{
olua_assert(olua_isuserdata(L, idx), "expect userdata");
idx = lua_absindex(L, idx);
if (olua_loadref(L, idx, name) == LUA_TTABLE) {
lua_pushnil(L);
while (lua_next(L, -2)) {
int kidx = lua_gettop(L) - 1;
if (walk(L, -2)) { // remove?
lua_pushvalue(L, kidx);