new versions from lhf

This commit is contained in:
Roberto Ierusalimschy
2002-06-05 14:26:23 -03:00
parent 5b8ee9fa8d
commit 711b936849
2 changed files with 44 additions and 33 deletions

View File

@@ -1,11 +1,9 @@
/* /*
** $Id: lundump.c,v 1.47 2002/05/15 18:57:44 roberto Exp roberto $ ** $Id: lundump.c,v 1.40 2002/03/01 01:48:42 lhf Exp lhf $
** load pre-compiled Lua chunks ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <string.h>
#include "lua.h" #include "lua.h"
#include "ldebug.h" #include "ldebug.h"
@@ -14,19 +12,24 @@
#include "lopcodes.h" #include "lopcodes.h"
#include "lstring.h" #include "lstring.h"
#include "lundump.h" #include "lundump.h"
#include "lzio.h"
#define LoadByte ezgetc #define LoadByte (lu_byte) ezgetc
#define LoadShort (short) LoadInt
static const char* ZNAME (ZIO* Z) static const char* ZNAME (ZIO* Z)
{ {
const char* s=zname(Z); const char* s=zname(Z);
return (*s=='@') ? s+1 : s; if (*s=='@' || *s=='=')
return s+1;
else if (*s==LUA_SIGNATURE[0])
return "binary string";
else
return s;
} }
static void unexpectedEOZ (lua_State* L, ZIO* Z) static void unexpectedEOZ (lua_State* L, ZIO* Z)
{ {
luaG_runerror(L,"unexpected end of file in `%s'",ZNAME(Z)); luaG_runerror(L,"unexpected end of file in %s",ZNAME(Z));
} }
static int ezgetc (lua_State* L, ZIO* Z) static int ezgetc (lua_State* L, ZIO* Z)
@@ -75,6 +78,7 @@ static int LoadInt (lua_State* L, ZIO* Z, int swap)
{ {
int x; int x;
LoadBlock(L,&x,sizeof(x),Z,swap); LoadBlock(L,&x,sizeof(x),Z,swap);
if (x<0) luaG_runerror(L,"bad integer in %s",ZNAME(Z));
return x; return x;
} }
@@ -155,8 +159,10 @@ static void LoadConstants (lua_State* L, Proto* f, ZIO* Z, int swap)
case LUA_TSTRING: case LUA_TSTRING:
tsvalue(o)=LoadString(L,Z,swap); tsvalue(o)=LoadString(L,Z,swap);
break; break;
case LUA_TNIL:
break;
default: default:
luaG_runerror(L,"bad constant type (%d) in `%s'",ttype(o),ZNAME(Z)); luaG_runerror(L,"bad constant type (%d) in %s",ttype(o),ZNAME(Z));
break; break;
} }
} }
@@ -171,16 +177,16 @@ static Proto* LoadFunction (lua_State* L, TString* p, ZIO* Z, int swap)
Proto* f=luaF_newproto(L); Proto* f=luaF_newproto(L);
f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p; f->source=LoadString(L,Z,swap); if (f->source==NULL) f->source=p;
f->lineDefined=LoadInt(L,Z,swap); f->lineDefined=LoadInt(L,Z,swap);
f->nupvalues=LoadShort(L,Z,swap); f->nupvalues=LoadByte(L,Z);
f->numparams=LoadShort(L,Z,swap); f->numparams=LoadByte(L,Z);
f->is_vararg=LoadShort(L,Z,swap); f->is_vararg=LoadByte(L,Z);
f->maxstacksize=LoadShort(L,Z,swap); f->maxstacksize=LoadByte(L,Z);
LoadLocals(L,f,Z,swap); LoadLocals(L,f,Z,swap);
LoadLines(L,f,Z,swap); LoadLines(L,f,Z,swap);
LoadConstants(L,f,Z,swap); LoadConstants(L,f,Z,swap);
LoadCode(L,f,Z,swap); LoadCode(L,f,Z,swap);
#ifndef TRUST_BINARIES #ifndef TRUST_BINARIES
if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in `%s'",ZNAME(Z)); if (!luaG_checkcode(f)) luaG_runerror(L,"bad code in %s",ZNAME(Z));
#endif #endif
return f; return f;
} }
@@ -190,15 +196,15 @@ static void LoadSignature (lua_State* L, ZIO* Z)
const char* s=LUA_SIGNATURE; const char* s=LUA_SIGNATURE;
while (*s!=0 && ezgetc(L,Z)==*s) while (*s!=0 && ezgetc(L,Z)==*s)
++s; ++s;
if (*s!=0) luaG_runerror(L,"bad signature in `%s'",ZNAME(Z)); if (*s!=0) luaG_runerror(L,"bad signature in %s",ZNAME(Z));
} }
static void TestSize (lua_State* L, int s, const char* what, ZIO* Z) static void TestSize (lua_State* L, int s, const char* what, ZIO* Z)
{ {
int r=LoadByte(L,Z); int r=LoadByte(L,Z);
if (r!=s) if (r!=s)
luaG_runerror(L,"virtual machine mismatch in `%s':\n" luaG_runerror(L,"virtual machine mismatch in %s: "
" size of %.20s is %d but read %d",ZNAME(Z),what,s,r); "size of %s is %d but read %d",ZNAME(Z),what,s,r);
} }
#define TESTSIZE(s,w) TestSize(L,s,w,Z) #define TESTSIZE(s,w) TestSize(L,s,w,Z)
@@ -211,17 +217,17 @@ static int LoadHeader (lua_State* L, ZIO* Z)
LoadSignature(L,Z); LoadSignature(L,Z);
version=LoadByte(L,Z); version=LoadByte(L,Z);
if (version>VERSION) if (version>VERSION)
luaG_runerror(L,"`%s' too new:\n" luaG_runerror(L,"%s too new: "
" read version %d.%d; expected at most %d.%d", "read version %d.%d; expected at most %d.%d",
ZNAME(Z),V(version),V(VERSION)); ZNAME(Z),V(version),V(VERSION));
if (version<VERSION0) /* check last major change */ if (version<VERSION0) /* check last major change */
luaG_runerror(L,"`%s' too old:\n" luaG_runerror(L,"%s too old: "
" read version %d.%d; expected at least %d.%d", "read version %d.%d; expected at least %d.%d",
ZNAME(Z),V(version),V(VERSION)); ZNAME(Z),V(version),V(VERSION0));
swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */ swap=(luaU_endianness()!=LoadByte(L,Z)); /* need to swap bytes? */
TESTSIZE(sizeof(int),"int"); TESTSIZE(sizeof(int),"int");
TESTSIZE(sizeof(size_t), "size_t"); TESTSIZE(sizeof(size_t), "size_t");
TESTSIZE(sizeof(Instruction), "size_t"); TESTSIZE(sizeof(Instruction), "Instruction");
TESTSIZE(SIZE_OP, "OP"); TESTSIZE(SIZE_OP, "OP");
TESTSIZE(SIZE_A, "A"); TESTSIZE(SIZE_A, "A");
TESTSIZE(SIZE_B, "B"); TESTSIZE(SIZE_B, "B");
@@ -229,8 +235,7 @@ static int LoadHeader (lua_State* L, ZIO* Z)
TESTSIZE(sizeof(lua_Number), "number"); TESTSIZE(sizeof(lua_Number), "number");
x=LoadNumber(L,Z,swap); x=LoadNumber(L,Z,swap);
if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */ if ((long)x!=(long)tx) /* disregard errors in last bits of fraction */
luaG_runerror(L,"unknown number format in `%s':\n" luaG_runerror(L,"unknown number format in %s: read %f; expected %f",
" read " LUA_NUMBER_FMT "; expected " LUA_NUMBER_FMT,
ZNAME(Z),x,tx); ZNAME(Z),x,tx);
return swap; return swap;
} }
@@ -247,7 +252,7 @@ Proto* luaU_undump (lua_State* L, ZIO* Z)
{ {
Proto* f=LoadChunk(L,Z); Proto* f=LoadChunk(L,Z);
if (zgetc(Z)!=EOZ) if (zgetc(Z)!=EOZ)
luaG_runerror(L,"`%s' apparently contains more than one chunk",ZNAME(Z)); luaG_runerror(L,"%s apparently contains more than one chunk",ZNAME(Z));
return f; return f;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.24 2002/06/03 17:46:34 roberto Exp roberto $ ** $Id: lundump.h,v 1.24 2001/07/19 14:34:06 lhf Exp lhf $
** load pre-compiled Lua chunks ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -10,21 +10,27 @@
#include "lobject.h" #include "lobject.h"
#include "lzio.h" #include "lzio.h"
/* load one chunk */ typedef size_t (*Writer)(const void* p, size_t size, size_t n, void* u);
/* load one chunk; from lundump.c */
Proto* luaU_undump (lua_State* L, ZIO* Z); Proto* luaU_undump (lua_State* L, ZIO* Z);
/* find byte order */ /* find byte order; from lundump.c */
int luaU_endianness (void); int luaU_endianness (void);
/* dump one chunk; from dump.c */
void luaU_dump (const Proto* Main, Writer w, void* data);
/* print one chunk; from print.c */
void luaU_print (const Proto* Main);
/* definitions for headers of binary files */ /* definitions for headers of binary files */
#define VERSION 0x41 /* last format change was in 4.1 */ #define LUA_SIGNATURE "\033Lua" /* binary files start with <esc>Lua */
#define VERSION0 0x41 /* last major change was in 4.1 */ #define VERSION 0x50 /* last format change was in 5.0 */
#define VERSION0 0x50 /* last major change was in 5.0 */
/* 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
/* binary files start with <esc>Lua */
#define LUA_SIGNATURE "\033Lua"
#endif #endif