source: trunk/src/lua/ldump.c @ 2542

Last change on this file since 2542 was 2542, checked in by sam, 10 years ago

lua: compilation fixes for non-quite-POSIX platforms.

File size: 3.2 KB
Line 
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*/
6
7#if defined HAVE_CONFIG_H // LOL BEGIN
8#   include "config.h"
9#endif // LOL END
10
11#include <stddef.h>
12
13#define ldump_c
14#define LUA_CORE
15
16#include "lua.h"
17
18#include "lobject.h"
19#include "lstate.h"
20#include "lundump.h"
21
22typedef struct {
23 lua_State* L;
24 lua_Writer writer;
25 void* data;
26 int strip;
27 int status;
28} DumpState;
29
30#define DumpMem(b,n,size,D)     DumpBlock(b,(n)*(size),D)
31#define DumpVar(x,D)            DumpMem(&x,1,sizeof(x),D)
32
33static void DumpBlock(const void* b, size_t size, DumpState* D)
34{
35 if (D->status==0)
36 {
37  lua_unlock(D->L);
38  D->status=(*D->writer)(D->L,b,size,D->data);
39  lua_lock(D->L);
40 }
41}
42
43static void DumpChar(int y, DumpState* D)
44{
45 char x=(char)y;
46 DumpVar(x,D);
47}
48
49static void DumpInt(int x, DumpState* D)
50{
51 DumpVar(x,D);
52}
53
54static void DumpNumber(lua_Number x, DumpState* D)
55{
56 DumpVar(x,D);
57}
58
59static void DumpVector(const void* b, int n, size_t size, DumpState* D)
60{
61 DumpInt(n,D);
62 DumpMem(b,n,size,D);
63}
64
65static void DumpString(const TString* s, DumpState* D)
66{
67 if (s==NULL)
68 {
69  size_t size=0;
70  DumpVar(size,D);
71 }
72 else
73 {
74  size_t size=s->tsv.len+1;             /* include trailing '\0' */
75  DumpVar(size,D);
76  DumpBlock(getstr(s),size*sizeof(char),D);
77 }
78}
79
80#define DumpCode(f,D)    DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
81
82static void DumpFunction(const Proto* f, DumpState* D);
83
84static void DumpConstants(const Proto* f, DumpState* D)
85{
86 int i,n=f->sizek;
87 DumpInt(n,D);
88 for (i=0; i<n; i++)
89 {
90  const TValue* o=&f->k[i];
91  DumpChar(ttypenv(o),D);
92  switch (ttypenv(o))
93  {
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: lua_assert(0);
106  }
107 }
108 n=f->sizep;
109 DumpInt(n,D);
110 for (i=0; i<n; i++) DumpFunction(f->p[i],D);
111}
112
113static void DumpUpvalues(const Proto* f, DumpState* D)
114{
115 int i,n=f->sizeupvalues;
116 DumpInt(n,D);
117 for (i=0; i<n; i++)
118 {
119  DumpChar(f->upvalues[i].instack,D);
120  DumpChar(f->upvalues[i].idx,D);
121 }
122}
123
124static void DumpDebug(const Proto* f, DumpState* D)
125{
126 int i,n;
127 DumpString((D->strip) ? NULL : f->source,D);
128 n= (D->strip) ? 0 : f->sizelineinfo;
129 DumpVector(f->lineinfo,n,sizeof(int),D);
130 n= (D->strip) ? 0 : f->sizelocvars;
131 DumpInt(n,D);
132 for (i=0; i<n; i++)
133 {
134  DumpString(f->locvars[i].varname,D);
135  DumpInt(f->locvars[i].startpc,D);
136  DumpInt(f->locvars[i].endpc,D);
137 }
138 n= (D->strip) ? 0 : f->sizeupvalues;
139 DumpInt(n,D);
140 for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
141}
142
143static void DumpFunction(const Proto* f, DumpState* D)
144{
145 DumpInt(f->linedefined,D);
146 DumpInt(f->lastlinedefined,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 DumpUpvalues(f,D);
153 DumpDebug(f,D);
154}
155
156static void DumpHeader(DumpState* D)
157{
158 lu_byte h[LUAC_HEADERSIZE];
159 luaU_header(h);
160 DumpBlock(h,LUAC_HEADERSIZE,D);
161}
162
163/*
164** dump Lua function as precompiled chunk
165*/
166int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
167{
168 DumpState D;
169 D.L=L;
170 D.writer=w;
171 D.data=data;
172 D.strip=strip;
173 D.status=0;
174 DumpHeader(&D);
175 DumpFunction(f,&D);
176 return D.status;
177}
Note: See TracBrowser for help on using the repository browser.