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