lua/lundump.c

270 lines
5.7 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
2001-07-06 00:29:15 +04:00
** $Id: lundump.c,v 1.35 2001/06/28 13:55:17 lhf Exp lhf $
** load pre-compiled Lua chunks
1998-01-14 18:49:01 +03:00
** See Copyright Notice in lua.h
*/
#include <stdio.h>
1999-03-31 00:29:34 +04:00
#include <string.h>
1999-12-02 22:11:51 +03:00
2001-03-26 18:31:49 +04:00
#define LUA_PRIVATE
#include "lua.h"
2001-07-06 00:29:15 +04:00
#include "ldebug.h"
1998-01-14 18:49:01 +03:00
#include "lfunc.h"
#include "lmem.h"
1999-03-31 00:29:34 +04:00
#include "lopcodes.h"
1998-01-14 18:49:01 +03:00
#include "lstring.h"
#include "lundump.h"
2000-04-25 20:44:31 +04:00
#define LoadByte ezgetc
2001-02-23 20:17:25 +03:00
static const l_char* ZNAME (ZIO* Z)
2000-04-25 20:44:31 +04:00
{
2001-02-23 20:17:25 +03:00
const l_char* s=zname(Z);
return (*s==l_c('@')) ? s+1 : s;
2000-04-25 20:44:31 +04:00
}
1998-03-26 17:50:19 +03:00
1999-12-02 22:11:51 +03:00
static void unexpectedEOZ (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("unexpected end of file in `%.99s'"),ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static int ezgetc (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
1999-12-02 22:11:51 +03:00
if (c==EOZ) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
return c;
}
1999-12-02 22:11:51 +03:00
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
1998-01-14 18:49:01 +03:00
{
int r=zread(Z,b,n);
1999-12-02 22:11:51 +03:00
if (r!=0) unexpectedEOZ(L,Z);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-20 22:43:54 +04:00
if (swap)
{
2001-07-06 00:29:15 +04:00
l_char *p=(l_char*) b+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
2001-02-23 20:17:25 +03:00
while (n--) *p--=(l_char)ezgetc(L,Z);
2000-09-20 22:43:54 +04:00
}
else
ezread(L,Z,b,size);
1998-01-14 18:49:01 +03:00
}
2000-09-20 22:43:54 +04:00
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
if (swap)
2000-09-20 22:43:54 +04:00
{
2001-07-06 00:29:15 +04:00
l_char *q=(l_char*) b;
2000-09-20 22:43:54 +04:00
while (m--)
{
2001-02-23 20:17:25 +03:00
l_char *p=q+size-1;
2000-09-20 22:43:54 +04:00
int n=size;
2001-02-23 20:17:25 +03:00
while (n--) *p--=(l_char)ezgetc(L,Z);
2000-09-20 22:43:54 +04:00
q+=size;
}
}
2000-09-04 22:53:41 +04:00
else
2000-09-20 22:43:54 +04:00
ezread(L,Z,b,m*size);
}
static int LoadInt (lua_State* L, ZIO* Z, int swap)
{
int x;
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
{
2000-09-04 22:53:41 +04:00
size_t x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1998-01-14 18:49:01 +03:00
}
static lua_Number LoadNumber (lua_State* L, ZIO* Z, int swap)
1999-03-31 00:29:34 +04:00
{
lua_Number x;
2000-09-20 22:43:54 +04:00
LoadBlock(L,&x,sizeof(x),Z,swap);
2000-09-04 22:53:41 +04:00
return x;
1999-03-31 00:29:34 +04:00
}
2000-09-04 22:53:41 +04:00
static TString* LoadString (lua_State* L, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
size_t size=LoadSize(L,Z,swap);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
2001-07-06 00:29:15 +04:00
l_char* s=luaO_openspace(L,size,l_char);
2000-09-20 22:43:54 +04:00
LoadBlock(L,s,size,Z,0);
2001-07-06 00:29:15 +04:00
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
1998-01-14 18:49:01 +03:00
}
}
2001-07-06 00:29:15 +04:00
static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-12-28 15:59:41 +03:00
int size=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->code=luaM_newvector(L,size,Instruction);
f->sizecode=size;
LoadVector(L,f->code,size,sizeof(*f->code),Z,swap);
2000-04-25 20:44:31 +04:00
}
2001-07-06 00:29:15 +04:00
static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-04-25 20:44:31 +04:00
{
2000-09-04 22:53:41 +04:00
int i,n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->locvars=luaM_newvector(L,n,LocVar);
f->sizelocvars=n;
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2000-04-25 20:44:31 +04:00
{
2001-07-06 00:29:15 +04:00
f->locvars[i].varname=LoadString(L,Z,swap);
f->locvars[i].startpc=LoadInt(L,Z,swap);
f->locvars[i].endpc=LoadInt(L,Z,swap);
2000-04-25 20:44:31 +04:00
}
}
2001-07-06 00:29:15 +04:00
static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap)
2000-09-04 22:53:41 +04:00
{
2000-10-20 20:39:03 +04:00
int n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->lineinfo=luaM_newvector(L,n,int);
f->sizelineinfo=n;
LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
1998-01-14 18:49:01 +03:00
}
2000-09-04 22:53:41 +04:00
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
1998-03-26 17:50:19 +03:00
2001-07-06 00:29:15 +04:00
static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2000-03-03 17:58:26 +03:00
int i,n;
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->k=luaM_newvector(L,n,TObject);
f->sizek=n;
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2001-07-06 00:29:15 +04:00
{
TObject* o=&f->k[i];
ttype(o)=LoadByte(L,Z);
switch (ttype(o))
{
case LUA_TNUMBER:
nvalue(o)=LoadNumber(L,Z,swap);
break;
case LUA_TSTRING:
tsvalue(o)=LoadString(L,Z,swap);
break;
default:
luaO_verror(L,l_s("bad constant type (%d) in `%.99s'"),ttype(o),ZNAME(Z));
break;
}
}
2000-12-28 15:59:41 +03:00
n=LoadInt(L,Z,swap);
2001-07-06 00:29:15 +04:00
f->p=luaM_newvector(L,n,Proto*);
f->sizep=n;
2000-09-04 22:53:41 +04:00
for (i=0; i<n; i++)
2001-07-06 00:29:15 +04:00
f->p[i]=LoadFunction(L,Z,swap);
2000-09-04 22:53:41 +04:00
}
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
1998-01-14 18:49:01 +03:00
{
2001-07-06 00:29:15 +04:00
Proto* f=luaF_newproto(L);
f->source=LoadString(L,Z,swap);
f->lineDefined=LoadInt(L,Z,swap);
f->nupvalues=LoadInt(L,Z,swap);
f->numparams=LoadInt(L,Z,swap);
f->is_vararg=LoadByte(L,Z);
f->maxstacksize=LoadInt(L,Z,swap);
LoadLocals(L,f,Z,swap);
LoadLines(L,f,Z,swap);
LoadConstants(L,f,Z,swap);
LoadCode(L,f,Z,swap);
#ifndef TRUST_BINARIES
if (luaG_checkcode(f)==0) luaO_verror(L,l_s("bad code in `%.99s'"),ZNAME(Z));
#endif
return f;
1998-01-14 18:49:01 +03:00
}
1999-12-02 22:11:51 +03:00
static void LoadSignature (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2001-03-26 18:31:49 +04:00
const l_char* s=l_s(SIGNATURE);
1999-12-02 22:11:51 +03:00
while (*s!=0 && ezgetc(L,Z)==*s)
1998-01-14 18:49:01 +03:00
++s;
2001-02-23 20:17:25 +03:00
if (*s!=0) luaO_verror(L,l_s("bad signature in `%.99s'"),ZNAME(Z));
1998-01-14 18:49:01 +03:00
}
2001-02-23 20:17:25 +03:00
static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z)
2000-09-04 22:53:41 +04:00
{
int r=ezgetc(L,Z);
if (r!=s)
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("virtual machine mismatch in `%.99s':\n")
l_s(" %.20s is %d but read %d"),ZNAME(Z),what,s,r);
2000-09-04 22:53:41 +04:00
}
#define TESTSIZE(s) TestSize(L,s,#s,Z)
2000-03-03 17:58:26 +03:00
#define V(v) v/16,v%16
1999-12-02 22:11:51 +03:00
static int LoadHeader (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2000-09-04 22:53:41 +04:00
int version,swap;
2001-07-06 00:29:15 +04:00
lua_Number x=0,tx=TEST_NUMBER;
1999-12-02 22:11:51 +03:00
LoadSignature(L,Z);
version=ezgetc(L,Z);
1998-01-14 18:49:01 +03:00
if (version>VERSION)
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("`%.99s' too new:\n")
l_s(" read version %d.%d; expected at most %d.%d"),
2000-04-25 20:44:31 +04:00
ZNAME(Z),V(version),V(VERSION));
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("`%.99s' too old:\n")
l_s(" read version %d.%d; expected at least %d.%d"),
2000-04-25 20:44:31 +04:00
ZNAME(Z),V(version),V(VERSION));
2001-07-06 00:29:15 +04:00
swap=(luaU_endianness()!=ezgetc(L,Z)); /* need to swap bytes? */
2000-09-04 22:53:41 +04:00
TESTSIZE(sizeof(int));
TESTSIZE(sizeof(size_t));
TESTSIZE(sizeof(Instruction));
TESTSIZE(SIZE_OP);
2001-07-06 00:29:15 +04:00
TESTSIZE(SIZE_A);
2000-09-04 22:53:41 +04:00
TESTSIZE(SIZE_B);
2001-07-06 00:29:15 +04:00
TESTSIZE(SIZE_C);
TESTSIZE(sizeof(lua_Number));
2001-07-06 00:29:15 +04:00
x=LoadNumber(L,Z,swap);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("unknown number format in `%.99s':\n")
2001-03-26 18:31:49 +04:00
l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT),
2001-07-06 00:29:15 +04:00
ZNAME(Z),x,tx);
2000-09-04 22:53:41 +04:00
return swap;
1998-01-14 18:49:01 +03:00
}
2000-03-10 21:37:44 +03:00
static Proto* LoadChunk (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-12-02 22:11:51 +03:00
return LoadFunction(L,Z,LoadHeader(L,Z));
1998-01-14 18:49:01 +03:00
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
2000-09-04 22:53:41 +04:00
Proto* luaU_undump (lua_State* L, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
2001-07-06 00:29:15 +04:00
Proto* f=NULL;
1998-01-14 18:49:01 +03:00
int c=zgetc(Z);
if (c==ID_CHUNK)
2001-07-06 00:29:15 +04:00
f=LoadChunk(L,Z);
2000-09-18 23:42:05 +04:00
c=zgetc(Z);
if (c!=EOZ)
2001-02-23 20:17:25 +03:00
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
2001-07-06 00:29:15 +04:00
return f;
2000-09-04 22:53:41 +04:00
}
1999-12-02 22:11:51 +03:00
/*
2000-09-04 22:53:41 +04:00
** find byte order
1999-12-02 22:11:51 +03:00
*/
2001-07-06 00:29:15 +04:00
int luaU_endianness (void)
1999-12-02 22:11:51 +03:00
{
2000-09-04 22:53:41 +04:00
int x=1;
2001-02-23 20:17:25 +03:00
return *(l_char*)&x;
1999-03-31 00:29:34 +04:00
}