new version by lhf

This commit is contained in:
Roberto Ierusalimschy 1998-06-18 13:52:04 -03:00
parent 0789451458
commit 112c9d53ab
2 changed files with 32 additions and 27 deletions

View File

@ -1,10 +1,11 @@
/* /*
** $Id: lundump.c,v 1.7 1998/03/05 15:45:08 lhf Exp lhf $ ** $Id: lundump.c,v 1.9 1998/06/13 16:54:15 lhf Exp $
** load bytecodes from files ** load bytecodes from files
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
#include <stdio.h> #include <stdio.h>
#include "lauxlib.h" #include "lauxlib.h"
#include "lfunc.h" #include "lfunc.h"
#include "lmem.h" #include "lmem.h"
@ -15,11 +16,7 @@
#define LoadNative(t,D) LoadBlock(&t,sizeof(t),D) #define LoadNative(t,D) LoadBlock(&t,sizeof(t),D)
/* LUA_NUMBER */ /* LUA_NUMBER */
/* if you change the definition of real, make sure you set ID_NUMBER /* see comment in lundump.h */
* accordingly lundump.h, specially if sizeof(long)!=4.
* for types other than the ones listed below, you'll have to write your own
* dump and undump routines.
*/
#if ID_NUMBER==ID_REAL4 #if ID_NUMBER==ID_REAL4
#define LoadNumber LoadFloat #define LoadNumber LoadFloat
@ -65,20 +62,23 @@ static unsigned long LoadLong(ZIO* Z)
return (hi<<16)|lo; return (hi<<16)|lo;
} }
#if ID_NUMBER==ID_REAL4
/* LUA_NUMBER */ /* LUA_NUMBER */
/* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */ /* assumes sizeof(long)==4 and sizeof(float)==4 (IEEE) */
static float LoadFloat(ZIO* Z) static float LoadFloat(ZIO* Z)
{ {
long l=LoadLong(Z); unsigned long l=LoadLong(Z);
float f=*(float*)&l; float f=*(float*)&l;
return f; return f;
} }
#endif
#if ID_NUMBER==ID_REAL8
/* LUA_NUMBER */ /* LUA_NUMBER */
/* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */ /* assumes sizeof(long)==4 and sizeof(double)==8 (IEEE) */
static double LoadDouble(ZIO* Z) static double LoadDouble(ZIO* Z)
{ {
long l[2]; unsigned long l[2];
double f; double f;
int x=1; int x=1;
if (*(char*)&x==1) /* little-endian */ if (*(char*)&x==1) /* little-endian */
@ -94,10 +94,11 @@ static double LoadDouble(ZIO* Z)
f=*(double*)l; f=*(double*)l;
return f; return f;
} }
#endif
static Byte* LoadCode(ZIO* Z) static Byte* LoadCode(ZIO* Z)
{ {
long size=LoadLong(Z); unsigned long size=LoadLong(Z);
unsigned int s=size; unsigned int s=size;
void* b; void* b;
if (s!=size) luaL_verror("code too long (%ld bytes) in %s",size,zname(Z)); if (s!=size) luaL_verror("code too long (%ld bytes) in %s",size,zname(Z));
@ -214,7 +215,7 @@ static void LoadHeader(ZIO* Z)
f=LoadNumber(Z); f=LoadNumber(Z);
if (f!=tf) if (f!=tf)
luaL_verror("unknown number representation in %s: " luaL_verror("unknown number representation in %s: "
"read %g; expected %g", /* LUA_NUMBER */ "read " NUMBER_FMT "; expected " NUMBER_FMT "", /* LUA_NUMBER */
zname(Z),(double)f,(double)tf); zname(Z),(double)f,(double)tf);
} }

View File

@ -1,5 +1,5 @@
/* /*
** $Id: lundump.h,v 1.5 1998/02/06 20:05:39 lhf Exp lhf $ ** $Id: lundump.h,v 1.6 1998/06/13 16:54:15 lhf Exp $
** load pre-compiled Lua chunks ** load pre-compiled Lua chunks
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -22,31 +22,35 @@ TProtoFunc* luaU_undump1(ZIO* Z); /* load one chunk */
#define ID_STR 'S' #define ID_STR 'S'
#define ID_FUN 'F' #define ID_FUN 'F'
#define ID_INT4 'l' /* number representation */
#define ID_REAL4 'f' #define ID_INT4 'l' /* 4-byte integers */
#define ID_REAL8 'd' #define ID_REAL4 'f' /* 4-byte reals */
#define ID_NATIVE '?' #define ID_REAL8 'd' /* 8-byte reals */
#define ID_NATIVE '?' /* whatever your machine uses */
/* /*
* use a multiple of PI for testing number representation. * use a multiple of PI for testing number representation.
* multiplying by 10E8 gives notrivial integer values. * multiplying by 1E8 gives notrivial integer values.
*/ */
#define TEST_NUMBER 3.14159265358979323846E8 #define TEST_NUMBER 3.14159265358979323846E8
/* LUA_NUMBER */ /* LUA_NUMBER
/* if you change the definition of real, make sure you set ID_NUMBER * choose one below for the number representation in precompiled chunks.
* accordingly, specially if sizeof(long)!=4. * the default is ID_REAL8 because the default for LUA_NUM_TYPE is double.
* if your machine does not use IEEE 754, use ID_NATIVE.
* the next version will support conversion to/from IEEE 754.
*
* if you change LUA_NUM_TYPE, make sure you set ID_NUMBER accordingly,
* specially if sizeof(long)!=4.
* for types other than the ones listed below, you'll have to write your own * for types other than the ones listed below, you'll have to write your own
* dump and undump routines. * dump and undump routines.
*/ */
#if real==float
#define ID_NUMBER ID_REAL4
#elif real==double
#define ID_NUMBER ID_REAL8 #define ID_NUMBER ID_REAL8
#elif real==long
#if 0
#define ID_NUMBER ID_REAL4
#define ID_NUMBER ID_INT4 #define ID_NUMBER ID_INT4
#else
#define ID_NUMBER ID_NATIVE #define ID_NUMBER ID_NATIVE
#endif #endif