lua/ldump.c

180 lines
3.5 KiB
C
Raw Normal View History

2002-10-26 01:31:28 +04:00
/*
2005-05-17 23:49:15 +04:00
** $Id: ldump.c,v 2.5 2005/05/05 20:47:02 roberto Exp roberto $
** save pre-compiled Lua chunks
2002-10-26 01:31:28 +04:00
** See Copyright Notice in lua.h
*/
#include <stddef.h>
2002-12-04 20:38:31 +03:00
#define ldump_c
#define LUA_CORE
2002-12-04 20:38:31 +03:00
2002-10-26 01:31:28 +04:00
#include "lua.h"
#include "lobject.h"
#include "lopcodes.h"
#include "lstate.h"
#include "lundump.h"
#define DumpVector(b,n,size,D) DumpBlock(b,(n)*(size),D)
#define DumpLiteral(s,D) DumpBlock("" s,(sizeof(s))-1,D)
typedef struct {
2003-01-27 18:52:57 +03:00
lua_State* L;
2005-05-17 23:49:15 +04:00
lua_Writer writer;
2003-01-27 18:52:57 +03:00
void* data;
int strip;
int status;
2002-10-26 01:31:28 +04:00
} DumpState;
2003-01-27 18:52:57 +03:00
static void DumpBlock(const void* b, size_t size, DumpState* D)
2002-10-26 01:31:28 +04:00
{
if (D->status==0)
{
lua_unlock(D->L);
D->status=(*D->writer)(D->L,b,size,D->data);
lua_lock(D->L);
}
2002-10-26 01:31:28 +04:00
}
static void DumpByte(int y, DumpState* D)
{
char x=(char)y;
DumpBlock(&x,sizeof(x),D);
}
static void DumpInt(int x, DumpState* D)
{
DumpBlock(&x,sizeof(x),D);
}
static void DumpSize(size_t x, DumpState* D)
{
DumpBlock(&x,sizeof(x),D);
}
static void DumpNumber(lua_Number x, DumpState* D)
{
DumpBlock(&x,sizeof(x),D);
}
static void DumpString(const TString* s, DumpState* D)
2002-10-26 01:31:28 +04:00
{
if (s==NULL || getstr(s)==NULL)
DumpSize(0,D);
else
{
size_t size=s->tsv.len+1; /* include trailing '\0' */
DumpSize(size,D);
DumpBlock(getstr(s),size,D);
}
}
static void DumpCode(const Proto* f, DumpState* D)
{
DumpInt(f->sizecode,D);
DumpVector(f->code,f->sizecode,sizeof(*f->code),D);
}
static void DumpLocals(const Proto* f, DumpState* D)
{
int i,n=f->sizelocvars;
DumpInt(n,D);
for (i=0; i<n; i++)
{
DumpString(f->locvars[i].varname,D);
DumpInt(f->locvars[i].startpc,D);
DumpInt(f->locvars[i].endpc,D);
}
}
static void DumpLines(const Proto* f, DumpState* D)
{
DumpInt(f->sizelineinfo,D);
DumpVector(f->lineinfo,f->sizelineinfo,sizeof(*f->lineinfo),D);
}
2003-01-27 18:52:57 +03:00
static void DumpUpvalues(const Proto* f, DumpState* D)
{
2003-04-10 21:39:41 +04:00
int i,n=f->sizeupvalues;
DumpInt(n,D);
for (i=0; i<n; i++) DumpString(f->upvalues[i],D);
2003-01-27 18:52:57 +03:00
}
2002-10-26 01:31:28 +04:00
static void DumpFunction(const Proto* f, const TString* p, DumpState* D);
static void DumpConstants(const Proto* f, DumpState* D)
{
int i,n;
DumpInt(n=f->sizek,D);
for (i=0; i<n; i++)
{
const TValue* o=&f->k[i];
2002-10-26 01:31:28 +04:00
DumpByte(ttype(o),D);
switch (ttype(o))
{
case LUA_TNUMBER:
DumpNumber(nvalue(o),D);
break;
case LUA_TSTRING:
DumpString(rawtsvalue(o),D);
2002-10-26 01:31:28 +04:00
break;
case LUA_TNIL:
break;
case LUA_TBOOLEAN:
DumpByte(bvalue(o),D);
break;
2002-10-26 01:31:28 +04:00
default:
lua_assert(0); /* cannot happen */
break;
}
}
DumpInt(n=f->sizep,D);
for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
}
static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
{
DumpString((f->source==p) ? NULL : f->source,D);
DumpInt(f->linedefined,D);
DumpInt(f->lastlinedefined,D);
DumpByte(f->nups,D);
2002-10-26 01:31:28 +04:00
DumpByte(f->numparams,D);
DumpByte(f->is_vararg,D);
DumpByte(f->maxstacksize,D);
if (D->strip) DumpInt(0,D); else DumpLines(f,D);
if (D->strip) DumpInt(0,D); else DumpLocals(f,D);
if (D->strip) DumpInt(0,D); else DumpUpvalues(f,D);
2002-10-26 01:31:28 +04:00
DumpConstants(f,D);
DumpCode(f,D);
}
static void DumpHeader(DumpState* D)
{
DumpLiteral(LUA_SIGNATURE,D);
DumpByte(VERSION,D);
DumpByte(luaU_endianness(),D);
DumpByte(sizeof(int),D);
DumpByte(sizeof(size_t),D);
DumpByte(sizeof(Instruction),D);
DumpByte(sizeof(lua_Number),D);
DumpNumber(TEST_NUMBER,D);
}
2003-01-27 18:52:57 +03:00
/*
** dump Lua function as precompiled chunk
2003-01-27 18:52:57 +03:00
*/
2005-05-17 23:49:15 +04:00
int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data,
int strip)
2002-10-26 01:31:28 +04:00
{
DumpState D;
D.L=L;
D.writer=w;
2002-10-26 01:31:28 +04:00
D.data=data;
D.strip=strip;
D.status=0;
2002-10-26 01:31:28 +04:00
DumpHeader(&D);
DumpFunction(f,NULL,&D);
return D.status;
2002-10-26 01:31:28 +04:00
}