1998-01-14 18:49:01 +03:00
|
|
|
/*
|
1999-12-23 21:19:57 +03:00
|
|
|
** $Id: lundump.c,v 1.16 1999/12/02 19:11:51 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>
|
1999-12-02 22:11:51 +03:00
|
|
|
|
|
|
|
#define LUA_REENTRANT
|
|
|
|
|
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"
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
1998-03-26 17:50:19 +03:00
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,"unexpected end of file in %.255s",zname(Z));
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static int ezgetc (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
|
|
|
int c=zgetc(Z);
|
1999-12-02 22:11:51 +03:00
|
|
|
if (c==EOZ) unexpectedEOZ(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static void ezread (lua_State* L, ZIO* Z, void* b, int n)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
|
|
|
int r=zread(Z,b,n);
|
1999-12-02 22:11:51 +03:00
|
|
|
if (r!=0) unexpectedEOZ(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static unsigned int LoadWord (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
unsigned int hi=ezgetc(L,Z);
|
|
|
|
unsigned int lo=ezgetc(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
return (hi<<8)|lo;
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static unsigned long LoadLong (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
unsigned long hi=LoadWord(L,Z);
|
|
|
|
unsigned long lo=LoadWord(L,Z);
|
1998-03-26 17:50:19 +03:00
|
|
|
return (hi<<16)|lo;
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static real LoadNumber (lua_State* L, ZIO* Z, int native)
|
1999-07-08 16:43:23 +04:00
|
|
|
{
|
|
|
|
if (native)
|
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
real x;
|
|
|
|
LoadBlock(L,&x,sizeof(x),Z);
|
1999-07-08 16:43:23 +04:00
|
|
|
return x;
|
|
|
|
}
|
1999-04-14 00:16:37 +04:00
|
|
|
else
|
1999-07-08 16:43:23 +04:00
|
|
|
{
|
|
|
|
char b[256];
|
1999-12-02 22:11:51 +03:00
|
|
|
int size=ezgetc(L,Z);
|
|
|
|
LoadBlock(L,b,size,Z);
|
1999-07-08 16:43:23 +04:00
|
|
|
b[size]=0;
|
1999-12-02 22:11:51 +03:00
|
|
|
return luaU_str2d(L,b,zname(Z));
|
1999-07-08 16:43:23 +04:00
|
|
|
}
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
1999-03-31 00:29:34 +04:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
unsigned long l=LoadLong(L,Z);
|
1999-03-31 00:29:34 +04:00
|
|
|
unsigned int i=l;
|
1999-12-02 22:11:51 +03:00
|
|
|
if (i!=l) luaL_verror(L,message,l,zname(Z));
|
1999-03-31 00:29:34 +04:00
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
#define PAD 5 /* two word operands plus opcode */
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static Byte* LoadCode (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
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 %.255s",zname(Z));
|
1999-04-14 00:16:37 +04:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static TaggedString* LoadTString (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
long size=LoadLong(L,Z);
|
1998-03-26 17:50:19 +03:00
|
|
|
if (size==0)
|
|
|
|
return NULL;
|
1998-01-14 18:49:01 +03:00
|
|
|
else
|
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
char* s=luaL_openspace(L,size);
|
|
|
|
LoadBlock(L,s,size,Z);
|
|
|
|
return luaS_newlstr(L,s,size-1);
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static void LoadLocals (lua_State* L, TProtoFunc* tf, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
int i,n=LoadInt(L,Z,"too many locals (%lu) in %.255s");
|
1998-01-14 18:49:01 +03:00
|
|
|
if (n==0) return;
|
1999-12-02 22:11:51 +03:00
|
|
|
tf->locvars=luaM_newvector(L,n+1,LocVar);
|
1998-01-14 18:49:01 +03:00
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
tf->locvars[i].line=LoadInt(L,Z,"too many lines (%lu) in %.255s");
|
|
|
|
tf->locvars[i].varname=LoadTString(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
tf->locvars[i].line=-1; /* flag end of vector */
|
|
|
|
tf->locvars[i].varname=NULL;
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static TProtoFunc* LoadFunction (lua_State* L, ZIO* Z, int native);
|
1998-03-26 17:50:19 +03:00
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static void LoadConstants (lua_State* L, TProtoFunc* tf, ZIO* Z, int native)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
int i,n=LoadInt(L,Z,"too many constants (%lu) in %.255s");
|
1998-01-14 18:49:01 +03:00
|
|
|
tf->nconsts=n;
|
|
|
|
if (n==0) return;
|
1999-12-02 22:11:51 +03:00
|
|
|
tf->consts=luaM_newvector(L,n,TObject);
|
1998-01-14 18:49:01 +03:00
|
|
|
for (i=0; i<n; i++)
|
|
|
|
{
|
|
|
|
TObject* o=tf->consts+i;
|
1999-12-02 22:11:51 +03:00
|
|
|
ttype(o)=-ezgetc(L,Z); /* ttype(o) is negative - ORDER LUA_T */
|
1999-03-31 00:29:34 +04:00
|
|
|
switch (ttype(o))
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-03-31 00:29:34 +04:00
|
|
|
case LUA_T_NUMBER:
|
1999-12-02 22:11:51 +03:00
|
|
|
nvalue(o)=LoadNumber(L,Z,native);
|
1998-01-14 18:49:01 +03:00
|
|
|
break;
|
1999-03-31 00:29:34 +04:00
|
|
|
case LUA_T_STRING:
|
1999-12-02 22:11:51 +03:00
|
|
|
tsvalue(o)=LoadTString(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
break;
|
1999-12-23 21:19:57 +03:00
|
|
|
case LUA_T_LPROTO:
|
1999-12-02 22:11:51 +03:00
|
|
|
tfvalue(o)=LoadFunction(L,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 */
|
1999-12-02 22:11:51 +03:00
|
|
|
luaU_badconstant(L,"load",i,o,tf);
|
1998-01-14 18:49:01 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static TProtoFunc* LoadFunction (lua_State* L, ZIO* Z, int native)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-11-22 16:12:07 +03:00
|
|
|
TProtoFunc* tf=luaF_newproto(L);
|
1999-12-02 22:11:51 +03:00
|
|
|
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);
|
|
|
|
LoadLocals(L,tf,Z);
|
|
|
|
LoadConstants(L,tf,Z,native);
|
1998-01-14 18:49:01 +03:00
|
|
|
return tf;
|
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static void LoadSignature (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-08-17 00:52:00 +04:00
|
|
|
const char* s=SIGNATURE;
|
1999-12-02 22:11:51 +03:00
|
|
|
while (*s!=0 && ezgetc(L,Z)==*s)
|
1998-01-14 18:49:01 +03:00
|
|
|
++s;
|
1999-12-02 22:11:51 +03:00
|
|
|
if (*s!=0) luaL_verror(L,"bad signature in %.255s",zname(Z));
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static int LoadHeader (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
int version,sizeofR,native;
|
|
|
|
LoadSignature(L,Z);
|
|
|
|
version=ezgetc(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
if (version>VERSION)
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,
|
|
|
|
"%.255s too new: version=0x%02x; expected at most 0x%02x",
|
1998-03-26 17:50:19 +03:00
|
|
|
zname(Z),version,VERSION);
|
1998-06-25 20:48:44 +04:00
|
|
|
if (version<VERSION0) /* check last major change */
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,
|
|
|
|
"%.255s too old: version=0x%02x; expected at least 0x%02x",
|
1998-06-25 20:48:44 +04:00
|
|
|
zname(Z),version,VERSION0);
|
1999-12-02 22:11:51 +03:00
|
|
|
sizeofR=ezgetc(L,Z);
|
1999-07-08 16:43:23 +04:00
|
|
|
native=(sizeofR!=0);
|
|
|
|
if (native) /* test number representation */
|
1999-04-14 00:16:37 +04:00
|
|
|
{
|
1999-07-08 16:43:23 +04:00
|
|
|
if (sizeofR!=sizeof(real))
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,"unknown number size in %.255s: read %d; expected %d",
|
|
|
|
zname(Z),sizeofR,(int)sizeof(real));
|
1999-07-08 16:43:23 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
real tf=TEST_NUMBER;
|
1999-12-02 22:11:51 +03:00
|
|
|
real f=LoadNumber(L,Z,native);
|
1999-07-08 16:43:23 +04:00
|
|
|
if ((long)f!=(long)tf)
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,"unknown number format in %.255s: "
|
1999-07-08 16:43:23 +04:00
|
|
|
"read " NUMBER_FMT "; expected " NUMBER_FMT,
|
|
|
|
zname(Z),f,tf);
|
|
|
|
}
|
1999-04-14 00:16:37 +04:00
|
|
|
}
|
1999-07-08 16:43:23 +04:00
|
|
|
return native;
|
1998-01-14 18:49:01 +03:00
|
|
|
}
|
|
|
|
|
1999-12-02 22:11:51 +03:00
|
|
|
static TProtoFunc* LoadChunk (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
1999-12-02 22:11:51 +03:00
|
|
|
return LoadFunction(L,Z,LoadHeader(L,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-12-02 22:11:51 +03:00
|
|
|
TProtoFunc* luaU_undump1 (lua_State* L, ZIO* Z)
|
1998-01-14 18:49:01 +03:00
|
|
|
{
|
|
|
|
int c=zgetc(Z);
|
|
|
|
if (c==ID_CHUNK)
|
1999-12-02 22:11:51 +03:00
|
|
|
return LoadChunk(L,Z);
|
1998-01-14 18:49:01 +03:00
|
|
|
else if (c!=EOZ)
|
1999-12-02 22:11:51 +03:00
|
|
|
luaL_verror(L,"%.255s 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-12-02 22:11:51 +03:00
|
|
|
void luaU_badconstant (lua_State* L, const char* s, int i, const TObject* o, const 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-12-02 22:11:51 +03:00
|
|
|
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;
|
1999-03-31 00:29:34 +04:00
|
|
|
}
|