The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ldump.cpp
Go to the documentation of this file.
1 /*
2 ** save precompiled Lua chunks
3 ** See Copyright Notice in lua.h
4 */
5 
6 #include <stddef.h>
7 
8 #define ldump_c
9 #define LUA_CORE
10 
11 #include "lua.h"
12 
13 #include "lobject.h"
14 #include "lstate.h"
15 #include "lundump.h"
16 
17 typedef struct {
20  void* data;
21  int strip;
22  int status;
23 } DumpState;
24 
25 #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
26 #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
27 
28 static void DumpBlock(const void* b, size_t size, DumpState* D)
29 {
30  if (D->status==0)
31  {
32  lua_unlock(D->L);
33  D->status=(*D->writer)(D->L,b,size,D->data);
34  lua_lock(D->L);
35  }
36 }
37 
38 static void DumpChar(int y, DumpState* D)
39 {
40  char x=(char)y;
41  DumpVar(x,D);
42 }
43 
44 static void DumpInt(int x, DumpState* D)
45 {
46  DumpVar(x,D);
47 }
48 
49 static void DumpNumber(lua_Number x, DumpState* D)
50 {
51  DumpVar(x,D);
52 }
53 
54 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
55 {
56  DumpInt(n,D);
57  DumpMem(b,n,size,D);
58 }
59 
60 static void DumpString(const TString* s, DumpState* D)
61 {
62  if (s==NULL)
63  {
64  size_t size=0;
65  DumpVar(size,D);
66  }
67  else
68  {
69  size_t size=s->tsv.len+1; /* include trailing '\0' */
70  DumpVar(size,D);
71  DumpBlock(getstr(s),size*sizeof(char),D);
72  }
73 }
74 
75 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
76 
77 static void DumpFunction(const Proto* f, DumpState* D);
78 
79 static void DumpConstants(const Proto* f, DumpState* D)
80 {
81  int i,n=f->sizek;
82  DumpInt(n,D);
83  for (i=0; i<n; i++)
84  {
85  const TValue* o=&f->k[i];
86  DumpChar(ttypenv(o),D);
87  switch (ttypenv(o))
88  {
89  case LUA_TNIL:
90  break;
91  case LUA_TBOOLEAN:
92  DumpChar(bvalue(o),D);
93  break;
94  case LUA_TNUMBER:
95  DumpNumber(nvalue(o),D);
96  break;
97  case LUA_TSTRING:
98  DumpString(rawtsvalue(o),D);
99  break;
100  default: lua_assert(0);
101  }
102  }
103  n=f->sizep;
104  DumpInt(n,D);
105  for (i=0; i<n; i++) DumpFunction(f->p[i],D);
106 }
107 
108 static void DumpUpvalues(const Proto* f, DumpState* D)
109 {
110  int i,n=f->sizeupvalues;
111  DumpInt(n,D);
112  for (i=0; i<n; i++)
113  {
114  DumpChar(f->upvalues[i].instack,D);
115  DumpChar(f->upvalues[i].idx,D);
116  }
117 }
118 
119 static void DumpDebug(const Proto* f, DumpState* D)
120 {
121  int i,n;
122  DumpString((D->strip) ? NULL : f->source,D);
123  n= (D->strip) ? 0 : f->sizelineinfo;
124  DumpVector(f->lineinfo,n,sizeof(int),D);
125  n= (D->strip) ? 0 : f->sizelocvars;
126  DumpInt(n,D);
127  for (i=0; i<n; i++)
128  {
129  DumpString(f->locvars[i].varname,D);
130  DumpInt(f->locvars[i].startpc,D);
131  DumpInt(f->locvars[i].endpc,D);
132  }
133  n= (D->strip) ? 0 : f->sizeupvalues;
134  DumpInt(n,D);
135  for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
136 }
137 
138 static void DumpFunction(const Proto* f, DumpState* D)
139 {
140  DumpInt(f->linedefined,D);
141  DumpInt(f->lastlinedefined,D);
142  DumpChar(f->numparams,D);
143  DumpChar(f->is_vararg,D);
144  DumpChar(f->maxstacksize,D);
145  DumpCode(f,D);
146  DumpConstants(f,D);
147  DumpUpvalues(f,D);
148  DumpDebug(f,D);
149 }
150 
151 static void DumpHeader(DumpState* D)
152 {
154  luaU_header(h);
156 }
157 
158 /*
159 ** dump Lua function as precompiled chunk
160 */
161 int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
162 {
163  DumpState D;
164  D.L=L;
165  D.writer=w;
166  D.data=data;
167  D.strip=strip;
168  D.status=0;
169  DumpHeader(&D);
170  DumpFunction(f,&D);
171  return D.status;
172 }
TString * source
Definition: lobject.h:475
#define nvalue(o)
Definition: lobject.h:152
lua_State * L
Definition: ldump.cpp:18
static void DumpInt(int x, DumpState *D)
Definition: ldump.cpp:44
Definition: lobject.h:466
int luaU_dump(lua_State *L, const Proto *f, lua_Writer w, void *data, int strip)
Definition: ldump.cpp:161
int sizep
Definition: lobject.h:480
#define DumpVar(x, D)
Definition: ldump.cpp:26
#define rawtsvalue(o)
Definition: lobject.h:155
int status
Definition: ldump.cpp:22
#define lua_unlock(L)
Definition: llimits.h:155
GLint GLint GLint GLint GLint GLint y
Definition: glew.h:1220
LocVar * locvars
Definition: lobject.h:472
GLint GLenum GLsizei GLint GLsizei const GLvoid * data
Definition: glew.h:1347
int sizelocvars
Definition: lobject.h:481
Upvaldesc * upvalues
Definition: lobject.h:473
std::string & strip(std::string &str)
Remove whitespace from the front and back of the string 'str'.
GLdouble GLdouble GLdouble b
Definition: glew.h:6966
#define ttypenv(o)
Definition: lobject.h:126
#define lua_lock(L)
Definition: llimits.h:154
#define LUA_TSTRING
Definition: lua.h:81
unsigned char lu_byte
Definition: llimits.h:26
#define getstr(ts)
Definition: lobject.h:421
static void DumpNumber(lua_Number x, DumpState *D)
Definition: ldump.cpp:49
static void DumpDebug(const Proto *f, DumpState *D)
Definition: ldump.cpp:119
#define LUA_TNIL
Definition: lua.h:77
static void DumpConstants(const Proto *f, DumpState *D)
Definition: ldump.cpp:79
GLubyte GLubyte GLubyte GLubyte w
Definition: glew.h:1858
static void DumpChar(int y, DumpState *D)
Definition: ldump.cpp:38
static void DumpVector(const void *b, int n, size_t size, DumpState *D)
Definition: ldump.cpp:54
lua_Writer writer
Definition: ldump.cpp:19
#define LUA_TNUMBER
Definition: lua.h:80
int * lineinfo
Definition: lobject.h:471
int lastlinedefined
Definition: lobject.h:483
TString * varname
Definition: lobject.h:457
struct Proto ** p
Definition: lobject.h:470
lu_byte maxstacksize
Definition: lobject.h:487
int sizelineinfo
Definition: lobject.h:479
#define lua_assert(c)
Definition: llimits.h:65
int(* lua_Writer)(lua_State *L, const void *p, size_t sz, void *ud)
Definition: lua.h:63
lu_byte idx
Definition: lobject.h:448
lu_byte instack
Definition: lobject.h:447
int sizek
Definition: lobject.h:477
GLfloat GLfloat GLfloat GLfloat h
Definition: glew.h:5910
size_t i
Definition: function.cpp:1057
GLint GLint GLint GLint GLint x
Definition: glew.h:1220
lu_byte is_vararg
Definition: lobject.h:486
static void DumpFunction(const Proto *f, DumpState *D)
Definition: ldump.cpp:138
struct TString::@7 tsv
int linedefined
Definition: lobject.h:482
static void DumpString(const TString *s, DumpState *D)
Definition: ldump.cpp:60
int strip
Definition: ldump.cpp:21
#define DumpMem(b, n, size, D)
Definition: ldump.cpp:25
GLsizeiptr size
Definition: glew.h:1649
GLclampd n
Definition: glew.h:5903
#define bvalue(o)
Definition: lobject.h:164
static void DumpHeader(DumpState *D)
Definition: ldump.cpp:151
void luaU_header(lu_byte *h)
Definition: lundump.cpp:243
TString * name
Definition: lobject.h:446
static void DumpUpvalues(const Proto *f, DumpState *D)
Definition: ldump.cpp:108
int startpc
Definition: lobject.h:458
int endpc
Definition: lobject.h:459
int sizeupvalues
Definition: lobject.h:476
#define LUAC_HEADERSIZE
Definition: lundump.h:25
TValue * k
Definition: lobject.h:468
size_t len
Definition: lobject.h:415
GLdouble s
Definition: glew.h:1358
#define DumpCode(f, D)
Definition: ldump.cpp:75
LUA_NUMBER lua_Number
Definition: lua.h:102
#define LUA_TBOOLEAN
Definition: lua.h:78
static void DumpBlock(const void *b, size_t size, DumpState *D)
Definition: ldump.cpp:28
lu_byte numparams
Definition: lobject.h:485
void * data
Definition: ldump.cpp:20
GLclampf f
Definition: glew.h:3024