2 ** $Id: ldump.c,v 2.37.1.1 2017/04/19 17:20:42 roberto Exp $
3 ** save precompiled Lua chunks
4 ** See Copyright Notice in lua.h
32 ** All high-level dumps go through DumpVector; you can change it to
33 ** change the endianness of the result
35 #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
37 #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
40 static void DumpBlock (const void *b, size_t size, DumpState *D) {
41 if (D->status == 0 && size > 0) {
43 D->status = (*D->writer)(D->L, b, size, D->data);
49 #define DumpVar(x,D) DumpVector(&x,1,D)
52 static void DumpByte (int y, DumpState *D) {
53 lu_byte x = (lu_byte)y;
58 static void DumpInt (int x, DumpState *D) {
63 static void DumpNumber (lua_Number x, DumpState *D) {
68 static void DumpInteger (lua_Integer x, DumpState *D) {
73 static void DumpString (const TString *s, DumpState *D) {
77 size_t size = tsslen(s) + 1; /* include trailing '\0' */
78 const char *str = getstr(s);
80 DumpByte(cast_int(size), D);
85 DumpVector(str, size - 1, D); /* no need to save '\0' */
90 static void DumpCode (const Proto *f, DumpState *D) {
91 DumpInt(f->sizecode, D);
92 DumpVector(f->code, f->sizecode, D);
96 static void DumpFunction(const Proto *f, TString *psource, DumpState *D);
98 static void DumpConstants (const Proto *f, DumpState *D) {
102 for (i = 0; i < n; i++) {
103 const TValue *o = &f->k[i];
104 DumpByte(ttype(o), D);
109 DumpByte(bvalue(o), D);
112 DumpNumber(fltvalue(o), D);
115 DumpInteger(ivalue(o), D);
119 DumpString(tsvalue(o), D);
128 static void DumpProtos (const Proto *f, DumpState *D) {
132 for (i = 0; i < n; i++)
133 DumpFunction(f->p[i], f->source, D);
137 static void DumpUpvalues (const Proto *f, DumpState *D) {
138 int i, n = f->sizeupvalues;
140 for (i = 0; i < n; i++) {
141 DumpByte(f->upvalues[i].instack, D);
142 DumpByte(f->upvalues[i].idx, D);
147 static void DumpDebug (const Proto *f, DumpState *D) {
149 n = (D->strip) ? 0 : f->sizelineinfo;
151 DumpVector(f->lineinfo, n, D);
152 n = (D->strip) ? 0 : f->sizelocvars;
154 for (i = 0; i < n; i++) {
155 DumpString(f->locvars[i].varname, D);
156 DumpInt(f->locvars[i].startpc, D);
157 DumpInt(f->locvars[i].endpc, D);
159 n = (D->strip) ? 0 : f->sizeupvalues;
161 for (i = 0; i < n; i++)
162 DumpString(f->upvalues[i].name, D);
166 static void DumpFunction (const Proto *f, TString *psource, DumpState *D) {
167 if (D->strip || f->source == psource)
168 DumpString(NULL, D); /* no debug info or same source as its parent */
170 DumpString(f->source, D);
171 DumpInt(f->linedefined, D);
172 DumpInt(f->lastlinedefined, D);
173 DumpByte(f->numparams, D);
174 DumpByte(f->is_vararg, D);
175 DumpByte(f->maxstacksize, D);
184 static void DumpHeader (DumpState *D) {
185 DumpLiteral(LUA_SIGNATURE, D);
186 DumpByte(LUAC_VERSION, D);
187 DumpByte(LUAC_FORMAT, D);
188 DumpLiteral(LUAC_DATA, D);
189 DumpByte(sizeof(int), D);
190 DumpByte(sizeof(size_t), D);
191 DumpByte(sizeof(Instruction), D);
192 DumpByte(sizeof(lua_Integer), D);
193 DumpByte(sizeof(lua_Number), D);
194 DumpInteger(LUAC_INT, D);
195 DumpNumber(LUAC_NUM, D);
200 ** dump Lua function as precompiled chunk
202 int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data,
211 DumpByte(f->sizeupvalues, &D);
212 DumpFunction(f, NULL, &D);