2002-10-26 01:31:28 +04:00
|
|
|
/*
|
2009-09-30 19:38:37 +04:00
|
|
|
** $Id: ldump.c,v 2.11 2009/09/28 16:32:50 roberto Exp roberto $
|
2005-11-16 14:55:07 +03:00
|
|
|
** save precompiled 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
|
2004-05-01 00:13:38 +04:00
|
|
|
#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 "lstate.h"
|
|
|
|
#include "lundump.h"
|
|
|
|
|
|
|
|
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;
|
2003-08-15 17:48:53 +04:00
|
|
|
int strip;
|
2004-07-09 22:24:41 +04:00
|
|
|
int status;
|
2002-10-26 01:31:28 +04:00
|
|
|
} DumpState;
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
#define DumpMem(b,n,size,D) DumpBlock(b,(n)*(size),D)
|
2006-09-11 18:07:24 +04:00
|
|
|
#define DumpVar(x,D) DumpMem(&x,1,sizeof(x),D)
|
2005-11-16 14:55:07 +03:00
|
|
|
|
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
|
|
|
{
|
2004-07-09 22:24:41 +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
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void DumpChar(int y, DumpState* D)
|
2002-10-26 01:31:28 +04:00
|
|
|
{
|
|
|
|
char x=(char)y;
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpVar(x,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpInt(int x, DumpState* D)
|
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpVar(x,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void DumpNumber(lua_Number x, DumpState* D)
|
2002-10-26 01:31:28 +04:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpVar(x,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void DumpVector(const void* b, int n, size_t size, DumpState* D)
|
2002-10-26 01:31:28 +04:00
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpInt(n,D);
|
|
|
|
DumpMem(b,n,size,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2004-07-09 22:24:41 +04:00
|
|
|
static void DumpString(const TString* s, DumpState* D)
|
2002-10-26 01:31:28 +04:00
|
|
|
{
|
2008-07-03 18:25:05 +04:00
|
|
|
if (s==NULL)
|
2005-11-16 14:55:07 +03:00
|
|
|
{
|
|
|
|
size_t size=0;
|
|
|
|
DumpVar(size,D);
|
|
|
|
}
|
2002-10-26 01:31:28 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
size_t size=s->tsv.len+1; /* include trailing '\0' */
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpVar(size,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
DumpBlock(getstr(s),size,D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
#define DumpCode(f,D) DumpVector(f->code,f->sizecode,sizeof(Instruction),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)
|
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
int i,n=f->sizek;
|
|
|
|
DumpInt(n,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
2003-12-10 15:13:36 +03:00
|
|
|
const TValue* o=&f->k[i];
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpChar(ttype(o),D);
|
2002-10-26 01:31:28 +04:00
|
|
|
switch (ttype(o))
|
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
case LUA_TNIL:
|
|
|
|
break;
|
|
|
|
case LUA_TBOOLEAN:
|
|
|
|
DumpChar(bvalue(o),D);
|
|
|
|
break;
|
2002-10-26 01:31:28 +04:00
|
|
|
case LUA_TNUMBER:
|
|
|
|
DumpNumber(nvalue(o),D);
|
|
|
|
break;
|
|
|
|
case LUA_TSTRING:
|
2003-12-10 15:13:36 +03:00
|
|
|
DumpString(rawtsvalue(o),D);
|
2002-10-26 01:31:28 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
lua_assert(0); /* cannot happen */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-11-16 14:55:07 +03:00
|
|
|
n=f->sizep;
|
|
|
|
DumpInt(n,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
for (i=0; i<n; i++) DumpFunction(f->p[i],f->source,D);
|
|
|
|
}
|
|
|
|
|
2009-09-28 20:32:50 +04:00
|
|
|
static void DumpUpvalues(const Proto* f, DumpState* D)
|
|
|
|
{
|
|
|
|
int i,n=f->sizeupvalues;
|
|
|
|
DumpInt(n,D);
|
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
DumpChar(f->upvalues[i].instack, D);
|
|
|
|
DumpChar(f->upvalues[i].idx, D);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-11-16 14:55:07 +03:00
|
|
|
static void DumpDebug(const Proto* f, DumpState* D)
|
|
|
|
{
|
|
|
|
int i,n;
|
|
|
|
n= (D->strip) ? 0 : f->sizelineinfo;
|
|
|
|
DumpVector(f->lineinfo,n,sizeof(int),D);
|
|
|
|
n= (D->strip) ? 0 : 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);
|
|
|
|
}
|
|
|
|
n= (D->strip) ? 0 : f->sizeupvalues;
|
|
|
|
DumpInt(n,D);
|
2009-09-28 20:32:50 +04:00
|
|
|
for (i=0; i<n; i++) DumpString(f->upvalues[i].name,D);
|
2005-11-16 14:55:07 +03:00
|
|
|
}
|
|
|
|
|
2002-10-26 01:31:28 +04:00
|
|
|
static void DumpFunction(const Proto* f, const TString* p, DumpState* D)
|
|
|
|
{
|
2006-02-17 18:51:03 +03:00
|
|
|
DumpString((f->source==p || D->strip) ? NULL : f->source,D);
|
2005-05-06 00:47:02 +04:00
|
|
|
DumpInt(f->linedefined,D);
|
|
|
|
DumpInt(f->lastlinedefined,D);
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpChar(f->numparams,D);
|
|
|
|
DumpChar(f->is_vararg,D);
|
|
|
|
DumpChar(f->maxstacksize,D);
|
2009-09-30 19:38:37 +04:00
|
|
|
DumpChar(f->envreg,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
DumpCode(f,D);
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpConstants(f,D);
|
2009-09-28 20:32:50 +04:00
|
|
|
DumpUpvalues(f,D);
|
2005-11-16 14:55:07 +03:00
|
|
|
DumpDebug(f,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
static void DumpHeader(DumpState* D)
|
|
|
|
{
|
2005-11-16 14:55:07 +03:00
|
|
|
char h[LUAC_HEADERSIZE];
|
|
|
|
luaU_header(h);
|
|
|
|
DumpBlock(h,LUAC_HEADERSIZE,D);
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|
|
|
|
|
2003-01-27 18:52:57 +03:00
|
|
|
/*
|
2004-07-09 22:24:41 +04:00
|
|
|
** dump Lua function as precompiled chunk
|
2003-01-27 18:52:57 +03:00
|
|
|
*/
|
2005-11-16 14:55:07 +03: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;
|
2004-07-09 22:24:41 +04:00
|
|
|
D.writer=w;
|
2002-10-26 01:31:28 +04:00
|
|
|
D.data=data;
|
2003-08-15 17:48:53 +04:00
|
|
|
D.strip=strip;
|
2004-07-09 22:24:41 +04:00
|
|
|
D.status=0;
|
2002-10-26 01:31:28 +04:00
|
|
|
DumpHeader(&D);
|
2004-07-09 22:24:41 +04:00
|
|
|
DumpFunction(f,NULL,&D);
|
|
|
|
return D.status;
|
2002-10-26 01:31:28 +04:00
|
|
|
}
|