summaryrefslogtreecommitdiff
path: root/apps/plugins/lua/lundump.c
diff options
context:
space:
mode:
authorRichard Quirk <richard.quirk@gmail.com>2014-03-19 19:31:31 +0100
committerMarcin Bukat <marcin.bukat@gmail.com>2014-04-02 20:31:54 +0200
commit36378988ad4059982742f05f5eb50580b456840a (patch)
treeee70be810d894566c5e351f21a1ebb79be742a85 /apps/plugins/lua/lundump.c
parent020f16a1c7d5bc9a302671cef03f56ac735e20c5 (diff)
downloadrockbox-36378988ad4059982742f05f5eb50580b456840a.tar.gz
rockbox-36378988ad4059982742f05f5eb50580b456840a.zip
Update lua plugin to 5.2.3
Prior to this patch the Lua plugin used version 5.1.4. This change reduces the number of modifications in the Lua source using some new defines and because the upstream source is now more flexible. Unless otherwise stated, l*.[ch] files are taken unmodified from the upstream lua-5.2.3. fscanf.c: file descriptors in rockbox are just ints, they are hidden behind a void* now so liolib requires less modifications. fscanf is updated to use void* too. getc.c: this is a new file required for getc implementation in lauxlib.c lauxlib.c: LoadF replaced FILE* with int, the rockbox file descriptor int are cast to FILE* (actually void* due to typedef). getc uses the PREFIX version. stdin is not used, as per 5.1.4. lbaselib.c: now uses strspn in the number parsing. print uses DEBUGF now rather than being commented out. lbitlib.c: use the built-in version from 5.2.3 rather than Reuben Thomas's external library. Backwards compatible and adds some new bit operations. ldo.c: the LUAI_THROW/TRY defines are now in the core lua code, so have been removed from rockconf.h liolib.c: here the implementation has changed to use the LStream from the original source, and cast the FILE* pointers to int. This has reduced the number of modifications from the upstream version. llex.c: the only change from upstream is to remove the locale include. lmathlib.c: updated from the 5.2.3 version and re-applied the changes that were made vs 5.1.4 for random numbers and to remove unsupported float functions. loadlib.c: upstream version, with the 5.1.4 changes for missing functions. lobject.c: upstream version, with ctype.h added and sprintf changed to snprintf. loslib.c: upstream version with locale.h removed and 5.1.4 changes for unsupportable functions. lstrlib.c: sprintf changed to snprintf. ltable.c: upstream with the hashnum function from 5.1.4 to avoid frexp in luai_hashnum. luaconf.h: updated to 5.2.3 version, restored relevant parts from the original 5.1.4 configuration. The COMPAT defines that are no longer available are not included. lundump.c: VERSION macro conflicts with the core Rockbox equivalent. rocklib.c: luaL_reg is no longer available, replaced by luaL_Reg equivalent. Moved checkboolean/optboolean functions to this file and out of core lua files. luaL_getn is no longer available, replaced by luaL_rawlen. luaL_register is deprecated, use the newlib/setfuncs replacements. rli_init has to be called before setting up the newlib to avoid overwriting the rb table. rocklib_aux.pl: use rli_checkboolean from rocklib.c. rocklua.c: new default bits library used, update the library loading code with idiomatic 5.2 code. strcspn.c: no longer needed, but strspn.c is required for strspn in lbaselib.c Change-Id: I0c7945c755f79083afe98ec117e1e8cf13de2651 Reviewed-on: http://gerrit.rockbox.org/774 Tested: Richard Quirk <richard.quirk@gmail.com> Reviewed-by: Marcin Bukat <marcin.bukat@gmail.com>
Diffstat (limited to 'apps/plugins/lua/lundump.c')
-rw-r--r--apps/plugins/lua/lundump.c142
1 files changed, 87 insertions, 55 deletions
diff --git a/apps/plugins/lua/lundump.c b/apps/plugins/lua/lundump.c
index 8010a45795..0db70d20ea 100644
--- a/apps/plugins/lua/lundump.c
+++ b/apps/plugins/lua/lundump.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lundump.c,v 2.7.1.4 2008/04/04 19:51:41 roberto Exp $ 2** $Id: lundump.c,v 2.22.1.1 2013/04/12 18:48:47 roberto Exp $
3** load precompiled Lua chunks 3** load precompiled Lua chunks
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -27,28 +27,24 @@ typedef struct {
27 const char* name; 27 const char* name;
28} LoadState; 28} LoadState;
29 29
30#ifdef LUAC_TRUST_BINARIES 30static l_noret error(LoadState* S, const char* why)
31#define IF(c,s)
32#define error(S,s)
33#else
34#define IF(c,s) if (c) error(S,s)
35
36static void error(LoadState* S, const char* why)
37{ 31{
38 luaO_pushfstring(S->L,"%s: %s in precompiled chunk",S->name,why); 32 luaO_pushfstring(S->L,"%s: %s precompiled chunk",S->name,why);
39 luaD_throw(S->L,LUA_ERRSYNTAX); 33 luaD_throw(S->L,LUA_ERRSYNTAX);
40} 34}
41#endif
42 35
43#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size)) 36#define LoadMem(S,b,n,size) LoadBlock(S,b,(n)*(size))
44#define LoadByte(S) (lu_byte)LoadChar(S) 37#define LoadByte(S) (lu_byte)LoadChar(S)
45#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x)) 38#define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
46#define LoadVector(S,b,n,size) LoadMem(S,b,n,size) 39#define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
47 40
41#if !defined(luai_verifycode)
42#define luai_verifycode(L,b,f) /* empty */
43#endif
44
48static void LoadBlock(LoadState* S, void* b, size_t size) 45static void LoadBlock(LoadState* S, void* b, size_t size)
49{ 46{
50 size_t r=luaZ_read(S->Z,b,size); 47 if (luaZ_read(S->Z,b,size)!=0) error(S,"truncated");
51 IF (r!=0, "unexpected end");
52} 48}
53 49
54static int LoadChar(LoadState* S) 50static int LoadChar(LoadState* S)
@@ -62,7 +58,7 @@ static int LoadInt(LoadState* S)
62{ 58{
63 int x; 59 int x;
64 LoadVar(S,x); 60 LoadVar(S,x);
65 IF (x<0, "bad integer"); 61 if (x<0) error(S,"corrupted");
66 return x; 62 return x;
67} 63}
68 64
@@ -82,7 +78,7 @@ static TString* LoadString(LoadState* S)
82 else 78 else
83 { 79 {
84 char* s=luaZ_openspace(S->L,S->b,size); 80 char* s=luaZ_openspace(S->L,S->b,size);
85 LoadBlock(S,s,size); 81 LoadBlock(S,s,size*sizeof(char));
86 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */ 82 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
87 } 83 }
88} 84}
@@ -95,7 +91,7 @@ static void LoadCode(LoadState* S, Proto* f)
95 LoadVector(S,f->code,n,sizeof(Instruction)); 91 LoadVector(S,f->code,n,sizeof(Instruction));
96} 92}
97 93
98static Proto* LoadFunction(LoadState* S, TString* p); 94static void LoadFunction(LoadState* S, Proto* f);
99 95
100static void LoadConstants(LoadState* S, Proto* f) 96static void LoadConstants(LoadState* S, Proto* f)
101{ 97{
@@ -111,10 +107,10 @@ static void LoadConstants(LoadState* S, Proto* f)
111 switch (t) 107 switch (t)
112 { 108 {
113 case LUA_TNIL: 109 case LUA_TNIL:
114 setnilvalue(o); 110 setnilvalue(o);
115 break; 111 break;
116 case LUA_TBOOLEAN: 112 case LUA_TBOOLEAN:
117 setbvalue(o,LoadChar(S)!=0); 113 setbvalue(o,LoadChar(S));
118 break; 114 break;
119 case LUA_TNUMBER: 115 case LUA_TNUMBER:
120 setnvalue(o,LoadNumber(S)); 116 setnvalue(o,LoadNumber(S));
@@ -122,21 +118,38 @@ static void LoadConstants(LoadState* S, Proto* f)
122 case LUA_TSTRING: 118 case LUA_TSTRING:
123 setsvalue2n(S->L,o,LoadString(S)); 119 setsvalue2n(S->L,o,LoadString(S));
124 break; 120 break;
125 default: 121 default: lua_assert(0);
126 error(S,"bad constant");
127 break;
128 } 122 }
129 } 123 }
130 n=LoadInt(S); 124 n=LoadInt(S);
131 f->p=luaM_newvector(S->L,n,Proto*); 125 f->p=luaM_newvector(S->L,n,Proto*);
132 f->sizep=n; 126 f->sizep=n;
133 for (i=0; i<n; i++) f->p[i]=NULL; 127 for (i=0; i<n; i++) f->p[i]=NULL;
134 for (i=0; i<n; i++) f->p[i]=LoadFunction(S,f->source); 128 for (i=0; i<n; i++)
129 {
130 f->p[i]=luaF_newproto(S->L);
131 LoadFunction(S,f->p[i]);
132 }
133}
134
135static void LoadUpvalues(LoadState* S, Proto* f)
136{
137 int i,n;
138 n=LoadInt(S);
139 f->upvalues=luaM_newvector(S->L,n,Upvaldesc);
140 f->sizeupvalues=n;
141 for (i=0; i<n; i++) f->upvalues[i].name=NULL;
142 for (i=0; i<n; i++)
143 {
144 f->upvalues[i].instack=LoadByte(S);
145 f->upvalues[i].idx=LoadByte(S);
146 }
135} 147}
136 148
137static void LoadDebug(LoadState* S, Proto* f) 149static void LoadDebug(LoadState* S, Proto* f)
138{ 150{
139 int i,n; 151 int i,n;
152 f->source=LoadString(S);
140 n=LoadInt(S); 153 n=LoadInt(S);
141 f->lineinfo=luaM_newvector(S->L,n,int); 154 f->lineinfo=luaM_newvector(S->L,n,int);
142 f->sizelineinfo=n; 155 f->sizelineinfo=n;
@@ -152,49 +165,48 @@ static void LoadDebug(LoadState* S, Proto* f)
152 f->locvars[i].endpc=LoadInt(S); 165 f->locvars[i].endpc=LoadInt(S);
153 } 166 }
154 n=LoadInt(S); 167 n=LoadInt(S);
155 f->upvalues=luaM_newvector(S->L,n,TString*); 168 for (i=0; i<n; i++) f->upvalues[i].name=LoadString(S);
156 f->sizeupvalues=n;
157 for (i=0; i<n; i++) f->upvalues[i]=NULL;
158 for (i=0; i<n; i++) f->upvalues[i]=LoadString(S);
159} 169}
160 170
161static Proto* LoadFunction(LoadState* S, TString* p) 171static void LoadFunction(LoadState* S, Proto* f)
162{ 172{
163 Proto* f;
164 if (++S->L->nCcalls > LUAI_MAXCCALLS) error(S,"code too deep");
165 f=luaF_newproto(S->L);
166 setptvalue2s(S->L,S->L->top,f); incr_top(S->L);
167 f->source=LoadString(S); if (f->source==NULL) f->source=p;
168 f->linedefined=LoadInt(S); 173 f->linedefined=LoadInt(S);
169 f->lastlinedefined=LoadInt(S); 174 f->lastlinedefined=LoadInt(S);
170 f->nups=LoadByte(S);
171 f->numparams=LoadByte(S); 175 f->numparams=LoadByte(S);
172 f->is_vararg=LoadByte(S); 176 f->is_vararg=LoadByte(S);
173 f->maxstacksize=LoadByte(S); 177 f->maxstacksize=LoadByte(S);
174 LoadCode(S,f); 178 LoadCode(S,f);
175 LoadConstants(S,f); 179 LoadConstants(S,f);
180 LoadUpvalues(S,f);
176 LoadDebug(S,f); 181 LoadDebug(S,f);
177 IF (!luaG_checkcode(f), "bad code");
178 S->L->top--;
179 S->L->nCcalls--;
180 return f;
181} 182}
182 183
184/* the code below must be consistent with the code in luaU_header */
185#define N0 LUAC_HEADERSIZE
186#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
187#define N2 N1+2
188#define N3 N2+6
189
183static void LoadHeader(LoadState* S) 190static void LoadHeader(LoadState* S)
184{ 191{
185 char h[LUAC_HEADERSIZE]; 192 lu_byte h[LUAC_HEADERSIZE];
186 char s[LUAC_HEADERSIZE]; 193 lu_byte s[LUAC_HEADERSIZE];
187 luaU_header(h); 194 luaU_header(h);
188 LoadBlock(S,s,LUAC_HEADERSIZE); 195 memcpy(s,h,sizeof(char)); /* first char already read */
189 IF (memcmp(h,s,LUAC_HEADERSIZE)!=0, "bad header"); 196 LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
197 if (memcmp(h,s,N0)==0) return;
198 if (memcmp(h,s,N1)!=0) error(S,"not a");
199 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
200 if (memcmp(h,s,N3)!=0) error(S,"incompatible"); else error(S,"corrupted");
190} 201}
191 202
192/* 203/*
193** load precompiled chunk 204** load precompiled chunk
194*/ 205*/
195Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) 206Closure* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
196{ 207{
197 LoadState S; 208 LoadState S;
209 Closure* cl;
198 if (*name=='@' || *name=='=') 210 if (*name=='@' || *name=='=')
199 S.name=name+1; 211 S.name=name+1;
200 else if (*name==LUA_SIGNATURE[0]) 212 else if (*name==LUA_SIGNATURE[0])
@@ -205,23 +217,43 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name)
205 S.Z=Z; 217 S.Z=Z;
206 S.b=buff; 218 S.b=buff;
207 LoadHeader(&S); 219 LoadHeader(&S);
208 return LoadFunction(&S,luaS_newliteral(L,"=?")); 220 cl=luaF_newLclosure(L,1);
221 setclLvalue(L,L->top,cl); incr_top(L);
222 cl->l.p=luaF_newproto(L);
223 LoadFunction(&S,cl->l.p);
224 if (cl->l.p->sizeupvalues != 1)
225 {
226 Proto* p=cl->l.p;
227 cl=luaF_newLclosure(L,cl->l.p->sizeupvalues);
228 cl->l.p=p;
229 setclLvalue(L,L->top-1,cl);
230 }
231 luai_verifycode(L,buff,cl->l.p);
232 return cl;
209} 233}
210 234
235#define MYINT(s) (s[0]-'0')
236#undef VERSION
237#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
238#define FORMAT 0 /* this is the official format */
239
211/* 240/*
212* make header 241* make header for precompiled chunks
242* if you change the code below be sure to update LoadHeader and FORMAT above
243* and LUAC_HEADERSIZE in lundump.h
213*/ 244*/
214void luaU_header (char* h) 245void luaU_header (lu_byte* h)
215{ 246{
216 int x=1; 247 int x=1;
217 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1); 248 memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
218 h+=sizeof(LUA_SIGNATURE)-1; 249 h+=sizeof(LUA_SIGNATURE)-sizeof(char);
219 *h++=(char)LUAC_VERSION; 250 *h++=cast_byte(VERSION);
220 *h++=(char)LUAC_FORMAT; 251 *h++=cast_byte(FORMAT);
221 *h++=(char)*(char*)&x; /* endianness */ 252 *h++=cast_byte(*(char*)&x); /* endianness */
222 *h++=(char)sizeof(int); 253 *h++=cast_byte(sizeof(int));
223 *h++=(char)sizeof(size_t); 254 *h++=cast_byte(sizeof(size_t));
224 *h++=(char)sizeof(Instruction); 255 *h++=cast_byte(sizeof(Instruction));
225 *h++=(char)sizeof(lua_Number); 256 *h++=cast_byte(sizeof(lua_Number));
226 *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */ 257 *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
258 memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
227} 259}