lua/lundump.c

240 lines
4.9 KiB
C
Raw Normal View History

1998-01-14 18:49:01 +03:00
/*
** $Id: lundump.c,v 1.13 1999/08/16 20:52:00 roberto Exp roberto $
1998-01-14 18:49:01 +03:00
** load bytecodes from files
** See Copyright Notice in lua.h
*/
#include <stdio.h>
1999-03-31 00:29:34 +04:00
#include <string.h>
1998-01-14 18:49:01 +03:00
#include "lauxlib.h"
#include "lfunc.h"
#include "lmem.h"
1999-03-31 00:29:34 +04:00
#include "lopcodes.h"
1998-01-14 18:49:01 +03:00
#include "lstring.h"
#include "lundump.h"
1998-03-26 17:50:19 +03:00
#define LoadBlock(b,size,Z) ezread(Z,b,size)
1998-12-15 17:59:43 +03:00
static void unexpectedEOZ (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1998-03-26 17:50:19 +03:00
luaL_verror("unexpected end of file in %s",zname(Z));
1998-01-14 18:49:01 +03:00
}
1998-12-15 17:59:43 +03:00
static int ezgetc (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
if (c==EOZ) unexpectedEOZ(Z);
return c;
}
1998-12-15 17:59:43 +03:00
static void ezread (ZIO* Z, void* b, int n)
1998-01-14 18:49:01 +03:00
{
int r=zread(Z,b,n);
if (r!=0) unexpectedEOZ(Z);
}
1998-12-15 17:59:43 +03:00
static unsigned int LoadWord (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1998-03-26 17:50:19 +03:00
unsigned int hi=ezgetc(Z);
unsigned int lo=ezgetc(Z);
1998-01-14 18:49:01 +03:00
return (hi<<8)|lo;
}
1998-12-15 17:59:43 +03:00
static unsigned long LoadLong (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1998-03-26 17:50:19 +03:00
unsigned long hi=LoadWord(Z);
unsigned long lo=LoadWord(Z);
return (hi<<16)|lo;
1998-01-14 18:49:01 +03:00
}
/*
* convert number from text
*/
real luaU_str2d (const char* b, const char* where)
{
real x;
if (!luaO_str2d(b, &x))
luaL_verror("cannot convert number '%s' in %s",b,where);
return x;
}
static real LoadNumber (ZIO* Z, int native)
1998-01-14 18:49:01 +03:00
{
1999-05-17 18:12:01 +04:00
real x;
if (native)
{
LoadBlock(&x,sizeof(x),Z);
return x;
}
else
{
char b[256];
int size=ezgetc(Z);
LoadBlock(b,size,Z);
b[size]=0;
return luaU_str2d(b,zname(Z));
}
1998-01-14 18:49:01 +03:00
}
1999-08-17 00:52:00 +04:00
static int LoadInt (ZIO* Z, const char* message)
1999-03-31 00:29:34 +04:00
{
unsigned long l=LoadLong(Z);
unsigned int i=l;
if (i!=l) luaL_verror(message,l,zname(Z));
return i;
}
#define PAD 5 /* two word operands plus opcode */
1998-12-15 17:59:43 +03:00
static Byte* LoadCode (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-03-31 00:29:34 +04:00
int size=LoadInt(Z,"code too long (%ld bytes) in %s");
Byte* b=luaM_malloc(size+PAD);
1998-03-26 17:50:19 +03:00
LoadBlock(b,size,Z);
1999-03-31 00:29:34 +04:00
if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
memset(b+size,ENDCODE,PAD); /* pad code for safety */
1998-03-26 17:50:19 +03:00
return b;
1998-01-14 18:49:01 +03:00
}
1998-12-15 17:59:43 +03:00
static TaggedString* LoadTString (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-03-31 00:29:34 +04:00
long size=LoadLong(Z);
1998-03-26 17:50:19 +03:00
if (size==0)
return NULL;
1998-01-14 18:49:01 +03:00
else
{
1998-03-26 17:50:19 +03:00
char* s=luaL_openspace(size);
LoadBlock(s,size,Z);
return luaS_newlstr(s,size-1);
1998-01-14 18:49:01 +03:00
}
}
1998-12-15 17:59:43 +03:00
static void LoadLocals (TProtoFunc* tf, ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-03-31 00:29:34 +04:00
int i,n=LoadInt(Z,"too many locals (%ld) in %s");
1998-01-14 18:49:01 +03:00
if (n==0) return;
tf->locvars=luaM_newvector(n+1,LocVar);
for (i=0; i<n; i++)
{
1999-03-31 00:29:34 +04:00
tf->locvars[i].line=LoadInt(Z,"too many lines (%ld) in %s");
1998-01-14 18:49:01 +03:00
tf->locvars[i].varname=LoadTString(Z);
}
tf->locvars[i].line=-1; /* flag end of vector */
tf->locvars[i].varname=NULL;
}
static TProtoFunc* LoadFunction (ZIO* Z, int native);
1998-03-26 17:50:19 +03:00
static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native)
1998-01-14 18:49:01 +03:00
{
1999-03-31 00:29:34 +04:00
int i,n=LoadInt(Z,"too many constants (%ld) in %s");
1998-01-14 18:49:01 +03:00
tf->nconsts=n;
if (n==0) return;
tf->consts=luaM_newvector(n,TObject);
for (i=0; i<n; i++)
{
TObject* o=tf->consts+i;
1999-03-31 00:29:34 +04:00
ttype(o)=-ezgetc(Z); /* ttype(o) is negative - ORDER LUA_T */
switch (ttype(o))
1998-01-14 18:49:01 +03:00
{
1999-03-31 00:29:34 +04:00
case LUA_T_NUMBER:
nvalue(o)=LoadNumber(Z,native);
1998-01-14 18:49:01 +03:00
break;
1999-03-31 00:29:34 +04:00
case LUA_T_STRING:
1998-03-26 17:50:19 +03:00
tsvalue(o)=LoadTString(Z);
1998-01-14 18:49:01 +03:00
break;
1999-03-31 00:29:34 +04:00
case LUA_T_PROTO:
tfvalue(o)=LoadFunction(Z,native);
1998-01-14 18:49:01 +03:00
break;
1999-03-31 00:29:34 +04:00
case LUA_T_NIL:
break;
default: /* cannot happen */
luaU_badconstant("load",i,o,tf);
1998-01-14 18:49:01 +03:00
break;
}
}
}
static TProtoFunc* LoadFunction (ZIO* Z, int native)
1998-01-14 18:49:01 +03:00
{
TProtoFunc* tf=luaF_newproto();
1999-03-31 00:29:34 +04:00
tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
tf->source=LoadTString(Z);
1999-03-31 00:29:34 +04:00
if (tf->source==NULL) tf->source=luaS_new(zname(Z));
1998-03-26 17:50:19 +03:00
tf->code=LoadCode(Z);
1998-01-14 18:49:01 +03:00
LoadLocals(tf,Z);
LoadConstants(tf,Z,native);
1998-01-14 18:49:01 +03:00
return tf;
}
1998-12-15 17:59:43 +03:00
static void LoadSignature (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
1999-08-17 00:52:00 +04:00
const char* s=SIGNATURE;
1998-01-14 18:49:01 +03:00
while (*s!=0 && ezgetc(Z)==*s)
++s;
1998-03-26 17:50:19 +03:00
if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
1998-01-14 18:49:01 +03:00
}
static int LoadHeader (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int version,sizeofR;
int native;
1998-01-14 18:49:01 +03:00
LoadSignature(Z);
version=ezgetc(Z);
if (version>VERSION)
luaL_verror(
1998-03-26 17:50:19 +03:00
"%s too new: version=0x%02x; expected at most 0x%02x",
zname(Z),version,VERSION);
1998-06-25 20:48:44 +04:00
if (version<VERSION0) /* check last major change */
1998-01-14 18:49:01 +03:00
luaL_verror(
1998-03-26 17:50:19 +03:00
"%s too old: version=0x%02x; expected at least 0x%02x",
1998-06-25 20:48:44 +04:00
zname(Z),version,VERSION0);
sizeofR=ezgetc(Z);
native=(sizeofR!=0);
if (native) /* test number representation */
{
if (sizeofR!=sizeof(real))
luaL_verror("unknown number size in %s: read %d; expected %d",
zname(Z),sizeofR,sizeof(real));
else
{
real tf=TEST_NUMBER;
real f=LoadNumber(Z,native);
if ((long)f!=(long)tf)
luaL_verror("unknown number format in %s: "
"read " NUMBER_FMT "; expected " NUMBER_FMT,
zname(Z),f,tf);
}
}
return native;
1998-01-14 18:49:01 +03:00
}
1998-12-15 17:59:43 +03:00
static TProtoFunc* LoadChunk (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
return LoadFunction(Z,LoadHeader(Z));
1998-01-14 18:49:01 +03:00
}
/*
** load one chunk from a file or buffer
** return main if ok and NULL at EOF
*/
1999-04-07 19:37:02 +04:00
TProtoFunc* luaU_undump1 (ZIO* Z)
1998-01-14 18:49:01 +03:00
{
int c=zgetc(Z);
if (c==ID_CHUNK)
1998-03-26 17:50:19 +03:00
return LoadChunk(Z);
1998-01-14 18:49:01 +03:00
else if (c!=EOZ)
1998-03-26 17:50:19 +03:00
luaL_verror("%s is not a Lua binary file",zname(Z));
1998-01-14 18:49:01 +03:00
return NULL;
}
1999-03-31 00:29:34 +04:00
/*
* handle constants that cannot happen
*/
1999-08-17 00:52:00 +04:00
void luaU_badconstant (const char* s, int i, const TObject* o, TProtoFunc* tf)
1999-03-31 00:29:34 +04:00
{
int t=ttype(o);
1999-08-17 00:52:00 +04:00
const char* name= (t>0 || t<LUA_T_LINE) ? "?" : luaO_typenames[-t];
1999-03-31 00:29:34 +04:00
luaL_verror("cannot %s constant #%d: type=%d [%s]" IN,s,i,t,name,INLOC);
}