initial endianness and bit size support
[ekto.git] / src / lib / ldump.c
blob136207cd510e29e1177ad8a2bddf5e40687191d8
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 */
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, uint32_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 int32_t *y = &x;
48 int32_t z = htons (*y);
49 DumpVar(z,D);
52 static void DumpNumber(lua_Number x, DumpState* D)
54 int64_t *y = &x;
55 int64_t z = htonl (*y);
56 DumpVar(z,D);
59 static void DumpVector(const void* b, int n, uint32_t size, DumpState* D)
61 DumpInt(n,D);
62 DumpMem(b,n,size,D);
65 static void DumpString(const TString* s, DumpState* D)
67 if (s==NULL || getstr(s)==NULL)
69 uint32_t size=0;
70 DumpVar(size,D);
72 else
74 uint32_t size=s->tsv.len+1; /* include trailing '\0' */
75 DumpVar(size,D);
76 DumpBlock(getstr(s),size,D);
80 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
82 static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
84 static void DumpConstants(const Proto* f, DumpState* D)
86 int i,n=f->sizek;
87 DumpInt(n,D);
88 for (i=0; i<n; i++)
90 const TValue* o=&f->k[i];
91 DumpChar(ttype(o),D);
92 switch (ttype(o))
94 case LUA_TNIL:
95 break;
96 case LUA_TBOOLEAN:
97 DumpChar(bvalue(o),D);
98 break;
99 case LUA_TNUMBER:
100 DumpNumber(nvalue(o),D);
101 break;
102 case LUA_TSTRING:
103 DumpString(rawtsvalue(o),D);
104 break;
105 default:
106 lua_assert(0); /* cannot happen */
107 break;
110 n=f->sizep;
111 DumpInt(n,D);
112 for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
115 static void DumpDebug(const Proto* f, DumpState* D)
117 int i,n;
118 n= (D->strip) ? 0 : f->sizelineinfo;
119 DumpVector(f->lineinfo,n,sizeof(int),D);
120 n= (D->strip) ? 0 : f->sizelocvars;
121 DumpInt(n,D);
122 for (i=0; i<n; i++)
124 DumpString(f->locvars[i].varname,D);
125 DumpInt(f->locvars[i].startpc,D);
126 DumpInt(f->locvars[i].endpc,D);
128 n= (D->strip) ? 0 : f->sizeupvalues;
129 DumpInt(n,D);
130 for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
133 static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
135 DumpString((f->source==p || D->strip) ? NULL : f->source,D);
136 DumpInt(f->linedefined,D);
137 DumpInt(f->lastlinedefined,D);
138 DumpChar(f->nups,D);
139 DumpChar(f->numparams,D);
140 DumpChar(f->is_vararg,D);
141 DumpChar(f->maxstacksize,D);
142 DumpCode(f,D);
143 DumpConstants(f,D);
144 DumpDebug(f,D);
147 static void DumpHeader(DumpState* D)
149 char h[LUAC_HEADERSIZE];
150 luaU_header(h);
151 DumpBlock(h,LUAC_HEADERSIZE,D);
155 ** dump Lua function as precompiled chunk
157 int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
159 DumpState D;
160 D.L=L;
161 D.writer=w;
162 D.data=data;
163 D.strip=strip;
164 D.status=0;
165 DumpHeader(&D);
166 DumpFunction(f,NULL,&D);
167 return D.status;