forked from cloudwu/lua-snapshot
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsnapshot.c
823 lines (745 loc) · 17.2 KB
/
snapshot.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
#include <lua.h>
#include <lauxlib.h>
#include "lstate.h"
#include "lstring.h"
#include "ltable.h"
#include "lfunc.h"
#include "lgc.h"
#ifdef isshared
static int
check_shared(lua_State* L, int idx, int t) {
switch(t) {
case LUA_TTABLE:
case LUA_TSTRING: {
GCObject* o = (GCObject*)lua_topointer(L, idx);
if(o) {
return isshared(o);
}
}
default:
break;
}
return 0;
}
#endif
struct snapshot_params {
int max_count;
int current_mark_count;
};
static void mark_object(lua_State *L, lua_State *dL, const void * parent, const char * desc, struct snapshot_params* args);
#define check_limit(L, args) do { \
if((args)->max_count > 0) { \
if(((args)->current_mark_count)++ > (args)->max_count) { \
lua_pop((L),1); \
return; \
} \
} \
} while(0);
#if LUA_VERSION_NUM == 501
static void
luaL_checkversion(lua_State *L) {
if (lua_pushthread(L) == 0) {
luaL_error(L, "Must require in main thread");
}
lua_setfield(L, LUA_REGISTRYINDEX, "mainthread");
}
static void
lua_rawsetp(lua_State *L, int idx, const void *p) {
if (idx < 0) {
idx += lua_gettop(L) + 1;
}
lua_pushlightuserdata(L, (void *)p);
lua_insert(L, -2);
lua_rawset(L, idx);
}
static void
lua_rawgetp(lua_State *L, int idx, const void *p) {
if (idx < 0) {
idx += lua_gettop(L) + 1;
}
lua_pushlightuserdata(L, (void *)p);
lua_rawget(L, idx);
}
static void
lua_getuservalue(lua_State *L, int idx) {
lua_getfenv(L, idx);
}
static void
mark_function_env(lua_State *L, lua_State *dL, const void * t, struct snapshot_params* args) {
lua_getfenv(L,-1);
if (lua_istable(L,-1)) {
mark_object(L, dL, t, "[environment]");
} else {
lua_pop(L,1);
}
}
// lua 5.1 has no light c function
#define is_lightcfunction(L, idx) (0)
#else
#define mark_function_env(L,dL,t,args)
static int
is_lightcfunction(lua_State *L, int idx) {
if (lua_iscfunction(L, idx)) {
if (lua_getupvalue(L, idx, 1) == NULL) {
return 1;
}
lua_pop(L, 1);
}
return 0;
}
#endif
#if LUA_VERSION_NUM == 504
/*
** True if value of 'alimit' is equal to the real size of the array
** part of table 't'. (Otherwise, the array part must be larger than
** 'alimit'.)
*/
#define limitequalsasize(t) (isrealasize(t) || ispow2((t)->alimit))
/*
** Returns the real size of the 'array' array
*/
static unsigned int _luaH_realasize (const Table *t) {
if (limitequalsasize(t))
return t->alimit; /* this is the size */
else {
unsigned int size = t->alimit;
/* compute the smallest power of 2 not smaller than 'n' */
size |= (size >> 1);
size |= (size >> 2);
size |= (size >> 4);
size |= (size >> 8);
size |= (size >> 16);
#if (UINT_MAX >> 30) > 3
size |= (size >> 32); /* unsigned int has more than 32 bits */
#endif
size++;
lua_assert(ispow2(size) && size/2 < t->alimit && t->alimit < size);
return size;
}
}
#endif
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#define TABLE 1
#define FUNCTION 2
#define SOURCE 3
#define THREAD 4
#define USERDATA 5
#define STRING 6
#define MARK 7
static bool
ismarked(lua_State *dL, const void *p) {
lua_rawgetp(dL, MARK, p);
if (lua_isnil(dL,-1)) {
lua_pop(dL,1);
lua_pushboolean(dL,1);
lua_rawsetp(dL, MARK, p);
return false;
}
lua_pop(dL,1);
return true;
}
static const void *
readobject(lua_State *L, lua_State *dL, const void *parent, const char *desc) {
int t = lua_type(L, -1);
#ifdef isshared
if(check_shared(L, -1, t)) {
lua_pop(L, 1);
return NULL;
}
#endif
int tidx = 0;
switch (t) {
case LUA_TTABLE:
tidx = TABLE;
break;
case LUA_TFUNCTION:
if (is_lightcfunction(L, -1)) {
lua_pop(L, 1);
return NULL;
}
tidx = FUNCTION;
break;
case LUA_TTHREAD:
tidx = THREAD;
break;
case LUA_TUSERDATA:
tidx = USERDATA;
break;
#ifdef DUMP_STRING
case LUA_TSTRING:
tidx = STRING;
break;
#endif
default:
lua_pop(L, 1);
return NULL;
}
const void * p = NULL;
if(t == LUA_TUSERDATA) {
const TValue *o = NULL;
#if LUA_VERSION_NUM == 504
#if LUA_VERSION_RELEASE_NUM < 50405
o = s2v(L->top - 1);
#else
o = s2v(L->top.p - 1);
#endif
#else
o = L->top - 1;
#endif
// const TValue *o = index2value(L, -1);
p = (const void*)uvalue(o);
} else if (t == LUA_TSTRING) {
p = lua_tostring(L, -1);
}
else {
p = (const void*)lua_topointer(L, -1);
}
if (ismarked(dL, p)) {
lua_rawgetp(dL, tidx, p);
if (!lua_isnil(dL,-1)) {
lua_pushstring(dL,desc);
lua_rawsetp(dL, -2, parent);
}
lua_pop(dL,1);
lua_pop(L,1);
return NULL;
}
lua_newtable(dL);
lua_pushstring(dL,desc);
lua_rawsetp(dL, -2, parent);
lua_rawsetp(dL, tidx, p);
return p;
}
static const char *
keystring(lua_State *L, int index, char * buffer) {
int t = lua_type(L,index);
switch (t) {
case LUA_TSTRING:
return lua_tostring(L,index);
case LUA_TNUMBER:
sprintf(buffer,"[%lg]",lua_tonumber(L,index));
break;
case LUA_TBOOLEAN:
sprintf(buffer,"[%s]",lua_toboolean(L,index) ? "true" : "false");
break;
case LUA_TNIL:
sprintf(buffer,"[nil]");
break;
default:
sprintf(buffer,"[%s:%p]",lua_typename(L,t),lua_topointer(L,index));
break;
}
return buffer;
}
static void
mark_table(lua_State *L, lua_State *dL, const void * parent, const char * desc, struct snapshot_params* args) {
const void * t = readobject(L, dL, parent, desc);
if (t == NULL)
return;
check_limit(L, args);
bool weakk = false;
bool weakv = false;
if (lua_getmetatable(L, -1)) {
lua_pushliteral(L, "__mode");
lua_rawget(L, -2);
if (lua_isstring(L,-1)) {
const char *mode = lua_tostring(L, -1);
if (strchr(mode, 'k')) {
weakk = true;
}
if (strchr(mode, 'v')) {
weakv = true;
}
}
lua_pop(L,1);
luaL_checkstack(L, LUA_MINSTACK, NULL);
mark_table(L, dL, t, "[metatable]", args);
}
lua_pushnil(L);
while (lua_next(L, -2) != 0) {
if (weakv) {
lua_pop(L,1);
} else {
char temp[32];
const char * desc = keystring(L, -2, temp);
mark_object(L, dL, t , desc, args);
}
if (!weakk) {
lua_pushvalue(L,-1);
mark_object(L, dL, t , "[key]", args);
}
}
lua_pop(L,1);
}
static void
mark_string(lua_State *L, lua_State *dL, const void * parent, const char *desc) {
const void* t = readobject(L, dL, parent, desc);
if(t == NULL)
return;
lua_pop(L,1);
}
static void
mark_userdata(lua_State *L, lua_State *dL, const void * parent, const char *desc, struct snapshot_params* args) {
const void * t = readobject(L, dL, parent, desc);
if (t == NULL)
return;
check_limit(L, args);
if (lua_getmetatable(L, -1)) {
mark_table(L, dL, t, "[metatable]", args);
}
lua_getuservalue(L,-1);
if (lua_isnil(L,-1)) {
lua_pop(L,2);
} else {
mark_object(L, dL, t, "[uservalue]", args);
lua_pop(L,1);
}
}
static void
mark_function(lua_State *L, lua_State *dL, const void * parent, const char *desc, struct snapshot_params* args) {
const void * t = readobject(L, dL, parent, desc);
if (t == NULL)
return;
check_limit(L, args);
mark_function_env(L,dL,t, args);
int i;
for (i=1;;i++) {
const char *name = lua_getupvalue(L,-1,i);
if (name == NULL)
break;
mark_object(L, dL, t, name[0] ? name : "[upvalue]", args);
}
if (lua_iscfunction(L,-1)) {
lua_pop(L,1);
} else {
lua_Debug ar;
lua_getinfo(L, ">S", &ar);
luaL_Buffer b;
luaL_buffinit(dL, &b);
luaL_addstring(&b, ar.short_src);
char tmp[16];
sprintf(tmp,":%d",ar.linedefined);
luaL_addstring(&b, tmp);
luaL_pushresult(&b);
lua_rawsetp(dL, SOURCE, t);
}
}
static void
mark_thread(lua_State *L, lua_State *dL, const void * parent, const char *desc, struct snapshot_params* args) {
const void * t = readobject(L, dL, parent, desc);
if (t == NULL)
return;
check_limit(L, args);
int level = 0;
lua_State *cL = lua_tothread(L,-1);
if (cL == L) {
level = 1;
} else {
// mark stack
int top = lua_gettop(cL);
luaL_checkstack(cL, 1, NULL);
int i;
char tmp[16];
for (i=0;i<top;i++) {
lua_pushvalue(cL, i+1);
sprintf(tmp, "[%d]", i+1);
mark_object(cL, dL, cL, tmp, args);
}
}
lua_Debug ar;
luaL_Buffer b;
luaL_buffinit(dL, &b);
while (lua_getstack(cL, level, &ar)) {
char tmp[128];
lua_getinfo(cL, "Sl", &ar);
luaL_addstring(&b, ar.short_src);
if (ar.currentline >=0) {
char tmp[16];
sprintf(tmp,":%d ",ar.currentline);
luaL_addstring(&b, tmp);
}
int i,j;
for (j=1;j>-1;j-=2) {
for (i=j;;i+=j) {
const char * name = lua_getlocal(cL, &ar, i);
if (name == NULL)
break;
snprintf(tmp, sizeof(tmp), "%s{#%s:%d}",name,ar.short_src,ar.linedefined);
mark_object(cL, dL, t, tmp, args);
}
}
++level;
}
luaL_pushresult(&b);
lua_rawsetp(dL, SOURCE, t);
lua_pop(L,1);
}
static void
mark_object(lua_State *L, lua_State *dL, const void * parent, const char *desc, struct snapshot_params* args) {
luaL_checkstack(L, LUA_MINSTACK, NULL);
int t = lua_type(L, -1);
#ifdef isshared
if(check_shared(L, -1, t)) {
lua_pop(L, 1);
return;
}
#endif
switch (t) {
case LUA_TTABLE:
mark_table(L, dL, parent, desc, args);
break;
case LUA_TUSERDATA:
mark_userdata(L, dL, parent, desc, args);
break;
case LUA_TFUNCTION:
mark_function(L, dL, parent, desc, args);
break;
case LUA_TTHREAD:
mark_thread(L, dL, parent, desc, args);
break;
case LUA_TSTRING:
mark_string(L, dL, parent, desc);
break;
default:
lua_pop(L,1);
break;
}
}
static int
count_table(lua_State *L, int idx) {
int n = 0;
lua_pushnil(L);
while (lua_next(L, idx) != 0) {
++n;
lua_pop(L,1);
}
return n;
}
static void
gen_table_desc(lua_State *dL, luaL_Buffer *b, const void * parent, const char *desc) {
char tmp[32];
size_t l = sprintf(tmp,"%p : ",parent);
luaL_addlstring(b, tmp, l);
luaL_addstring(b, desc);
luaL_addchar(b, '\n');
}
static size_t
_table_size(Table* p) {
size_t size = sizeof(*p);
size_t tl = (p->lastfree == NULL)?(0):(sizenode(p));
size += tl*sizeof(Node);
#if LUA_VERSION_NUM == 504
size += _luaH_realasize(p)*sizeof(TValue);
#else
size += p->sizearray*sizeof(TValue);
#endif
return size;
}
static size_t
_thread_size(struct lua_State* p) {
size_t size = sizeof(*p);
size += p->nci*sizeof(CallInfo);
#ifdef stacksize
#if LUA_VERSION_RELEASE_NUM < 50405
size += stacksize(p)*sizeof(*p->stack);
#else
size += stacksize(p)*sizeof(*p->stack.p);
#endif
#else
#if LUA_VERSION_RELEASE_NUM < 50405
size += p->stacksize*sizeof(*p->stack);
#else
size += p->stacksize*sizeof(*p->stack.p);
#endif
#endif
return size;
}
static size_t
_userdata_size(Udata *p) {
#if LUA_VERSION_NUM == 504
int size = sizeudata(p->nuvalue, p->len);
return size;
#else
int l = sizeudata(p);
size_t size = sizeludata(l);
return size;
#endif
}
static size_t
_cfunc_size(CClosure* p) {
int n = (int)(p->nupvalues);
size_t size = sizeCclosure(n);
return size;
}
static size_t
_lfunc_size(LClosure *p) {
int n = (int)(p->nupvalues);
size_t size = sizeLclosure(n);
return size;
}
static size_t
_lstring_size(const void* s) {
#if LUA_VERSION_NUM == 504
TString* ts = (TString*)(((char*)s) - offsetof(TString, contents));
#else
TString* ts = (TString*)(((char*)s) - sizeof(UTString));
#endif
size_t size = tsslen(ts);
return size;
}
static void
pdesc(lua_State *L, lua_State *dL, int idx, const char * typename) {
lua_pushnil(dL);
size_t size = 0;
char buff_sz[128] = {0};
while (lua_next(dL, idx) != 0) {
luaL_Buffer b;
luaL_buffinit(L, &b);
const void * key = lua_touserdata(dL, -2);
switch(idx) {
case FUNCTION: {
lua_rawgetp(dL, SOURCE, key);
if (lua_isnil(dL, -1)) {
size = _cfunc_size((CClosure*)key);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
luaL_addstring(&b,"cfunction");
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
} else {
size_t l = 0;
size = _lfunc_size((LClosure*)key);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
const char * s = lua_tolstring(dL, -1, &l);
if(l==0) {
s = "?";
l = 1;
}
luaL_addlstring(&b,s,l);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
}
lua_pop(dL, 1);
}break;
case THREAD: {
lua_rawgetp(dL, SOURCE, key);
size_t l = 0;
size = _thread_size((struct lua_State*)key);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
const char * s = lua_tolstring(dL, -1, &l);
luaL_addstring(&b,"thread ");
luaL_addlstring(&b,s,l);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
lua_pop(dL, 1);
}break;
case TABLE: {
size = _table_size((Table*)key);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
luaL_addstring(&b, typename);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
}break;
case USERDATA: {
Udata* p = (Udata*)key;
size = _userdata_size(p);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
luaL_addstring(&b, typename);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
}break;
case STRING: {
size = _lstring_size(key);
snprintf(buff_sz, sizeof(buff_sz), "{%zd}", size);
luaL_addstring(&b, typename);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
}break;
default: {
snprintf(buff_sz, sizeof(buff_sz), "{0}");
luaL_addstring(&b, typename);
luaL_addchar(&b,' ');
luaL_addstring(&b, buff_sz);
luaL_addchar(&b,'\n');
}break;
}
lua_pushnil(dL);
while (lua_next(dL, -2) != 0) {
const void * parent = lua_touserdata(dL,-2);
const char * desc = luaL_checkstring(dL,-1);
gen_table_desc(dL, &b, parent, desc);
lua_pop(dL,1);
}
luaL_pushresult(&b);
lua_rawsetp(L, -2, key);
lua_pop(dL,1);
}
}
static void
gen_result(lua_State *L, lua_State *dL) {
int count = 0;
count += count_table(dL, TABLE);
count += count_table(dL, FUNCTION);
count += count_table(dL, USERDATA);
count += count_table(dL, THREAD);
count += count_table(dL, STRING);
lua_createtable(L, 0, count);
pdesc(L, dL, TABLE, "table");
pdesc(L, dL, USERDATA, "userdata");
pdesc(L, dL, FUNCTION, "function");
pdesc(L, dL, THREAD, "thread");
pdesc(L, dL, STRING, "string");
}
static int
snapshot(lua_State *L) {
int i;
struct snapshot_params args = {0};
args.max_count = luaL_optinteger(L, 1, 0);
lua_State *dL = luaL_newstate();
for (i=0;i<MARK;i++) {
lua_newtable(dL);
}
lua_pushvalue(L, LUA_REGISTRYINDEX);
mark_table(L, dL, NULL, "[registry]", &args);
gen_result(L, dL);
lua_close(dL);
return 1;
}
static int
l_str2lightuserdata(lua_State* L) {
const char* s = luaL_checklstring(L, 1, NULL);
void* p = NULL;
sscanf(s, "%p", &p);
lua_pushlightuserdata(L, p);
return 1;
}
static int
l_lightuserdata2str(lua_State* L) {
void* p = lua_touserdata(L, 1);
char buff[128];
snprintf(buff, sizeof(buff), "%p", p);
lua_pushstring(L, buff);
return 1;
}
static int
l_obj2addr(lua_State* L) {
int t = lua_type(L, 1);
void * p = NULL;
if(t == LUA_TUSERDATA) {
const TValue *o = NULL;
#if LUA_VERSION_NUM == 504
#if LUA_VERSION_RELEASE_NUM < 50405
o = s2v(L->top - 1);
#else
o = s2v(L->top.p - 1);
#endif
#else
o = L->top - 1;
#endif
// const TValue *o = index2value(L, -1);
p = (void*)uvalue(o);
} else if (t == LUA_TSTRING) {
p = (void*)lua_tostring(L, -1);
}
else {
p = (void*)lua_topointer(L, -1);
}
lua_pushlightuserdata(L, p);
return 1;
}
static void
_objectsize(lua_State* L, int value_idx, int map_idx, int max_deep, int cur_deep, size_t* sz_p) {
size_t size = 0;
int t = lua_type(L, value_idx);
if(cur_deep > max_deep) {
return;
}
// check share object
void* key = (void*)lua_topointer(L, value_idx);
#ifdef isshared
if(t == LUA_TSTRING || t == LUA_TTABLE) {
GCObject* o = (GCObject*)key;
if(isshared(o)) {
return;
}
}
#endif
luaL_checkstack(L, LUA_MINSTACK, NULL);
lua_pushlightuserdata(L, key);
int mt = lua_gettable(L, map_idx);
lua_pop(L, 1);
if(mt != LUA_TNIL) {
return;
}
lua_pushlightuserdata(L, key);
lua_pushboolean(L, true);
lua_settable(L, map_idx);
switch(t) {
case LUA_TSTRING: {
key = (void*)lua_tostring(L, value_idx);
size = _lstring_size(key);
*sz_p += size;
} break;
case LUA_TFUNCTION: {
if (is_lightcfunction(L, value_idx)) {
lua_pop(L, 1);
size = _cfunc_size((CClosure*)key);
} else {
size = _lfunc_size((LClosure*)key);
}
*sz_p += size;
} break;
case LUA_TTHREAD: {
size = _thread_size((struct lua_State*)key);
*sz_p += size;
} break;
case LUA_TTABLE: {
size = _table_size((Table*)key);
*sz_p += size;
lua_pushnil(L);
while(lua_next(L, value_idx) != 0) {
int vidx = lua_gettop(L);
int kidx = vidx-1;
_objectsize(L, vidx, map_idx, max_deep, cur_deep+1, sz_p);
_objectsize(L, kidx, map_idx, max_deep, cur_deep+1, sz_p);
lua_pop(L, 1);
}
} break;
}
lua_pushlightuserdata(L, key);
lua_pushnil(L);
lua_settable(L, map_idx);
}
static int
l_objsize(lua_State* L) {
lua_newtable(L);
int map_idx = lua_gettop(L);
bool recursive = lua_toboolean(L, 2);
size_t sz = 0;
int max_deep = (recursive)?(128):(0);
_objectsize(L, 1, map_idx, max_deep, 0, &sz);
lua_pushinteger(L, sz);
return 1;
}
int
luaopen_snapshot(lua_State *L) {
luaL_checkversion(L);
luaL_Reg l[] = {
{"snapshot", snapshot},
{"str2ud", l_str2lightuserdata},
{"ud2str", l_lightuserdata2str},
{"obj2addr", l_obj2addr},
{"objsize", l_objsize},
{NULL, NULL},
};
// lua_pushcfunction(L, snapshot);
luaL_newlib(L, l);
return 1;
}