]> rtime.felk.cvut.cz Git - l4.git/blob - l4/pkg/lua/lib/contrib/src/ldump.c
update
[l4.git] / l4 / pkg / lua / lib / contrib / src / ldump.c
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 static void DumpBlock(const void* b, size_t size, DumpState* D)
30 {
31  if (D->status==0)
32  {
33   lua_unlock(D->L);
34   D->status=(*D->writer)(D->L,b,size,D->data);
35   lua_lock(D->L);
36  }
37 }
38
39 static void DumpChar(int y, DumpState* D)
40 {
41  char x=(char)y;
42  DumpVar(x,D);
43 }
44
45 static void DumpInt(int x, DumpState* D)
46 {
47  DumpVar(x,D);
48 }
49
50 static void DumpNumber(lua_Number x, DumpState* D)
51 {
52  DumpVar(x,D);
53 }
54
55 #ifdef LUA_TINT
56 static void DumpInteger(lua_Integer x, DumpState* D)
57 {
58  DumpVar(x,D);
59 }
60 #endif
61
62 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
63 {
64  DumpInt(n,D);
65  DumpMem(b,n,size,D);
66 }
67
68 static void DumpString(const TString* s, DumpState* D)
69 {
70  if (s==NULL || getstr(s)==NULL)
71  {
72   size_t size=0;
73   DumpVar(size,D);
74  }
75  else
76  {
77   size_t size=s->tsv.len+1;             /* include trailing '\0' */
78   DumpVar(size,D);
79   DumpBlock(getstr(s),size,D);
80  }
81 }
82
83 #define DumpCode(f,D)    DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
84
85 static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
86
87 static void DumpConstants(const Proto* f, DumpState* D)
88 {
89  int i,n=f->sizek;
90  DumpInt(n,D);
91  for (i=0; i<n; i++)
92  {
93   const TValue* o=&f->k[i];
94   DumpChar(ttype(o),D);
95   switch (ttype(o))
96   {
97    case LUA_TNIL:
98         break;
99    case LUA_TBOOLEAN:
100         DumpChar(bvalue(o),D);
101         break;
102 #ifdef LUA_TINT
103    case LUA_TINT:
104         DumpInteger(ivalue(o),D);
105     break;
106 #endif
107    case LUA_TNUMBER:
108         DumpNumber(nvalue_fast(o),D);
109         break;
110    case LUA_TSTRING:
111         DumpString(rawtsvalue(o),D);
112         break;
113    default:
114         lua_assert(0);                  /* cannot happen */
115         break;
116   }
117  }
118  n=f->sizep;
119  DumpInt(n,D);
120  for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
121 }
122
123 static void DumpDebug(const Proto* f, DumpState* D)
124 {
125  int i,n;
126  n= (D->strip) ? 0 : f->sizelineinfo;
127  DumpVector(f->lineinfo,n,sizeof(int),D);
128  n= (D->strip) ? 0 : f->sizelocvars;
129  DumpInt(n,D);
130  for (i=0; i<n; i++)
131  {
132   DumpString(f->locvars[i].varname,D);
133   DumpInt(f->locvars[i].startpc,D);
134   DumpInt(f->locvars[i].endpc,D);
135  }
136  n= (D->strip) ? 0 : f->sizeupvalues;
137  DumpInt(n,D);
138  for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
139 }
140
141 static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
142 {
143  DumpString((f->source==p || D->strip) ? NULL : f->source,D);
144  DumpInt(f->linedefined,D);
145  DumpInt(f->lastlinedefined,D);
146  DumpChar(f->nups,D);
147  DumpChar(f->numparams,D);
148  DumpChar(f->is_vararg,D);
149  DumpChar(f->maxstacksize,D);
150  DumpCode(f,D);
151  DumpConstants(f,D);
152  DumpDebug(f,D);
153 }
154
155 static void DumpHeader(DumpState* D)
156 {
157  char h[LUAC_HEADERSIZE];
158  luaU_header(h);
159  DumpBlock(h,LUAC_HEADERSIZE,D);
160 }
161
162 /*
163 ** dump Lua function as precompiled chunk
164 */
165 int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
166 {
167  DumpState D;
168  D.L=L;
169  D.writer=w;
170  D.data=data;
171  D.strip=strip;
172  D.status=0;
173  DumpHeader(&D);
174  DumpFunction(f,NULL,&D);
175  return D.status;
176 }