1 /* $NetBSD: ldump.c,v 1.1.1.2 2012/03/15 00:08:12 alnsn Exp $ */
4 ** $Id: ldump.c,v 1.1.1.2 2012/03/15 00:08:12 alnsn Exp $
5 ** save precompiled Lua chunks
6 ** See Copyright Notice in lua.h
28 #define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
29 #define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
31 static void DumpBlock(const void* b
, size_t size
, DumpState
* D
)
36 D
->status
=(*D
->writer
)(D
->L
,b
,size
,D
->data
);
41 static void DumpChar(int y
, DumpState
* D
)
47 static void DumpInt(int x
, DumpState
* D
)
52 static void DumpNumber(lua_Number x
, DumpState
* D
)
57 static void DumpVector(const void* b
, int n
, size_t size
, DumpState
* D
)
63 static void DumpString(const TString
* s
, DumpState
* D
)
65 if (s
==NULL
|| getstr(s
)==NULL
)
72 size_t size
=s
->tsv
.len
+1; /* include trailing '\0' */
74 DumpBlock(getstr(s
),size
,D
);
78 #define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),D)
80 static void DumpFunction(const Proto
* f
, const TString
* p
, DumpState
* D
);
82 static void DumpConstants(const Proto
* f
, DumpState
* D
)
88 const TValue
* o
=&f
->k
[i
];
95 DumpChar(bvalue(o
),D
);
98 DumpNumber(nvalue(o
),D
);
101 DumpString(rawtsvalue(o
),D
);
104 lua_assert(0); /* cannot happen */
110 for (i
=0; i
<n
; i
++) DumpFunction(f
->p
[i
],f
->source
,D
);
113 static void DumpDebug(const Proto
* f
, DumpState
* D
)
116 n
= (D
->strip
) ? 0 : f
->sizelineinfo
;
117 DumpVector(f
->lineinfo
,n
,sizeof(int),D
);
118 n
= (D
->strip
) ? 0 : f
->sizelocvars
;
122 DumpString(f
->locvars
[i
].varname
,D
);
123 DumpInt(f
->locvars
[i
].startpc
,D
);
124 DumpInt(f
->locvars
[i
].endpc
,D
);
126 n
= (D
->strip
) ? 0 : f
->sizeupvalues
;
128 for (i
=0; i
<n
; i
++) DumpString(f
->upvalues
[i
],D
);
131 static void DumpFunction(const Proto
* f
, const TString
* p
, DumpState
* D
)
133 DumpString((f
->source
==p
|| D
->strip
) ? NULL
: f
->source
,D
);
134 DumpInt(f
->linedefined
,D
);
135 DumpInt(f
->lastlinedefined
,D
);
137 DumpChar(f
->numparams
,D
);
138 DumpChar(f
->is_vararg
,D
);
139 DumpChar(f
->maxstacksize
,D
);
145 static void DumpHeader(DumpState
* D
)
147 char h
[LUAC_HEADERSIZE
];
149 DumpBlock(h
,LUAC_HEADERSIZE
,D
);
153 ** dump Lua function as precompiled chunk
155 int luaU_dump (lua_State
* L
, const Proto
* f
, lua_Writer w
, void* data
, int strip
)
164 DumpFunction(f
,NULL
,&D
);