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 $
** load bytecodes from files
** $Id: lundump.c,v 1.35 2001/06/28 13:55:17 lhf Exp lhf $
** load pre-compiled Lua chunks
** See Copyright Notice in lua.h
*/
@ -10,6 +10,7 @@
#define LUA_PRIVATE
#include "lua.h"
#include "ldebug.h"
#include "lfunc.h"
#include "lmem.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)
{
l_char *p=(l_char *) b+size-1;
l_char *p=(l_char*) b+size-1;
int n=size;
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)
{
l_char *q=(l_char *) b;
l_char *q=(l_char*) b;
while (m--)
{
l_char *p=q+size-1;
@ -99,77 +100,92 @@ static TString* LoadString (lua_State* L, ZIO* Z, int swap)
return NULL;
else
{
char* s=luaO_openspace(L,size,char);
l_char* s=luaO_openspace(L,size,l_char);
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);
tf->code=luaM_newvector(L,size,Instruction);
tf->sizecode=size;
LoadVector(L,tf->code,size,sizeof(*tf->code),Z,swap);
f->code=luaM_newvector(L,size,Instruction);
f->sizecode=size;
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;
n=LoadInt(L,Z,swap);
tf->locvars=luaM_newvector(L,n,LocVar);
tf->sizelocvars=n;
f->locvars=luaM_newvector(L,n,LocVar);
f->sizelocvars=n;
for (i=0; i<n; i++)
{
tf->locvars[i].varname=LoadString(L,Z,swap);
tf->locvars[i].startpc=LoadInt(L,Z,swap);
tf->locvars[i].endpc=LoadInt(L,Z,swap);
f->locvars[i].varname=LoadString(L,Z,swap);
f->locvars[i].startpc=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;
n=LoadInt(L,Z,swap);
tf->lineinfo=luaM_newvector(L,n,int);
tf->sizelineinfo=n;
LoadVector(L,tf->lineinfo,n,sizeof(*tf->lineinfo),Z,swap);
f->lineinfo=luaM_newvector(L,n,int);
f->sizelineinfo=n;
LoadVector(L,f->lineinfo,n,sizeof(*f->lineinfo),Z,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;
n=LoadInt(L,Z,swap);
tf->kstr=luaM_newvector(L,n,TString*);
tf->sizekstr=n;
f->k=luaM_newvector(L,n,TObject);
f->sizek=n;
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);
tf->knum=luaM_newvector(L,n,lua_Number);
tf->sizeknum=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;
f->p=luaM_newvector(L,n,Proto*);
f->sizep=n;
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)
{
Proto* tf=luaF_newproto(L);
tf->source=LoadString(L,Z,swap);
tf->lineDefined=LoadInt(L,Z,swap);
tf->numparams=LoadInt(L,Z,swap);
tf->is_vararg=LoadByte(L,Z);
tf->maxstacksize=LoadInt(L,Z,swap);
LoadLocals(L,tf,Z,swap);
LoadLines(L,tf,Z,swap);
LoadConstants(L,tf,Z,swap);
LoadCode(L,tf,Z,swap);
return tf;
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;
}
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)
{
int version,swap;
lua_Number f=0,tf=TEST_NUMBER;
lua_Number x=0,tx=TEST_NUMBER;
LoadSignature(L,Z);
version=ezgetc(L,Z);
if (version>VERSION)
@ -205,19 +221,20 @@ static int LoadHeader (lua_State* L, ZIO* Z)
luaO_verror(L,l_s("`%.99s' too old:\n")
l_s(" 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? */
swap=(luaU_endianness()!=ezgetc(L,Z)); /* need to swap bytes? */
TESTSIZE(sizeof(int));
TESTSIZE(sizeof(size_t));
TESTSIZE(sizeof(Instruction));
TESTSIZE(SIZE_INSTRUCTION);
TESTSIZE(SIZE_OP);
TESTSIZE(SIZE_A);
TESTSIZE(SIZE_B);
TESTSIZE(SIZE_C);
TESTSIZE(sizeof(lua_Number));
f=LoadNumber(L,Z,swap);
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
x=LoadNumber(L,Z,swap);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
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),
ZNAME(Z),f,tf);
ZNAME(Z),x,tx);
return swap;
}
@ -232,20 +249,20 @@ static Proto* LoadChunk (lua_State* L, ZIO* Z)
*/
Proto* luaU_undump (lua_State* L, ZIO* Z)
{
Proto* tf=NULL;
Proto* f=NULL;
int c=zgetc(Z);
if (c==ID_CHUNK)
tf=LoadChunk(L,Z);
f=LoadChunk(L,Z);
c=zgetc(Z);
if (c!=EOZ)
luaO_verror(L,l_s("`%.99s' apparently contains more than one chunk"),ZNAME(Z));
return tf;
return f;
}
/*
** find byte order
*/
int luaU_endianess (void)
int luaU_endianness (void)
{
int x=1;
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
** See Copyright Notice in lua.h
*/
@ -14,18 +14,14 @@
Proto* luaU_undump (lua_State* L, ZIO* Z);
/* find byte order */
int luaU_endianess (void);
int luaU_endianness (void);
/* definitions for headers of binary files */
#define VERSION 0x40 /* last format change was in 4.0 */
#define VERSION0 0x40 /* last major change was in 4.0 */
#define VERSION 0x41 /* last format change was in 4.1 */
#define VERSION0 0x41 /* last major change was in 4.1 */
#define ID_CHUNK 27 /* binary files start with ESC... */
#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 */
/* multiplying by 1E8 gives non-trivial integer values */
#define TEST_NUMBER 3.14159265358979323846E8