mirror of https://github.com/rusefi/lua.git
final version (by lhf)
This commit is contained in:
parent
2ae9c856cf
commit
0e8855e171
133
lundump.c
133
lundump.c
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lundump.c,v 1.18 2000/03/03 14:58:26 roberto Exp roberto $
|
** $Id: lundump.c,v 1.28 2000/04/24 19:32:58 lhf Exp $
|
||||||
** load bytecodes from files
|
** load bytecodes from files
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -17,10 +17,17 @@
|
||||||
#include "lundump.h"
|
#include "lundump.h"
|
||||||
|
|
||||||
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
#define LoadBlock(L,b,size,Z) ezread(L,Z,b,size)
|
||||||
|
#define LoadByte ezgetc
|
||||||
|
|
||||||
|
static const char* ZNAME(ZIO* Z)
|
||||||
|
{
|
||||||
|
const char* s=zname(Z);
|
||||||
|
return (*s=='@') ? s+1 : s;
|
||||||
|
}
|
||||||
|
|
||||||
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
static void unexpectedEOZ (lua_State* L, ZIO* Z)
|
||||||
{
|
{
|
||||||
luaL_verror(L,"unexpected end of file in %.255s",zname(Z));
|
luaL_verror(L,"unexpected end of file in `%.255s'",ZNAME(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int ezgetc (lua_State* L, ZIO* Z)
|
static int ezgetc (lua_State* L, ZIO* Z)
|
||||||
|
@ -56,25 +63,18 @@ static Number LoadNumber (lua_State* L, ZIO* Z)
|
||||||
int size=ezgetc(L,Z);
|
int size=ezgetc(L,Z);
|
||||||
LoadBlock(L,b,size,Z);
|
LoadBlock(L,b,size,Z);
|
||||||
b[size]=0;
|
b[size]=0;
|
||||||
return luaU_str2d(L,b,zname(Z));
|
return luaU_str2d(L,b,ZNAME(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
static int LoadInt (lua_State* L, ZIO* Z, const char* message)
|
||||||
{
|
{
|
||||||
unsigned long l=LoadLong(L,Z);
|
unsigned long l=LoadLong(L,Z);
|
||||||
unsigned int i=l;
|
unsigned int i=l;
|
||||||
if (i!=l) luaL_verror(L,message,l,zname(Z));
|
if (i!=l) luaL_verror(L,"%s in `%.255s';\n"
|
||||||
|
" read %lu; expected %u",message,ZNAME(Z),l,-1);
|
||||||
return i;
|
return i;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z)
|
|
||||||
{
|
|
||||||
int size=LoadInt(L,Z,"code too long (%lu bytes) in %.255s");
|
|
||||||
tf->code=luaM_newvector(L,size,Instruction);
|
|
||||||
LoadBlock(L,tf->code,size*sizeof(tf->code[0]),Z);
|
|
||||||
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in %.255s",zname(Z));
|
|
||||||
}
|
|
||||||
|
|
||||||
static TString* LoadString (lua_State* L, ZIO* Z)
|
static TString* LoadString (lua_State* L, ZIO* Z)
|
||||||
{
|
{
|
||||||
long size=LoadLong(L,Z);
|
long size=LoadLong(L,Z);
|
||||||
|
@ -88,14 +88,55 @@ static TString* LoadString (lua_State* L, ZIO* Z)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void SwapCode (lua_State* L, Instruction* code, int size, ZIO* Z)
|
||||||
|
{
|
||||||
|
unsigned char* p;
|
||||||
|
int c;
|
||||||
|
if (sizeof(Instruction)==4)
|
||||||
|
while (size--)
|
||||||
|
{
|
||||||
|
p=(unsigned char *) code++;
|
||||||
|
c=p[0]; p[0]=p[3]; p[3]=c;
|
||||||
|
c=p[1]; p[1]=p[2]; p[2]=c;
|
||||||
|
}
|
||||||
|
else if (sizeof(Instruction)==2)
|
||||||
|
while (size--)
|
||||||
|
{
|
||||||
|
p=(unsigned char *) code++;
|
||||||
|
c=p[0]; p[0]=p[1]; p[1]=c;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
luaL_verror(L,"cannot swap code with %d-byte instructions in `%.255s'",
|
||||||
|
(int)sizeof(Instruction),ZNAME(Z));
|
||||||
|
}
|
||||||
|
|
||||||
|
static void LoadCode (lua_State* L, Proto* tf, ZIO* Z)
|
||||||
|
{
|
||||||
|
int size=LoadInt(L,Z,"code too long");
|
||||||
|
Instruction t=0,tt=TEST_CODE;
|
||||||
|
tf->code=luaM_newvector(L,size,Instruction);
|
||||||
|
LoadBlock(L,tf->code,size*sizeof(*tf->code),Z);
|
||||||
|
if (tf->code[size-1]!=OP_END) luaL_verror(L,"bad code in `%.255s'",ZNAME(Z));
|
||||||
|
LoadBlock(L,&t,sizeof(t),Z);
|
||||||
|
if (t!=tt)
|
||||||
|
{
|
||||||
|
Instruction ot=t;
|
||||||
|
SwapCode(L,&t,1,Z);
|
||||||
|
if (t!=tt) luaL_verror(L,"cannot swap code in `%.255s';\n"
|
||||||
|
" read 0x%08X; swapped to 0x%08X; expected 0x%08X",
|
||||||
|
ZNAME(Z),(unsigned long)ot,(unsigned long)t,(unsigned long)tt);
|
||||||
|
SwapCode(L,tf->code,size,Z);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
|
static void LoadLocals (lua_State* L, Proto* tf, ZIO* Z)
|
||||||
{
|
{
|
||||||
int i,n=LoadInt(L,Z,"too many locals (%lu) in %.255s");
|
int i,n=LoadInt(L,Z,"too many locals");
|
||||||
if (n==0) return;
|
if (n==0) return;
|
||||||
tf->locvars=luaM_newvector(L,n+1,LocVar);
|
tf->locvars=luaM_newvector(L,n+1,LocVar);
|
||||||
for (i=0; i<n; i++)
|
for (i=0; i<n; i++)
|
||||||
{
|
{
|
||||||
tf->locvars[i].line=LoadInt(L,Z,"too many lines (%lu) in %.255s");
|
tf->locvars[i].line=LoadInt(L,Z,"too many lines");
|
||||||
tf->locvars[i].varname=LoadString(L,Z);
|
tf->locvars[i].varname=LoadString(L,Z);
|
||||||
}
|
}
|
||||||
tf->locvars[i].line=-1; /* flag end of vector */
|
tf->locvars[i].line=-1; /* flag end of vector */
|
||||||
|
@ -107,22 +148,28 @@ static Proto* LoadFunction (lua_State* L, ZIO* Z, int native);
|
||||||
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
||||||
{
|
{
|
||||||
int i,n;
|
int i,n;
|
||||||
tf->nkstr=n=LoadInt(L,Z,"too many strings (%lu) in %.255s");
|
tf->nkstr=n=LoadInt(L,Z,"too many strings");
|
||||||
if (n>0)
|
if (n>0)
|
||||||
{
|
{
|
||||||
tf->kstr=luaM_newvector(L,n,TString*);
|
tf->kstr=luaM_newvector(L,n,TString*);
|
||||||
for (i=0; i<n; i++) tf->kstr[i]=LoadString(L,Z);
|
for (i=0; i<n; i++)
|
||||||
|
{
|
||||||
|
TString* s=LoadString(L,Z);
|
||||||
|
int isglobal=LoadByte(L,Z);
|
||||||
|
if (isglobal) luaS_assertglobal(L,s);
|
||||||
|
tf->kstr[i]=s;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
tf->nknum=n=LoadInt(L,Z,"too many numbers (%lu) in %.255s");
|
tf->nknum=n=LoadInt(L,Z,"too many numbers");
|
||||||
if (n>0)
|
if (n>0)
|
||||||
{
|
{
|
||||||
tf->knum=luaM_newvector(L,n,Number);
|
tf->knum=luaM_newvector(L,n,Number);
|
||||||
if (native)
|
if (native)
|
||||||
LoadBlock(L,tf->knum,n*sizeof(tf->knum[0]),Z);
|
LoadBlock(L,tf->knum,n*sizeof(*tf->knum),Z);
|
||||||
else
|
else
|
||||||
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
|
for (i=0; i<n; i++) tf->knum[i]=LoadNumber(L,Z);
|
||||||
}
|
}
|
||||||
tf->nkproto=n=LoadInt(L,Z,"too many functions (%lu) in %.255s");
|
tf->nkproto=n=LoadInt(L,Z,"too many functions");
|
||||||
if (n>0)
|
if (n>0)
|
||||||
{
|
{
|
||||||
tf->kproto=luaM_newvector(L,n,Proto*);
|
tf->kproto=luaM_newvector(L,n,Proto*);
|
||||||
|
@ -133,12 +180,12 @@ static void LoadConstants (lua_State* L, Proto* tf, ZIO* Z, int native)
|
||||||
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
|
static Proto* LoadFunction (lua_State* L, ZIO* Z, int native)
|
||||||
{
|
{
|
||||||
Proto* tf=luaF_newproto(L);
|
Proto* tf=luaF_newproto(L);
|
||||||
tf->lineDefined=LoadInt(L,Z,"lineDefined too large (%lu) in %.255s");
|
|
||||||
tf->source=LoadString(L,Z);
|
tf->source=LoadString(L,Z);
|
||||||
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
|
if (tf->source==NULL) tf->source=luaS_new(L,zname(Z));
|
||||||
tf->numparams=LoadInt(L,Z,"numparams too large (%lu) in %.255s");
|
tf->lineDefined=LoadInt(L,Z,"lineDefined too large");
|
||||||
tf->is_vararg=LoadInt(L,Z,"is_vararg too large (%lu) in %.255s");
|
tf->numparams=LoadInt(L,Z,"too many parameters");
|
||||||
tf->maxstacksize=LoadInt(L,Z,"maxstacksize too large (%lu) in %.255s");
|
tf->is_vararg=LoadByte(L,Z);
|
||||||
|
tf->maxstacksize=LoadInt(L,Z,"too much stack needed");
|
||||||
LoadCode(L,tf,Z);
|
LoadCode(L,tf,Z);
|
||||||
LoadLocals(L,tf,Z);
|
LoadLocals(L,tf,Z);
|
||||||
LoadConstants(L,tf,Z,native);
|
LoadConstants(L,tf,Z,native);
|
||||||
|
@ -150,7 +197,7 @@ static void LoadSignature (lua_State* L, ZIO* Z)
|
||||||
const char* s=SIGNATURE;
|
const char* s=SIGNATURE;
|
||||||
while (*s!=0 && ezgetc(L,Z)==*s)
|
while (*s!=0 && ezgetc(L,Z)==*s)
|
||||||
++s;
|
++s;
|
||||||
if (*s!=0) luaL_verror(L,"bad signature in %.255s",zname(Z));
|
if (*s!=0) luaL_verror(L,"bad signature in `%.255s'",ZNAME(Z));
|
||||||
}
|
}
|
||||||
|
|
||||||
#define V(v) v/16,v%16
|
#define V(v) v/16,v%16
|
||||||
|
@ -161,28 +208,40 @@ static int LoadHeader (lua_State* L, ZIO* Z)
|
||||||
LoadSignature(L,Z);
|
LoadSignature(L,Z);
|
||||||
version=ezgetc(L,Z);
|
version=ezgetc(L,Z);
|
||||||
if (version>VERSION)
|
if (version>VERSION)
|
||||||
luaL_verror(L,
|
luaL_verror(L,"`%.255s' too new:\n"
|
||||||
"%.255s too new: its version is %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 */
|
||||||
luaL_verror(L,
|
luaL_verror(L,"`%.255s' too old:\n"
|
||||||
"%.255s too old: its version is %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(VERSION));
|
||||||
|
{
|
||||||
|
int I=ezgetc(L,Z);
|
||||||
|
int i=ezgetc(L,Z);
|
||||||
|
int op=ezgetc(L,Z);
|
||||||
|
int b=ezgetc(L,Z);
|
||||||
|
if (I!=sizeof(Instruction) || i!=SIZE_INSTRUCTION || op!=SIZE_OP || b!=SIZE_B)
|
||||||
|
luaL_verror(L,"virtual machine mismatch in `%.255s':\n"
|
||||||
|
" read %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments;\n"
|
||||||
|
" expected %d-bit,%d-byte instructions, %d-bit opcodes, %d-bit b-arguments",
|
||||||
|
ZNAME(Z),i,I,op,b,SIZE_INSTRUCTION,(int)sizeof(Instruction),SIZE_OP,SIZE_B);
|
||||||
|
}
|
||||||
sizeofR=ezgetc(L,Z);
|
sizeofR=ezgetc(L,Z);
|
||||||
native=(sizeofR!=0);
|
native=(sizeofR!=0);
|
||||||
if (native) /* test number representation */
|
if (native) /* test number representation */
|
||||||
{
|
{
|
||||||
if (sizeofR!=sizeof(Number))
|
if (sizeofR!=sizeof(Number))
|
||||||
luaL_verror(L,"unknown number size in %.255s: read %d; expected %d",
|
luaL_verror(L,"native number size mismatch in `%.255s':\n"
|
||||||
zname(Z),sizeofR,(int)sizeof(Number));
|
" read %d; expected %d",
|
||||||
|
ZNAME(Z),sizeofR,(int)sizeof(Number));
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
Number f=0,tf=TEST_NUMBER;
|
Number f=0,tf=TEST_NUMBER;
|
||||||
LoadBlock(L,&f,sizeof(f),Z);
|
LoadBlock(L,&f,sizeof(f),Z);
|
||||||
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
if ((long)f!=(long)tf) /* disregard errors in last bit of fraction */
|
||||||
luaL_verror(L,"unknown number format in %.255s: "
|
luaL_verror(L,"unknown number format in `%.255s':\n"
|
||||||
"read " NUMBER_FMT "; expected " NUMBER_FMT,
|
" read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||||
zname(Z),f,tf);
|
ZNAME(Z),f,tf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return native;
|
return native;
|
||||||
|
@ -203,7 +262,7 @@ Proto* luaU_undump1 (lua_State* L, ZIO* Z)
|
||||||
if (c==ID_CHUNK)
|
if (c==ID_CHUNK)
|
||||||
return LoadChunk(L,Z);
|
return LoadChunk(L,Z);
|
||||||
else if (c!=EOZ)
|
else if (c!=EOZ)
|
||||||
luaL_verror(L,"%.255s is not a Lua binary file",zname(Z));
|
luaL_verror(L,"`%.255s' is not a Lua binary file",ZNAME(Z));
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -214,6 +273,6 @@ double luaU_str2d (lua_State* L, const char* b, const char* where)
|
||||||
{
|
{
|
||||||
double x;
|
double x;
|
||||||
if (!luaO_str2d(b,&x))
|
if (!luaO_str2d(b,&x))
|
||||||
luaL_verror(L,"cannot convert number '%.255s' in %.255s",b,where);
|
luaL_verror(L,"cannot convert number `%.255s' in `%.255s'",b,where);
|
||||||
return x;
|
return x;
|
||||||
}
|
}
|
||||||
|
|
13
lundump.h
13
lundump.h
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
** $Id: lundump.h,v 1.13 2000/03/03 14:58:26 roberto Exp roberto $
|
** $Id: lundump.h,v 1.19 2000/04/24 17:32:29 lhf Exp $
|
||||||
** load pre-compiled Lua chunks
|
** load pre-compiled Lua chunks
|
||||||
** See Copyright Notice in lua.h
|
** See Copyright Notice in lua.h
|
||||||
*/
|
*/
|
||||||
|
@ -13,16 +13,12 @@
|
||||||
/* load one chunk */
|
/* load one chunk */
|
||||||
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
Proto* luaU_undump1 (lua_State* L, ZIO* Z);
|
||||||
|
|
||||||
/* handle cases that cannot happen */
|
|
||||||
void luaU_badconstant (lua_State* L, const char* s, int i,
|
|
||||||
const TObject* o, const Proto* tf);
|
|
||||||
|
|
||||||
/* convert number from text */
|
/* convert number from text */
|
||||||
double luaU_str2d (lua_State* L, const char* b, const char* where);
|
double luaU_str2d (lua_State* L, const char* b, const char* where);
|
||||||
|
|
||||||
/* definitions for headers of binary files */
|
/* definitions for headers of binary files */
|
||||||
#define VERSION 0x33 /* last format change was in 3.3 */
|
#define VERSION 0x40 /* last format change was in 4.0 */
|
||||||
#define VERSION0 0x33 /* last major change was in 3.3 */
|
#define VERSION0 0x40 /* last major change was in 4.0 */
|
||||||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||||
|
|
||||||
|
@ -41,4 +37,7 @@ double luaU_str2d (lua_State* L, const char* b, const char* where);
|
||||||
/* 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
|
||||||
|
|
||||||
|
/* something for testing byte order in instructions */
|
||||||
|
#define TEST_CODE 0x01020304
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Reference in New Issue