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
26 #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
27 #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
29 static void DumpBlock(const void* b
, uint32_t size
, DumpState
* D
)
34 D
->status
=(*D
->writer
)(D
->L
,b
,size
,D
->data
);
39 static void DumpChar(int y
, DumpState
* D
)
45 static void DumpInt(int x
, DumpState
* D
)
48 int32_t z
= htons (*y
);
52 static void DumpNumber(lua_Number x
, DumpState
* D
)
55 int64_t z
= htonl (*y
);
59 static void DumpVector(const void* b
, int n
, uint32_t size
, DumpState
* D
)
65 static void DumpString(const TString
* s
, DumpState
* D
)
67 if (s
==NULL
|| getstr(s
)==NULL
)
74 uint32_t size
=s
->tsv
.len
+1; /* include trailing '\0' */
76 DumpBlock(getstr(s
),size
,D
);
80 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
82 static void DumpFunction(const Proto
* f
, const TString
* p
, DumpState
* D
);
84 static void DumpConstants(const Proto
* f
, DumpState
* D
)
90 const TValue
* o
=&f
->k
[i
];
97 DumpChar(bvalue(o
),D
);
100 DumpNumber(nvalue(o
),D
);
103 DumpString(rawtsvalue(o
),D
);
106 lua_assert(0); /* cannot happen */
112 for (i
=0; i
<n
; i
++) DumpFunction(f
->p
[i
],f
->source
,D
);
115 static void DumpDebug(const Proto
* f
, DumpState
* D
)
118 n
= (D
->strip
) ? 0 : f
->sizelineinfo
;
119 DumpVector(f
->lineinfo
,n
,sizeof(int),D
);
120 n
= (D
->strip
) ? 0 : f
->sizelocvars
;
124 DumpString(f
->locvars
[i
].varname
,D
);
125 DumpInt(f
->locvars
[i
].startpc
,D
);
126 DumpInt(f
->locvars
[i
].endpc
,D
);
128 n
= (D
->strip
) ? 0 : f
->sizeupvalues
;
130 for (i
=0; i
<n
; i
++) DumpString(f
->upvalues
[i
],D
);
133 static void DumpFunction(const Proto
* f
, const TString
* p
, DumpState
* D
)
135 DumpString((f
->source
==p
|| D
->strip
) ? NULL
: f
->source
,D
);
136 DumpInt(f
->linedefined
,D
);
137 DumpInt(f
->lastlinedefined
,D
);
139 DumpChar(f
->numparams
,D
);
140 DumpChar(f
->is_vararg
,D
);
141 DumpChar(f
->maxstacksize
,D
);
147 static void DumpHeader(DumpState
* D
)
149 char h
[LUAC_HEADERSIZE
];
151 DumpBlock(h
,LUAC_HEADERSIZE
,D
);
155 ** dump Lua function as precompiled chunk
157 int luaU_dump (lua_State
* L
, const Proto
* f
, lua_Writer w
, void* data
, int strip
)
166 DumpFunction(f
,NULL
,&D
);