lua/lundump.c

225 lines
4.4 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
2010-10-25 18:33:38 +04:00
** $Id: lundump.c,v 1.67 2010/10/13 21:04:52 lhf Exp $
2005-11-16 14:55:07 +03:00
** load precompiled Lua chunks
1998-01-14 18:49:01 +03:00
** See Copyright Notice in lua.h
*/
2005-11-16 14:55:07 +03:00
#include <string.h>
2002-12-04 20:38:31 +03:00
#define lundump_c
#define LUA_CORE
2002-12-04 20:38:31 +03:00
2001-03-26 18:31:49 +04:00
#include "lua.h"
2001-07-06 00:29:15 +04:00
#include "ldebug.h"
2003-08-28 01:01:44 +04:00
#include "ldo.h"
1998-01-14 18:49:01 +03:00
#include "lfunc.h"
#include "lmem.h"
2005-11-16 14:55:07 +03:00
#include "lobject.h"
1998-01-14 18:49:01 +03:00
#include "lstring.h"
#include "lundump.h"
2002-06-05 21:26:23 +04:00
#include "lzio.h"
1998-01-14 18:49:01 +03:00
typedef struct {
lua_State* L;
ZIO* Z;
2002-10-26 01:30:41 +04:00
Mbuffer* b;
const char* name;
} LoadState;
1998-03-26 17:50:19 +03:00
2005-11-16 14:55:07 +03:00
static void error(LoadState* S, const char* why)
1998-01-14 18:49:01 +03:00
{
2010-10-25 18:33:38 +04:00
luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
2005-11-16 14:55:07 +03:00
luaD_throw(S->L,LUA_ERRSYNTAX);
1998-01-14 18:49:01 +03:00
}
2005-11-16 14:55:07 +03:00
#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
2010-10-25 18:33:38 +04:00
#define LoadByte(S) (lu_byte)LoadChar(S)
2005-11-16 14:55:07 +03:00
#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
1998-01-14 18:49:01 +03:00
2005-11-16 14:55:07 +03:00
static void LoadBlock(LoadState* S, void* b, size_t size)
1998-01-14 18:49:01 +03:00
{
2010-10-25 18:33:38 +04:00
if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
1998-01-14 18:49:01 +03:00
}
2005-11-16 14:55:07 +03:00
static int LoadChar(LoadState* S)
1998-01-14 18:49:01 +03:00
{
2005-11-16 14:55:07 +03:00
char x;
LoadVar(S,x);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2005-11-16 14:55:07 +03:00
static int LoadInt(LoadState* S)
{
2005-11-16 14:55:07 +03:00
int x;
LoadVar(S,x);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2005-11-16 14:55:07 +03:00
static lua_Number LoadNumber(LoadState* S)
1999-03-31 00:29:34 +04:00
{
lua_Number x;
2005-11-16 14:55:07 +03:00
LoadVar(S,x);
2000-09-04 22:53:41 +04:00
return x;
1999-03-31 00:29:34 +04:00
}
2005-11-16 14:55:07 +03:00
static TString* LoadString(LoadState* S)
1998-01-14 18:49:01 +03:00
{
2005-11-16 14:55:07 +03:00
size_t size;
LoadVar(S,size);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
2002-10-26 01:30:41 +04:00
char* s=luaZ_openspace(S->L,S->b,size);
2005-11-16 14:55:07 +03:00
LoadBlock(S,s,size);
2003-04-10 21:39:41 +04:00
return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
2005-11-16 14:55:07 +03:00
static void LoadCode(LoadState* S, Proto* f)
2000-04-25 20:44:31 +04:00
{
2005-11-16 14:55:07 +03:00
int n=LoadInt(S);
f->code=luaM_newvector(S->L,n,Instruction);
f->sizecode=n;
LoadVector(S,f->code,n,sizeof(Instruction));
2000-04-25 20:44:31 +04:00
}
2010-10-25 18:33:38 +04:00
static Proto* LoadFunction(LoadState* S);
1998-01-14 18:49:01 +03:00
2005-11-16 14:55:07 +03:00
static void LoadConstants(LoadState* S, Proto* f)
2003-01-27 18:52:57 +03:00
{
int i,n;
2003-01-27 18:52:57 +03:00
n=LoadInt(S);
2005-11-16 14:55:07 +03:00
f->k=luaM_newvector(S->L,n,TValue);
2001-07-06 00:29:15 +04:00
f->sizek=n;
2003-08-28 01:01:44 +04:00
for (i=0; i<n; i++) setnilvalue(&f->k[i]);
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2001-07-06 00:29:15 +04:00
{
TValue* o=&f->k[i];
2005-11-16 14:55:07 +03:00
int t=LoadChar(S);
switch (t)
2001-07-06 00:29:15 +04:00
{
2002-06-05 21:26:23 +04:00
case LUA_TNIL:
2006-09-11 18:07:24 +04:00
setnilvalue(o);
2002-06-05 21:26:23 +04:00
break;
case LUA_TBOOLEAN:
2010-10-25 18:33:38 +04:00
setbvalue(o,LoadChar(S));
2005-11-16 14:55:07 +03:00
break;
case LUA_TNUMBER:
setnvalue(o,LoadNumber(S));
break;
case LUA_TSTRING:
setsvalue2n(S->L,o,LoadString(S));
break;
2001-07-06 00:29:15 +04:00
}
}
n=LoadInt(S);
2005-11-16 14:55:07 +03:00
f->p=luaM_newvector(S->L,n,Proto*);
2001-07-06 00:29:15 +04:00
f->sizep=n;
2003-08-28 01:01:44 +04:00
for (i=0; i<n; i++) f->p[i]=NULL;
2010-10-25 18:33:38 +04:00
for (i=0; i<n; i++) f->p[i]=LoadFunction(S);
2000-09-04 22:53:41 +04:00
}
static void LoadUpvalues(LoadState* S, Proto* f)
{
int i,n;
n=LoadInt(S);
f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
f->sizeupvalues=n;
for (i=0; i<n; i++) f->upvalues[i].name=NULL;
for (i=0; i<n; i++)
{
f->upvalues[i].instack=LoadChar(S);
f->upvalues[i].idx=LoadChar(S);
}
}
2005-11-16 14:55:07 +03:00
static void LoadDebug(LoadState* S, Proto* f)
{
int i,n;
2010-10-25 18:33:38 +04:00
f->source=LoadString(S);
2005-11-16 14:55:07 +03:00
n=LoadInt(S);
f->lineinfo=luaM_newvector(S->L,n,int);
f->sizelineinfo=n;
LoadVector(S,f->lineinfo,n,sizeof(int));
n=LoadInt(S);
f->locvars=luaM_newvector(S->L,n,LocVar);
f->sizelocvars=n;
for (i=0; i<n; i++) f->locvars[i].varname=NULL;
for (i=0; i<n; i++)
{
f->locvars[i].varname=LoadString(S);
f->locvars[i].startpc=LoadInt(S);
f->locvars[i].endpc=LoadInt(S);
}
n=LoadInt(S);
for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
2005-11-16 14:55:07 +03:00
}
2010-10-25 18:33:38 +04:00
static Proto* LoadFunction(LoadState* S)
1998-01-14 18:49:01 +03:00
{
2010-10-25 18:33:38 +04:00
Proto* f=luaF_newproto(S->L);
2005-11-16 14:55:07 +03:00
setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
f->linedefined=LoadInt(S);
f->lastlinedefined=LoadInt(S);
f->numparams=LoadByte(S);
f->is_vararg=LoadByte(S);
f->maxstacksize=LoadByte(S);
LoadCode(S,f);
2005-11-16 14:55:07 +03:00
LoadConstants(S,f);
LoadUpvalues(S,f);
2005-11-16 14:55:07 +03:00
LoadDebug(S,f);
S->L->top--;
2001-07-06 00:29:15 +04:00
return f;
1998-01-14 18:49:01 +03:00
}
2005-11-16 14:55:07 +03:00
static void LoadHeader(LoadState* S)
1998-01-14 18:49:01 +03:00
{
2005-11-16 14:55:07 +03:00
char h[LUAC_HEADERSIZE];
char s[LUAC_HEADERSIZE];
luaU_header(h);
LoadBlock(S,s,LUAC_HEADERSIZE);
2010-10-25 18:33:38 +04:00
if (memcmp(h,s,LUAC_HEADERSIZE)!=0) error(S,"incompatible");
1998-01-14 18:49:01 +03:00
}
/*
2002-10-26 01:30:41 +04:00
** load precompiled chunk
1998-01-14 18:49:01 +03:00
*/
2005-11-16 14:55:07 +03:00
Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
1998-01-14 18:49:01 +03:00
{
LoadState S;
2005-11-16 14:55:07 +03:00
if (*name=='@' || *name=='=')
S.name=name+1;
else if (*name==LUA_SIGNATURE[0])
S.name="binary string";
else
2005-11-16 14:55:07 +03:00
S.name=name;
S.L=L;
S.Z=Z;
2002-10-26 01:30:41 +04:00
S.b=buff;
2005-11-16 14:55:07 +03:00
LoadHeader(&S);
2010-10-25 18:33:38 +04:00
return LoadFunction(&S);
2000-09-04 22:53:41 +04:00
}
1999-12-02 22:11:51 +03:00
/*
2005-11-16 14:55:07 +03:00
* make header
2010-10-25 18:33:38 +04:00
* if you make any changes in the header or in LUA_SIGNATURE,
* be sure to update LUAC_HEADERSIZE accordingly in lundump.h.
1999-12-02 22:11:51 +03:00
*/
2005-11-16 14:55:07 +03:00
void luaU_header (char* h)
1999-12-02 22:11:51 +03:00
{
2000-09-04 22:53:41 +04:00
int x=1;
2005-11-16 14:55:07 +03:00
memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
h+=sizeof(LUA_SIGNATURE)-1;
*h++=(char)LUAC_VERSION;
*h++=(char)LUAC_FORMAT;
*h++=(char)*(char*)&x; /* endianness */
*h++=(char)sizeof(int);
*h++=(char)sizeof(size_t);
*h++=(char)sizeof(Instruction);
*h++=(char)sizeof(lua_Number);
*h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
1999-03-31 00:29:34 +04:00
}