summaryrefslogtreecommitdiff
path: root/lundump.c
diff options
context:
space:
mode:
Diffstat (limited to 'lundump.c')
-rw-r--r--lundump.c93
1 files changed, 50 insertions, 43 deletions
diff --git a/lundump.c b/lundump.c
index 8136e2e5..0c3b5fd7 100644
--- a/lundump.c
+++ b/lundump.c
@@ -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
50static real LoadNumber (ZIO* Z) 50/*
51* convert number from text
52*/
53double 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
61static 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
68static int LoadInt (ZIO* Z, char* message) 79static 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
115static TProtoFunc* LoadFunction (ZIO* Z); 126static TProtoFunc* LoadFunction (ZIO* Z, int native);
116 127
117static void LoadConstants (TProtoFunc* tf, ZIO* Z) 128static 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
147static TProtoFunc* LoadFunction (ZIO* Z) 158static 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
167static void LoadHeader (ZIO* Z) 178static 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
204static TProtoFunc* LoadChunk (ZIO* Z) 212static 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/*