mirror of https://github.com/rusefi/lua.git
numbers are stored in ascii format for better portability and simplicity
This commit is contained in:
parent
cb7f027380
commit
b4ad600b93
101
lundump.c
101
lundump.c
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lundump.c,v 1.8 1999/03/30 20:29:34 roberto Exp roberto $
|
||||
** $Id: lundump.c,v 1.18 1999/04/09 03:10:40 lhf Exp lhf $
|
||||
** load bytecodes from files
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -14,12 +14,11 @@
|
|||
#include "lundump.h"
|
||||
|
||||
#define LoadBlock(b,size,Z) ezread(Z,b,size)
|
||||
#define LoadNative(t,Z) LoadBlock(&t,sizeof(t),Z)
|
||||
|
||||
#if ID_NUMBER==ID_NATIVE
|
||||
#define doLoadNumber(f,Z) LoadNative(f,Z)
|
||||
#if LUAC_NATIVE
|
||||
#define doLoadNumber(x,Z) LoadBlock(&x,sizeof(x),Z)
|
||||
#else
|
||||
#define doLoadNumber(f,Z) f=LoadNumber(Z)
|
||||
#define doLoadNumber(x,Z) x=LoadNumber(Z)
|
||||
#endif
|
||||
|
||||
static void unexpectedEOZ (ZIO* Z)
|
||||
|
@ -54,38 +53,17 @@ static unsigned long LoadLong (ZIO* Z)
|
|||
return (hi<<16)|lo;
|
||||
}
|
||||
|
||||
#if ID_NUMBER==ID_REAL4 /* LUA_NUMBER */
|
||||
/* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */
|
||||
static float LoadFloat (ZIO* Z)
|
||||
static real LoadNumber (ZIO* Z)
|
||||
{
|
||||
unsigned long l=LoadLong(Z);
|
||||
float f;
|
||||
memcpy(&f,&l,sizeof(f));
|
||||
return f;
|
||||
char b[256];
|
||||
int size=ezgetc(Z);
|
||||
LoadBlock(b,size,Z);
|
||||
b[size]=0;
|
||||
if (b[0]=='-')
|
||||
return -luaO_str2d(b+1);
|
||||
else
|
||||
return luaO_str2d(b);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if ID_NUMBER==ID_REAL8 /* LUA_NUMBER */
|
||||
/* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */
|
||||
static double LoadDouble (ZIO* Z)
|
||||
{
|
||||
unsigned long l[2];
|
||||
double f;
|
||||
int x=1;
|
||||
if (*(char*)&x==1) /* little-endian */
|
||||
{
|
||||
l[1]=LoadLong(Z);
|
||||
l[0]=LoadLong(Z);
|
||||
}
|
||||
else /* big-endian */
|
||||
{
|
||||
l[0]=LoadLong(Z);
|
||||
l[1]=LoadLong(Z);
|
||||
}
|
||||
memcpy(&f,l,sizeof(f));
|
||||
return f;
|
||||
}
|
||||
#endif
|
||||
|
||||
static int LoadInt (ZIO* Z, char* message)
|
||||
{
|
||||
|
@ -103,7 +81,7 @@ static Byte* LoadCode (ZIO* Z)
|
|||
Byte* b=luaM_malloc(size+PAD);
|
||||
LoadBlock(b,size,Z);
|
||||
if (b[size-1]!=ENDCODE) luaL_verror("bad code in %s",zname(Z));
|
||||
memset(b+size,ENDCODE,PAD); /* pad for safety */
|
||||
memset(b+size,ENDCODE,PAD); /* pad code for safety */
|
||||
return b;
|
||||
}
|
||||
|
||||
|
@ -188,9 +166,7 @@ static void LoadSignature (ZIO* Z)
|
|||
|
||||
static void LoadHeader (ZIO* Z)
|
||||
{
|
||||
int version,id,sizeofR;
|
||||
real f=-TEST_NUMBER,tf=TEST_NUMBER;
|
||||
luaU_testnumber();
|
||||
int version,sizeofR;
|
||||
LoadSignature(Z);
|
||||
version=ezgetc(Z);
|
||||
if (version>VERSION)
|
||||
|
@ -201,17 +177,30 @@ static void LoadHeader (ZIO* Z)
|
|||
luaL_verror(
|
||||
"%s too old: version=0x%02x; expected at least 0x%02x",
|
||||
zname(Z),version,VERSION0);
|
||||
id=ezgetc(Z); /* test number representation */
|
||||
sizeofR=ezgetc(Z);
|
||||
if (id!=ID_NUMBER || sizeofR!=sizeof(real))
|
||||
luaL_verror("unknown number signature in %s: "
|
||||
"read 0x%02x%02x; expected 0x%02x%02x",
|
||||
zname(Z),id,sizeofR,ID_NUMBER,sizeof(real));
|
||||
sizeofR=ezgetc(Z); /* test number representation */
|
||||
#if LUAC_NATIVE
|
||||
if (sizeofR==0)
|
||||
luaL_verror("cannot read numbers in %s: "
|
||||
"support for decimal format not enabled",
|
||||
zname(Z));
|
||||
if (sizeofR!=sizeof(real))
|
||||
luaL_verror("unknown number size in %s: read %d; expected %d",
|
||||
zname(Z),sizeofR,sizeof(real));
|
||||
else
|
||||
{
|
||||
real f=-TEST_NUMBER,tf=TEST_NUMBER;
|
||||
doLoadNumber(f,Z);
|
||||
if (f!=tf)
|
||||
luaL_verror("unknown number representation in %s: "
|
||||
"read " NUMBER_FMT "; expected " NUMBER_FMT,
|
||||
zname(Z),f,tf);
|
||||
}
|
||||
#else
|
||||
if (sizeofR!=0)
|
||||
luaL_verror("cannot read numbers in %s: "
|
||||
"support for native format not enabled",
|
||||
zname(Z));
|
||||
#endif
|
||||
}
|
||||
|
||||
static TProtoFunc* LoadChunk (ZIO* Z)
|
||||
|
@ -234,28 +223,6 @@ TProtoFunc* luaU_undump1 (ZIO* Z)
|
|||
return NULL;
|
||||
}
|
||||
|
||||
/*
|
||||
** test number representation
|
||||
*/
|
||||
void luaU_testnumber (void)
|
||||
{
|
||||
if (sizeof(real)!=SIZEOF_NUMBER)
|
||||
luaL_verror("numbers have %d bytes; expected %d. see lundump.h",
|
||||
(int)sizeof(real),SIZEOF_NUMBER);
|
||||
#if ID_NUMBER==ID_REAL4 || ID_NUMBER==ID_REAL8
|
||||
if (sizeof(long)!=4)
|
||||
luaL_verror("longs have %d bytes; expected %d. see lundump.h",
|
||||
(int)sizeof(long),4);
|
||||
#endif
|
||||
{
|
||||
real t=TEST_NUMBER;
|
||||
TYPEOF_NUMBER v=TEST_NUMBER;
|
||||
if (t!=v)
|
||||
luaL_verror("unsupported number type; expected %d-byte " NAMEOF_NUMBER "."
|
||||
" see config and lundump.h",SIZEOF_NUMBER);
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
* handle constants that cannot happen
|
||||
*/
|
||||
|
|
97
lundump.h
97
lundump.h
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
** $Id: lundump.h,v 1.6 1999/03/30 20:29:34 roberto Exp roberto $
|
||||
** $Id: lundump.h,v 1.13 1999/03/29 16:16:18 lhf Exp lhf $
|
||||
** load pre-compiled Lua chunks
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
|
@ -10,39 +10,7 @@
|
|||
#include "lobject.h"
|
||||
#include "lzio.h"
|
||||
|
||||
/* -- start of user configuration ------------------------------------------ */
|
||||
|
||||
/* LUA_NUMBER
|
||||
* choose below the number representation in precompiled chunks by choosing an
|
||||
* an adequate definition for ID_NUMBER.
|
||||
* if you change LUA_NUM_TYPE, you must set ID_NUMBER accordingly.
|
||||
* the default is ID_REAL8 because the default for LUA_NUM_TYPE is double.
|
||||
* if you want to use this default, do nothing.
|
||||
* if your machine does not use the IEEE 754 floating-point standard, use
|
||||
* ID_NATIVE, but precompiled chunks may not be portable to all architectures.
|
||||
*
|
||||
* for types other than the ones listed below, you'll have to write your own
|
||||
* dump and undump routines, specially if sizeof(long)!=4.
|
||||
*/
|
||||
|
||||
/* choose one definition for ID_NUMBER and move it to after #endif */
|
||||
#if 0
|
||||
#define ID_NUMBER ID_INT4 /* 4-byte integers */
|
||||
#define ID_NUMBER ID_REAL4 /* 4-byte reals */
|
||||
#define ID_NUMBER ID_REAL8 /* 8-byte reals */
|
||||
#define ID_NUMBER ID_NATIVE /* whatever your machine uses */
|
||||
#endif
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
|
||||
/* -- end of user configuration -- DO NOT CHANGE ANYTHING BELOW THIS LINE -- */
|
||||
|
||||
|
||||
TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */
|
||||
void luaU_testnumber (void); /* test number representation */
|
||||
void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
|
||||
/* handle cases that cannot happen */
|
||||
|
||||
|
@ -52,52 +20,29 @@ void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf);
|
|||
#define ID_CHUNK 27 /* binary files start with ESC... */
|
||||
#define SIGNATURE "Lua" /* ...followed by this signature */
|
||||
|
||||
/* number representation */
|
||||
#define ID_INT4 'l' /* 4-byte integers */
|
||||
#define ID_REAL4 'f' /* 4-byte reals */
|
||||
#define ID_REAL8 'd' /* 8-byte reals */
|
||||
#define ID_NATIVE '?' /* whatever your machine uses */
|
||||
|
||||
/* the default is ID_REAL8 because the default for LUA_NUM_TYPE is double */
|
||||
#ifndef ID_NUMBER
|
||||
#define ID_NUMBER ID_REAL8 /* 8-byte reals */
|
||||
#endif
|
||||
|
||||
/* a multiple of PI for testing number representation */
|
||||
/* multiplying by 1E8 gives non-trivial integer values */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
|
||||
#if ID_NUMBER==ID_REAL4
|
||||
#define DumpNumber DumpFloat
|
||||
#define LoadNumber LoadFloat
|
||||
#define SIZEOF_NUMBER 4
|
||||
#define TYPEOF_NUMBER float
|
||||
#define NAMEOF_NUMBER "float"
|
||||
#elif ID_NUMBER==ID_REAL8
|
||||
#define DumpNumber DumpDouble
|
||||
#define LoadNumber LoadDouble
|
||||
#define SIZEOF_NUMBER 8
|
||||
#define TYPEOF_NUMBER double
|
||||
#define NAMEOF_NUMBER "double"
|
||||
#elif ID_NUMBER==ID_INT4
|
||||
#define DumpNumber DumpLong
|
||||
#define LoadNumber LoadLong
|
||||
#define SIZEOF_NUMBER 4
|
||||
#define TYPEOF_NUMBER long
|
||||
#define NAMEOF_NUMBER "long"
|
||||
#elif ID_NUMBER==ID_NATIVE
|
||||
#define DumpNumber DumpNative
|
||||
#define LoadNumber LoadNative
|
||||
#define SIZEOF_NUMBER sizeof(real)
|
||||
#define TYPEOF_NUMBER real
|
||||
#define NAMEOF_NUMBER "native"
|
||||
#else
|
||||
#error bad ID_NUMBER
|
||||
#endif
|
||||
|
||||
/* formats for error messages */
|
||||
#define SOURCE "<%s:%d>"
|
||||
#define IN " in %p " SOURCE
|
||||
#define INLOC tf,tf->source->str,tf->lineDefined
|
||||
|
||||
/* format for numbers in listings and error messages */
|
||||
#ifndef NUMBER_FMT
|
||||
#define NUMBER_FMT "%.16g" /* LUA_NUMBER */
|
||||
#endif
|
||||
|
||||
/* LUA_NUMBER
|
||||
* 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 */
|
||||
#define TEST_NUMBER 3.14159265358979323846E8
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in New Issue