Browse Source

final version (by lhf)

v5-2
Roberto Ierusalimschy 24 years ago
parent
commit
d742b88fa1
  1. 68
      lundump.c

68
lundump.c

@ -1,5 +1,5 @@
/*
** $Id: lundump.c,v 1.29 2000/06/28 14:12:55 lhf Exp lhf $
** $Id: lundump.c,v 1.31 2000/09/19 18:18:38 lhf Exp $
** load bytecodes from files
** See Copyright Notice in lua.h
*/
@ -13,11 +13,9 @@
#include "lstring.h"
#include "lundump.h"
#define LoadVector(L,b,n,s,Z) LoadBlock(L,b,(n)*(s),Z)
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
#define LoadByte ezgetc
static const char* ZNAME(ZIO* Z)
static const char* ZNAME (ZIO* Z)
{
const char* s=zname(Z);
return (*s=='@') ? s+1 : s;
@ -41,40 +39,53 @@ static void ezread (lua_State* L, ZIO* Z, void* b, int n)
if (r!=0) unexpectedEOZ(L,Z);
}
static void LoadReverse (lua_State* L, void* b, size_t size, ZIO* Z)
static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
{
char *p=(char *) b+size;
int n=size;
while (n--) *p--=ezgetc(L,Z);
if (swap)
{
char *p=(char *) b+size-1;
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
}
else
ezread(L,Z,b,size);
}
static int LoadInt (lua_State* L, ZIO* Z, int swap)
static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int swap)
{
int x;
if (swap)
LoadReverse(L,&x,sizeof(x),Z);
{
char *q=(char *) b;
while (m--)
{
char *p=q+size-1;
int n=size;
while (n--) *p--=(char)ezgetc(L,Z);
q+=size;
}
}
else
LoadBlock(L,&x,sizeof(x),Z);
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);
return x;
}
static size_t LoadSize (lua_State* L, ZIO* Z, int swap)
{
size_t x;
if (swap)
LoadReverse(L,&x,sizeof(x),Z);
else
LoadBlock(L,&x,sizeof(x),Z);
LoadBlock(L,&x,sizeof(x),Z,swap);
return x;
}
static Number LoadNumber (lua_State* L, ZIO* Z, int swap)
{
Number x;
if (swap)
LoadReverse(L,&x,sizeof(x),Z);
else
LoadBlock(L,&x,sizeof(x),Z);
LoadBlock(L,&x,sizeof(x),Z,swap);
return x;
}
@ -86,7 +97,7 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
else
{
char* s=luaO_openspace(L,size);
LoadBlock(L,s,size,Z);
LoadBlock(L,s,size,Z,0);
return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
}
}
@ -95,10 +106,7 @@ static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap)
{
int size=LoadInt(L,Z,swap);
tf->code=luaM_newvector(L,size,Instruction);
LoadVector(L,tf->code,size,sizeof(*tf->code),Z);
#if 0
if (swap) SwapBytes(tf->code,sizeof(*tf->code),size);
#endif
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
if (tf->code[size-1]!=OP_END) luaO_verror(L,"bad code in `%.255s'",ZNAME(Z));
}
@ -120,10 +128,7 @@ static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap)
int n=LoadInt(L,Z,swap);
if (n==0) return;
tf->lineinfo=luaM_newvector(L,n,int);
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z);
#if 0
if (swap) SwapBytes(tf->lineinfo,sizeof(*tf->lineinfo),n);
#endif
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
}
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
@ -137,9 +142,7 @@ static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap)
tf->kstr[i]=LoadString(L,Z,swap);
tf->nknum=n=LoadInt(L,Z,swap);
tf->knum=luaM_newvector(L,n,Number);
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z);
if (swap)
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z,swap); /* TODO */
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
tf->nkproto=n=LoadInt(L,Z,swap);
tf->kproto=luaM_newvector(L,n,Proto*);
for (i=0; i<n; i++)
@ -195,6 +198,7 @@ static int LoadHeader (lua_State* L, ZIO* Z)
" read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION));
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */
if (swap) puts("swap!");
TESTSIZE(sizeof(int));
TESTSIZE(sizeof(size_t));
TESTSIZE(sizeof(Instruction));

Loading…
Cancel
Save