1
0
mirror of https://github.com/lua/lua.git synced 2025-01-28 06:03:00 +08:00

new format for numbers in precompiled code (as strings)

This commit is contained in:
Roberto Ierusalimschy 1999-07-08 09:43:23 -03:00
parent 3f43aaa23f
commit 16024861bd
2 changed files with 54 additions and 56 deletions

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.c,v 1.19 1999/04/15 12:30:03 lhf Exp lhf $ ** $Id: lundump.c,v 1.21 1999/07/02 19:34:26 lhf Exp $
** load bytecodes from files ** load bytecodes from files
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -47,22 +47,33 @@ static unsigned long LoadLong (ZIO* Z)
return (hi<<16)|lo; return (hi<<16)|lo;
} }
static real LoadNumber (ZIO* Z) /*
* convert number from text
*/
double luaU_str2d (char* b, char* where)
{
int negative=(b[0]=='-');
double x=luaO_str2d(b+negative);
if (x<0) luaL_verror("cannot convert number '%s' in %s",b,where);
return negative ? -x : x;
}
static real LoadNumber (ZIO* Z, int native)
{ {
#ifdef LUAC_NATIVE
real x; real x;
if (native)
{
LoadBlock(&x,sizeof(x),Z); LoadBlock(&x,sizeof(x),Z);
return x; return x;
#else }
else
{
char b[256]; char b[256];
int size=ezgetc(Z); int size=ezgetc(Z);
LoadBlock(b,size,Z); LoadBlock(b,size,Z);
b[size]=0; b[size]=0;
if (b[0]=='-') return luaU_str2d(b,zname(Z));
return -luaO_str2d(b+1); }
else
return luaO_str2d(b);
#endif
} }
static int LoadInt (ZIO* Z, char* message) static int LoadInt (ZIO* Z, char* message)
@ -112,9 +123,9 @@ static void LoadLocals (TProtoFunc* tf, ZIO* Z)
tf->locvars[i].varname=NULL; tf->locvars[i].varname=NULL;
} }
static TProtoFunc* LoadFunction (ZIO* Z); static TProtoFunc* LoadFunction (ZIO* Z, int native);
static void LoadConstants (TProtoFunc* tf, ZIO* Z) static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native)
{ {
int i,n=LoadInt(Z,"too many constants (%ld) in %s"); int i,n=LoadInt(Z,"too many constants (%ld) in %s");
tf->nconsts=n; tf->nconsts=n;
@ -127,13 +138,13 @@ static void LoadConstants (TProtoFunc* tf, ZIO* Z)
switch (ttype(o)) switch (ttype(o))
{ {
case LUA_T_NUMBER: case LUA_T_NUMBER:
nvalue(o)=LoadNumber(Z); nvalue(o)=LoadNumber(Z,native);
break; break;
case LUA_T_STRING: case LUA_T_STRING:
tsvalue(o)=LoadTString(Z); tsvalue(o)=LoadTString(Z);
break; break;
case LUA_T_PROTO: case LUA_T_PROTO:
tfvalue(o)=LoadFunction(Z); tfvalue(o)=LoadFunction(Z,native);
break; break;
case LUA_T_NIL: case LUA_T_NIL:
break; break;
@ -144,7 +155,7 @@ static void LoadConstants (TProtoFunc* tf, ZIO* Z)
} }
} }
static TProtoFunc* LoadFunction (ZIO* Z) static TProtoFunc* LoadFunction (ZIO* Z, int native)
{ {
TProtoFunc* tf=luaF_newproto(); TProtoFunc* tf=luaF_newproto();
tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s"); tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s");
@ -152,7 +163,7 @@ static TProtoFunc* LoadFunction (ZIO* Z)
if (tf->source==NULL) tf->source=luaS_new(zname(Z)); if (tf->source==NULL) tf->source=luaS_new(zname(Z));
tf->code=LoadCode(Z); tf->code=LoadCode(Z);
LoadLocals(tf,Z); LoadLocals(tf,Z);
LoadConstants(tf,Z); LoadConstants(tf,Z,native);
return tf; return tf;
} }
@ -164,9 +175,10 @@ static void LoadSignature (ZIO* Z)
if (*s!=0) luaL_verror("bad signature in %s",zname(Z)); if (*s!=0) luaL_verror("bad signature in %s",zname(Z));
} }
static void LoadHeader (ZIO* Z) static int LoadHeader (ZIO* Z)
{ {
int version,sizeofR; int version,sizeofR;
int native;
LoadSignature(Z); LoadSignature(Z);
version=ezgetc(Z); version=ezgetc(Z);
if (version>VERSION) if (version>VERSION)
@ -177,34 +189,29 @@ static void LoadHeader (ZIO* Z)
luaL_verror( luaL_verror(
"%s too old: version=0x%02x; expected at least 0x%02x", "%s too old: version=0x%02x; expected at least 0x%02x",
zname(Z),version,VERSION0); zname(Z),version,VERSION0);
sizeofR=ezgetc(Z); /* test number representation */ sizeofR=ezgetc(Z);
#ifdef LUAC_NATIVE native=(sizeofR!=0);
if (sizeofR==0) if (native) /* test number representation */
luaL_verror("cannot read numbers in %s: no support for decimal format", {
zname(Z));
if (sizeofR!=sizeof(real)) if (sizeofR!=sizeof(real))
luaL_verror("unknown number size in %s: read %d; expected %d", luaL_verror("unknown number size in %s: read %d; expected %d",
zname(Z),sizeofR,sizeof(real)); zname(Z),sizeofR,sizeof(real));
else else
{ {
real f=-TEST_NUMBER,tf=TEST_NUMBER; real tf=TEST_NUMBER;
f=LoadNumber(Z); real f=LoadNumber(Z,native);
if ((long)f!=(long)tf) if ((long)f!=(long)tf)
luaL_verror("unknown number format in %s: " luaL_verror("unknown number format in %s: "
"read " NUMBER_FMT "; expected " NUMBER_FMT, "read " NUMBER_FMT "; expected " NUMBER_FMT,
zname(Z),f,tf); zname(Z),f,tf);
} }
#else }
if (sizeofR!=0) return native;
luaL_verror("cannot read numbers in %s: no support for native format",
zname(Z));
#endif
} }
static TProtoFunc* LoadChunk (ZIO* Z) static TProtoFunc* LoadChunk (ZIO* Z)
{ {
LoadHeader(Z); return LoadFunction(Z,LoadHeader(Z));
return LoadFunction(Z);
} }
/* /*

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.13 1999/03/29 16:16:18 lhf Exp lhf $ ** $Id: lundump.h,v 1.15 1999/07/02 19:34:26 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,6 +13,8 @@
TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */ TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf); void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
/* handle cases that cannot happen */ /* handle cases that cannot happen */
double luaU_str2d (char* b, char* where);
/* convert number from text */
/* definitions for headers of binary files */ /* definitions for headers of binary files */
#define VERSION 0x32 /* last format change was in 3.2 */ #define VERSION 0x32 /* last format change was in 3.2 */
@ -30,19 +32,8 @@ void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */ #define NUMBER_FMT "%.16g" /* LUA_NUMBER */
#endif #endif
/* LUA_NUMBER /* a multiple of PI for testing native format */
* by default, numbers are stored in precompiled chunks as decimal strings.
* this is completely portable and fast enough for most applications.
* if you want to use this default, do nothing.
* if you want additional speed at the expense of portability, move the line
* below out of this comment.
#define LUAC_NATIVE
*/
#ifdef LUAC_NATIVE
/* a multiple of PI for testing number representation */
/* 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
#endif
#endif #endif