1 /* $NetBSD: ldump.c,v 1.4 2015/10/08 13:21:00 mbalmer Exp $ */
4 ** Id: ldump.c,v 2.36 2015/03/30 15:43:51 roberto Exp
5 ** save precompiled Lua chunks
6 ** See Copyright Notice in lua.h
36 ** All high-level dumps go through DumpVector; you can change it to
37 ** change the endianness of the result
39 #define DumpVector(v,n,D) DumpBlock(v,(n)*sizeof((v)[0]),D)
41 #define DumpLiteral(s,D) DumpBlock(s, sizeof(s) - sizeof(char), D)
44 static void DumpBlock (const void *b
, size_t size
, DumpState
*D
) {
47 D
->status
= (*D
->writer
)(D
->L
, b
, size
, D
->data
);
53 #define DumpVar(x,D) DumpVector(&x,1,D)
56 static void DumpByte (int y
, DumpState
*D
) {
57 lu_byte x
= (lu_byte
)y
;
62 static void DumpInt (int x
, DumpState
*D
) {
67 static void DumpNumber (lua_Number x
, DumpState
*D
) {
72 static void DumpInteger (lua_Integer x
, DumpState
*D
) {
77 static void DumpString (const TString
*s
, DumpState
*D
) {
81 size_t size
= tsslen(s
) + 1; /* include trailing '\0' */
82 const char *str
= getstr(s
);
84 DumpByte(cast_int(size
), D
);
89 DumpVector(str
, size
- 1, D
); /* no need to save '\0' */
94 static void DumpCode (const Proto
*f
, DumpState
*D
) {
95 DumpInt(f
->sizecode
, D
);
96 DumpVector(f
->code
, f
->sizecode
, D
);
100 static void DumpFunction(const Proto
*f
, TString
*psource
, DumpState
*D
);
102 static void DumpConstants (const Proto
*f
, DumpState
*D
) {
106 for (i
= 0; i
< n
; i
++) {
107 const TValue
*o
= &f
->k
[i
];
108 DumpByte(ttype(o
), D
);
113 DumpByte(bvalue(o
), D
);
117 DumpNumber(fltvalue(o
), D
);
121 DumpInteger(ivalue(o
), D
);
125 DumpString(tsvalue(o
), D
);
134 static void DumpProtos (const Proto
*f
, DumpState
*D
) {
138 for (i
= 0; i
< n
; i
++)
139 DumpFunction(f
->p
[i
], f
->source
, D
);
143 static void DumpUpvalues (const Proto
*f
, DumpState
*D
) {
144 int i
, n
= f
->sizeupvalues
;
146 for (i
= 0; i
< n
; i
++) {
147 DumpByte(f
->upvalues
[i
].instack
, D
);
148 DumpByte(f
->upvalues
[i
].idx
, D
);
153 static void DumpDebug (const Proto
*f
, DumpState
*D
) {
155 n
= (D
->strip
) ? 0 : f
->sizelineinfo
;
157 DumpVector(f
->lineinfo
, n
, D
);
158 n
= (D
->strip
) ? 0 : f
->sizelocvars
;
160 for (i
= 0; i
< n
; i
++) {
161 DumpString(f
->locvars
[i
].varname
, D
);
162 DumpInt(f
->locvars
[i
].startpc
, D
);
163 DumpInt(f
->locvars
[i
].endpc
, D
);
165 n
= (D
->strip
) ? 0 : f
->sizeupvalues
;
167 for (i
= 0; i
< n
; i
++)
168 DumpString(f
->upvalues
[i
].name
, D
);
172 static void DumpFunction (const Proto
*f
, TString
*psource
, DumpState
*D
) {
173 if (D
->strip
|| f
->source
== psource
)
174 DumpString(NULL
, D
); /* no debug info or same source as its parent */
176 DumpString(f
->source
, D
);
177 DumpInt(f
->linedefined
, D
);
178 DumpInt(f
->lastlinedefined
, D
);
179 DumpByte(f
->numparams
, D
);
180 DumpByte(f
->is_vararg
, D
);
181 DumpByte(f
->maxstacksize
, D
);
190 static void DumpHeader (DumpState
*D
) {
191 DumpLiteral(LUA_SIGNATURE
, D
);
192 DumpByte(LUAC_VERSION
, D
);
193 DumpByte(LUAC_FORMAT
, D
);
194 DumpLiteral(LUAC_DATA
, D
);
195 DumpByte(sizeof(int), D
);
196 DumpByte(sizeof(size_t), D
);
197 DumpByte(sizeof(Instruction
), D
);
198 DumpByte(sizeof(lua_Integer
), D
);
199 DumpByte(sizeof(lua_Number
), D
);
200 DumpInteger(LUAC_INT
, D
);
201 DumpNumber(LUAC_NUM
, D
);
206 ** dump Lua function as precompiled chunk
208 int luaU_dump(lua_State
*L
, const Proto
*f
, lua_Writer w
, void *data
,
217 DumpByte(f
->sizeupvalues
, &D
);
218 DumpFunction(f
, NULL
, &D
);