Merge pull request #117 from marshmellow42/master
[legacy-proxmark3.git] / liblua / ldump.c
blobd5e6a47cb38c0d07b54b41474099bc553230f3f7
1 /*
2 ** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $
3 ** save precompiled Lua chunks
4 ** See Copyright Notice in lua.h
5 */
7 #include <stddef.h>
9 #define ldump_c
10 #define LUA_CORE
12 #include "lua.h"
14 #include "lobject.h"
15 #include "lstate.h"
16 #include "lundump.h"
18 typedef struct {
19 lua_State* L;
20 lua_Writer writer;
21 void* data;
22 int strip;
23 int status;
24 } DumpState;
26 #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
27 #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
29 static void DumpBlock(const void* b, size_t size, DumpState* D)
31 if (D->status==0)
33 lua_unlock(D->L);
34 D->status=(*D->writer)(D->L,b,size,D->data);
35 lua_lock(D->L);
39 static void DumpChar(int y, DumpState* D)
41 char x=(char)y;
42 DumpVar(x,D);
45 static void DumpInt(int x, DumpState* D)
47 DumpVar(x,D);
50 static void DumpNumber(lua_Number x, DumpState* D)
52 DumpVar(x,D);
55 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
57 DumpInt(n,D);
58 DumpMem(b,n,size,D);
61 static void DumpString(const TString* s, DumpState* D)
63 if (s==NULL)
65 size_t size=0;
66 DumpVar(size,D);
68 else
70 size_t size=s->tsv.len+1; /* include trailing '\0' */
71 DumpVar(size,D);
72 DumpBlock(getstr(s),size*sizeof(char),D);
76 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
78 static void DumpFunction(const Proto* f, DumpState* D);
80 static void DumpConstants(const Proto* f, DumpState* D)
82 int i,n=f->sizek;
83 DumpInt(n,D);
84 for (i=0; i<n; i++)
86 const TValue* o=&f->k[i];
87 DumpChar(ttypenv(o),D);
88 switch (ttypenv(o))
90 case LUA_TNIL:
91 break;
92 case LUA_TBOOLEAN:
93 DumpChar(bvalue(o),D);
94 break;
95 case LUA_TNUMBER:
96 DumpNumber(nvalue(o),D);
97 break;
98 case LUA_TSTRING:
99 DumpString(rawtsvalue(o),D);
100 break;
101 default: lua_assert(0);
104 n=f->sizep;
105 DumpInt(n,D);
106 for (i=0; i<n; i++) DumpFunction(f->p[i],D);
109 static void DumpUpvalues(const Proto* f, DumpState* D)
111 int i,n=f->sizeupvalues;
112 DumpInt(n,D);
113 for (i=0; i<n; i++)
115 DumpChar(f->upvalues[i].instack,D);
116 DumpChar(f->upvalues[i].idx,D);
120 static void DumpDebug(const Proto* f, DumpState* D)
122 int i,n;
123 DumpString((D->strip) ? NULL : f->source,D);
124 n= (D->strip) ? 0 : f->sizelineinfo;
125 DumpVector(f->lineinfo,n,sizeof(int),D);
126 n= (D->strip) ? 0 : f->sizelocvars;
127 DumpInt(n,D);
128 for (i=0; i<n; i++)
130 DumpString(f->locvars[i].varname,D);
131 DumpInt(f->locvars[i].startpc,D);
132 DumpInt(f->locvars[i].endpc,D);
134 n= (D->strip) ? 0 : f->sizeupvalues;
135 DumpInt(n,D);
136 for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
139 static void DumpFunction(const Proto* f, DumpState* D)
141 DumpInt(f->linedefined,D);
142 DumpInt(f->lastlinedefined,D);
143 DumpChar(f->numparams,D);
144 DumpChar(f->is_vararg,D);
145 DumpChar(f->maxstacksize,D);
146 DumpCode(f,D);
147 DumpConstants(f,D);
148 DumpUpvalues(f,D);
149 DumpDebug(f,D);
152 static void DumpHeader(DumpState* D)
154 lu_byte h[LUAC_HEADERSIZE];
155 luaU_header(h);
156 DumpBlock(h,LUAC_HEADERSIZE,D);
160 ** dump Lua function as precompiled chunk
162 int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
164 DumpState D;
165 D.L=L;
166 D.writer=w;
167 D.data=data;
168 D.strip=strip;
169 D.status=0;
170 DumpHeader(&D);
171 DumpFunction(f,&D);
172 return D.status;