new versions by lhf

This commit is contained in:
Roberto Ierusalimschy
2001-07-05 17:29:15 -03:00
parent 9924668931
commit dc4e0ecdaf
2 changed files with 73 additions and 60 deletions

121
lundump.c
View File

@@ -1,6 +1,6 @@
/* /*
** $Id: lundump.c,v 1.39 2001/02/23 17:17:25 roberto Exp roberto $ ** $Id: lundump.c,v 1.35 2001/06/28 13:55:17 lhf Exp lhf $
** load bytecodes from files ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -10,6 +10,7 @@
#define LUA_PRIVATE #define LUA_PRIVATE
#include "lua.h" #include "lua.h"
#include "ldebug.h"
#include "lfunc.h" #include "lfunc.h"
#include "lmem.h" #include "lmem.h"
#include "lopcodes.h" #include "lopcodes.h"
@@ -46,7 +47,7 @@ static void LoadBlock (lua_State* L, void* b, size_t size, ZIO* Z, int swap)
{ {
if (swap) if (swap)
{ {
l_char *p=(l_char *) b+size-1; l_char *p=(l_char*) b+size-1;
int n=size; int n=size;
while (n--) *p--=(l_char)ezgetc(L,Z); while (n--) *p--=(l_char)ezgetc(L,Z);
} }
@@ -58,7 +59,7 @@ static void LoadVector (lua_State* L, void* b, int m, size_t size, ZIO* Z, int s
{ {
if (swap) if (swap)
{ {
l_char *q=(l_char *) b; l_char *q=(l_char*) b;
while (m--) while (m--)
{ {
l_char *p=q+size-1; l_char *p=q+size-1;
@@ -99,77 +100,92 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
return NULL; return NULL;
else else
{ {
char* s=luaO_openspace(L,size,char); l_char* s=luaO_openspace(L,size,l_char);
LoadBlock(L,s,size,Z,0); LoadBlock(L,s,size,Z,0);
return luaS_newlstr(L,s,size-1); /* remove trailing l_c('\0') */ return luaS_newlstr(L,s,size-1); /* remove trailing '\0' */
} }
} }
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z, int swap) static void LoadCode (lua_State* L, Proto* f, ZIO* Z, int swap)
{ {
int size=LoadInt(L,Z,swap); int size=LoadInt(L,Z,swap);
tf->code=luaM_newvector(L,size,Instruction); f->code=luaM_newvector(L,size,Instruction);
tf->sizecode=size; f->sizecode=size;
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap); LoadVector(L,f->code,size,sizeof(*f->code),Z,swap);
} }
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z, int swap) static void LoadLocals (lua_State* L, Proto* f, ZIO* Z, int swap)
{ {
int i,n; int i,n;
n=LoadInt(L,Z,swap); n=LoadInt(L,Z,swap);
tf->locvars=luaM_newvector(L,n,LocVar); f->locvars=luaM_newvector(L,n,LocVar);
tf->sizelocvars=n; f->sizelocvars=n;
for (i=0; i<n; i++) for (i=0; i<n; i++)
{ {
tf->locvars[i].varname=LoadString(L,Z,swap); f->locvars[i].varname=LoadString(L,Z,swap);
tf->locvars[i].startpc=LoadInt(L,Z,swap); f->locvars[i].startpc=LoadInt(L,Z,swap);
tf->locvars[i].endpc=LoadInt(L,Z,swap); f->locvars[i].endpc=LoadInt(L,Z,swap);
} }
} }
static void LoadLines (lua_State* L, Proto* tf, ZIO* Z, int swap) static void LoadLines (lua_State* L, Proto* f, ZIO* Z, int swap)
{ {
int n; int n;
n=LoadInt(L,Z,swap); n=LoadInt(L,Z,swap);
tf->lineinfo=luaM_newvector(L,n,int); f->lineinfo=luaM_newvector(L,n,int);
tf->sizelineinfo=n; f->sizelineinfo=n;
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap); LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,swap);
} }
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap); static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap);
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int swap) static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
{ {
int i,n; int i,n;
n=LoadInt(L,Z,swap); n=LoadInt(L,Z,swap);
tf->kstr=luaM_newvector(L,n,TString*); f->k=luaM_newvector(L,n,TObject);
tf->sizekstr=n; f->sizek=n;
for (i=0; i<n; i++) for (i=0; i<n; i++)
tf->kstr[i]=LoadString(L,Z,swap); {
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;
}
}
n=LoadInt(L,Z,swap); n=LoadInt(L,Z,swap);
tf->knum=luaM_newvector(L,n,lua_Number); f->p=luaM_newvector(L,n,Proto*);
tf->sizeknum=n; f->sizep=n;
LoadVector(L,tf->knum,n,sizeof(*tf->knum),Z,swap);
n=LoadInt(L,Z,swap);
tf->kproto=luaM_newvector(L,n,Proto*);
tf->sizekproto=n;
for (i=0; i<n; i++) for (i=0; i<n; i++)
tf->kproto[i]=LoadFunction(L,Z,swap); f->p[i]=LoadFunction(L,Z,swap);
} }
static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap) static Proto* LoadFunction (lua_State* L, ZIO* Z, int swap)
{ {
Proto* tf=luaF_newproto(L); Proto* f=luaF_newproto(L);
tf->source=LoadString(L,Z,swap); f->source=LoadString(L,Z,swap);
tf->lineDefined=LoadInt(L,Z,swap); f->lineDefined=LoadInt(L,Z,swap);
tf->numparams=LoadInt(L,Z,swap); f->nupvalues=LoadInt(L,Z,swap);
tf->is_vararg=LoadByte(L,Z); f->numparams=LoadInt(L,Z,swap);
tf->maxstacksize=LoadInt(L,Z,swap); f->is_vararg=LoadByte(L,Z);
LoadLocals(L,tf,Z,swap); f->maxstacksize=LoadInt(L,Z,swap);
LoadLines(L,tf,Z,swap); LoadLocals(L,f,Z,swap);
LoadConstants(L,tf,Z,swap); LoadLines(L,f,Z,swap);
LoadCode(L,tf,Z,swap); LoadConstants(L,f,Z,swap);
return tf; 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;
} }
static void LoadSignature (lua_State* L, ZIO* Z) static void LoadSignature (lua_State* L, ZIO* Z)
@@ -194,7 +210,7 @@ static void TestSize (lua_State* L, int s, const l_char* what, ZIO* Z)
static int LoadHeader (lua_State* L, ZIO* Z) static int LoadHeader (lua_State* L, ZIO* Z)
{ {
int version,swap; int version,swap;
lua_Number f=0,tf=TEST_NUMBER; lua_Number x=0,tx=TEST_NUMBER;
LoadSignature(L,Z); LoadSignature(L,Z);
version=ezgetc(L,Z); version=ezgetc(L,Z);
if (version>VERSION) if (version>VERSION)
@@ -205,19 +221,20 @@ static int LoadHeader (lua_State* L, ZIO* Z)
luaO_verror(L,l_s("`%.99s' too old:\n") luaO_verror(L,l_s("`%.99s' too old:\n")
l_s(" read version %d.%d; expected at least %d.%d"), l_s(" read version %d.%d; expected at least %d.%d"),
ZNAME(Z),V(version),V(VERSION)); ZNAME(Z),V(version),V(VERSION));
swap=(luaU_endianess()!=ezgetc(L,Z)); /* need to swap bytes? */ swap=(luaU_endianness()!=ezgetc(L,Z)); /* need to swap bytes? */
TESTSIZE(sizeof(int)); TESTSIZE(sizeof(int));
TESTSIZE(sizeof(size_t)); TESTSIZE(sizeof(size_t));
TESTSIZE(sizeof(Instruction)); TESTSIZE(sizeof(Instruction));
TESTSIZE(SIZE_INSTRUCTION);
TESTSIZE(SIZE_OP); TESTSIZE(SIZE_OP);
TESTSIZE(SIZE_A);
TESTSIZE(SIZE_B); TESTSIZE(SIZE_B);
TESTSIZE(SIZE_C);
TESTSIZE(sizeof(lua_Number)); TESTSIZE(sizeof(lua_Number));
f=LoadNumber(L,Z,swap); x=LoadNumber(L,Z,swap);
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */ if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
luaO_verror(L,l_s("unknown number format in `%.99s':\n") luaO_verror(L,l_s("unknown number format in `%.99s':\n")
l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT), l_s(" read ") l_s(LUA_NUMBER_FMT) l_s("; expected ") l_s(LUA_NUMBER_FMT),
ZNAME(Z),f,tf); ZNAME(Z),x,tx);
return swap; return swap;
} }
@@ -232,20 +249,20 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z)
*/ */
Proto* luaU_undump (lua_State* L, ZIO* Z) Proto* luaU_undump (lua_State* L, ZIO* Z)
{ {
Proto* tf=NULL; Proto* f=NULL;
int c=zgetc(Z); int c=zgetc(Z);
if (c==ID_CHUNK) if (c==ID_CHUNK)
tf=LoadChunk(L,Z); f=LoadChunk(L,Z);
c=zgetc(Z); c=zgetc(Z);
if (c!=EOZ) if (c!=EOZ)
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z)); luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
return tf; return f;
} }
/* /*
** find byte order ** find byte order
*/ */
int luaU_endianess (void) int luaU_endianness (void)
{ {
int x=1; int x=1;
return *(l_char*)&x; return *(l_char*)&x;

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.20 2001/02/23 17:17:25 roberto Exp roberto $ ** $Id: lundump.h,v 1.23 2001/06/28 13:55:17 lhf Exp $
** load pre-compiled Lua chunks ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -14,18 +14,14 @@
Proto* luaU_undump (lua_State* L, ZIO* Z); Proto* luaU_undump (lua_State* L, ZIO* Z);
/* find byte order */ /* find byte order */
int luaU_endianess (void); int luaU_endianness (void);
/* definitions for headers of binary files */ /* definitions for headers of binary files */
#define VERSION 0x40 /* last format change was in 4.0 */ #define VERSION 0x41 /* last format change was in 4.1 */
#define VERSION0 0x40 /* last major change was in 4.0 */ #define VERSION0 0x41 /* last major change was in 4.1 */
#define ID_CHUNK 27 /* binary files start with ESC... */ #define ID_CHUNK 27 /* binary files start with ESC... */
#define SIGNATURE "Lua" /* ...followed by this signature */ #define SIGNATURE "Lua" /* ...followed by this signature */
/* formats for error messages */
#define SOURCE tf->lineDefined,tf->source->str
#define IN tf,SOURCE
/* a multiple of PI for testing native format */ /* a multiple of PI for testing native format */
/* multiplying by 1E8 gives non-trivial integer values */ /* multiplying by 1E8 gives non-trivial integer values */
#define TEST_NUMBER 3.14159265358979323846E8 #define TEST_NUMBER 3.14159265358979323846E8