Line data Source code
1 : /*
2 : ** $Id: ldump.c,v 2.8.1.1 2007/12/27 13:02:25 roberto Exp $
3 : ** save precompiled Lua chunks
4 : ** See Copyright Notice in lua.h
5 : */
6 :
7 : #include <stddef.h>
8 :
9 : #define ldump_c
10 : #define LUA_CORE
11 :
12 : #include "lua.h"
13 :
14 : #include "lobject.h"
15 : #include "lstate.h"
16 : #include "lundump.h"
17 :
18 : typedef struct {
19 : lua_State* L;
20 : lua_Writer writer;
21 : void* data;
22 : int strip;
23 : int status;
24 : } DumpState;
25 :
26 : #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
27 : #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
28 :
29 501 : static void DumpBlock(const void* b, size_t size, DumpState* D)
30 : {
31 501 : if (D->status==0)
32 : {
33 : lua_unlock(D->L);
34 501 : D->status=(*D->writer)(D->L,b,size,D->data);
35 : lua_lock(D->L);
36 : }
37 501 : }
38 :
39 122 : static void DumpChar(int y, DumpState* D)
40 : {
41 122 : char x=(char)y;
42 122 : DumpVar(x,D);
43 122 : }
44 :
45 176 : static void DumpInt(int x, DumpState* D)
46 : {
47 176 : DumpVar(x,D);
48 176 : }
49 :
50 6 : static void DumpNumber(lua_Number x, DumpState* D)
51 : {
52 6 : DumpVar(x,D);
53 6 : }
54 :
55 34 : static void DumpVector(const void* b, int n, size_t size, DumpState* D)
56 : {
57 34 : DumpInt(n,D);
58 34 : DumpMem(b,n,size,D);
59 34 : }
60 :
61 82 : static void DumpString(const TString* s, DumpState* D)
62 : {
63 82 : if (s==NULL || getstr(s)==NULL)
64 10 : {
65 10 : size_t size=0;
66 10 : DumpVar(size,D);
67 : }
68 : else
69 : {
70 72 : size_t size=s->tsv.len+1; /* include trailing '\0' */
71 72 : DumpVar(size,D);
72 72 : DumpBlock(getstr(s),size,D);
73 : }
74 82 : }
75 :
76 : #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
77 :
78 : static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
79 :
80 17 : static void DumpConstants(const Proto* f, DumpState* D)
81 : {
82 17 : int i,n=f->sizek;
83 17 : DumpInt(n,D);
84 68 : for (i=0; i<n; i++)
85 : {
86 51 : const TValue* o=&f->k[i];
87 51 : DumpChar(ttype(o),D);
88 51 : switch (ttype(o))
89 : {
90 0 : case LUA_TNIL:
91 0 : break;
92 3 : case LUA_TBOOLEAN:
93 3 : DumpChar(bvalue(o),D);
94 3 : break;
95 6 : case LUA_TNUMBER:
96 6 : DumpNumber(nvalue(o),D);
97 6 : break;
98 42 : case LUA_TSTRING:
99 42 : DumpString(rawtsvalue(o),D);
100 42 : break;
101 0 : default:
102 : lua_assert(0); /* cannot happen */
103 0 : break;
104 : }
105 : }
106 17 : n=f->sizep;
107 17 : DumpInt(n,D);
108 25 : for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
109 17 : }
110 :
111 17 : static void DumpDebug(const Proto* f, DumpState* D)
112 : {
113 : int i,n;
114 17 : n= (D->strip) ? 0 : f->sizelineinfo;
115 17 : DumpVector(f->lineinfo,n,sizeof(int),D);
116 17 : n= (D->strip) ? 0 : f->sizelocvars;
117 17 : DumpInt(n,D);
118 37 : for (i=0; i<n; i++)
119 : {
120 20 : DumpString(f->locvars[i].varname,D);
121 20 : DumpInt(f->locvars[i].startpc,D);
122 20 : DumpInt(f->locvars[i].endpc,D);
123 : }
124 17 : n= (D->strip) ? 0 : f->sizeupvalues;
125 17 : DumpInt(n,D);
126 20 : for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
127 17 : }
128 :
129 17 : static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
130 : {
131 17 : DumpString((f->source==p || D->strip) ? NULL : f->source,D);
132 17 : DumpInt(f->linedefined,D);
133 17 : DumpInt(f->lastlinedefined,D);
134 17 : DumpChar(f->nups,D);
135 17 : DumpChar(f->numparams,D);
136 17 : DumpChar(f->is_vararg,D);
137 17 : DumpChar(f->maxstacksize,D);
138 17 : DumpCode(f,D);
139 17 : DumpConstants(f,D);
140 17 : DumpDebug(f,D);
141 17 : }
142 :
143 9 : static void DumpHeader(DumpState* D)
144 : {
145 : char h[LUAC_HEADERSIZE];
146 9 : luaU_header(h);
147 9 : DumpBlock(h,LUAC_HEADERSIZE,D);
148 9 : }
149 :
150 : /*
151 : ** dump Lua function as precompiled chunk
152 : */
153 9 : int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
154 : {
155 : DumpState D;
156 9 : D.L=L;
157 9 : D.writer=w;
158 9 : D.data=data;
159 9 : D.strip=strip;
160 9 : D.status=0;
161 9 : DumpHeader(&D);
162 9 : DumpFunction(f,NULL,&D);
163 9 : return D.status;
164 : }
|