annotate lib/lua/lua-5.2.2/ldump.c @ 1996:f7e8c4d4a01b

sound id fix
author zipi
date Sun, 03 Nov 2013 14:29:45 +0000
parents 41cc4dd3c122
children
rev   line source
1866
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
1 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
2 ** $Id: ldump.c,v 2.17 2012/01/23 23:02:10 roberto Exp $
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
3 ** save precompiled Lua chunks
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
4 ** See Copyright Notice in lua.h
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
5 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
6
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
7 #include <stddef.h>
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
8
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
9 #define ldump_c
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
10 #define LUA_CORE
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
11
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
12 #include "lua.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
13
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
14 #include "lobject.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
15 #include "lstate.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
16 #include "lundump.h"
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
17
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
18 typedef struct {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
19 lua_State* L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
20 lua_Writer writer;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
21 void* data;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
22 int strip;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
23 int status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
24 } DumpState;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
25
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
26 #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
27 #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
28
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
29 static void DumpBlock(const void* b, size_t size, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
30 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
31 if (D->status==0)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
32 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
33 lua_unlock(D->L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
34 D->status=(*D->writer)(D->L,b,size,D->data);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
35 lua_lock(D->L);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
36 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
37 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
38
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
39 static void DumpChar(int y, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
40 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
41 char x=(char)y;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
42 DumpVar(x,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
43 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
44
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
45 static void DumpInt(int x, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
46 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
47 DumpVar(x,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
48 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
49
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
50 static void DumpNumber(lua_Number x, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
51 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
52 DumpVar(x,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
53 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
54
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
55 static void DumpVector(const void* b, int n, size_t size, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
56 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
57 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
58 DumpMem(b,n,size,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
59 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
60
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
61 static void DumpString(const TString* s, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
62 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
63 if (s==NULL)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
64 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
65 size_t size=0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
66 DumpVar(size,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
67 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
68 else
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
69 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
70 size_t size=s->tsv.len+1; /* include trailing '\0' */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
71 DumpVar(size,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
72 DumpBlock(getstr(s),size*sizeof(char),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
73 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
74 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
75
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
76 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
77
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
78 static void DumpFunction(const Proto* f, DumpState* D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
79
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
80 static void DumpConstants(const Proto* f, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
81 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
82 int i,n=f->sizek;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
83 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
84 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
85 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
86 const TValue* o=&f->k[i];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
87 DumpChar(ttypenv(o),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
88 switch (ttypenv(o))
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
89 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
90 case LUA_TNIL:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
91 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
92 case LUA_TBOOLEAN:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
93 DumpChar(bvalue(o),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
94 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
95 case LUA_TNUMBER:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
96 DumpNumber(nvalue(o),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
97 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
98 case LUA_TSTRING:
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
99 DumpString(rawtsvalue(o),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
100 break;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
101 default: lua_assert(0);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
102 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
103 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
104 n=f->sizep;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
105 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
106 for (i=0; i<n; i++) DumpFunction(f->p[i],D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
107 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
108
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
109 static void DumpUpvalues(const Proto* f, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
110 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
111 int i,n=f->sizeupvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
112 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
113 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
114 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
115 DumpChar(f->upvalues[i].instack,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
116 DumpChar(f->upvalues[i].idx,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
117 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
118 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
119
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
120 static void DumpDebug(const Proto* f, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
121 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
122 int i,n;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
123 DumpString((D->strip) ? NULL : f->source,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
124 n= (D->strip) ? 0 : f->sizelineinfo;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
125 DumpVector(f->lineinfo,n,sizeof(int),D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
126 n= (D->strip) ? 0 : f->sizelocvars;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
127 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
128 for (i=0; i<n; i++)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
129 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
130 DumpString(f->locvars[i].varname,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
131 DumpInt(f->locvars[i].startpc,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
132 DumpInt(f->locvars[i].endpc,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
133 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
134 n= (D->strip) ? 0 : f->sizeupvalues;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
135 DumpInt(n,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
136 for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
137 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
138
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
139 static void DumpFunction(const Proto* f, DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
140 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
141 DumpInt(f->linedefined,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
142 DumpInt(f->lastlinedefined,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
143 DumpChar(f->numparams,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
144 DumpChar(f->is_vararg,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
145 DumpChar(f->maxstacksize,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
146 DumpCode(f,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
147 DumpConstants(f,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
148 DumpUpvalues(f,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
149 DumpDebug(f,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
150 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
151
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
152 static void DumpHeader(DumpState* D)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
153 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
154 lu_byte h[LUAC_HEADERSIZE];
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
155 luaU_header(h);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
156 DumpBlock(h,LUAC_HEADERSIZE,D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
157 }
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
158
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
159 /*
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
160 ** dump Lua function as precompiled chunk
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
161 */
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
162 int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip)
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
163 {
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
164 DumpState D;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
165 D.L=L;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
166 D.writer=w;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
167 D.data=data;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
168 D.strip=strip;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
169 D.status=0;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
170 DumpHeader(&D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
171 DumpFunction(f,&D);
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
172 return D.status;
41cc4dd3c122 Lua 5.2.2 added.
Nomad
parents:
diff changeset
173 }