This repository has been archived by the owner on Nov 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
pluto.vc.c
executable file
·1555 lines (1421 loc) · 40 KB
/
pluto.vc.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
/* $Id$ */
/* Pluto - Heavy-duty persistence for Lua
* Copyright (C) 2004 by Ben Sunshine-Hill, and released into the public
* domain. People making use of this software as part of an application
* are politely requested to email the author at [email protected]
* with a brief description of the application, primarily to satisfy his
* curiosity.
*
* 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 "lua.h"
#include "pluto.h"
#define USE_PDEP
#ifdef USE_PDEP
#include "pdep/pdep.h"
#define LIF(prefix, name) pdep ## _ ## name
#else
#include "lapi.h"
#include "ldo.h"
#include "lfunc.h"
#include "lgc.h"
#include "llimits.h"
#include "lmem.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lstring.h"
#include "lauxlib.h"
#define LIF(prefix, name) lua ## prefix ## _ ## name
#endif
#include <string.h>
#define PLUTO_DEBUG
#ifdef PLUTO_DEBUG
#include <stdio.h>
#endif
#define PLUTO_TPERMANENT 101
#define verify(x) { int v = (int)((x)); v=v; lua_assert(v); }
typedef struct PersistInfo_t {
lua_State *L;
int counter;
lua_Chunkwriter writer;
void *ud;
#ifdef PLUTO_DEBUG
int level;
#endif
} PersistInfo;
#ifdef PLUTO_DEBUG
void printindent(int indent)
{
int il;
for(il=0; il<indent; il++) {
printf(" ");
}
}
#endif
/* Mutual recursion requires prototype */
static void persist(PersistInfo *pi);
/* A simple reimplementation of the unfortunately static function luaA_index.
* Does not support the global table, registry, or upvalues. */
static StkId getobject(lua_State *L, int stackpos)
{
if(stackpos > 0) {
lua_assert(L->base+stackpos-1 < L->top);
return L->base+stackpos-1;
} else {
lua_assert(L->top-stackpos >= L->base);
return L->top+stackpos;
}
}
/* Choose whether to do a regular or special persistence based on an object's
* metatable. "default" is whether the object, if it doesn't have a __persist
* entry, is literally persistable or not.
* Pushes the unpersist closure and returns true if special persistence is
* used. */
static int persistspecialobject(PersistInfo *pi, int defaction)
{
/* perms reftbl ... obj */
lua_checkstack(pi->L, 4);
/* Check whether we should persist literally, or via the __persist
* metafunction */
if(!lua_getmetatable(pi->L, -1)) {
if(defaction) {
{
int zero = 0;
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
}
return 0;
} else {
lua_pushstring(pi->L, "Type not literally persistable by default");
lua_error(pi->L);
}
}
/* perms reftbl sptbl ... obj mt */
lua_pushstring(pi->L, "__persist");
/* perms reftbl sptbl ... obj mt "__persist" */
lua_rawget(pi->L, -2);
/* perms reftbl sptbl ... obj mt __persist? */
if(lua_isnil(pi->L, -1)) {
/* perms reftbl sptbl ... obj mt nil */
lua_pop(pi->L, 2);
/* perms reftbl sptbl ... obj */
if(defaction) {
{
int zero = 0;
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
}
return 0;
} else {
lua_pushstring(pi->L, "Type not literally persistable by default");
lua_error(pi->L);
return 0; /* not reached */
}
} else if(lua_isboolean(pi->L, -1)) {
/* perms reftbl sptbl ... obj mt bool */
if(lua_toboolean(pi->L, -1)) {
/* perms reftbl sptbl ... obj mt true */
lua_pop(pi->L, 2);
/* perms reftbl sptbl ... obj */
{
int zero = 0;
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
}
return 0;
} else {
lua_pushstring(pi->L, "Metatable forbade persistence");
lua_error(pi->L);
return 0; /* not reached */
}
} else if(!lua_isfunction(pi->L, -1)) {
lua_pushstring(pi->L, "__persist not nil, boolean, or function");
lua_error(pi->L);
}
/* perms reftbl ... obj mt __persist */
lua_pushvalue(pi->L, -3);
/* perms reftbl ... obj mt __persist obj */
#ifdef PLUTO_PASS_USERDATA_TO_PERSIST
lua_pushlightuserdata(pi->L, (void*)pi->writer);
lua_pushlightuserdata(pi->L, pi->ud);
/* perms reftbl ... obj mt __persist obj ud */
lua_call(pi->L, 3, 1);
/* perms reftbl ... obj mt func? */
#else
lua_call(pi->L, 1, 1);
/* perms reftbl ... obj mt func? */
#endif
/* perms reftbl ... obj mt func? */
if(!lua_isfunction(pi->L, -1)) {
lua_pushstring(pi->L, "__persist function did not return a function");
lua_error(pi->L);
}
/* perms reftbl ... obj mt func */
{
int one = 1;
pi->writer(pi->L, &one, sizeof(int), pi->ud);
}
persist(pi);
/* perms reftbl ... obj mt func */
lua_pop(pi->L, 2);
/* perms reftbl ... obj */
return 1;
}
static void persisttable(PersistInfo *pi)
{
/* perms reftbl ... tbl */
lua_checkstack(pi->L, 3);
if(persistspecialobject(pi, 1)) {
/* perms reftbl ... tbl */
return;
}
/* perms reftbl ... tbl */
/* First, persist the metatable (if any) */
if(!lua_getmetatable(pi->L, -1)) {
lua_pushnil(pi->L);
}
/* perms reftbl ... tbl mt/nil */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... tbl */
/* Now, persist all k/v pairs */
lua_pushnil(pi->L);
/* perms reftbl ... tbl nil */
while(lua_next(pi->L, -2)) {
/* perms reftbl ... tbl k v */
lua_pushvalue(pi->L, -2);
/* perms reftbl ... tbl k v k */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... tbl k v */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... tbl k */
}
/* perms reftbl ... tbl */
/* Terminate list */
lua_pushnil(pi->L);
/* perms reftbl ... tbl nil */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... tbl */
}
static void persistuserdata(PersistInfo *pi) {
/* perms reftbl ... udata */
lua_checkstack(pi->L, 2);
if(persistspecialobject(pi, 0)) {
/* perms reftbl ... udata */
return;
} else {
/* Use literal persistence */
size_t length = uvalue(getobject(pi->L, -1))->len;
pi->writer(pi->L, &length, sizeof(size_t), pi->ud);
pi->writer(pi->L, lua_touserdata(pi->L, -1), length, pi->ud);
if(!lua_getmetatable(pi->L, -1)) {
/* perms reftbl ... udata */
lua_pushnil(pi->L);
/* perms reftbl ... udata mt/nil */
}
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... udata */
}
}
static Proto *toproto(lua_State *L, int stackpos)
{
return gco2p(getobject(L, stackpos)->value.gc);
}
static UpVal *toupval(lua_State *L, int stackpos)
{
lua_assert(ttype(getobject(L, stackpos)) == LUA_TUPVAL);
return gco2uv(getobject(L, stackpos)->value.gc);
}
static void pushproto(lua_State *L, Proto *proto)
{
TValue o;
setptvalue(L, &o, proto);
LIF(A,pushobject)(L, &o);
}
#define setuvvalue(L,obj,x) \
{ TValue *i_o=(obj); \
i_o->value.gc=cast(GCObject *, (x)); i_o->tt=LUA_TUPVAL; \
checkliveness(G(L),i_o); }
static void pushupval(lua_State *L, UpVal *upval)
{
TValue o;
setuvvalue(L, &o, upval);
LIF(A,pushobject)(L, &o);
}
static void pushclosure(lua_State *L, Closure *closure)
{
TValue o;
setclvalue(L, &o, closure);
LIF(A,pushobject)(L, &o);
}
static void persistfunction(PersistInfo *pi)
{
/* perms reftbl ... func */
Closure *cl = clvalue(getobject(pi->L, -1));
lua_checkstack(pi->L, 2);
if(cl->c.isC) {
/* It's a C function. For now, we aren't going to allow
* persistence of C closures, even if the "C proto" is
* already in the permanents table. */
lua_pushstring(pi->L, "Attempt to persist a C function");
lua_error(pi->L);
} else {
/* It's a Lua closure. */
{
/* We don't really _NEED_ the number of upvals,
* but it'll simplify things a bit */
pi->writer(pi->L, &cl->l.p->nups, sizeof(lu_byte), pi->ud);
}
/* Persist prototype */
{
pushproto(pi->L, cl->l.p);
/* perms reftbl ... func proto */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... func */
}
/* Persist upvalue values (not the upvalue objects
* themselves) */
{
int i;
for(i=0; i<cl->l.p->nups; i++) {
/* perms reftbl ... func */
pushupval(pi->L, cl->l.upvals[i]);
/* perms reftbl ... func upval */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... func */
}
/* perms reftbl ... func */
}
/* Persist function environment */
{
lua_getfenv(pi->L, -1);
/* perms reftbl ... func fenv */
if(lua_equal(pi->L, -1, LUA_GLOBALSINDEX)) {
/* Function has the default fenv */
/* perms reftbl ... func _G */
lua_pop(pi->L, 1);
/* perms reftbl ... func */
lua_pushnil(pi->L);
/* perms reftbl ... func nil */
}
/* perms reftbl ... func fenv/nil */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... func */
}
}
}
/* Upvalues are tricky. Here's why.
*
* A particular upvalue may be either "open", in which case its member v
* points into a thread's stack, or "closed" in which case it points to the
* upvalue itself. An upvalue is closed under any of the following conditions:
* -- The function that initially declared the variable "local" returns
* -- The thread in which the closure was created is garbage collected
*
* To make things wackier, just because a thread is reachable by Lua doesn't
* mean it's in our root set. We need to be able to treat an open upvalue
* from an unreachable thread as a closed upvalue.
*
* The solution:
* (a) For the purposes of persisting, don't indicate whether an upvalue is
* closed or not.
* (b) When unpersisting, pretend that all upvalues are closed.
* (c) When persisting, persist all open upvalues referenced by a thread
* that is persisted, and tag each one with the corresponding stack position
* (d) When unpersisting, "reopen" each of these upvalues as the thread is
* unpersisted
*/
static void persistupval(PersistInfo *pi)
{
/* perms reftbl ... upval */
UpVal *uv = toupval(pi->L, -1);
lua_checkstack(pi->L, 1);
/* We can't permit the upval to linger around on the stack, as Lua
* will bail if its GC finds it. */
lua_pop(pi->L, 1);
/* perms reftbl ... */
LIF(A,pushobject)(pi->L, uv->v);
/* perms reftbl ... obj */
persist(pi);
/* perms reftbl ... obj */
}
static void persistproto(PersistInfo *pi)
{
/* perms reftbl ... proto */
Proto *p = toproto(pi->L, -1);
lua_checkstack(pi->L, 2);
/* Persist constant refs */
{
int i;
pi->writer(pi->L, &p->sizek, sizeof(int), pi->ud);
for(i=0; i<p->sizek; i++) {
LIF(A,pushobject)(pi->L, &p->k[i]);
/* perms reftbl ... proto const */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... proto */
}
}
/* perms reftbl ... proto */
/* serialize inner Proto refs */
{
int i;
pi->writer(pi->L, &p->sizep, sizeof(int), pi->ud);
for(i=0; i<p->sizep; i++)
{
pushproto(pi->L, p->p[i]);
/* perms reftbl ... proto subproto */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... proto */
}
}
/* perms reftbl ... proto */
/* Serialize code */
{
pi->writer(pi->L, &p->sizecode, sizeof(int), pi->ud);
pi->writer(pi->L, p->code, sizeof(Instruction) * p->sizecode, pi->ud);
}
/* Serialize misc values */
{
pi->writer(pi->L, &p->nups, sizeof(lu_byte), pi->ud);
pi->writer(pi->L, &p->numparams, sizeof(lu_byte), pi->ud);
pi->writer(pi->L, &p->is_vararg, sizeof(lu_byte), pi->ud);
pi->writer(pi->L, &p->maxstacksize, sizeof(lu_byte), pi->ud);
}
/* We do not currently persist upvalue names, local variable names,
* variable lifetimes, line info, or source code. */
}
/* Copies a stack, but the stack is reversed in the process
*/
static size_t revappendstack(lua_State *from, lua_State *to)
{
StkId o;
for(o=from->top-1; o>=from->stack; o--) {
setobj2s(to, to->top, o);
to->top++;
}
return from->top - from->stack;
}
/* Persist all stack members
*/
static void persistthread(PersistInfo *pi)
{
size_t posremaining;
lua_State *L2;
/* perms reftbl ... thr */
L2 = lua_tothread(pi->L, -1);
lua_checkstack(pi->L, L2->top - L2->stack + 1);
if(pi->L == L2) {
lua_pushstring(pi->L, "Can't persist currently running thread");
lua_error(pi->L);
return; /* not reached */
}
/* Persist the stack */
posremaining = revappendstack(L2, pi->L);
/* perms reftbl ... thr (rev'ed contents of L2) */
pi->writer(pi->L, &posremaining, sizeof(size_t), pi->ud);
for(; posremaining > 0; posremaining--) {
persist(pi);
lua_pop(pi->L, 1);
}
/* perms reftbl ... thr */
/* Now, persist the CallInfo stack. */
{
size_t i, numframes = (L2->ci - L2->base_ci) + 1;
pi->writer(pi->L, &numframes, sizeof(size_t), pi->ud);
for(i=0; i<numframes; i++) {
CallInfo *ci = L2->base_ci + i;
size_t stackbase = ci->base - L2->stack;
size_t stackfunc = ci->func - L2->stack;
size_t stacktop = ci->top - L2->stack;
size_t savedpc = (ci != L2->base_ci) ?
ci->savedpc - ci_func(ci)->l.p->code :
0;
pi->writer(pi->L, &stackbase, sizeof(size_t), pi->ud);
pi->writer(pi->L, &stackfunc, sizeof(size_t), pi->ud);
pi->writer(pi->L, &stacktop, sizeof(size_t), pi->ud);
pi->writer(pi->L, &ci->nresults, sizeof(int), pi->ud);
pi->writer(pi->L, &savedpc, sizeof(size_t), pi->ud);
}
}
/* Serialize the state's other parameters, with the exception of upval stuff */
{
size_t stackbase = L2->base - L2->stack;
size_t stacktop = L2->top - L2->stack;
lua_assert(L2->nCcalls <= 1);
pi->writer(pi->L, &L2->status, sizeof(lu_byte), pi->ud);
pi->writer(pi->L, &stackbase, sizeof(size_t), pi->ud);
pi->writer(pi->L, &stacktop, sizeof(size_t), pi->ud);
pi->writer(pi->L, &L2->errfunc, sizeof(ptrdiff_t), pi->ud);
}
/* Finally, record upvalues which need to be reopened */
/* See the comment above persistupval() for why we do this */
{
GCObject *gco;
UpVal *uv;
/* perms reftbl ... thr */
for(gco = L2->openupval; gco != NULL; gco = uv->next) {
size_t stackpos;
uv = gco2uv(gco);
/* Make sure upvalue is really open */
lua_assert(uv->v != &uv->u.value);
pushupval(pi->L, uv);
/* perms reftbl ... thr uv */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... thr */
stackpos = uv->v - L2->stack;
pi->writer(pi->L, &stackpos, sizeof(size_t), pi->ud);
}
/* perms reftbl ... thr */
lua_pushnil(pi->L);
/* perms reftbl ... thr nil */
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... thr */
}
/* perms reftbl ... thr */
}
static void persistboolean(PersistInfo *pi)
{
int b = lua_toboolean(pi->L, -1);
pi->writer(pi->L, &b, sizeof(int), pi->ud);
}
static void persistlightuserdata(PersistInfo *pi)
{
void *p = lua_touserdata(pi->L, -1);
pi->writer(pi->L, &p, sizeof(void *), pi->ud);
}
static void persistnumber(PersistInfo *pi)
{
lua_Number n = lua_tonumber(pi->L, -1);
pi->writer(pi->L, &n, sizeof(lua_Number), pi->ud);
}
static void persiststring(PersistInfo *pi)
{
size_t length = lua_strlen(pi->L, -1);
pi->writer(pi->L, &length, sizeof(size_t), pi->ud);
pi->writer(pi->L, lua_tostring(pi->L, -1), length, pi->ud);
}
/* Top-level delegating persist function
*/
static void persist(PersistInfo *pi)
{
/* perms reftbl ... obj */
lua_checkstack(pi->L, 2);
/* If the object has already been written, write a reference to it */
lua_pushvalue(pi->L, -1);
/* perms reftbl ... obj obj */
lua_rawget(pi->L, 2);
/* perms reftbl ... obj ref? */
if(!lua_isnil(pi->L, -1)) {
/* perms reftbl ... obj ref */
int zero = 0;
int ref = (int)lua_touserdata(pi->L, -1);
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
pi->writer(pi->L, &ref, sizeof(int), pi->ud);
lua_pop(pi->L, 1);
/* perms reftbl ... obj ref */
#ifdef PLUTO_DEBUG
printindent(pi->level);
printf("0 %d\n", ref);
#endif
return;
}
/* perms reftbl ... obj nil */
lua_pop(pi->L, 1);
/* perms reftbl ... obj */
/* If the object is nil, write the pseudoreference 0 */
if(lua_isnil(pi->L, -1)) {
int zero = 0;
/* firsttime */
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
/* ref */
pi->writer(pi->L, &zero, sizeof(int), pi->ud);
#ifdef PLUTO_DEBUG
printindent(pi->level);
printf("0 0\n");
#endif
return;
}
{
/* indicate that it's the first time */
int one = 1;
pi->writer(pi->L, &one, sizeof(int), pi->ud);
}
lua_pushvalue(pi->L, -1);
/* perms reftbl ... obj obj */
lua_pushlightuserdata(pi->L, (void*)(++(pi->counter)));
/* perms reftbl ... obj obj ref */
lua_rawset(pi->L, 2);
/* perms reftbl ... obj */
pi->writer(pi->L, &pi->counter, sizeof(int), pi->ud);
/* At this point, we'll give the permanents table a chance to play. */
{
lua_pushvalue(pi->L, -1);
/* perms reftbl ... obj obj */
lua_gettable(pi->L, 1);
/* perms reftbl ... obj permkey? */
if(!lua_isnil(pi->L, -1)) {
/* perms reftbl ... obj permkey */
int type = PLUTO_TPERMANENT;
#ifdef PLUTO_DEBUG
printindent(pi->level);
printf("1 %d PERM\n", pi->counter);
pi->level++;
#endif
pi->writer(pi->L, &type, sizeof(int), pi->ud);
persist(pi);
lua_pop(pi->L, 1);
/* perms reftbl ... obj */
#ifdef PLUTO_DEBUG
pi->level--;
#endif
return;
} else {
/* perms reftbl ... obj nil */
lua_pop(pi->L, 1);
/* perms reftbl ... obj */
}
/* perms reftbl ... obj */
}
{
int type = lua_type(pi->L, -1);
pi->writer(pi->L, &type, sizeof(int), pi->ud);
#ifdef PLUTO_DEBUG
printindent(pi->level);
printf("1 %d %d\n", pi->counter, type);
pi->level++;
#endif
}
switch(lua_type(pi->L, -1)) {
case LUA_TBOOLEAN:
persistboolean(pi);
break;
case LUA_TLIGHTUSERDATA:
persistlightuserdata(pi);
break;
case LUA_TNUMBER:
persistnumber(pi);
break;
case LUA_TSTRING:
persiststring(pi);
break;
case LUA_TTABLE:
persisttable(pi);
break;
case LUA_TFUNCTION:
persistfunction(pi);
break;
case LUA_TTHREAD:
persistthread(pi);
break;
case LUA_TPROTO:
persistproto(pi);
break;
case LUA_TUPVAL:
persistupval(pi);
break;
case LUA_TUSERDATA:
persistuserdata(pi);
break;
default:
lua_assert(0);
}
#ifdef PLUTO_DEBUG
pi->level--;
#endif
}
void pluto_persist(lua_State *L, lua_Chunkwriter writer, void *ud)
{
PersistInfo pi;
pi.counter = 0;
pi.L = L;
pi.writer = writer;
pi.ud = ud;
#ifdef PLUTO_DEBUG
pi.level = 0;
#endif
lua_checkstack(L, 4);
/* perms? rootobj? ...? */
lua_assert(lua_gettop(L) == 2);
/* perms rootobj */
lua_assert(!lua_isnil(L, 2));
/* perms rootobj */
lua_newtable(L);
/* perms rootobj reftbl */
/* Now we're going to make the table weakly keyed. This prevents the
* GC from visiting it and trying to mark things it doesn't want to
* mark in tables, e.g. upvalues. All objects in the table are
* a priori reachable, so it doesn't matter that we do this. */
lua_newtable(L);
/* perms rootobj reftbl mt */
lua_pushstring(L, "__mode");
/* perms rootobj reftbl mt "__mode" */
lua_pushstring(L, "k");
/* perms rootobj reftbl mt "__mode" "k" */
lua_settable(L, 4);
/* perms rootobj reftbl mt */
lua_setmetatable(L, 3);
/* perms rootobj reftbl */
lua_insert(L, 2);
/* perms reftbl rootobj */
persist(&pi);
/* perms reftbl rootobj */
lua_remove(L, 2);
/* perms rootobj */
}
typedef struct WriterInfo_t {
char* buf;
size_t buflen;
} WriterInfo;
static int bufwriter (lua_State *L, const void* p, size_t sz, void* ud) {
const char* cp = (const char*)p;
WriterInfo *wi = (WriterInfo *)ud;
LIF(M,reallocvector)(L, wi->buf, wi->buflen, wi->buflen+sz, char);
while(sz)
{
/* how dearly I love ugly C pointer twiddling */
wi->buf[wi->buflen++] = *cp++;
sz--;
}
return 0;
}
int persist_l(lua_State *L)
{
/* perms? rootobj? ...? */
WriterInfo wi;
wi.buf = NULL;
wi.buflen = 0;
lua_settop(L, 2);
/* perms? rootobj? */
luaL_checktype(L, 1, LUA_TTABLE);
/* perms rootobj? */
luaL_checktype(L, 1, LUA_TTABLE);
/* perms rootobj */
pluto_persist(L, bufwriter, &wi);
lua_settop(L, 0);
/* (empty) */
lua_pushlstring(L, wi.buf, wi.buflen);
/* str */
pdep_freearray(L, wi.buf, wi.buflen, char);
return 1;
}
typedef struct UnpersistInfo_t {
lua_State *L;
ZIO zio;
#ifdef PLUTO_DEBUG
int level;
#endif
} UnpersistInfo;
static void unpersist(UnpersistInfo *upi);
/* The object is left on the stack. This is primarily used by unpersist, but
* may be used by GCed objects that may incur cycles in order to preregister
* the object. */
static void registerobject(int ref, UnpersistInfo *upi)
{
/* perms reftbl ... obj */
lua_checkstack(upi->L, 2);
lua_pushlightuserdata(upi->L, (void*)ref);
/* perms reftbl ... obj ref */
lua_pushvalue(upi->L, -2);
/* perms reftbl ... obj ref obj */
lua_settable(upi->L, 2);
/* perms reftbl ... obj */
}
static void unpersistboolean(UnpersistInfo *upi)
{
/* perms reftbl ... */
int b;
lua_checkstack(upi->L, 1);
verify(LIF(Z,read)(&upi->zio, &b, sizeof(int)) == 0);
lua_pushboolean(upi->L, b);
/* perms reftbl ... bool */
}
static void unpersistlightuserdata(UnpersistInfo *upi)
{
/* perms reftbl ... */
void *p;
lua_checkstack(upi->L, 1);
verify(LIF(Z,read)(&upi->zio, &p, sizeof(void *)) == 0);
lua_pushlightuserdata(upi->L, p);
/* perms reftbl ... ludata */
}
static void unpersistnumber(UnpersistInfo *upi)
{
/* perms reftbl ... */
lua_Number n;
lua_checkstack(upi->L, 1);
verify(LIF(Z,read)(&upi->zio, &n, sizeof(lua_Number)) == 0);
lua_pushnumber(upi->L, n);
/* perms reftbl ... num */
}
static void unpersiststring(UnpersistInfo *upi)
{
/* perms reftbl sptbl ref */
int length;
char* string;
lua_checkstack(upi->L, 1);
verify(LIF(Z,read)(&upi->zio, &length, sizeof(int)) == 0);
string = pdep_newvector(upi->L, length, char);
verify(LIF(Z,read)(&upi->zio, string, length) == 0);
lua_pushlstring(upi->L, string, length);
/* perms reftbl sptbl ref str */
pdep_freearray(upi->L, string, length, char);
}
static void unpersistspecialtable(int ref, UnpersistInfo *upi)
{
/* perms reftbl ... */
lua_checkstack(upi->L, 1);
unpersist(upi);
/* perms reftbl ... spfunc? */
lua_assert(lua_isfunction(upi->L, -1));
/* perms reftbl ... spfunc */
lua_call(upi->L, 0, 1);
/* perms reftbl ... tbl? */
lua_assert(lua_istable(upi->L, -1));
/* perms reftbl ... tbl */
}
static void unpersistliteraltable(int ref, UnpersistInfo *upi)
{
/* perms reftbl ... */
lua_checkstack(upi->L, 3);
/* Preregister table for handling of cycles */
lua_newtable(upi->L);
/* perms reftbl ... tbl */
registerobject(ref, upi);
/* perms reftbl ... tbl */
/* Unpersist metatable */
{
unpersist(upi);
/* perms reftbl ... tbl mt/nil? */
if(lua_istable(upi->L, -1)) {
/* perms reftbl ... tbl mt */
lua_setmetatable(upi->L, -2);
/* perms reftbl ... tbl */
} else {
/* perms reftbl ... tbl nil? */
lua_assert(lua_isnil(upi->L, -1));
/* perms reftbl ... tbl nil */
lua_pop(upi->L, 1);
/* perms reftbl ... tbl */
}
/* perms reftbl ... tbl */
}
while(1)
{
/* perms reftbl ... tbl */
unpersist(upi);
/* perms reftbl ... tbl key/nil */
if(lua_isnil(upi->L, -1)) {
/* perms reftbl ... tbl nil */
lua_pop(upi->L, 1);
/* perms reftbl ... tbl */
break;
}
/* perms reftbl ... tbl key */
unpersist(upi);
/* perms reftbl ... tbl key value? */
lua_assert(!lua_isnil(upi->L, -1));
/* perms reftbl ... tbl key value */
lua_rawset(upi->L, -3);
/* perms reftbl ... tbl */
}
}
static void unpersisttable(int ref, UnpersistInfo *upi)
{
/* perms reftbl ... */
lua_checkstack(upi->L, 1);
{
int isspecial;
verify(LIF(Z,read)(&upi->zio, &isspecial, sizeof(int)) == 0);
if(isspecial) {
unpersistspecialtable(ref, upi);
/* perms reftbl ... tbl */
} else {
unpersistliteraltable(ref, upi);
/* perms reftbl ... tbl */
}
/* perms reftbl ... tbl */
}
}
static UpVal *makeupval(lua_State *L, int stackpos)
{
UpVal *uv = pdep_new(L, UpVal);
pdep_link(L, (GCObject*)uv, LUA_TUPVAL);
uv->tt = LUA_TUPVAL;
uv->v = &uv->u.value;
uv->u.l.prev = NULL;
uv->u.l.next = NULL;
setobj(L, uv->v, getobject(L, stackpos));
return uv;
}
static Proto *makefakeproto(lua_State *L, lu_byte nups)
{
Proto *p = pdep_newproto(L);
p->sizelineinfo = 1;
p->lineinfo = pdep_newvector(L, 1, int);
p->lineinfo[0] = 1;
p->sizecode = 1;
p->code = pdep_newvector(L, 1, Instruction);
p->code[0] = CREATE_ABC(OP_RETURN, 0, 1, 0);
p->source = pdep_newlstr(L, "", 0);
p->maxstacksize = 2;
p->nups = nups;
p->sizek = 0;
p->sizep = 0;
return p;
}
/* The GC is not fond of finding upvalues in tables. We get around this
* during persistence using a weakly keyed table, so that the GC doesn't
* bother to mark them. This won't work in unpersisting, however, since
* if we make the values weak they'll be collected (since nothing else
* references them). Our solution, during unpersisting, is to represent
* upvalues as dummy functions, each with one upvalue. */
static void boxupval_start(lua_State *L)
{
LClosure *lcl;
lcl = (LClosure*)pdep_newLclosure(L, 1, hvalue(&L->l_gt));
pushclosure(L, (Closure*)lcl);
/* ... func */
lcl->p = makefakeproto(L, 1);
/* Temporarily initialize the upvalue to nil */
lua_pushnil(L);
lcl->upvals[0] = makeupval(L, -1);
lua_pop(L, 1);
}
static void boxupval_finish(lua_State *L)
{
/* ... func obj */
UpVal *uv;
LClosure *lcl = (LClosure *) clvalue(getobject(L, -2));
lcl->upvals[0]->u.value = *getobject(L, -1);
lua_pop(L, 1);
}
static void unboxupval(lua_State *L)
{
/* ... func */
LClosure *lcl;
UpVal *uv;
lcl = (LClosure*)clvalue(getobject(L, -1));
uv = lcl->upvals[0];