modifications by lhf

This commit is contained in:
Roberto Ierusalimschy
1999-12-02 17:11:51 -02:00
parent b097076678
commit 72afb6debb
2 changed files with 104 additions and 100 deletions

View File

@@ -1,13 +1,14 @@
/*
** $Id: lundump.c,v 1.14 1999/09/06 13:55:09 roberto Exp roberto $
** $Id: lundump.c,v 1.24 1999/12/02 18:45:03 lhf Exp $
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#define LUA_REENTRANT
#include <stdio.h>
#include <string.h>
#define LUA_REENTRANT
#include "lauxlib.h"
#include "lfunc.h"
#include "lmem.h"
@@ -19,7 +20,7 @@
static void unexpectedEOZ (lua_State* L, ZIO* Z)
{
luaL_verror(L, "unexpected end of file in %s",zname(Z));
luaL_verror(L,"unexpected end of file in %.255s",zname(Z));
}
static int ezgetc (lua_State* L, ZIO* Z)
@@ -49,22 +50,11 @@ static unsigned long LoadLong (lua_State *L, ZIO* Z)
return (hi<<16)|lo;
}
/*
* convert number from text
*/
real luaU_str2d (lua_State *L, const char* b, const char* where)
{
real x;
if (!luaO_str2d(b, &x))
luaL_verror(L, "cannot convert number '%s' in %s",b,where);
return x;
}
static real LoadNumber (lua_State* L, ZIO* Z, int native)
{
real x;
if (native)
{
real x;
LoadBlock(L,&x,sizeof(x),Z);
return x;
}
@@ -90,10 +80,10 @@ static int LoadInt (lua_State *L, ZIO* Z, const char* message)
static Byte* LoadCode (lua_State* L, ZIO* Z)
{
int size=LoadInt(L, Z,"code too long (%ld bytes) in %s");
int size=LoadInt(L,Z,"code too long (%lu bytes) in %.255s");
Byte* b=luaM_malloc(L,size+PAD);
LoadBlock(L,b,size,Z);
if (b[size-1]!=ENDCODE) luaL_verror(L, "bad code in %s",zname(Z));
if (b[size-1]!=ENDCODE) luaL_verror(L,"bad code in %.255s",zname(Z));
memset(b+size,ENDCODE,PAD); /* pad code for safety */
return b;
}
@@ -113,12 +103,12 @@ static TaggedString* LoadTString (lua_State *L, ZIO* Z)
static void LoadLocals (lua_State* L, TProtoFunc* tf, ZIO* Z)
{
int i,n=LoadInt(L, Z,"too many locals (%ld) in %s");
int i,n=LoadInt(L,Z,"too many locals (%lu) in %.255s");
if (n==0) return;
tf->locvars=luaM_newvector(L,n+1,LocVar);
for (i=0; i<n; i++)
{
tf->locvars[i].line=LoadInt(L, Z,"too many lines (%ld) in %s");
tf->locvars[i].line=LoadInt(L,Z,"too many lines (%lu) in %.255s");
tf->locvars[i].varname=LoadTString(L,Z);
}
tf->locvars[i].line=-1; /* flag end of vector */
@@ -129,7 +119,7 @@ static TProtoFunc* LoadFunction (lua_State *L, ZIO* Z, int native);
static void LoadConstants (lua_State* L, TProtoFunc* tf, ZIO* Z, int native)
{
int i,n=LoadInt(L, Z,"too many constants (%ld) in %s");
int i,n=LoadInt(L,Z,"too many constants (%lu) in %.255s");
tf->nconsts=n;
if (n==0) return;
tf->consts=luaM_newvector(L,n,TObject);
@@ -160,7 +150,7 @@ static void LoadConstants (lua_State *L, TProtoFunc* tf, ZIO* Z, int native)
static TProtoFunc* LoadFunction (lua_State* L, ZIO* Z, int native)
{
TProtoFunc* tf=luaF_newproto(L);
tf->lineDefined=LoadInt(L, Z,"lineDefined too large (%ld) in %s");
tf->lineDefined=LoadInt(L,Z,"lineDefined too large (%lu) in %.255s");
tf->source=LoadTString(L,Z);
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
tf->code=LoadCode(L,Z);
@@ -174,36 +164,35 @@ static void LoadSignature (lua_State *L, ZIO* Z)
const char* s=SIGNATURE;
while (*s!=0 && ezgetc(L,Z)==*s)
++s;
if (*s!=0) luaL_verror(L, "bad signature in %s",zname(Z));
if (*s!=0) luaL_verror(L,"bad signature in %.255s",zname(Z));
}
static int LoadHeader (lua_State* L, ZIO* Z)
{
int version,sizeofR;
int native;
int version,sizeofR,native;
LoadSignature(L,Z);
version=ezgetc(L,Z);
if (version>VERSION)
luaL_verror(L,
"%s too new: version=0x%02x; expected at most 0x%02x",
"%.255s too new: version=0x%02x; expected at most 0x%02x",
zname(Z),version,VERSION);
if (version<VERSION0) /* check last major change */
luaL_verror(L,
"%s too old: version=0x%02x; expected at least 0x%02x",
"%.255s too old: version=0x%02x; expected at least 0x%02x",
zname(Z),version,VERSION0);
sizeofR=ezgetc(L,Z);
native=(sizeofR!=0);
if (native) /* test number representation */
{
if (sizeofR!=sizeof(real))
luaL_verror(L, "unknown number size in %s: read %d; expected %d",
zname(Z),sizeofR,sizeof(real));
luaL_verror(L,"unknown number size in %.255s: read %d; expected %d",
zname(Z),sizeofR,(int)sizeof(real));
else
{
real tf=TEST_NUMBER;
real f=LoadNumber(L,Z,native);
if ((long)f!=(long)tf)
luaL_verror(L, "unknown number format in %s: "
luaL_verror(L,"unknown number format in %.255s: "
"read " NUMBER_FMT "; expected " NUMBER_FMT,
zname(Z),f,tf);
}
@@ -226,16 +215,27 @@ TProtoFunc* luaU_undump1 (lua_State *L, ZIO* Z)
if (c==ID_CHUNK)
return LoadChunk(L,Z);
else if (c!=EOZ)
luaL_verror(L, "%s is not a Lua binary file",zname(Z));
luaL_verror(L,"%.255s is not a Lua binary file",zname(Z));
return NULL;
}
/*
* handle constants that cannot happen
*/
void luaU_badconstant (lua_State *L, const char* s, int i, const TObject* o, TProtoFunc* tf)
void luaU_badconstant (lua_State* L, const char* s, int i, const TObject* o, const TProtoFunc* tf)
{
int t=ttype(o);
const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
luaL_verror(L, "cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
luaL_verror(L,"cannot %.255s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
}
/*
* convert number from text
*/
double luaU_str2d (lua_State* L, const char* b, const char* where)
{
double x;
if (!luaO_str2d(b,&x))
luaL_verror(L,"cannot convert number '%.255s' in %.255s",b,where);
return x;
}

View File

@@ -1,5 +1,5 @@
/*
** $Id: lundump.h,v 1.10 1999/08/16 20:52:00 roberto Exp roberto $
** $Id: lundump.h,v 1.17 1999/12/02 18:51:09 lhf Exp $
** load pre-compiled Lua chunks
** See Copyright Notice in lua.h
*/
@@ -10,20 +10,24 @@
#include "lobject.h"
#include "lzio.h"
TProtoFunc* luaU_undump1 (lua_State *L, ZIO* Z); /* load one chunk */
void luaU_badconstant (lua_State *L, const char* s, int i, const TObject* o, TProtoFunc* tf);
/* load one chunk */
TProtoFunc* luaU_undump1 (lua_State* L, ZIO* Z);
/* handle cases that cannot happen */
double luaU_str2d (lua_State *L, const char* b, const char* where);
void luaU_badconstant (lua_State* L, const char* s, int i,
const TObject* o, const TProtoFunc* tf);
/* convert number from text */
double luaU_str2d (lua_State* L, const char* b, const char* where);
/* definitions for headers of binary files */
#define VERSION 0x32 /* last format change was in 3.2 */
#define VERSION0 0x32 /* last major change was in 3.2 */
#define VERSION 0x33 /* last format change was in 3.3 */
#define VERSION0 0x33 /* last major change was in 3.3 */
#define ID_CHUNK 27 /* binary files start with ESC... */
#define SIGNATURE "Lua" /* ...followed by this signature */
/* formats for error messages */
#define SOURCE "<%s:%d>"
#define SOURCE "<%.255s:%d>"
#define IN " in %p " SOURCE
#define INLOC tf,tf->source->str,tf->lineDefined