diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-07-08 09:43:23 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 1999-07-08 09:43:23 -0300 |
| commit | 16024861bd23ac9f837f956fbeec739878e5d895 (patch) | |
| tree | 05cac8d123c6cfc05eaa56b806f18177dba39304 | |
| parent | 3f43aaa23f9005a295056d0dce46a59565f1ef82 (diff) | |
| download | lua-3.2.tar.gz lua-3.2.tar.bz2 lua-3.2.zip | |
new format for numbers in precompiled code (as strings)v3.2
| -rw-r--r-- | lundump.c | 93 | ||||
| -rw-r--r-- | lundump.h | 17 |
2 files changed, 54 insertions, 56 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.c,v 1.19 1999/04/15 12:30:03 lhf Exp lhf $ | 2 | ** $Id: lundump.c,v 1.21 1999/07/02 19:34:26 lhf Exp $ |
| 3 | ** load bytecodes from files | 3 | ** load bytecodes from files |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -47,22 +47,33 @@ static unsigned long LoadLong (ZIO* Z) | |||
| 47 | return (hi<<16)|lo; | 47 | return (hi<<16)|lo; |
| 48 | } | 48 | } |
| 49 | 49 | ||
| 50 | static real LoadNumber (ZIO* Z) | 50 | /* |
| 51 | * convert number from text | ||
| 52 | */ | ||
| 53 | double luaU_str2d (char* b, char* where) | ||
| 54 | { | ||
| 55 | int negative=(b[0]=='-'); | ||
| 56 | double x=luaO_str2d(b+negative); | ||
| 57 | if (x<0) luaL_verror("cannot convert number '%s' in %s",b,where); | ||
| 58 | return negative ? -x : x; | ||
| 59 | } | ||
| 60 | |||
| 61 | static real LoadNumber (ZIO* Z, int native) | ||
| 51 | { | 62 | { |
| 52 | #ifdef LUAC_NATIVE | ||
| 53 | real x; | 63 | real x; |
| 54 | LoadBlock(&x,sizeof(x),Z); | 64 | if (native) |
| 55 | return x; | 65 | { |
| 56 | #else | 66 | LoadBlock(&x,sizeof(x),Z); |
| 57 | char b[256]; | 67 | return x; |
| 58 | int size=ezgetc(Z); | 68 | } |
| 59 | LoadBlock(b,size,Z); | ||
| 60 | b[size]=0; | ||
| 61 | if (b[0]=='-') | ||
| 62 | return -luaO_str2d(b+1); | ||
| 63 | else | 69 | else |
| 64 | return luaO_str2d(b); | 70 | { |
| 65 | #endif | 71 | char b[256]; |
| 72 | int size=ezgetc(Z); | ||
| 73 | LoadBlock(b,size,Z); | ||
| 74 | b[size]=0; | ||
| 75 | return luaU_str2d(b,zname(Z)); | ||
| 76 | } | ||
| 66 | } | 77 | } |
| 67 | 78 | ||
| 68 | static int LoadInt (ZIO* Z, char* message) | 79 | static int LoadInt (ZIO* Z, char* message) |
| @@ -112,9 +123,9 @@ static void LoadLocals (TProtoFunc* tf, ZIO* Z) | |||
| 112 | tf->locvars[i].varname=NULL; | 123 | tf->locvars[i].varname=NULL; |
| 113 | } | 124 | } |
| 114 | 125 | ||
| 115 | static TProtoFunc* LoadFunction (ZIO* Z); | 126 | static TProtoFunc* LoadFunction (ZIO* Z, int native); |
| 116 | 127 | ||
| 117 | static void LoadConstants (TProtoFunc* tf, ZIO* Z) | 128 | static void LoadConstants (TProtoFunc* tf, ZIO* Z, int native) |
| 118 | { | 129 | { |
| 119 | int i,n=LoadInt(Z,"too many constants (%ld) in %s"); | 130 | int i,n=LoadInt(Z,"too many constants (%ld) in %s"); |
| 120 | tf->nconsts=n; | 131 | tf->nconsts=n; |
| @@ -127,13 +138,13 @@ static void LoadConstants (TProtoFunc* tf, ZIO* Z) | |||
| 127 | switch (ttype(o)) | 138 | switch (ttype(o)) |
| 128 | { | 139 | { |
| 129 | case LUA_T_NUMBER: | 140 | case LUA_T_NUMBER: |
| 130 | nvalue(o)=LoadNumber(Z); | 141 | nvalue(o)=LoadNumber(Z,native); |
| 131 | break; | 142 | break; |
| 132 | case LUA_T_STRING: | 143 | case LUA_T_STRING: |
| 133 | tsvalue(o)=LoadTString(Z); | 144 | tsvalue(o)=LoadTString(Z); |
| 134 | break; | 145 | break; |
| 135 | case LUA_T_PROTO: | 146 | case LUA_T_PROTO: |
| 136 | tfvalue(o)=LoadFunction(Z); | 147 | tfvalue(o)=LoadFunction(Z,native); |
| 137 | break; | 148 | break; |
| 138 | case LUA_T_NIL: | 149 | case LUA_T_NIL: |
| 139 | break; | 150 | break; |
| @@ -144,7 +155,7 @@ static void LoadConstants (TProtoFunc* tf, ZIO* Z) | |||
| 144 | } | 155 | } |
| 145 | } | 156 | } |
| 146 | 157 | ||
| 147 | static TProtoFunc* LoadFunction (ZIO* Z) | 158 | static TProtoFunc* LoadFunction (ZIO* Z, int native) |
| 148 | { | 159 | { |
| 149 | TProtoFunc* tf=luaF_newproto(); | 160 | TProtoFunc* tf=luaF_newproto(); |
| 150 | tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s"); | 161 | tf->lineDefined=LoadInt(Z,"lineDefined too large (%ld) in %s"); |
| @@ -152,7 +163,7 @@ static TProtoFunc* LoadFunction (ZIO* Z) | |||
| 152 | if (tf->source==NULL) tf->source=luaS_new(zname(Z)); | 163 | if (tf->source==NULL) tf->source=luaS_new(zname(Z)); |
| 153 | tf->code=LoadCode(Z); | 164 | tf->code=LoadCode(Z); |
| 154 | LoadLocals(tf,Z); | 165 | LoadLocals(tf,Z); |
| 155 | LoadConstants(tf,Z); | 166 | LoadConstants(tf,Z,native); |
| 156 | return tf; | 167 | return tf; |
| 157 | } | 168 | } |
| 158 | 169 | ||
| @@ -164,9 +175,10 @@ static void LoadSignature (ZIO* Z) | |||
| 164 | if (*s!=0) luaL_verror("bad signature in %s",zname(Z)); | 175 | if (*s!=0) luaL_verror("bad signature in %s",zname(Z)); |
| 165 | } | 176 | } |
| 166 | 177 | ||
| 167 | static void LoadHeader (ZIO* Z) | 178 | static int LoadHeader (ZIO* Z) |
| 168 | { | 179 | { |
| 169 | int version,sizeofR; | 180 | int version,sizeofR; |
| 181 | int native; | ||
| 170 | LoadSignature(Z); | 182 | LoadSignature(Z); |
| 171 | version=ezgetc(Z); | 183 | version=ezgetc(Z); |
| 172 | if (version>VERSION) | 184 | if (version>VERSION) |
| @@ -177,34 +189,29 @@ static void LoadHeader (ZIO* Z) | |||
| 177 | luaL_verror( | 189 | luaL_verror( |
| 178 | "%s too old: version=0x%02x; expected at least 0x%02x", | 190 | "%s too old: version=0x%02x; expected at least 0x%02x", |
| 179 | zname(Z),version,VERSION0); | 191 | zname(Z),version,VERSION0); |
| 180 | sizeofR=ezgetc(Z); /* test number representation */ | 192 | sizeofR=ezgetc(Z); |
| 181 | #ifdef LUAC_NATIVE | 193 | native=(sizeofR!=0); |
| 182 | if (sizeofR==0) | 194 | if (native) /* test number representation */ |
| 183 | luaL_verror("cannot read numbers in %s: no support for decimal format", | ||
| 184 | zname(Z)); | ||
| 185 | if (sizeofR!=sizeof(real)) | ||
| 186 | luaL_verror("unknown number size in %s: read %d; expected %d", | ||
| 187 | zname(Z),sizeofR,sizeof(real)); | ||
| 188 | else | ||
| 189 | { | 195 | { |
| 190 | real f=-TEST_NUMBER,tf=TEST_NUMBER; | 196 | if (sizeofR!=sizeof(real)) |
| 191 | f=LoadNumber(Z); | 197 | luaL_verror("unknown number size in %s: read %d; expected %d", |
| 192 | if ((long)f!=(long)tf) | 198 | zname(Z),sizeofR,sizeof(real)); |
| 193 | luaL_verror("unknown number format in %s: " | 199 | else |
| 194 | "read " NUMBER_FMT "; expected " NUMBER_FMT, | 200 | { |
| 195 | zname(Z),f,tf); | 201 | real tf=TEST_NUMBER; |
| 202 | real f=LoadNumber(Z,native); | ||
| 203 | if ((long)f!=(long)tf) | ||
| 204 | luaL_verror("unknown number format in %s: " | ||
| 205 | "read " NUMBER_FMT "; expected " NUMBER_FMT, | ||
| 206 | zname(Z),f,tf); | ||
| 207 | } | ||
| 196 | } | 208 | } |
| 197 | #else | 209 | return native; |
| 198 | if (sizeofR!=0) | ||
| 199 | luaL_verror("cannot read numbers in %s: no support for native format", | ||
| 200 | zname(Z)); | ||
| 201 | #endif | ||
| 202 | } | 210 | } |
| 203 | 211 | ||
| 204 | static TProtoFunc* LoadChunk (ZIO* Z) | 212 | static TProtoFunc* LoadChunk (ZIO* Z) |
| 205 | { | 213 | { |
| 206 | LoadHeader(Z); | 214 | return LoadFunction(Z,LoadHeader(Z)); |
| 207 | return LoadFunction(Z); | ||
| 208 | } | 215 | } |
| 209 | 216 | ||
| 210 | /* | 217 | /* |
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: lundump.h,v 1.13 1999/03/29 16:16:18 lhf Exp lhf $ | 2 | ** $Id: lundump.h,v 1.15 1999/07/02 19:34:26 lhf Exp $ |
| 3 | ** load pre-compiled Lua chunks | 3 | ** load pre-compiled Lua chunks |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -13,6 +13,8 @@ | |||
| 13 | TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */ | 13 | TProtoFunc* luaU_undump1 (ZIO* Z); /* load one chunk */ |
| 14 | void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf); | 14 | void luaU_badconstant (char* s, int i, TObject* o, TProtoFunc* tf); |
| 15 | /* handle cases that cannot happen */ | 15 | /* handle cases that cannot happen */ |
| 16 | double luaU_str2d (char* b, char* where); | ||
| 17 | /* convert number from text */ | ||
| 16 | 18 | ||
| 17 | /* definitions for headers of binary files */ | 19 | /* definitions for headers of binary files */ |
| 18 | #define VERSION 0x32 /* last format change was in 3.2 */ | 20 | #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); | |||
| 30 | #define NUMBER_FMT "%.16g" /* LUA_NUMBER */ | 32 | #define NUMBER_FMT "%.16g" /* LUA_NUMBER */ |
| 31 | #endif | 33 | #endif |
| 32 | 34 | ||
| 33 | /* LUA_NUMBER | 35 | /* a multiple of PI for testing native format */ |
| 34 | * by default, numbers are stored in precompiled chunks as decimal strings. | ||
| 35 | * this is completely portable and fast enough for most applications. | ||
| 36 | * if you want to use this default, do nothing. | ||
| 37 | * if you want additional speed at the expense of portability, move the line | ||
| 38 | * below out of this comment. | ||
| 39 | #define LUAC_NATIVE | ||
| 40 | */ | ||
| 41 | |||
| 42 | #ifdef LUAC_NATIVE | ||
| 43 | /* a multiple of PI for testing number representation */ | ||
| 44 | /* multiplying by 1E8 gives non-trivial integer values */ | 36 | /* multiplying by 1E8 gives non-trivial integer values */ |
| 45 | #define TEST_NUMBER 3.14159265358979323846E8 | 37 | #define TEST_NUMBER 3.14159265358979323846E8 |
| 46 | #endif | ||
| 47 | 38 | ||
| 48 | #endif | 39 | #endif |
