History

Lua 5.2.0-beta-rc1

Lua Team committed 15 years ago

Commit 30837cc1 parent 9bce00553 files · +2418 −1456Browse files

Showing the changes to src (48 of 53 files). Show all

Unified · Split

48 files
  1. src/Makefile +2 −2
  2. src/lapi.c +88 −74
  3. src/lauxlib.c +77 −13
  4. src/lauxlib.h +5 −2
  5. src/lbaselib.c +76 −108
  6. src/lbitlib.c +67 −21
  7. src/lcode.c +42 −38
  8. src/lcode.h +3 −4
  9. src/ldblib.c +8 −19
  10. src/ldebug.c +74 −36
  11. src/ldebug.h +4 −1
  12. src/ldo.c +64 −66
  13. src/ldump.c +3 −3
  14. src/lgc.c +79 −58
  15. src/lgc.h +8 −9
  16. src/linit.c +5 −5
  17. src/liolib.c +34 −57
  18. src/llex.c +13 −17
  19. src/llex.h +7 −7
  20. src/llimits.h +15 −10
  21. src/lmem.c +5 −9
  22. src/loadlib.c +43 −30
  23. src/lobject.c +86 −38
  24. src/lobject.h +241 −87
  25. src/lopcodes.c +5 −5
  26. src/lopcodes.h +6 −7
  27. src/loslib.c +11 −20
  28. src/lparser.c +320 −145
  29. src/lparser.h +34 −11
  30. src/lstate.c +12 −8
  31. src/lstate.h +11 −7
  32. src/lstring.c +4 −3
  33. src/lstrlib.c +51 −22
  34. src/ltable.c +10 −8
  35. src/ltablib.c +4 −4
  36. src/ltm.c +4 −4
  37. src/ltm.h +2 −2
  38. src/lua.c +10 −11
  39. src/lua.h +4 −4
  40. src/luac.c +17 −12
  41. src/luaconf.h +57 −6
  42. src/lualib.h +2 −2
  43. src/lundump.c +30 −23
  44. src/lundump.h +7 −4
  45. src/lvm.c +97 −53
  46. src/lvm.h +7 −4
  47. src/lzio.c +12 −18
  48. src/lzio.h +2 −3
src/Makefile+2−2
@@ -126,7 +126,7 @@ lbaselib.o: lbaselib.c lua.h luaconf.h lauxlib.h lualib.h · 125 unmodified lines
126126 lbitlib.o: lbitlib.c lua.h luaconf.h lauxlib.h lualib.h
127127 lcode.o: lcode.c lua.h luaconf.h lcode.h llex.h lobject.h llimits.h \
128128 lzio.h lmem.h lopcodes.h lparser.h ldebug.h lstate.h ltm.h ldo.h lgc.h \
129− lstring.h ltable.h
129+ lstring.h ltable.h lvm.h
130130 lcorolib.o: lcorolib.c lua.h luaconf.h lauxlib.h lualib.h
131131 lctype.o: lctype.c lctype.h lua.h luaconf.h llimits.h
132132 ldblib.o: ldblib.c lua.h luaconf.h lauxlib.h lualib.h
@@ -164,7 +164,7 @@ lstring.o: lstring.c lua.h luaconf.h lmem.h llimits.h lobject.h lstate.h \ · 31 unmodified lines
164164 ltm.h lzio.h lstring.h lgc.h
165165 lstrlib.o: lstrlib.c lua.h luaconf.h lauxlib.h lualib.h
166166 ltable.o: ltable.c lua.h luaconf.h ldebug.h lstate.h lobject.h llimits.h \
167− ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h
167+ ltm.h lzio.h lmem.h ldo.h lgc.h lstring.h ltable.h lvm.h
168168 ltablib.o: ltablib.c lua.h luaconf.h lauxlib.h lualib.h
169169 ltm.o: ltm.c lua.h luaconf.h lobject.h llimits.h lstate.h ltm.h lzio.h \
170170 lmem.h lstring.h lgc.h ltable.h
src/lapi.c+88−74
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lapi.c,v 2.140 2010/11/03 15:16:17 roberto Exp $
2+** $Id: lapi.c,v 2.149 2011/06/13 14:13:06 roberto Exp $
33 ** Lua API
44 ** See Copyright Notice in lua.h
55 */
@@ -59,9 +59,9 @@ static TValue *index2addr (lua_State *L, int idx) { · 53 unmodified lines
5959 if (ttislcf(ci->func)) /* light C function? */
6060 return cast(TValue *, luaO_nilobject); /* it has no upvalues */
6161 else {
62− Closure *func = clvalue(ci->func);
63− return (idx <= func->c.nupvalues)
64− ? &func->c.upvalue[idx-1]
62+ CClosure *func = clCvalue(ci->func);
63+ return (idx <= func->nupvalues)
64+ ? &func->upvalue[idx-1]
6565 : cast(TValue *, luaO_nilobject);
6666 }
6767 }
@@ -195,10 +195,8 @@ static void moveto (lua_State *L, TValue *fr, int idx) { · 127 unmodified lines
195195 TValue *to = index2addr(L, idx);
196196 api_checkvalidindex(L, to);
197197 setobj(L, to, fr);
198− if (idx < LUA_REGISTRYINDEX) { /* function upvalue? */
199− lua_assert(ttisclosure(L->ci->func));
200− luaC_barrier(L, clvalue(L->ci->func), fr);
201− }
198+ if (idx < LUA_REGISTRYINDEX) /* function upvalue? */
199+ luaC_barrier(L, clCvalue(L->ci->func), fr);
202200 /* LUA_REGISTRYINDEX does not need gc barrier
203201 (collector revisits it before finishing collection) */
204202 }
@@ -251,7 +249,7 @@ LUA_API const char *lua_typename (lua_State *L, int t) { · 46 unmodified lines
251249
252250 LUA_API int lua_iscfunction (lua_State *L, int idx) {
253251 StkId o = index2addr(L, idx);
254− return (ttislcf(o) || (ttisclosure(o) && clvalue(o)->c.isC));
252+ return (ttislcf(o) || (ttisCclosure(o)));
255253 }
256254
257255
@@ -278,20 +276,28 @@ LUA_API int lua_rawequal (lua_State *L, int index1, int index2) { · 20 unmodified lines
278276 StkId o1 = index2addr(L, index1);
279277 StkId o2 = index2addr(L, index2);
280278 return (o1 == luaO_nilobject || o2 == luaO_nilobject) ? 0
281− : luaO_rawequalObj(o1, o2);
279+ : luaV_rawequalobj(o1, o2);
282280 }
283281
284282
285283 LUA_API void lua_arith (lua_State *L, int op) {
284+ StkId o1; /* 1st operand */
285+ StkId o2; /* 2nd operand */
286286 lua_lock(L);
287− api_checknelems(L, 2);
288− if (ttisnumber(L->top - 2) && ttisnumber(L->top - 1)) {
289− changenvalue(L->top - 2,
290− luaO_arith(op, nvalue(L->top - 2), nvalue(L->top - 1)));
287+ if (op != LUA_OPUNM) /* all other operations expect two operands */
288+ api_checknelems(L, 2);
289+ else { /* for unary minus, add fake 2nd operand */
290+ api_checknelems(L, 1);
291+ setobjs2s(L, L->top, L->top - 1);
292+ L->top++;
293+ }
294+ o1 = L->top - 2;
295+ o2 = L->top - 1;
296+ if (ttisnumber(o1) && ttisnumber(o2)) {
297+ changenvalue(o1, luaO_arith(op, nvalue(o1), nvalue(o2)));
291298 }
292299 else
293− luaV_arith(L, L->top - 2, L->top - 2, L->top - 1,
294− cast(TMS, op - LUA_OPADD + TM_ADD));
300+ luaV_arith(L, o1, o1, o2, cast(TMS, op - LUA_OPADD + TM_ADD));
295301 L->top--;
296302 lua_unlock(L);
297303 }
@@ -390,7 +396,7 @@ LUA_API const char *lua_tolstring (lua_State *L, int idx, size_t *len) { · 92 unmodified lines
390396
391397 LUA_API size_t lua_rawlen (lua_State *L, int idx) {
392398 StkId o = index2addr(L, idx);
393− switch (ttype(o)) {
399+ switch (ttypenv(o)) {
394400 case LUA_TSTRING: return tsvalue(o)->len;
395401 case LUA_TUSERDATA: return uvalue(o)->len;
396402 case LUA_TTABLE: return luaH_getn(hvalue(o));
@@ -402,15 +408,15 @@ LUA_API size_t lua_rawlen (lua_State *L, int idx) { · 5 unmodified lines
402408 LUA_API lua_CFunction lua_tocfunction (lua_State *L, int idx) {
403409 StkId o = index2addr(L, idx);
404410 if (ttislcf(o)) return fvalue(o);
405− else if (ttisclosure(o) && clvalue(o)->c.isC)
406− return clvalue(o)->c.f;
411+ else if (ttisCclosure(o))
412+ return clCvalue(o)->f;
407413 else return NULL; /* not a C function */
408414 }
409415
410416
411417 LUA_API void *lua_touserdata (lua_State *L, int idx) {
412418 StkId o = index2addr(L, idx);
413− switch (ttype(o)) {
419+ switch (ttypenv(o)) {
414420 case LUA_TUSERDATA: return (rawuvalue(o) + 1);
415421 case LUA_TLIGHTUSERDATA: return pvalue(o);
416422 default: return NULL;
@@ -428,7 +434,8 @@ LUA_API const void *lua_topointer (lua_State *L, int idx) { · 11 unmodified lines
428434 StkId o = index2addr(L, idx);
429435 switch (ttype(o)) {
430436 case LUA_TTABLE: return hvalue(o);
431− case LUA_TFUNCTION: return clvalue(o);
437+ case LUA_TLCL: return clLvalue(o);
438+ case LUA_TCCL: return clCvalue(o);
432439 case LUA_TLCF: return cast(void *, cast(size_t, fvalue(o)));
433440 case LUA_TTHREAD: return thvalue(o);
434441 case LUA_TUSERDATA:
@@ -456,6 +463,8 @@ LUA_API void lua_pushnil (lua_State *L) { · 21 unmodified lines
456463 LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
457464 lua_lock(L);
458465 setnvalue(L->top, n);
466+ luai_checknum(L, L->top,
467+ luaG_runerror(L, "C API - attempt to push a signaling NaN"));
459468 api_incr_top(L);
460469 lua_unlock(L);
461470 }
@@ -548,7 +557,7 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { · 86 unmodified lines
548557 L->top -= n;
549558 while (n--)
550559 setobj2n(L, &cl->c.upvalue[n], L->top + n);
551− setclvalue(L, L->top, cl);
560+ setclCvalue(L, L->top, cl);
552561 }
553562 api_incr_top(L);
554563 lua_unlock(L);
@@ -648,7 +657,7 @@ LUA_API int lua_getmetatable (lua_State *L, int objindex) { · 93 unmodified lines
648657 int res;
649658 lua_lock(L);
650659 obj = index2addr(L, objindex);
651− switch (ttype(obj)) {
660+ switch (ttypenv(obj)) {
652661 case LUA_TTABLE:
653662 mt = hvalue(obj)->metatable;
654663 break;
@@ -755,18 +764,19 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { · 100 unmodified lines
755764 api_check(L, ttistable(L->top - 1), "table expected");
756765 mt = hvalue(L->top - 1);
757766 }
758− switch (ttype(obj)) {
767+ switch (ttypenv(obj)) {
759768 case LUA_TTABLE: {
760769 hvalue(obj)->metatable = mt;
761770 if (mt)
762771 luaC_objbarrierback(L, gcvalue(obj), mt);
772+ luaC_checkfinalizer(L, gcvalue(obj), mt);
763773 break;
764774 }
765775 case LUA_TUSERDATA: {
766776 uvalue(obj)->metatable = mt;
767777 if (mt) {
768778 luaC_objbarrier(L, rawuvalue(obj), mt);
769− luaC_checkfinalizer(L, rawuvalue(obj));
779+ luaC_checkfinalizer(L, gcvalue(obj), mt);
770780 }
771781 break;
772782 }
@@ -912,15 +922,14 @@ LUA_API int lua_load (lua_State *L, lua_Reader reader, void *data, · 139 unmodified lines
912922 luaZ_init(L, &z, reader, data);
913923 status = luaD_protectedparser(L, &z, chunkname);
914924 if (status == LUA_OK) { /* no errors? */
915− Closure *f = clvalue(L->top - 1); /* get newly created function */
916− lua_assert(!f->c.isC);
917− if (f->l.nupvalues == 1) { /* does it have one upvalue? */
925+ LClosure *f = clLvalue(L->top - 1); /* get newly created function */
926+ if (f->nupvalues == 1) { /* does it have one upvalue? */
918927 /* get global table from registry */
919928 Table *reg = hvalue(&G(L)->l_registry);
920929 const TValue *gt = luaH_getint(reg, LUA_RIDX_GLOBALS);
921930 /* set global table as 1st upvalue of 'f' (may be LUA_ENV) */
922− setobj(L, f->l.upvals[0]->v, gt);
923− luaC_barrier(L, f->l.upvals[0], gt);
931+ setobj(L, f->upvals[0]->v, gt);
932+ luaC_barrier(L, f->upvals[0], gt);
924933 }
925934 }
926935 lua_unlock(L);
@@ -959,11 +968,12 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 32 unmodified lines
959968 g = G(L);
960969 switch (what) {
961970 case LUA_GCSTOP: {
962− stopgc(g);
971+ g->gcrunning = 0;
963972 break;
964973 }
965974 case LUA_GCRESTART: {
966− g->GCdebt = 0;
975+ luaE_setdebt(g, 0);
976+ g->gcrunning = 1;
967977 break;
968978 }
969979 case LUA_GCCOLLECT: {
@@ -972,30 +982,27 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 2 unmodified lines
972982 }
973983 case LUA_GCCOUNT: {
974984 /* GC values are expressed in Kbytes: #bytes/2^10 */
975− res = cast_int(g->totalbytes >> 10);
985+ res = cast_int(gettotalbytes(g) >> 10);
976986 break;
977987 }
978988 case LUA_GCCOUNTB: {
979− res = cast_int(g->totalbytes & 0x3ff);
989+ res = cast_int(gettotalbytes(g) & 0x3ff);
980990 break;
981991 }
982992 case LUA_GCSTEP: {
983− int stopped = gcstopped(g);
984993 if (g->gckind == KGC_GEN) { /* generational mode? */
985994 res = (g->lastmajormem == 0); /* 1 if will do major collection */
986− luaC_step(L); /* do a single step */
995+ luaC_forcestep(L); /* do a single step */
987996 }
988997 else {
989998 while (data-- >= 0) {
990− luaC_step(L);
999+ luaC_forcestep(L);
9911000 if (g->gcstate == GCSpause) { /* end of cycle? */
9921001 res = 1; /* signal it */
9931002 break;
9941003 }
9951004 }
9961005 }
997− if (stopped) /* collector was stopped? */
998− stopgc(g); /* keep it that way */
9991006 break;
10001007 }
10011008 case LUA_GCSETPAUSE: {
@@ -1014,7 +1021,7 @@ LUA_API int lua_gc (lua_State *L, int what, int data) { · 12 unmodified lines
10141021 break;
10151022 }
10161023 case LUA_GCISRUNNING: {
1017− res = !gcstopped(g);
1024+ res = g->gcrunning;
10181025 break;
10191026 }
10201027 case LUA_GCGEN: { /* change collector to generational mode */
@@ -1124,25 +1131,27 @@ LUA_API void *lua_newuserdata (lua_State *L, size_t size) { · 103 unmodified lines
11241131
11251132 static const char *aux_upvalue (StkId fi, int n, TValue **val,
11261133 GCObject **owner) {
1127− Closure *f;
1128− if (!ttisclosure(fi)) return NULL;
1129− f = clvalue(fi);
1130− if (f->c.isC) {
1131− if (!(1 <= n && n <= f->c.nupvalues)) return NULL;
1132− *val = &f->c.upvalue[n-1];
1133− if (owner) *owner = obj2gco(f);
1134− return "";
1135− }
1136− else {
1137− const char *name;
1138− Proto *p = f->l.p;
1139− if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1140− *val = f->l.upvals[n-1]->v;
1141− if (owner) *owner = obj2gco(f->l.upvals[n - 1]);
1142− name = getstr(p->upvalues[n-1].name);
1143− if (name == NULL) /* no debug information? */
1144− name = "";
1145− return name;
1134+ switch (ttype(fi)) {
1135+ case LUA_TCCL: { /* C closure */
1136+ CClosure *f = clCvalue(fi);
1137+ if (!(1 <= n && n <= f->nupvalues)) return NULL;
1138+ *val = &f->upvalue[n-1];
1139+ if (owner) *owner = obj2gco(f);
1140+ return "";
1141+ }
1142+ case LUA_TLCL: { /* Lua closure */
1143+ LClosure *f = clLvalue(fi);
1144+ const char *name;
1145+ Proto *p = f->p;
1146+ if (!(1 <= n && n <= p->sizeupvalues)) return NULL;
1147+ *val = f->upvals[n-1]->v;
1148+ if (owner) *owner = obj2gco(f->upvals[n - 1]);
1149+ name = getstr(p->upvalues[n-1].name);
1150+ if (name == NULL) /* no debug information? */
1151+ name = "";
1152+ return name;
1153+ }
1154+ default: return NULL; /* not a closure */
11461155 }
11471156 }
11481157
@@ -1180,34 +1189,39 @@ LUA_API const char *lua_setupvalue (lua_State *L, int funcindex, int n) { · 31 unmodified lines
11801189 }
11811190
11821191
1183−static UpVal **getupvalref (lua_State *L, int fidx, int n, Closure **pf) {
1184− Closure *f;
1192+static UpVal **getupvalref (lua_State *L, int fidx, int n, LClosure **pf) {
1193+ LClosure *f;
11851194 StkId fi = index2addr(L, fidx);
1186− api_check(L, ttisclosure(fi), "Lua function expected");
1187− f = clvalue(fi);
1188− api_check(L, !f->c.isC, "Lua function expected");
1189− api_check(L, (1 <= n && n <= f->l.p->sizeupvalues), "invalid upvalue index");
1195+ api_check(L, ttisLclosure(fi), "Lua function expected");
1196+ f = clLvalue(fi);
1197+ api_check(L, (1 <= n && n <= f->p->sizeupvalues), "invalid upvalue index");
11901198 if (pf) *pf = f;
1191− return &f->l.upvals[n - 1]; /* get its upvalue pointer */
1199+ return &f->upvals[n - 1]; /* get its upvalue pointer */
11921200 }
11931201
11941202
11951203 LUA_API void *lua_upvalueid (lua_State *L, int fidx, int n) {
1196− Closure *f;
11971204 StkId fi = index2addr(L, fidx);
1198− api_check(L, ttisclosure(fi), "function expected");
1199− f = clvalue(fi);
1200− if (f->c.isC) {
1201− api_check(L, 1 <= n && n <= f->c.nupvalues, "invalid upvalue index");
1202− return &f->c.upvalue[n - 1];
1205+ switch (ttype(fi)) {
1206+ case LUA_TLCL: { /* lua closure */
1207+ return *getupvalref(L, fidx, n, NULL);
1208+ }
1209+ case LUA_TCCL: { /* C closure */
1210+ CClosure *f = clCvalue(fi);
1211+ api_check(L, 1 <= n && n <= f->nupvalues, "invalid upvalue index");
1212+ return &f->upvalue[n - 1];
1213+ }
1214+ default: {
1215+ api_check(L, 0, "closure expected");
1216+ return NULL;
1217+ }
12031218 }
1204− else return *getupvalref(L, fidx, n, NULL);
12051219 }
12061220
12071221
12081222 LUA_API void lua_upvaluejoin (lua_State *L, int fidx1, int n1,
12091223 int fidx2, int n2) {
1210− Closure *f1;
1224+ LClosure *f1;
12111225 UpVal **up1 = getupvalref(L, fidx1, n1, &f1);
12121226 UpVal **up2 = getupvalref(L, fidx2, n2, NULL);
12131227 *up1 = *up2;
src/lauxlib.c+77−13
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.c,v 1.227 2010/11/10 18:05:36 roberto Exp $
2+** $Id: lauxlib.c,v 1.232 2011/05/03 16:01:57 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -203,6 +203,64 @@ LUALIB_API int luaL_error (lua_State *L, const char *fmt, ...) { · 197 unmodified lines
203203 return lua_error(L);
204204 }
205205
206+
207+LUALIB_API int luaL_fileresult (lua_State *L, int stat, const char *fname) {
208+ int en = errno; /* calls to Lua API may change this value */
209+ if (stat) {
210+ lua_pushboolean(L, 1);
211+ return 1;
212+ }
213+ else {
214+ lua_pushnil(L);
215+ if (fname)
216+ lua_pushfstring(L, "%s: %s", fname, strerror(en));
217+ else
218+ lua_pushfstring(L, "%s", strerror(en));
219+ lua_pushinteger(L, en);
220+ return 3;
221+ }
222+}
223+
224+
225+#if !defined(inspectstat) /* { */
226+
227+#if defined(LUA_USE_POSIX)
228+
229+#include <sys/wait.h>
230+
231+/*
232+** use appropriate macros to interpret 'pclose' return status
233+*/
234+#define inspectstat(stat,what) \
235+ if (WIFEXITED(stat)) { stat = WEXITSTATUS(stat); } \
236+ else if (WIFSIGNALED(stat)) { stat = WTERMSIG(stat); what = "signal"; }
237+
238+#else
239+
240+#define inspectstat(stat,what) /* no op */
241+
242+#endif
243+
244+#endif /* } */
245+
246+
247+LUALIB_API int luaL_execresult (lua_State *L, int stat) {
248+ const char *what = "exit"; /* type of termination */
249+ if (stat == -1) /* error? */
250+ return luaL_fileresult(L, 0, NULL);
251+ else {
252+ inspectstat(stat, what); /* interpret result */
253+ if (*what == 'e' && stat == 0) /* successful termination? */
254+ return luaL_fileresult(L, 1, NULL);
255+ else { /* return nil,what,code */
256+ lua_pushnil(L);
257+ lua_pushstring(L, what);
258+ lua_pushinteger(L, stat);
259+ return 3;
260+ }
261+ }
262+}
263+
206264 /* }====================================================== */
207265
208266
@@ -384,8 +442,10 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { · 175 unmodified lines
384442 newsize = B->n + sz;
385443 if (newsize < B->n || newsize - B->n < sz)
386444 luaL_error(L, "buffer too large");
387− newbuff = (char *)lua_newuserdata(L, newsize); /* create larger buffer */
388− memcpy(newbuff, B->b, B->n); /* move content to new buffer */
445+ /* create larger buffer */
446+ newbuff = (char *)lua_newuserdata(L, newsize * sizeof(char));
447+ /* move content to new buffer */
448+ memcpy(newbuff, B->b, B->n * sizeof(char));
389449 if (buffonstack(B))
390450 lua_remove(L, -2); /* remove old buffer */
391451 B->b = newbuff;
@@ -397,7 +457,7 @@ LUALIB_API char *luaL_prepbuffsize (luaL_Buffer *B, size_t sz) { · 5 unmodified lines
397457
398458 LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) {
399459 char *b = luaL_prepbuffsize(B, l);
400− memcpy(b, s, l);
460+ memcpy(b, s, l * sizeof(char));
401461 luaL_addsize(B, l);
402462 }
403463
@@ -700,8 +760,8 @@ LUALIB_API const char *luaL_tolstring (lua_State *L, int idx, size_t *len) { · 296 unmodified lines
700760 */
701761 #if defined(LUA_COMPAT_MODULE)
702762
703−static const char *luaL_findtablex (lua_State *L, int idx,
704− const char *fname, int szhint) {
763+static const char *luaL_findtable (lua_State *L, int idx,
764+ const char *fname, int szhint) {
705765 const char *e;
706766 if (idx) lua_pushvalue(L, idx);
707767 do {
@@ -745,13 +805,13 @@ static int libsize (const luaL_Reg *l) { · 37 unmodified lines
745805 */
746806 LUALIB_API void luaL_pushmodule (lua_State *L, const char *modname,
747807 int sizehint) {
748− luaL_findtablex(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
808+ luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 1); /* get _LOADED table */
749809 lua_getfield(L, -1, modname); /* get _LOADED[modname] */
750810 if (!lua_istable(L, -1)) { /* not found? */
751811 lua_pop(L, 1); /* remove previous result */
752812 /* try global variable (and create one if it does not exist) */
753813 lua_pushglobaltable(L);
754− if (luaL_findtablex(L, 0, modname, sizehint) != NULL)
814+ if (luaL_findtable(L, 0, modname, sizehint) != NULL)
755815 luaL_error(L, "name conflict for module " LUA_QS, modname);
756816 lua_pushvalue(L, -1);
757817 lua_setfield(L, -3, modname); /* _LOADED[modname] = new table */
@@ -767,7 +827,10 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, · 9 unmodified lines
767827 luaL_pushmodule(L, libname, libsize(l)); /* get/create library table */
768828 lua_insert(L, -(nup + 1)); /* move library table to below upvalues */
769829 }
770− luaL_setfuncs(L, l, nup);
830+ if (l)
831+ luaL_setfuncs(L, l, nup);
832+ else
833+ lua_pop(L, nup); /* remove upvalues */
771834 }
772835
773836 #endif
@@ -780,7 +843,7 @@ LUALIB_API void luaL_openlib (lua_State *L, const char *libname, · 6 unmodified lines
780843 */
781844 LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) {
782845 luaL_checkstack(L, nup, "too many upvalues");
783− for (; l && l->name; l++) { /* fill the table with given functions */
846+ for (; l->name != NULL; l++) { /* fill the table with given functions */
784847 int i;
785848 for (i = 0; i < nup; i++) /* copy upvalues to the top */
786849 lua_pushvalue(L, -nup);
@@ -795,15 +858,16 @@ LUALIB_API void luaL_setfuncs (lua_State *L, const luaL_Reg *l, int nup) { · 8 unmodified lines
795858 ** ensure that stack[idx][fname] has a table and push that table
796859 ** into the stack
797860 */
798−LUALIB_API void luaL_findtable (lua_State *L, int idx, const char *fname) {
861+LUALIB_API int luaL_getsubtable (lua_State *L, int idx, const char *fname) {
799862 lua_getfield(L, idx, fname);
800− if (lua_istable(L, -1)) return; /* table already there */
863+ if (lua_istable(L, -1)) return 1; /* table already there */
801864 else {
802865 idx = lua_absindex(L, idx);
803866 lua_pop(L, 1); /* remove previous result */
804867 lua_newtable(L);
805868 lua_pushvalue(L, -1); /* copy to be left at top */
806869 lua_setfield(L, idx, fname); /* assign new table to field */
870+ return 0; /* false, because did not find table there */
807871 }
808872 }
809873
@@ -819,7 +883,7 @@ LUALIB_API void luaL_requiref (lua_State *L, const char *modname, · 9 unmodified lines
819883 lua_pushcfunction(L, openf);
820884 lua_pushstring(L, modname); /* argument to open function */
821885 lua_call(L, 1, 1); /* open module */
822− luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
886+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
823887 lua_pushvalue(L, -2); /* make copy of module (call result) */
824888 lua_setfield(L, -2, modname); /* _LOADED[modname] = module */
825889 lua_pop(L, 1); /* remove _LOADED table */
src/lauxlib.h+5−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lauxlib.h,v 1.113 2010/11/16 19:20:01 roberto Exp $
2+** $Id: lauxlib.h,v 1.116 2011/04/08 19:17:36 roberto Exp $
33 ** Auxiliary functions for building Lua libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -62,6 +62,9 @@ LUALIB_API int (luaL_error) (lua_State *L, const char *fmt, ...); · 56 unmodified lines
6262 LUALIB_API int (luaL_checkoption) (lua_State *L, int narg, const char *def,
6363 const char *const lst[]);
6464
65+LUALIB_API int (luaL_fileresult) (lua_State *L, int stat, const char *fname);
66+LUALIB_API int (luaL_execresult) (lua_State *L, int stat);
67+
6568 /* pre-defined references */
6669 #define LUA_NOREF (-2)
6770 #define LUA_REFNIL (-1)
@@ -83,7 +86,7 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, · 15 unmodified lines
8386
8487 LUALIB_API void (luaL_setfuncs) (lua_State *L, const luaL_Reg *l, int nup);
8588
86−LUALIB_API void (luaL_findtable) (lua_State *L, int idx, const char *fname);
89+LUALIB_API int (luaL_getsubtable) (lua_State *L, int idx, const char *fname);
8790
8891 LUALIB_API void (luaL_traceback) (lua_State *L, lua_State *L1,
8992 const char *msg, int level);
src/lbaselib.c+76−108
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbaselib.c,v 1.251 2010/10/28 15:36:30 roberto Exp $
2+** $Id: lbaselib.c,v 1.261 2011/05/26 16:09:40 roberto Exp $
33 ** Basic library
44 ** See Copyright Notice in lua.h
55 */
@@ -32,44 +32,54 @@ static int luaB_print (lua_State *L) { · 26 unmodified lines
3232 lua_call(L, 1, 1);
3333 s = lua_tolstring(L, -1, &l); /* get result */
3434 if (s == NULL)
35− return luaL_error(L, LUA_QL("tostring") " must return a string to "
36− LUA_QL("print"));
35+ return luaL_error(L,
36+ LUA_QL("tostring") " must return a string to " LUA_QL("print"));
3737 if (i>1) luai_writestring("\t", 1);
3838 luai_writestring(s, l);
3939 lua_pop(L, 1); /* pop result */
4040 }
41− luai_writestring("\n", 1);
41+ luai_writeline();
4242 return 0;
4343 }
4444
4545
46+#define SPACECHARS " \f\n\r\t\v"
47+
4648 static int luaB_tonumber (lua_State *L) {
4749 int base = luaL_optint(L, 2, 10);
50+ luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
4851 if (base == 10) { /* standard conversion */
4952 luaL_checkany(L, 1);
5053 if (lua_isnumber(L, 1)) {
5154 lua_pushnumber(L, lua_tonumber(L, 1));
5255 return 1;
53− }
56+ } /* else not a number */
5457 }
5558 else {
56− const char *s1 = luaL_checkstring(L, 1);
57− char *s2;
58− unsigned long n;
59+ size_t l;
60+ const char *s = luaL_checklstring(L, 1, &l);
61+ const char *e = s + l; /* end point for 's' */
5962 int neg = 0;
60− luaL_argcheck(L, 2 <= base && base <= 36, 2, "base out of range");
61− while (isspace((unsigned char)(*s1))) s1++; /* skip initial spaces */
62− if (*s1 == '-') { s1++; neg = 1; }
63− n = strtoul(s1, &s2, base);
64− if (s1 != s2) { /* at least one valid digit? */
65− while (isspace((unsigned char)(*s2))) s2++; /* skip trailing spaces */
66− if (*s2 == '\0') { /* no invalid trailing characters? */
67− lua_pushnumber(L, (neg) ? -(lua_Number)n : (lua_Number)n);
63+ s += strspn(s, SPACECHARS); /* skip initial spaces */
64+ if (*s == '-') { s++; neg = 1; } /* handle signal */
65+ else if (*s == '+') s++;
66+ if (isalnum((unsigned char)*s)) {
67+ lua_Number n = 0;
68+ do {
69+ int digit = (isdigit((unsigned char)*s)) ? *s - '0'
70+ : toupper((unsigned char)*s) - 'A' + 10;
71+ if (digit >= base) break; /* invalid numeral; force a fail */
72+ n = n * (lua_Number)base + (lua_Number)digit;
73+ s++;
74+ } while (isalnum((unsigned char)*s));
75+ s += strspn(s, SPACECHARS); /* skip trailing spaces */
76+ if (s == e) { /* no invalid trailing characters? */
77+ lua_pushnumber(L, (neg) ? -n : n);
6878 return 1;
69− }
70− }
79+ } /* else not a number */
80+ } /* else not a number */
7181 }
72− lua_pushnil(L); /* else not a number */
82+ lua_pushnil(L); /* not a number */
7383 return 1;
7484 }
7585
@@ -110,12 +120,10 @@ static int luaB_setmetatable (lua_State *L) { · 34 unmodified lines
110120 }
111121
112122
113−static int luaB_getfenv (lua_State *L) {
114− return luaL_error(L, "getfenv/setfenv deprecated");
123+static int luaB_deprecated (lua_State *L) {
124+ return luaL_error(L, "deprecated function");
115125 }
116126
117−#define luaB_setfenv luaB_getfenv
118−
119127
120128 static int luaB_rawequal (lua_State *L) {
121129 luaL_checkany(L, 1);
@@ -125,6 +133,15 @@ static int luaB_rawequal (lua_State *L) { · 3 unmodified lines
125133 }
126134
127135
136+static int luaB_rawlen (lua_State *L) {
137+ int t = lua_type(L, 1);
138+ luaL_argcheck(L, t == LUA_TTABLE || t == LUA_TSTRING, 1,
139+ "table or string expected");
140+ lua_pushinteger(L, lua_rawlen(L, 1));
141+ return 1;
142+}
143+
144+
128145 static int luaB_rawget (lua_State *L) {
129146 luaL_checktype(L, 1, LUA_TTABLE);
130147 luaL_checkany(L, 2);
@@ -251,6 +268,11 @@ static int luaB_loadfile (lua_State *L) { · 120 unmodified lines
251268 ** =======================================================
252269 */
253270
271+/*
272+** check whether a chunk (prefix in 's') satisfies given 'mode'
273+** ('t' for text, 'b' for binary). Returns error message (also
274+** pushed on the stack) in case of errors.
275+*/
254276 static const char *checkrights (lua_State *L, const char *mode, const char *s) {
255277 if (strchr(mode, 'b') == NULL && *s == LUA_SIGNATURE[0])
256278 return lua_pushstring(L, "attempt to load a binary chunk");
@@ -261,10 +283,11 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { · 4 unmodified lines
261283
262284
263285 /*
264−** reserves a slot, above all arguments, to hold a copy of the returned
265−** string to avoid it being collected while parsed
286+** reserved slot, above all arguments, to hold a copy of the returned
287+** string to avoid it being collected while parsed. 'load' has four
288+** optional arguments (chunk, source name, mode, and environment).
266289 */
267−#define RESERVEDSLOT 4
290+#define RESERVEDSLOT 5
268291
269292
270293 /*
@@ -273,25 +296,20 @@ static const char *checkrights (lua_State *L, const char *mode, const char *s) { · 2 unmodified lines
273296 ** stack top. Instead, it keeps its resulting string in a
274297 ** reserved slot inside the stack.
275298 */
276−typedef struct { /* reader state */
277− int f; /* position of reader function on stack */
278− const char *mode; /* allowed modes (binary/text) */
279−} Readstat;
280−
281299 static const char *generic_reader (lua_State *L, void *ud, size_t *size) {
282300 const char *s;
283− Readstat *stat = (Readstat *)ud;
301+ const char **mode = (const char **)ud;
284302 luaL_checkstack(L, 2, "too many nested functions");
285− lua_pushvalue(L, stat->f); /* get function */
303+ lua_pushvalue(L, 1); /* get function */
286304 lua_call(L, 0, 1); /* call it */
287305 if (lua_isnil(L, -1)) {
288306 *size = 0;
289307 return NULL;
290308 }
291309 else if ((s = lua_tostring(L, -1)) != NULL) {
292− if (stat->mode != NULL) { /* first time? */
293− s = checkrights(L, stat->mode, s); /* check mode */
294− stat->mode = NULL; /* to avoid further checks */
310+ if (*mode != NULL) { /* first time? */
311+ s = checkrights(L, *mode, s); /* check mode */
312+ *mode = NULL; /* to avoid further checks */
295313 if (s) luaL_error(L, s);
296314 }
297315 lua_replace(L, RESERVEDSLOT); /* save string in reserved slot */
@@ -304,52 +322,38 @@ static const char *generic_reader (lua_State *L, void *ud, size_t *size) { · 6 unmodified lines
304322 }
305323
306324
307−static int luaB_load_aux (lua_State *L, int farg) {
325+static int luaB_load (lua_State *L) {
308326 int status;
309− Readstat stat;
310327 size_t l;
311− const char *s = lua_tolstring(L, farg, &l);
312− stat.mode = luaL_optstring(L, farg + 2, "bt");
328+ int top = lua_gettop(L);
329+ const char *s = lua_tolstring(L, 1, &l);
330+ const char *mode = luaL_optstring(L, 3, "bt");
313331 if (s != NULL) { /* loading a string? */
314− const char *chunkname = luaL_optstring(L, farg + 1, s);
315− status = (checkrights(L, stat.mode, s) != NULL)
332+ const char *chunkname = luaL_optstring(L, 2, s);
333+ status = (checkrights(L, mode, s) != NULL)
316334 || luaL_loadbuffer(L, s, l, chunkname);
317335 }
318336 else { /* loading from a reader function */
319− const char *chunkname = luaL_optstring(L, farg + 1, "=(load)");
320− luaL_checktype(L, farg, LUA_TFUNCTION);
321− stat.f = farg;
337+ const char *chunkname = luaL_optstring(L, 2, "=(load)");
338+ luaL_checktype(L, 1, LUA_TFUNCTION);
322339 lua_settop(L, RESERVEDSLOT); /* create reserved slot */
323− status = lua_load(L, generic_reader, &stat, chunkname);
340+ status = lua_load(L, generic_reader, &mode, chunkname);
341+ }
342+ if (status == LUA_OK && top >= 4) { /* is there an 'env' argument */
343+ lua_pushvalue(L, 4); /* environment for loaded function */
344+ lua_setupvalue(L, -2, 1); /* set it as 1st upvalue */
324345 }
325346 return load_aux(L, status);
326347 }
327348
328349
329−static int luaB_load (lua_State *L) {
330− return luaB_load_aux(L, 1);
331−}
332−
333−
334−static int luaB_loadin (lua_State *L) {
335− int n;
336− luaL_checkany(L, 1);
337− n = luaB_load_aux(L, 2);
338− if (n == 1) { /* success? */
339− lua_pushvalue(L, 1); /* environment for loaded function */
340− if (lua_setupvalue(L, -2, 1) == NULL)
341− luaL_error(L, "loaded chunk does not have an upvalue");
342− }
343− return n;
344−}
350+#if defined(LUA_COMPAT_LOADSTRING)
351+#define luaB_loadstring luaB_load
352+#else
353+#define luaB_loadstring luaB_deprecated
354+#endif
345355
346356
347−static int luaB_loadstring (lua_State *L) {
348− lua_settop(L, 2);
349− lua_pushliteral(L, "tb");
350− return luaB_load(L); /* dostring(s, n) == load(s, n, "tb") */
351−
352−}
353357 /* }====================================================== */
354358
355359
@@ -391,7 +395,7 @@ static int luaB_select (lua_State *L) { · 35 unmodified lines
391395
392396
393397 static int pcallcont (lua_State *L) {
394− int errfunc; /* call has an error function in bottom of the stack */
398+ int errfunc = 0; /* =0 to avoid warnings */
395399 int status = lua_getctx(L, &errfunc);
396400 lua_assert(status != LUA_OK);
397401 lua_pushboolean(L, (status == LUA_YIELD)); /* first result (status) */
@@ -436,55 +440,27 @@ static int luaB_tostring (lua_State *L) { · 38 unmodified lines
436440 }
437441
438442
439−static int luaB_newproxy (lua_State *L) {
440− lua_settop(L, 1);
441− lua_newuserdata(L, 0); /* create proxy */
442− if (lua_toboolean(L, 1) == 0)
443− return 1; /* no metatable */
444− else if (lua_isboolean(L, 1)) {
445− lua_createtable(L, 0, 1); /* create a new metatable `m' ... */
446− lua_pushboolean(L, 1);
447− lua_setfield(L, -2, "__gc"); /* ... m.__gc = false (HACK!!)... */
448− lua_pushvalue(L, -1); /* ... and mark `m' as a valid metatable */
449− lua_pushboolean(L, 1);
450− lua_rawset(L, lua_upvalueindex(1)); /* weaktable[m] = true */
451− }
452− else {
453− int validproxy = 0; /* to check if weaktable[metatable(u)] == true */
454− if (lua_getmetatable(L, 1)) {
455− lua_rawget(L, lua_upvalueindex(1));
456− validproxy = lua_toboolean(L, -1);
457− lua_pop(L, 1); /* remove value */
458− }
459− luaL_argcheck(L, validproxy, 1, "boolean or proxy expected");
460− lua_getmetatable(L, 1); /* metatable is valid; get it */
461− }
462− lua_setmetatable(L, 2);
463− return 1;
464−}
465−
466−
467443 static const luaL_Reg base_funcs[] = {
468444 {"assert", luaB_assert},
469445 {"collectgarbage", luaB_collectgarbage},
470446 {"dofile", luaB_dofile},
471447 {"error", luaB_error},
472− {"getfenv", luaB_getfenv},
448+ {"getfenv", luaB_deprecated},
473449 {"getmetatable", luaB_getmetatable},
474450 {"ipairs", luaB_ipairs},
475451 {"loadfile", luaB_loadfile},
476452 {"load", luaB_load},
477− {"loadin", luaB_loadin},
478453 {"loadstring", luaB_loadstring},
479454 {"next", luaB_next},
480455 {"pairs", luaB_pairs},
481456 {"pcall", luaB_pcall},
482457 {"print", luaB_print},
483458 {"rawequal", luaB_rawequal},
459+ {"rawlen", luaB_rawlen},
484460 {"rawget", luaB_rawget},
485461 {"rawset", luaB_rawset},
486462 {"select", luaB_select},
487− {"setfenv", luaB_setfenv},
463+ {"setfenv", luaB_deprecated},
488464 {"setmetatable", luaB_setmetatable},
489465 {"tonumber", luaB_tonumber},
490466 {"tostring", luaB_tostring},
@@ -503,14 +479,6 @@ LUAMOD_API int luaopen_base (lua_State *L) { · 12 unmodified lines
503479 luaL_setfuncs(L, base_funcs, 0);
504480 lua_pushliteral(L, LUA_VERSION);
505481 lua_setfield(L, -2, "_VERSION"); /* set global _VERSION */
506− /* `newproxy' needs a weaktable as upvalue */
507− lua_createtable(L, 0, 1); /* new table `w' */
508− lua_pushvalue(L, -1); /* `w' will be its own metatable */
509− lua_setmetatable(L, -2);
510− lua_pushliteral(L, "kv");
511− lua_setfield(L, -2, "__mode"); /* metatable(w).__mode = "kv" */
512− lua_pushcclosure(L, luaB_newproxy, 1);
513− lua_setfield(L, -2, "newproxy"); /* set global `newproxy' */
514482 return 1;
515483 }
516484
src/lbitlib.c+67−21
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lbitlib.c,v 1.13 2010/11/22 18:06:33 roberto Exp $
2+** $Id: lbitlib.c,v 1.15 2010/12/17 13:26:38 roberto Exp $
33 ** Standard library for bitwise operations
44 ** See Copyright Notice in lua.h
55 */
@@ -14,25 +14,30 @@ · 8 unmodified lines
1414
1515
1616 /* number of bits to consider in a number */
17−#define NBITS 32
17+#if !defined(LUA_NBITS)
18+#define LUA_NBITS 32
19+#endif
1820
19−#define ALLONES (~(((~(lua_Unsigned)0) << (NBITS - 1)) << 1))
2021
21−/* mask to trim extra bits */
22+#define ALLONES (~(((~(lua_Unsigned)0) << (LUA_NBITS - 1)) << 1))
23+
24+/* macro to trim extra bits */
2225 #define trim(x) ((x) & ALLONES)
2326
2427
25−typedef lua_Unsigned b_uint;
28+/* builds a number with 'n' ones (1 <= n <= LUA_NBITS) */
29+#define mask(n) (~((ALLONES << 1) << ((n) - 1)))
30+
2631
32+typedef lua_Unsigned b_uint;
2733
28−#define getuintarg(L,arg) luaL_checkunsigned(L,arg)
2934
3035
3136 static b_uint andaux (lua_State *L) {
3237 int i, n = lua_gettop(L);
3338 b_uint r = ~(b_uint)0;
3439 for (i = 1; i <= n; i++)
35− r &= getuintarg(L, i);
40+ r &= luaL_checkunsigned(L, i);
3641 return trim(r);
3742 }
3843
@@ -55,7 +60,7 @@ static int b_or (lua_State *L) { · 16 unmodified lines
5560 int i, n = lua_gettop(L);
5661 b_uint r = 0;
5762 for (i = 1; i <= n; i++)
58− r |= getuintarg(L, i);
63+ r |= luaL_checkunsigned(L, i);
5964 lua_pushunsigned(L, trim(r));
6065 return 1;
6166 }
@@ -65,14 +70,14 @@ static int b_xor (lua_State *L) { · 3 unmodified lines
6570 int i, n = lua_gettop(L);
6671 b_uint r = 0;
6772 for (i = 1; i <= n; i++)
68− r ^= getuintarg(L, i);
73+ r ^= luaL_checkunsigned(L, i);
6974 lua_pushunsigned(L, trim(r));
7075 return 1;
7176 }
7277
7378
7479 static int b_not (lua_State *L) {
75− b_uint r = ~getuintarg(L, 1);
80+ b_uint r = ~luaL_checkunsigned(L, 1);
7681 lua_pushunsigned(L, trim(r));
7782 return 1;
7883 }
@@ -82,11 +87,11 @@ static int b_shift (lua_State *L, b_uint r, int i) { · 3 unmodified lines
8287 if (i < 0) { /* shift right? */
8388 i = -i;
8489 r = trim(r);
85− if (i >= NBITS) r = 0;
90+ if (i >= LUA_NBITS) r = 0;
8691 else r >>= i;
8792 }
8893 else { /* shift left */
89− if (i >= NBITS) r = 0;
94+ if (i >= LUA_NBITS) r = 0;
9095 else r <<= i;
9196 r = trim(r);
9297 }
@@ -96,22 +101,22 @@ static int b_shift (lua_State *L, b_uint r, int i) { · 3 unmodified lines
96101
97102
98103 static int b_lshift (lua_State *L) {
99− return b_shift(L, getuintarg(L, 1), luaL_checkint(L, 2));
104+ return b_shift(L, luaL_checkunsigned(L, 1), luaL_checkint(L, 2));
100105 }
101106
102107
103108 static int b_rshift (lua_State *L) {
104− return b_shift(L, getuintarg(L, 1), -luaL_checkint(L, 2));
109+ return b_shift(L, luaL_checkunsigned(L, 1), -luaL_checkint(L, 2));
105110 }
106111
107112
108113 static int b_arshift (lua_State *L) {
109− b_uint r = getuintarg(L, 1);
114+ b_uint r = luaL_checkunsigned(L, 1);
110115 int i = luaL_checkint(L, 2);
111− if (i < 0 || !(r & ((b_uint)1 << (NBITS - 1))))
116+ if (i < 0 || !(r & ((b_uint)1 << (LUA_NBITS - 1))))
112117 return b_shift(L, r, -i);
113118 else { /* arithmetic shift for 'negative' number */
114− if (i >= NBITS) r = ALLONES;
119+ if (i >= LUA_NBITS) r = ALLONES;
115120 else
116121 r = trim((r >> i) | ~(~(b_uint)0 >> i)); /* add signal bit */
117122 lua_pushunsigned(L, r);
@@ -121,10 +126,10 @@ static int b_arshift (lua_State *L) { · 3 unmodified lines
121126
122127
123128 static int b_rot (lua_State *L, int i) {
124− b_uint r = getuintarg(L, 1);
125− i &= (NBITS - 1); /* i = i % NBITS */
129+ b_uint r = luaL_checkunsigned(L, 1);
130+ i &= (LUA_NBITS - 1); /* i = i % NBITS */
126131 r = trim(r);
127− r = (r << i) | (r >> (NBITS - i));
132+ r = (r << i) | (r >> (LUA_NBITS - i));
128133 lua_pushunsigned(L, trim(r));
129134 return 1;
130135 }
@@ -140,17 +145,58 @@ static int b_rrot (lua_State *L) { · 9 unmodified lines
140145 }
141146
142147
148+/*
149+** get field and width arguments for field-manipulation functions,
150+** checking whether they are valid
151+*/
152+static int fieldargs (lua_State *L, int farg, int *width) {
153+ int f = luaL_checkint(L, farg);
154+ int w = luaL_optint(L, farg + 1, 1);
155+ luaL_argcheck(L, 0 <= f, farg, "field cannot be netative");
156+ luaL_argcheck(L, 0 < w, farg + 1, "width must be positive");
157+ if (f + w > LUA_NBITS)
158+ luaL_error(L, "trying to access non-existent bits");
159+ *width = w;
160+ return f;
161+}
162+
163+
164+static int b_extract (lua_State *L) {
165+ int w;
166+ b_uint r = luaL_checkunsigned(L, 1);
167+ int f = fieldargs(L, 2, &w);
168+ r = (r >> f) & mask(w);
169+ lua_pushunsigned(L, r);
170+ return 1;
171+}
172+
173+
174+static int b_replace (lua_State *L) {
175+ int w;
176+ b_uint r = luaL_checkunsigned(L, 1);
177+ b_uint v = luaL_checkunsigned(L, 2);
178+ int f = fieldargs(L, 3, &w);
179+ int m = mask(w);
180+ v &= m; /* erase bits outside given width */
181+ r = (r & ~(m << f)) | (v << f);
182+ lua_pushunsigned(L, r);
183+ return 1;
184+}
185+
186+
143187 static const luaL_Reg bitlib[] = {
144188 {"arshift", b_arshift},
145189 {"band", b_and},
146190 {"bnot", b_not},
147191 {"bor", b_or},
148192 {"bxor", b_xor},
193+ {"btest", b_test},
194+ {"extract", b_extract},
149195 {"lrotate", b_lrot},
150196 {"lshift", b_lshift},
197+ {"replace", b_replace},
151198 {"rrotate", b_rrot},
152199 {"rshift", b_rshift},
153− {"btest", b_test},
154200 {NULL, NULL}
155201 };
156202
src/lcode.c+42−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.c,v 2.49 2010/07/07 16:27:29 roberto Exp $
2+** $Id: lcode.c,v 2.56 2011/05/31 18:27:56 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -23,6 +23,7 @@ · 17 unmodified lines
2323 #include "lparser.h"
2424 #include "lstring.h"
2525 #include "ltable.h"
26+#include "lvm.h"
2627
2728
2829 #define hasjumps(e) ((e)->t != (e)->f)
@@ -35,19 +36,23 @@ static int isnumeral(expdesc *e) { · 6 unmodified lines
3536
3637 void luaK_nil (FuncState *fs, int from, int n) {
3738 Instruction *previous;
39+ int l = from + n - 1; /* last register to set nil */
3840 if (fs->pc > fs->lasttarget) { /* no jumps to current position? */
3941 previous = &fs->f->code[fs->pc-1];
4042 if (GET_OPCODE(*previous) == OP_LOADNIL) {
4143 int pfrom = GETARG_A(*previous);
42− int pto = GETARG_B(*previous);
43− if (pfrom <= from && from <= pto+1) { /* can connect both? */
44− if (from+n-1 > pto)
45− SETARG_B(*previous, from+n-1);
44+ int pl = pfrom + GETARG_B(*previous);
45+ if ((pfrom <= from && from <= pl + 1) ||
46+ (from <= pfrom && pfrom <= l + 1)) { /* can connect both? */
47+ if (pfrom < from) from = pfrom; /* from = min(from, pfrom) */
48+ if (pl > l) l = pl; /* l = max(l, pl) */
49+ SETARG_A(*previous, from);
50+ SETARG_B(*previous, l - from);
4651 return;
4752 }
48− }
53+ } /* else go through */
4954 }
50− luaK_codeABC(fs, OP_LOADNIL, from, from+n-1, 0); /* else no optimization */
55+ luaK_codeABC(fs, OP_LOADNIL, from, n - 1, 0); /* else no optimization */
5156 }
5257
5358
@@ -171,6 +176,19 @@ void luaK_patchlist (FuncState *fs, int list, int target) { · 117 unmodified lines
171176 }
172177
173178
179+LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level) {
180+ level++; /* argument is +1 to reserve 0 as non-op */
181+ while (list != NO_JUMP) {
182+ int next = getjump(fs, list);
183+ lua_assert(GET_OPCODE(fs->f->code[list]) == OP_JMP &&
184+ (GETARG_A(fs->f->code[list]) == 0 ||
185+ GETARG_A(fs->f->code[list]) >= level));
186+ SETARG_A(fs->f->code[list], level);
187+ list = next;
188+ }
189+}
190+
191+
174192 void luaK_patchtohere (FuncState *fs, int list) {
175193 luaK_getlabel(fs);
176194 luaK_concat(fs, &fs->jpc, list);
@@ -229,11 +247,11 @@ static int codeextraarg (FuncState *fs, int a) { · 52 unmodified lines
229247 }
230248
231249
232−int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k) {
233− if (k < MAXARG_Bx)
234− return luaK_codeABx(fs, o, reg, k + 1);
250+int luaK_codek (FuncState *fs, int reg, int k) {
251+ if (k <= MAXARG_Bx)
252+ return luaK_codeABx(fs, OP_LOADK, reg, k);
235253 else {
236− int p = luaK_codeABx(fs, o, reg, 0);
254+ int p = luaK_codeABx(fs, OP_LOADKX, reg, 0);
237255 codeextraarg(fs, k);
238256 return p;
239257 }
@@ -278,7 +296,7 @@ static int addk (FuncState *fs, TValue *key, TValue *v) { · 38 unmodified lines
278296 if (ttisnumber(idx)) {
279297 lua_Number n = nvalue(idx);
280298 lua_number2int(k, n);
281− if (luaO_rawequalObj(&f->k[k], v))
299+ if (luaV_rawequalobj(&f->k[k], v))
282300 return k;
283301 /* else may be a collision (e.g., between 0.0 and "\0\0\0\0\0\0\0\0");
284302 go through and create a new entry for this value */
@@ -564,15 +582,15 @@ void luaK_storevar (FuncState *fs, expdesc *var, expdesc *ex) { · 279 unmodified lines
564582
565583
566584 void luaK_self (FuncState *fs, expdesc *e, expdesc *key) {
567− int func;
585+ int ereg;
568586 luaK_exp2anyreg(fs, e);
587+ ereg = e->u.info; /* register where 'e' was placed */
569588 freeexp(fs, e);
570− func = fs->freereg;
571− luaK_codeABC(fs, OP_SELF, func, e->u.info, luaK_exp2RK(fs, key));
572− freeexp(fs, key);
573− luaK_reserveregs(fs, 2);
574− e->u.info = func;
589+ e->u.info = fs->freereg; /* base register for op_self */
575590 e->k = VNONRELOC;
591+ luaK_reserveregs(fs, 2); /* function and 'self' produced by op_self */
592+ luaK_codeABC(fs, OP_SELF, e->u.info, ereg, luaK_exp2RK(fs, key));
593+ freeexp(fs, key);
576594 }
577595
578596
@@ -603,21 +621,14 @@ void luaK_goiftrue (FuncState *fs, expdesc *e) { · 24 unmodified lines
603621 int pc; /* pc of last jump */
604622 luaK_dischargevars(fs, e);
605623 switch (e->k) {
606− case VK: case VKNUM: case VTRUE: {
607− pc = NO_JUMP; /* always true; do nothing */
608− break;
609− }
610624 case VJMP: {
611625 invertjump(fs, e);
612626 pc = e->u.info;
613627 break;
614628 }
615− case VFALSE: {
616− if (!hasjumps(e)) {
617− pc = luaK_jump(fs); /* always jump */
618− break;
619− }
620− /* else go through */
629+ case VK: case VKNUM: case VTRUE: {
630+ pc = NO_JUMP; /* always true; do nothing */
631+ break;
621632 }
622633 default: {
623634 pc = jumponcond(fs, e, 0);
@@ -634,20 +645,13 @@ static void luaK_goiffalse (FuncState *fs, expdesc *e) { · 10 unmodified lines
634645 int pc; /* pc of last jump */
635646 luaK_dischargevars(fs, e);
636647 switch (e->k) {
637− case VNIL: case VFALSE: {
638− pc = NO_JUMP; /* always false; do nothing */
639− break;
640− }
641648 case VJMP: {
642649 pc = e->u.info;
643650 break;
644651 }
645− case VTRUE: {
646− if (!hasjumps(e)) {
647− pc = luaK_jump(fs); /* always jump */
648− break;
649− }
650− /* else go through */
652+ case VNIL: case VFALSE: {
653+ pc = NO_JUMP; /* always false; do nothing */
654+ break;
651655 }
652656 default: {
653657 pc = jumponcond(fs, e, 1);
src/lcode.h+3−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lcode.h,v 1.55 2010/07/02 20:42:40 roberto Exp $
2+** $Id: lcode.h,v 1.57 2011/04/07 18:14:12 roberto Exp $
33 ** Code generator for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -44,11 +44,9 @@ typedef enum UnOpr { OPR_MINUS, OPR_NOT, OPR_LEN, OPR_NOUNOPR } UnOpr; · 38 unmodified lines
4444
4545 #define luaK_jumpto(fs,t) luaK_patchlist(fs, luaK_jump(fs), t)
4646
47−#define luaK_codek(fs,reg,k) luaK_codeABxX(fs, OP_LOADK, reg, k)
48−
4947 LUAI_FUNC int luaK_codeABx (FuncState *fs, OpCode o, int A, unsigned int Bx);
5048 LUAI_FUNC int luaK_codeABC (FuncState *fs, OpCode o, int A, int B, int C);
51−LUAI_FUNC int luaK_codeABxX (FuncState *fs, OpCode o, int reg, int k);
49+LUAI_FUNC int luaK_codek (FuncState *fs, int reg, int k);
5250 LUAI_FUNC void luaK_fixline (FuncState *fs, int line);
5351 LUAI_FUNC void luaK_nil (FuncState *fs, int from, int n);
5452 LUAI_FUNC void luaK_reserveregs (FuncState *fs, int n);
@@ -71,6 +69,7 @@ LUAI_FUNC int luaK_jump (FuncState *fs); · 16 unmodified lines
7169 LUAI_FUNC void luaK_ret (FuncState *fs, int first, int nret);
7270 LUAI_FUNC void luaK_patchlist (FuncState *fs, int list, int target);
7371 LUAI_FUNC void luaK_patchtohere (FuncState *fs, int list);
72+LUAI_FUNC void luaK_patchclose (FuncState *fs, int list, int level);
7473 LUAI_FUNC void luaK_concat (FuncState *fs, int *l1, int l2);
7574 LUAI_FUNC int luaK_getlabel (FuncState *fs);
7675 LUAI_FUNC void luaK_prefix (FuncState *fs, UnOpr op, expdesc *v, int line);
src/ldblib.c+8−19
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldblib.c,v 1.126 2010/11/16 18:01:28 roberto Exp $
2+** $Id: ldblib.c,v 1.130 2011/04/08 19:17:36 roberto Exp $
33 ** Interface from Lua to its debug API
44 ** See Copyright Notice in lua.h
55 */
@@ -18,6 +18,9 @@ · 12 unmodified lines
1818 #include "lualib.h"
1919
2020
21+#define HOOKKEY "_HKEY"
22+
23+
2124
2225 static int db_getregistry (lua_State *L) {
2326 lua_pushvalue(L, LUA_REGISTRYINDEX);
@@ -39,8 +42,8 @@ static int db_setmetatable (lua_State *L) { · 15 unmodified lines
3942 luaL_argcheck(L, t == LUA_TNIL || t == LUA_TTABLE, 2,
4043 "nil or table expected");
4144 lua_settop(L, 2);
42− lua_pushboolean(L, lua_setmetatable(L, 1));
43− return 1;
45+ lua_setmetatable(L, 1);
46+ return 1; /* return 1st argument */
4447 }
4548
4649
@@ -250,14 +253,13 @@ static int db_upvaluejoin (lua_State *L) { · 203 unmodified lines
250253 }
251254
252255
253−static const char KEY_HOOK = 'h';
256+#define gethooktable(L) luaL_getsubtable(L, LUA_REGISTRYINDEX, HOOKKEY);
254257
255258
256259 static void hookf (lua_State *L, lua_Debug *ar) {
257260 static const char *const hooknames[] =
258261 {"call", "return", "line", "count", "tail call"};
259− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
260− lua_rawget(L, LUA_REGISTRYINDEX);
262+ gethooktable(L);
261263 lua_pushlightuserdata(L, L);
262264 lua_rawget(L, -2);
263265 if (lua_isfunction(L, -1)) {
@@ -291,19 +293,6 @@ static char *unmakemask (int mask, char *smask) { · 27 unmodified lines
291293 }
292294
293295
294−static void gethooktable (lua_State *L) {
295− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
296− lua_rawget(L, LUA_REGISTRYINDEX);
297− if (!lua_istable(L, -1)) {
298− lua_pop(L, 1);
299− lua_createtable(L, 0, 1);
300− lua_pushlightuserdata(L, (void *)&KEY_HOOK);
301− lua_pushvalue(L, -2);
302− lua_rawset(L, LUA_REGISTRYINDEX);
303− }
304−}
305−
306−
307296 static int db_sethook (lua_State *L) {
308297 int arg, mask, count;
309298 lua_Hook func;
src/ldebug.c+74−36
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldebug.c,v 2.74 2010/10/11 20:24:42 roberto Exp $
2+** $Id: ldebug.c,v 2.82 2011/06/02 19:31:40 roberto Exp $
33 ** Debug Interface
44 ** See Copyright Notice in lua.h
55 */
@@ -35,12 +35,12 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name); · 29 unmodified lines
3535
3636 static int currentpc (CallInfo *ci) {
3737 lua_assert(isLua(ci));
38− return pcRel(ci->u.l.savedpc, ci_func(ci)->l.p);
38+ return pcRel(ci->u.l.savedpc, ci_func(ci)->p);
3939 }
4040
4141
4242 static int currentline (CallInfo *ci) {
43− return getfuncline(ci_func(ci)->l.p, currentpc(ci));
43+ return getfuncline(ci_func(ci)->p, currentpc(ci));
4444 }
4545
4646
@@ -94,13 +94,28 @@ LUA_API int lua_getstack (lua_State *L, int level, lua_Debug *ar) { · 47 unmodified lines
9494 }
9595
9696
97+static const char *findvararg (CallInfo *ci, int n, StkId *pos) {
98+ int nparams = clLvalue(ci->func)->p->numparams;
99+ if (n >= ci->u.l.base - ci->func - nparams)
100+ return NULL; /* no such vararg */
101+ else {
102+ *pos = ci->func + nparams + n;
103+ return "(*vararg)"; /* generic name for any vararg */
104+ }
105+}
106+
107+
97108 static const char *findlocal (lua_State *L, CallInfo *ci, int n,
98109 StkId *pos) {
99110 const char *name = NULL;
100111 StkId base;
101112 if (isLua(ci)) {
102− base = ci->u.l.base;
103− name = luaF_getlocalname(ci_func(ci)->l.p, n, currentpc(ci));
113+ if (n < 0) /* access to vararg values? */
114+ return findvararg(ci, -n, pos);
115+ else {
116+ base = ci->u.l.base;
117+ name = luaF_getlocalname(ci_func(ci)->p, n, currentpc(ci));
118+ }
104119 }
105120 else
106121 base = ci->func + 1;
@@ -108,10 +123,8 @@ static const char *findlocal (lua_State *L, CallInfo *ci, int n, · 1 unmodified line
108123 StkId limit = (ci == L->ci) ? L->top : ci->next->func;
109124 if (limit - base >= n && n > 0) /* is 'n' inside 'ci' stack? */
110125 name = "(*temporary)"; /* generic name for any valid slot */
111− else {
112− *pos = base; /* to avoid warnings */
126+ else
113127 return NULL; /* no name */
114− }
115128 }
116129 *pos = base + (n - 1);
117130 return name;
@@ -125,10 +138,10 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { · 7 unmodified lines
125138 if (!isLfunction(L->top - 1)) /* not a Lua function? */
126139 name = NULL;
127140 else /* consider live variables at function start (parameters) */
128− name = luaF_getlocalname(clvalue(L->top - 1)->l.p, n, 0);
141+ name = luaF_getlocalname(clLvalue(L->top - 1)->p, n, 0);
129142 }
130143 else { /* active function; get information through 'ar' */
131− StkId pos;
144+ StkId pos = 0; /* to avoid warnings */
132145 name = findlocal(L, ar->i_ci, n, &pos);
133146 if (name) {
134147 setobj2s(L, L->top, pos);
@@ -141,11 +154,11 @@ LUA_API const char *lua_getlocal (lua_State *L, const lua_Debug *ar, int n) { · 6 unmodified lines
141154
142155
143156 LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) {
144− StkId pos;
157+ StkId pos = 0; /* to avoid warnings */
145158 const char *name = findlocal(L, ar->i_ci, n, &pos);
146159 lua_lock(L);
147160 if (name)
148− setobjs2s(L, pos, L->top - 1);
161+ setobjs2s(L, pos, L->top - 1);
149162 L->top--; /* pop value */
150163 lua_unlock(L);
151164 return name;
@@ -243,7 +256,7 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { · 91 unmodified lines
243256 if (*what == '>') {
244257 ci = NULL;
245258 func = L->top - 1;
246− luai_apicheck(L, ttisfunction(func));
259+ api_check(L, ttisfunction(func), "function expected");
247260 what++; /* skip the '>' */
248261 L->top--; /* pop function */
249262 }
@@ -271,21 +284,38 @@ LUA_API int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { · 21 unmodified lines
271284 ** =======================================================
272285 */
273286
287+static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
288+ const char **name);
274289
275−static void kname (Proto *p, int c, int reg, const char *what,
276− const char **name) {
277− if (c == reg && what && *what == 'c')
278− return; /* index is a constant; name already correct */
279− else if (ISK(c) && ttisstring(&p->k[INDEXK(c)]))
280− *name = svalue(&p->k[INDEXK(c)]);
281− else
282− *name = "?";
290+
291+/*
292+** find a "name" for the RK value 'c'
293+*/
294+static void kname (lua_State *L, CallInfo *ci, int c, int oreg,
295+ const char *what, const char **name) {
296+ if (ISK(c)) { /* is 'c' a constant? */
297+ TValue *kvalue = &ci_func(ci)->p->k[INDEXK(c)];
298+ if (ttisstring(kvalue)) { /* literal constant? */
299+ *name = svalue(kvalue); /* it is its own name */
300+ return;
301+ }
302+ /* else no reasonable name found */
303+ }
304+ else { /* 'c' is a register */
305+ if (c != oreg) /* not the original register? */
306+ what = getobjname(L, ci, c, name); /* search for 'c' */
307+ if (what && *what == 'c') { /* found a constant name? */
308+ return; /* 'name' already filled */
309+ }
310+ /* else no reasonable name found */
311+ }
312+ *name = "?"; /* no reasonable name found */
283313 }
284314
285315
286316 static const char *getobjname (lua_State *L, CallInfo *ci, int reg,
287317 const char **name) {
288− Proto *p = ci_func(ci)->l.p;
318+ Proto *p = ci_func(ci)->p;
289319 const char *what = NULL;
290320 int lastpc = currentpc(ci);
291321 int pc;
@@ -312,10 +342,10 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 20 unmodified lines
312342 if (reg == a) {
313343 int k = GETARG_C(i); /* key index */
314344 int t = GETARG_B(i);
315− const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
345+ const char *vn = (op == OP_GETTABLE) /* name of indexed variable */
316346 ? luaF_getlocalname(p, t + 1, pc)
317347 : getstr(p->upvalues[t].name);
318− kname(p, k, a, what, name);
348+ kname(L, ci, k, a, what, name);
319349 what = (vn && strcmp(vn, LUA_ENV) == 0) ? "global" : "field";
320350 }
321351 break;
@@ -329,10 +359,11 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 7 unmodified lines
329359 }
330360 break;
331361 }
332− case OP_LOADK: {
362+ case OP_LOADK:
363+ case OP_LOADKX: {
333364 if (reg == a) {
334− int b = GETARG_Bx(i);
335− b = (b > 0) ? b - 1 : GETARG_Ax(p->code[pc + 1]);
365+ int b = (op == OP_LOADK) ? GETARG_Bx(i)
366+ : GETARG_Ax(p->code[pc + 1]);
336367 if (ttisstring(&p->k[b])) {
337368 what = "constant";
338369 *name = svalue(&p->k[b]);
@@ -341,15 +372,15 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 2 unmodified lines
341372 break;
342373 }
343374 case OP_LOADNIL: {
344− int b = GETARG_B(i); /* move from 'b' to 'a' */
345− if (a <= reg && reg <= b) /* set registers from 'a' to 'b' */
375+ int b = GETARG_B(i);
376+ if (a <= reg && reg <= a + b) /* set registers from 'a' to 'a+b' */
346377 what = NULL;
347378 break;
348379 }
349380 case OP_SELF: {
350381 if (reg == a) {
351382 int k = GETARG_C(i); /* key index */
352− kname(p, k, a, what, name);
383+ kname(L, ci, k, a, what, name);
353384 what = "method";
354385 }
355386 break;
@@ -371,6 +402,10 @@ static const char *getobjname (lua_State *L, CallInfo *ci, int reg, · 15 unmodified lines
371402 pc += b; /* do the jump */
372403 break;
373404 }
405+ case OP_TEST: {
406+ if (reg == a) what = NULL; /* jumped code can change 'a' */
407+ break;
408+ }
374409 default:
375410 if (testAMode(op) && reg == a) what = NULL;
376411 break;
@@ -386,9 +421,9 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { · 9 unmodified lines
386421 if ((ci->callstatus & CIST_TAIL) || !isLua(ci->previous))
387422 return NULL; /* calling function is not Lua (or is unknown) */
388423 ci = ci->previous; /* calling function */
389− i = ci_func(ci)->l.p->code[currentpc(ci)];
424+ i = ci_func(ci)->p->code[currentpc(ci)];
390425 if (GET_OPCODE(i) == OP_EXTRAARG) /* extra argument? */
391− i = ci_func(ci)->l.p->code[currentpc(ci) - 1]; /* get 'real' instruction */
426+ i = ci_func(ci)->p->code[currentpc(ci) - 1]; /* get 'real' instruction */
392427 switch (GET_OPCODE(i)) {
393428 case OP_CALL:
394429 case OP_TAILCALL:
@@ -425,7 +460,10 @@ static const char *getfuncname (lua_State *L, CallInfo *ci, const char **name) { · 30 unmodified lines
425460
426461
427462
428−/* only ANSI way to check whether a pointer points to an array */
463+/*
464+** only ANSI way to check whether a pointer points to an array
465+** (used only for error messages, so efficiency is not a big concern)
466+*/
429467 static int isinstack (CallInfo *ci, const TValue *o) {
430468 StkId p;
431469 for (p = ci->u.l.base; p < ci->top; p++)
@@ -436,7 +474,7 @@ static int isinstack (CallInfo *ci, const TValue *o) { · 4 unmodified lines
436474
437475 static const char *getupvalname (CallInfo *ci, const TValue *o,
438476 const char **name) {
439− LClosure *c = &ci_func(ci)->l;
477+ LClosure *c = ci_func(ci);
440478 int i;
441479 for (i = 0; i < c->nupvalues; i++) {
442480 if (c->upvals[i]->v == o) {
@@ -455,7 +493,7 @@ void luaG_typeerror (lua_State *L, const TValue *o, const char *op) { · 12 unmodified lines
455493 const char *kind = NULL;
456494 if (isLua(ci)) {
457495 kind = getupvalname(ci, o, &name); /* check whether 'o' is an upvalue */
458− if (!kind && isinstack(ci, o)) /* no? try a register */
496+ if (!kind && isinstack(ci, o)) /* no? try a register */
459497 kind = getobjname(L, ci, cast_int(o - ci->u.l.base), &name);
460498 }
461499 if (kind)
@@ -497,7 +535,7 @@ static void addinfo (lua_State *L, const char *msg) { · 35 unmodified lines
497535 if (isLua(ci)) { /* is Lua code? */
498536 char buff[LUA_IDSIZE]; /* add file:line information */
499537 int line = currentline(ci);
500− TString *src = ci_func(ci)->l.p->source;
538+ TString *src = ci_func(ci)->p->source;
501539 if (src)
502540 luaO_chunkid(buff, getstr(src), LUA_IDSIZE);
503541 else { /* no source available; use "?" instead */
src/ldebug.h+4−1
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldebug.h,v 2.5 2009/06/10 16:57:53 roberto Exp $
2+** $Id: ldebug.h,v 2.6 2011/06/02 19:31:40 roberto Exp $
33 ** Auxiliary functions from Debug Interface module
44 ** See Copyright Notice in lua.h
55 */
@@ -17,6 +17,9 @@ · 11 unmodified lines
1717
1818 #define resethookcount(L) (L->hookcount = L->basehookcount)
1919
20+/* Active Lua function (given call info) */
21+#define ci_func(ci) (clLvalue((ci)->func))
22+
2023
2124 LUAI_FUNC void luaG_typeerror (lua_State *L, const TValue *o,
2225 const char *opname);
src/ldo.c+64−66
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldo.c,v 2.90 2010/10/25 19:01:37 roberto Exp $
2+** $Id: ldo.c,v 2.95 2011/06/02 19:31:40 roberto Exp $
33 ** Stack and Call structure of Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -293,65 +293,59 @@ static StkId tryfuncTM (lua_State *L, StkId func) { · 287 unmodified lines
293293 ** returns true if function has been executed (C function)
294294 */
295295 int luaD_precall (lua_State *L, StkId func, int nresults) {
296− Closure *cl;
297296 lua_CFunction f;
298− ptrdiff_t funcr;
299− if (!ttisfunction(func)) /* `func' is not a function? */
300− func = tryfuncTM(L, func); /* check the `function' tag method */
301− funcr = savestack(L, func);
302− if (ttislcf(func)) { /* light C function? */
303− f = fvalue(func); /* get it */
304− goto isCfunc; /* go to call it */
305− }
306− cl = clvalue(func);
307− if (cl->c.isC) { /* C closure? */
308− CallInfo *ci;
309− int n;
310− f = cl->c.f;
311− isCfunc: /* call C function 'f' */
312− luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
313− ci = next_ci(L); /* now 'enter' new function */
314− ci->nresults = nresults;
315− ci->func = restorestack(L, funcr);
316− ci->top = L->top + LUA_MINSTACK;
317− lua_assert(ci->top <= L->stack_last);
318− ci->callstatus = 0;
319− if (L->hookmask & LUA_MASKCALL)
320− luaD_hook(L, LUA_HOOKCALL, -1);
321− lua_unlock(L);
322− n = (*f)(L); /* do the actual call */
323− lua_lock(L);
324− api_checknelems(L, n);
325− luaD_poscall(L, L->top - n);
326− return 1;
327− }
328− else { /* Lua function: prepare its call */
329− CallInfo *ci;
330− int nparams, nargs;
331− StkId base;
332− Proto *p = cl->l.p;
333− luaD_checkstack(L, p->maxstacksize);
334− func = restorestack(L, funcr);
335− nargs = cast_int(L->top - func) - 1; /* number of real arguments */
336− nparams = p->numparams; /* number of expected parameters */
337− for (; nargs < nparams; nargs++)
338− setnilvalue(L->top++); /* complete missing arguments */
339− if (!p->is_vararg) /* no varargs? */
340− base = func + 1;
341− else /* vararg function */
342− base = adjust_varargs(L, p, nargs);
343− ci = next_ci(L); /* now 'enter' new function */
344− ci->nresults = nresults;
345− ci->func = func;
346− ci->u.l.base = base;
347− ci->top = base + p->maxstacksize;
348− lua_assert(ci->top <= L->stack_last);
349− ci->u.l.savedpc = p->code; /* starting point */
350− ci->callstatus = CIST_LUA;
351− L->top = ci->top;
352− if (L->hookmask & LUA_MASKCALL)
353− callhook(L, ci);
354− return 0;
297+ CallInfo *ci;
298+ int n; /* number of arguments (Lua) or returns (C) */
299+ ptrdiff_t funcr = savestack(L, func);
300+ switch (ttype(func)) {
301+ case LUA_TLCF: /* light C function */
302+ f = fvalue(func);
303+ goto Cfunc;
304+ case LUA_TCCL: { /* C closure */
305+ f = clCvalue(func)->f;
306+ Cfunc:
307+ luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */
308+ ci = next_ci(L); /* now 'enter' new function */
309+ ci->nresults = nresults;
310+ ci->func = restorestack(L, funcr);
311+ ci->top = L->top + LUA_MINSTACK;
312+ lua_assert(ci->top <= L->stack_last);
313+ ci->callstatus = 0;
314+ if (L->hookmask & LUA_MASKCALL)
315+ luaD_hook(L, LUA_HOOKCALL, -1);
316+ lua_unlock(L);
317+ n = (*f)(L); /* do the actual call */
318+ lua_lock(L);
319+ api_checknelems(L, n);
320+ luaD_poscall(L, L->top - n);
321+ return 1;
322+ }
323+ case LUA_TLCL: { /* Lua function: prepare its call */
324+ StkId base;
325+ Proto *p = clLvalue(func)->p;
326+ luaD_checkstack(L, p->maxstacksize);
327+ func = restorestack(L, funcr);
328+ n = cast_int(L->top - func) - 1; /* number of real arguments */
329+ for (; n < p->numparams; n++)
330+ setnilvalue(L->top++); /* complete missing arguments */
331+ base = (!p->is_vararg) ? func + 1 : adjust_varargs(L, p, n);
332+ ci = next_ci(L); /* now 'enter' new function */
333+ ci->nresults = nresults;
334+ ci->func = func;
335+ ci->u.l.base = base;
336+ ci->top = base + p->maxstacksize;
337+ lua_assert(ci->top <= L->stack_last);
338+ ci->u.l.savedpc = p->code; /* starting point */
339+ ci->callstatus = CIST_LUA;
340+ L->top = ci->top;
341+ if (L->hookmask & LUA_MASKCALL)
342+ callhook(L, ci);
343+ return 0;
344+ }
345+ default: { /* not a function */
346+ func = tryfuncTM(L, func); /* retry with 'function' tag method */
347+ return luaD_precall(L, func, nresults);
348+ }
355349 }
356350 }
357351
@@ -615,8 +609,8 @@ int luaD_pcall (lua_State *L, Pfunc func, void *u, · 257 unmodified lines
615609 */
616610 struct SParser { /* data to `f_parser' */
617611 ZIO *z;
618− Mbuffer buff; /* buffer to be used by the scanner */
619− Varlist varl; /* list of local variables (to be used by the parser) */
612+ Mbuffer buff; /* dynamic structure used by the scanner */
613+ Dyndata dyd; /* dynamic structures used by the parser */
620614 const char *name;
621615 };
622616
@@ -625,14 +619,14 @@ static void f_parser (lua_State *L, void *ud) { · 2 unmodified lines
625619 Proto *tf;
626620 Closure *cl;
627621 struct SParser *p = cast(struct SParser *, ud);
628− int c = luaZ_lookahead(p->z);
622+ int c = zgetc(p->z); /* read first character */
629623 tf = (c == LUA_SIGNATURE[0])
630624 ? luaU_undump(L, p->z, &p->buff, p->name)
631− : luaY_parser(L, p->z, &p->buff, &p->varl, p->name);
625+ : luaY_parser(L, p->z, &p->buff, &p->dyd, p->name, c);
632626 setptvalue2s(L, L->top, tf);
633627 incr_top(L);
634628 cl = luaF_newLclosure(L, tf);
635− setclvalue(L, L->top - 1, cl);
629+ setclLvalue(L, L->top - 1, cl);
636630 for (i = 0; i < tf->sizeupvalues; i++) /* initialize upvalues */
637631 cl->l.upvals[i] = luaF_newupval(L);
638632 }
@@ -643,11 +637,15 @@ int luaD_protectedparser (lua_State *L, ZIO *z, const char *name) { · 4 unmodified lines
643637 int status;
644638 L->nny++; /* cannot yield during parsing */
645639 p.z = z; p.name = name;
646− p.varl.actvar = NULL; p.varl.nactvar = p.varl.actvarsize = 0;
640+ p.dyd.actvar.arr = NULL; p.dyd.actvar.size = 0;
641+ p.dyd.gt.arr = NULL; p.dyd.gt.size = 0;
642+ p.dyd.label.arr = NULL; p.dyd.label.size = 0;
647643 luaZ_initbuffer(L, &p.buff);
648644 status = luaD_pcall(L, f_parser, &p, savestack(L, L->top), L->errfunc);
649645 luaZ_freebuffer(L, &p.buff);
650− luaM_freearray(L, p.varl.actvar, p.varl.actvarsize);
646+ luaM_freearray(L, p.dyd.actvar.arr, p.dyd.actvar.size);
647+ luaM_freearray(L, p.dyd.gt.arr, p.dyd.gt.size);
648+ luaM_freearray(L, p.dyd.label.arr, p.dyd.label.size);
651649 L->nny--;
652650 return status;
653651 }
src/ldump.c+3−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ldump.c,v 1.17 2010/10/13 21:04:52 lhf Exp $
2+** $Id: ldump.c,v 1.18 2011/05/06 13:35:17 lhf Exp $
33 ** save precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -69,7 +69,7 @@ static void DumpString(const TString* s, DumpState* D) · 63 unmodified lines
6969 {
7070 size_t size=s->tsv.len+1; /* include trailing '\0' */
7171 DumpVar(size,D);
72− DumpBlock(getstr(s),size,D);
72+ DumpBlock(getstr(s),size*sizeof(char),D);
7373 }
7474 }
7575
@@ -150,7 +150,7 @@ static void DumpFunction(const Proto* f, DumpState* D) · 74 unmodified lines
150150
151151 static void DumpHeader(DumpState* D)
152152 {
153− char h[LUAC_HEADERSIZE];
153+ lu_byte h[LUAC_HEADERSIZE];
154154 luaU_header(h);
155155 DumpBlock(h,LUAC_HEADERSIZE,D);
156156 }
src/lgc.c+79−58
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.c,v 2.103 2010/10/25 19:01:37 roberto Exp $
2+** $Id: lgc.c,v 2.109 2011/05/05 19:42:25 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -51,7 +51,7 @@ · 45 unmodified lines
5151 ** standard negative debt for GC; a reasonable "time" to wait before
5252 ** starting a new cycle
5353 */
54−#define stddebt(g) (-cast(l_mem, g->totalbytes/100) * g->gcpause)
54+#define stddebt(g) (-cast(l_mem, gettotalbytes(g)/100) * g->gcpause)
5555
5656
5757 /*
@@ -68,11 +68,15 @@ · 10 unmodified lines
6868 #define stringmark(s) ((void)((s) && resetbits((s)->tsv.marked, WHITEBITS)))
6969
7070
71−#define isfinalized(u) testbit((u)->marked, FINALIZEDBIT)
71+#define isfinalized(x) testbit(gch(x)->marked, FINALIZEDBIT)
7272
7373 #define checkdeadkey(n) lua_assert(!ttisdeadkey(gkey(n)) || ttisnil(gval(n)))
7474
7575
76+#define checkconsistency(obj) \
77+ lua_longassert(!iscollectable(obj) || righttt(obj))
78+
79+
7680 #define markvalue(g,o) { checkconsistency(o); \
7781 if (valiswhite(o)) reallymarkobject(g,gcvalue(o)); }
7882
@@ -114,12 +118,11 @@ static void removeentry (Node *n) { · 35 unmodified lines
114118 */
115119 static int iscleared (const TValue *o, int iskey) {
116120 if (!iscollectable(o)) return 0;
117− if (ttisstring(o)) {
121+ else if (ttisstring(o)) {
118122 stringmark(rawtsvalue(o)); /* strings are `values', so are never weak */
119123 return 0;
120124 }
121− return iswhite(gcvalue(o)) ||
122− (ttisuserdata(o) && (!iskey && isfinalized(uvalue(o))));
125+ else return iswhite(gcvalue(o)) || (!iskey && isfinalized(gcvalue(o)));
123126 }
124127
125128
@@ -319,8 +322,7 @@ static void remarkupvals (global_State *g) { · 193 unmodified lines
319322 ** mark root set and reset all gray lists, to start a new
320323 ** incremental (or full) collection
321324 */
322−static void markroot (lua_State *L) {
323− global_State *g = G(L);
325+static void markroot (global_State *g) {
324326 g->gray = g->grayagain = NULL;
325327 g->weak = g->allweak = g->ephemeron = NULL;
326328 markobject(g, g->mainthread);
@@ -635,6 +637,7 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { · 308 unmodified lines
635637 int ow = otherwhite(g);
636638 int toclear, toset; /* bits to clear and to set in all live objects */
637639 int tostop; /* stop sweep when this is true */
640+ l_mem debt = g->GCdebt; /* current debt */
638641 if (isgenerational(g)) { /* generational mode? */
639642 toclear = ~0; /* clear nothing */
640643 toset = bitmask(OLDBIT); /* set the old bit of all surviving objects */
@@ -657,13 +660,15 @@ static GCObject **sweeplist (lua_State *L, GCObject **p, lu_mem count) { · 16 unmodified lines
657660 sweepthread(L, gco2th(curr)); /* sweep thread's upvalues */
658661 if (testbits(marked, tostop)) {
659662 static GCObject *nullp = NULL;
660− return &nullp; /* stop sweeping this list */
663+ p = &nullp; /* stop sweeping this list */
664+ break;
661665 }
662666 /* update marks */
663667 gch(curr)->marked = cast_byte((marked & toclear) | toset);
664668 p = &gch(curr)->next; /* go to next element */
665669 }
666670 }
671+ luaE_setdebt(g, debt); /* sweeping should not change debt */
667672 return p;
668673 }
669674
@@ -687,19 +692,17 @@ static void checkSizes (lua_State *L) { · 17 unmodified lines
687692 }
688693
689694
690−static Udata *udata2finalize (global_State *g) {
695+static GCObject *udata2finalize (global_State *g) {
691696 GCObject *o = g->tobefnz; /* get first element */
692− Udata *u = rawgco2u(o);
693− lua_assert(isfinalized(&u->uv));
694− lua_assert(!isold(o));
695− g->tobefnz = u->uv.next; /* remove it from 'tobefnz' list */
696− u->uv.next = g->allgc; /* return it to 'allgc' list */
697+ lua_assert(isfinalized(o));
698+ g->tobefnz = gch(o)->next; /* remove it from 'tobefnz' list */
699+ gch(o)->next = g->allgc; /* return it to 'allgc' list */
697700 g->allgc = o;
698− resetbit(u->uv.marked, SEPARATED); /* mark that it is not in 'tobefnz' */
699− resetoldbit(o); /* see MOVE OLD rule */
701+ resetbit(gch(o)->marked, SEPARATED); /* mark that it is not in 'tobefnz' */
702+ lua_assert(!isold(o)); /* see MOVE OLD rule */
700703 if (!keepinvariant(g)) /* not keeping invariant? */
701704 makewhite(g, o); /* "sweep" object */
702− return u;
705+ return o;
703706 }
704707
705708
@@ -711,20 +714,22 @@ static void dothecall (lua_State *L, void *ud) { · 5 unmodified lines
711714
712715 static void GCTM (lua_State *L, int propagateerrors) {
713716 global_State *g = G(L);
714− Udata *udata = udata2finalize(g);
715− const TValue *tm = gfasttm(g, udata->uv.metatable, TM_GC);
717+ const TValue *tm;
718+ TValue v;
719+ setgcovalue(L, &v, udata2finalize(g));
720+ tm = luaT_gettmbyobj(L, &v, TM_GC);
716721 if (tm != NULL && ttisfunction(tm)) { /* is there a finalizer? */
717722 int status;
718723 lu_byte oldah = L->allowhook;
719− lu_mem oldd = g->GCdebt;
724+ int running = g->gcrunning;
720725 L->allowhook = 0; /* stop debug hooks during GC tag method */
721− stopgc(g); /* avoid GC steps */
726+ g->gcrunning = 0; /* avoid GC steps */
722727 setobj2s(L, L->top, tm); /* push finalizer... */
723− setuvalue(L, L->top+1, udata); /* ... and its argument */
728+ setobj2s(L, L->top + 1, &v); /* ... and its argument */
724729 L->top += 2; /* and (next line) call the finalizer */
725730 status = luaD_pcall(L, dothecall, NULL, savestack(L, L->top - 2), 0);
726731 L->allowhook = oldah; /* restore hooks */
727− g->GCdebt = oldd; /* restore threshold */
732+ g->gcrunning = running; /* restore state */
728733 if (status != LUA_OK && propagateerrors) { /* error while running __gc? */
729734 if (status == LUA_ERRRUN) { /* is there an error msg.? */
730735 luaO_pushfstring(L, "error in __gc tag method (%s)",
@@ -738,25 +743,25 @@ static void GCTM (lua_State *L, int propagateerrors) { · 7 unmodified lines
738743
739744
740745 /*
741−** move all unreachable udata that need finalization from list 'udgc' to
742−** list 'tobefnz'
746+** move all unreachable objects that need finalization from list 'finobj'
747+** to list 'tobefnz'
743748 */
744749 void luaC_separateudata (lua_State *L, int all) {
745750 global_State *g = G(L);
746− GCObject **p = &g->udgc;
751+ GCObject **p = &g->finobj;
747752 GCObject *curr;
748753 GCObject **lastnext = &g->tobefnz;
749754 /* find last 'next' field in 'tobefnz' list (to add elements in its end) */
750755 while (*lastnext != NULL)
751756 lastnext = &gch(*lastnext)->next;
752757 while ((curr = *p) != NULL) { /* traverse all finalizable objects */
753− lua_assert(gch(curr)->tt == LUA_TUSERDATA && !isfinalized(gco2u(curr)));
758+ lua_assert(!isfinalized(curr));
754759 lua_assert(testbit(gch(curr)->marked, SEPARATED));
755760 if (!(all || iswhite(curr))) /* not being collected? */
756761 p = &gch(curr)->next; /* don't bother with it */
757762 else {
758763 l_setbit(gch(curr)->marked, FINALIZEDBIT); /* won't be finalized again */
759− *p = gch(curr)->next; /* remove 'curr' from 'udgc' list */
764+ *p = gch(curr)->next; /* remove 'curr' from 'finobj' list */
760765 gch(curr)->next = *lastnext; /* link at the end of 'tobefnz' list */
761766 *lastnext = curr;
762767 lastnext = &gch(curr)->next;
@@ -766,23 +771,23 @@ void luaC_separateudata (lua_State *L, int all) { · 3 unmodified lines
766771
767772
768773 /*
769−** if userdata 'u' has a finalizer, remove it from 'allgc' list (must
770−** search the list to find it) and link it in 'udgc' list.
774+** if object 'o' has a finalizer, remove it from 'allgc' list (must
775+** search the list to find it) and link it in 'finobj' list.
771776 */
772−void luaC_checkfinalizer (lua_State *L, Udata *u) {
777+void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt) {
773778 global_State *g = G(L);
774− if (testbit(u->uv.marked, SEPARATED) || /* udata is already separated... */
775− isfinalized(&u->uv) || /* ... or is finalized... */
776− gfasttm(g, u->uv.metatable, TM_GC) == NULL) /* or has no finalizer? */
779+ if (testbit(gch(o)->marked, SEPARATED) || /* obj. is already separated... */
780+ isfinalized(o) || /* ... or is finalized... */
781+ gfasttm(g, mt, TM_GC) == NULL) /* or has no finalizer? */
777782 return; /* nothing to be done */
778− else { /* move 'u' to 'udgc' list */
783+ else { /* move 'o' to 'finobj' list */
779784 GCObject **p;
780− for (p = &g->allgc; *p != obj2gco(u); p = &gch(*p)->next) ;
781− *p = u->uv.next; /* remove 'u' from root list */
782− u->uv.next = g->udgc; /* link it in list 'udgc' */
783− g->udgc = obj2gco(u);
784− l_setbit(u->uv.marked, SEPARATED); /* mark it as such */
785− resetoldbit(obj2gco(u)); /* see MOVE OLD rule */
785+ for (p = &g->allgc; *p != o; p = &gch(*p)->next) ;
786+ *p = gch(o)->next; /* remove 'o' from root list */
787+ gch(o)->next = g->finobj; /* link it in list 'finobj' */
788+ g->finobj = o;
789+ l_setbit(gch(o)->marked, SEPARATED); /* mark it as such */
790+ resetoldbit(o); /* see MOVE OLD rule */
786791 }
787792 }
788793
@@ -808,7 +813,7 @@ void luaC_changemode (lua_State *L, int mode) { · 19 unmodified lines
808813 if (mode == KGC_GEN) { /* change to generational mode */
809814 /* make sure gray lists are consistent */
810815 luaC_runtilstate(L, bitmask(GCSpropagate));
811− g->lastmajormem = g->totalbytes;
816+ g->lastmajormem = gettotalbytes(g);
812817 g->gckind = KGC_GEN;
813818 }
814819 else { /* change to incremental mode */
@@ -823,10 +828,14 @@ void luaC_changemode (lua_State *L, int mode) { · 8 unmodified lines
823828
824829
825830 /*
826−** call all pending finalizers */
831+** call all pending finalizers
832+*/
827833 static void callallpendingfinalizers (lua_State *L, int propagateerrors) {
828834 global_State *g = G(L);
829− while (g->tobefnz) GCTM(L, propagateerrors);
835+ while (g->tobefnz) {
836+ resetoldbit(g->tobefnz);
837+ GCTM(L, propagateerrors);
838+ }
830839 }
831840
832841
@@ -837,8 +846,8 @@ void luaC_freeallobjects (lua_State *L) { · 4 unmodified lines
837846 /* following "white" makes all objects look dead */
838847 g->currentwhite = WHITEBITS;
839848 g->gckind = KGC_NORMAL;
840− sweepwholelist(L, &g->udgc);
841− lua_assert(g->udgc == NULL);
849+ sweepwholelist(L, &g->finobj);
850+ lua_assert(g->finobj == NULL);
842851 sweepwholelist(L, &g->allgc);
843852 lua_assert(g->allgc == NULL);
844853 for (i = 0; i < g->strt.size; i++) /* free all string lists */
@@ -883,7 +892,7 @@ static l_mem singlestep (lua_State *L) { · 38 unmodified lines
883892 switch (g->gcstate) {
884893 case GCSpause: {
885894 if (!isgenerational(g))
886− markroot(L); /* start a new collection */
895+ markroot(g); /* start a new collection */
887896 /* in any case, root must be marked */
888897 lua_assert(!iswhite(obj2gco(g->mainthread))
889898 && !iswhite(gcvalue(&g->l_registry)));
@@ -905,7 +914,7 @@ static l_mem singlestep (lua_State *L) { · 15 unmodified lines
905914 return GCSWEEPCOST;
906915 }
907916 else { /* no more strings to sweep */
908− g->sweepgc = &g->udgc; /* prepare to sweep userdata */
917+ g->sweepgc = &g->finobj; /* prepare to sweep finalizable objects */
909918 g->gcstate = GCSsweepudata;
910919 return 0;
911920 }
@@ -955,15 +964,15 @@ static void generationalcollection (lua_State *L) { · 43 unmodified lines
955964 global_State *g = G(L);
956965 if (g->lastmajormem == 0) { /* signal for another major collection? */
957966 luaC_fullgc(L, 0); /* perform a full regular collection */
958− g->lastmajormem = g->totalbytes; /* update control */
967+ g->lastmajormem = gettotalbytes(g); /* update control */
959968 }
960969 else {
961970 luaC_runtilstate(L, ~bitmask(GCSpause)); /* run complete cycle */
962971 luaC_runtilstate(L, bitmask(GCSpause));
963− if (g->totalbytes > g->lastmajormem/100 * g->gcmajorinc)
972+ if (gettotalbytes(g) > g->lastmajormem/100 * g->gcmajorinc)
964973 g->lastmajormem = 0; /* signal for a major collection */
965974 }
966− g->GCdebt = stddebt(g);
975+ luaE_setdebt(g, stddebt(g));
967976 }
968977
969978
@@ -974,21 +983,33 @@ static void step (lua_State *L) { · 4 unmodified lines
974983 lim -= singlestep(L);
975984 } while (lim > 0 && g->gcstate != GCSpause);
976985 if (g->gcstate != GCSpause)
977− g->GCdebt -= GCSTEPSIZE;
986+ luaE_setdebt(g, g->GCdebt - GCSTEPSIZE);
978987 else
979− g->GCdebt = stddebt(g);
988+ luaE_setdebt(g, stddebt(g));
980989 }
981990
982991
983−void luaC_step (lua_State *L) {
992+/*
993+** performs a basic GC step even if the collector is stopped
994+*/
995+void luaC_forcestep (lua_State *L) {
996+ global_State *g = G(L);
984997 int i;
985− if (isgenerational(G(L))) generationalcollection(L);
998+ if (isgenerational(g)) generationalcollection(L);
986999 else step(L);
987− for (i = 0; i < GCFINALIZENUM && G(L)->tobefnz; i++)
1000+ for (i = 0; i < GCFINALIZENUM && g->tobefnz; i++)
9881001 GCTM(L, 1); /* Call a few pending finalizers */
9891002 }
9901003
9911004
1005+/*
1006+** performs a basic GC step only if collector is running
1007+*/
1008+void luaC_step (lua_State *L) {
1009+ if (G(L)->gcrunning) luaC_forcestep(L);
1010+}
1011+
1012+
9921013 /*
9931014 ** performs a full GC cycle; if "isemergency", does not call
9941015 ** finalizers (which could change stack positions)
@@ -1016,7 +1037,7 @@ void luaC_fullgc (lua_State *L, int isemergency) { · 21 unmodified lines
10161037 luaC_runtilstate(L, bitmask(GCSpropagate));
10171038 }
10181039 g->gckind = origkind;
1019− g->GCdebt = stddebt(g);
1040+ luaE_setdebt(g, stddebt(g));
10201041 if (!isemergency) /* do not run finalizers during emergency GC */
10211042 callallpendingfinalizers(L, 1);
10221043 }
src/lgc.h+8−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lgc.h,v 2.44 2010/06/30 14:11:17 roberto Exp $
2+** $Id: lgc.h,v 2.50 2011/01/26 16:30:02 roberto Exp $
33 ** Garbage Collector
44 ** See Copyright Notice in lua.h
55 */
@@ -52,10 +52,6 @@ · 46 unmodified lines
5252 #define keepinvariant(g) (isgenerational(g) || g->gcstate <= GCSatomic)
5353
5454
55−#define gcstopped(g) ((g)->GCdebt == MIN_LMEM)
56−#define stopgc(g) ((g)->GCdebt = MIN_LMEM)
57−
58−
5955 /*
6056 ** some useful bit tricks
6157 */
@@ -76,8 +72,8 @@ · 14 unmodified lines
7672 #define WHITE0BIT 0 /* object is white (type 0) */
7773 #define WHITE1BIT 1 /* object is white (type 1) */
7874 #define BLACKBIT 2 /* object is black */
79−#define FINALIZEDBIT 3 /* for userdata: has been finalized */
80−#define SEPARATED 4 /* " ": it's in 'udgc' list or in 'tobefnz' */
75+#define FINALIZEDBIT 3 /* object has been separated for finalization */
76+#define SEPARATED 4 /* object is in 'finobj' list or in 'tobefnz' */
8177 #define FIXEDBIT 5 /* object is fixed (should not be collected) */
8278 #define OLDBIT 6 /* object is old (only in generational mode) */
8379 /* bit 7 is currently used by tests (luaL_checkmemory) */
@@ -108,7 +104,9 @@ · 24 unmodified lines
108104 #define luaC_white(g) cast(lu_byte, (g)->currentwhite & WHITEBITS)
109105
110106
111−#define luaC_checkGC(L) {condchangemem(L); if (G(L)->GCdebt > 0) luaC_step(L);}
107+#define luaC_condGC(L,c) \
108+ {if (G(L)->GCdebt > 0) {c;}; condchangemem(L);}
109+#define luaC_checkGC(L) luaC_condGC(L, luaC_step(L);)
112110
113111
114112 #define luaC_barrier(L,p,v) { if (valiswhite(v) && isblack(obj2gco(p))) \
@@ -130,6 +128,7 @@ · 15 unmodified lines
130128 LUAI_FUNC void luaC_separateudata (lua_State *L, int all);
131129 LUAI_FUNC void luaC_freeallobjects (lua_State *L);
132130 LUAI_FUNC void luaC_step (lua_State *L);
131+LUAI_FUNC void luaC_forcestep (lua_State *L);
133132 LUAI_FUNC void luaC_runtilstate (lua_State *L, int statesmask);
134133 LUAI_FUNC void luaC_fullgc (lua_State *L, int isemergency);
135134 LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz,
@@ -137,7 +136,7 @@ LUAI_FUNC GCObject *luaC_newobj (lua_State *L, int tt, size_t sz, · 1 unmodified line
137136 LUAI_FUNC void luaC_barrier_ (lua_State *L, GCObject *o, GCObject *v);
138137 LUAI_FUNC void luaC_barrierback_ (lua_State *L, GCObject *o);
139138 LUAI_FUNC void luaC_barrierproto_ (lua_State *L, Proto *p, Closure *c);
140−LUAI_FUNC void luaC_checkfinalizer (lua_State *L, Udata *u);
139+LUAI_FUNC void luaC_checkfinalizer (lua_State *L, GCObject *o, Table *mt);
141140 LUAI_FUNC void luaC_checkupvalcolor (global_State *g, UpVal *uv);
142141 LUAI_FUNC void luaC_changemode (lua_State *L, int mode);
143142
src/linit.c+5−5
@@ -1,16 +1,16 @@
11 /*
2−** $Id: linit.c,v 1.30 2010/11/12 15:48:30 roberto Exp $
3−** Initialization of libraries for lua.c and other clients
2+** $Id: linit.c,v 1.32 2011/04/08 19:17:36 roberto Exp $
3+** Initialization of libraries for lua.c and other clients
44 ** See Copyright Notice in lua.h
55 */
66
77
8−/*
8+/*
99 ** If you embed Lua in your program and need to open the standard
1010 ** libraries, call luaL_openlibs in your program. If you need a
1111 ** different set of libraries, copy this file to your project and edit
1212 ** it to suit your needs.
13−*/
13+*/
1414
1515
1616 #define linit_c
@@ -57,7 +57,7 @@ LUALIB_API void luaL_openlibs (lua_State *L) { · 40 unmodified lines
5757 lua_pop(L, 1); /* remove lib */
5858 }
5959 /* add open functions from 'preloadedlibs' into 'package.preload' table */
60− luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
60+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
6161 for (lib = preloadedlibs; lib->func; lib++) {
6262 lua_pushcfunction(L, lib->func);
6363 lua_setfield(L, -2, lib->name);
src/liolib.c+34−57
@@ -1,5 +1,5 @@
11 /*
2−** $Id: liolib.c,v 2.95 2010/11/10 18:05:36 roberto Exp $
2+** $Id: liolib.c,v 2.99 2011/03/03 16:34:46 roberto Exp $
33 ** Standard I/O (and system) library
44 ** See Copyright Notice in lua.h
55 */
@@ -26,28 +26,29 @@ · 20 unmodified lines
2626 ** lua_popen spawns a new process connected to the current one through
2727 ** the file streams.
2828 */
29−#if !defined(lua_popen)
29+#if !defined(lua_popen) /* { */
3030
31−#if defined(LUA_USE_POPEN)
31+#if defined(LUA_USE_POPEN) /* { */
3232
33−#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34−#define lua_pclose(L,file) ((void)L, pclose(file))
33+#define lua_popen(L,c,m) ((void)L, fflush(NULL), popen(c,m))
34+#define lua_pclose(L,file) ((void)L, pclose(file))
3535
36−#elif defined(LUA_WIN)
36+#elif defined(LUA_WIN) /* }{ */
3737
38−#define lua_popen(L,c,m) ((void)L, _popen(c,m))
39−#define lua_pclose(L,file) ((void)L, _pclose(file))
38+#define lua_popen(L,c,m) ((void)L, _popen(c,m))
39+#define lua_pclose(L,file) ((void)L, _pclose(file))
4040
41−#else
4241
43−#define lua_popen(L,c,m) ((void)((void)c, m), \
44− luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
45−#define lua_pclose(L,file) ((void)((void)L, file), -1)
42+#else /* }{ */
4643
47−#endif
44+#define lua_popen(L,c,m) ((void)((void)c, m), \
45+ luaL_error(L, LUA_QL("popen") " not supported"), (FILE*)0)
46+#define lua_pclose(L,file) ((void)((void)L, file), -1)
4847
49−#endif
5048
49+#endif /* } */
50+
51+#endif /* } */
5152
5253
5354 #define IO_INPUT 1
@@ -57,24 +58,6 @@ · 3 unmodified lines
5758 static const char *const fnames[] = {"input", "output"};
5859
5960
60−static int pushresult (lua_State *L, int i, const char *filename) {
61− int en = errno; /* calls to Lua API may change this value */
62− if (i) {
63− lua_pushboolean(L, 1);
64− return 1;
65− }
66− else {
67− lua_pushnil(L);
68− if (filename)
69− lua_pushfstring(L, "%s: %s", filename, strerror(en));
70− else
71− lua_pushfstring(L, "%s", strerror(en));
72− lua_pushinteger(L, en);
73− return 3;
74− }
75−}
76−
77−
7861 static void fileerror (lua_State *L, int arg, const char *filename) {
7962 lua_pushfstring(L, "%s: %s", filename, strerror(errno));
8063 luaL_argerror(L, arg, lua_tostring(L, -1));
@@ -143,13 +126,8 @@ static int io_noclose (lua_State *L) { · 62 unmodified lines
143126 static int io_pclose (lua_State *L) {
144127 FILE **p = tofilep(L);
145128 int stat = lua_pclose(L, *p);
146− *p = NULL;
147− if (stat == -1) /* error? */
148− return pushresult(L, 0, NULL);
149− else {
150− lua_pushinteger(L, stat);
151− return 1; /* return status */
152− }
129+ *p = NULL; /* mark stream as closed (for GC) */
130+ return luaL_execresult(L, stat);
153131 }
154132
155133
@@ -159,8 +137,8 @@ static int io_pclose (lua_State *L) { · 3 unmodified lines
159137 static int io_fclose (lua_State *L) {
160138 FILE **p = tofilep(L);
161139 int ok = (fclose(*p) == 0);
162− *p = NULL;
163− return pushresult(L, ok, NULL);
140+ *p = NULL; /* mark stream as closed (for GC) */
141+ return luaL_fileresult(L, ok, NULL);
164142 }
165143
166144
@@ -181,8 +159,7 @@ static int io_close (lua_State *L) { · 14 unmodified lines
181159
182160 static int io_gc (lua_State *L) {
183161 FILE *f = *tofilep(L);
184− /* ignore closed files */
185− if (f != NULL)
162+ if (f != NULL) /* ignore closed files */
186163 aux_close(L);
187164 return 0;
188165 }
@@ -205,14 +182,14 @@ static int io_open (lua_State *L) { · 16 unmodified lines
205182 int i = 0;
206183 /* check whether 'mode' matches '[rwa]%+?b?' */
207184 if (!(mode[i] != '\0' && strchr("rwa", mode[i++]) != NULL &&
208− (mode[i] != '+' || ++i) && /* skip if char is '+' */
209− (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
185+ (mode[i] != '+' || ++i) && /* skip if char is '+' */
186+ (mode[i] != 'b' || ++i) && /* skip if char is 'b' */
210187 (mode[i] == '\0')))
211188 luaL_error(L, "invalid mode " LUA_QL("%s")
212189 " (should match " LUA_QL("[rwa]%%+?b?") ")", mode);
213190 pf = newfile(L);
214191 *pf = fopen(filename, mode);
215− return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
192+ return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
216193 }
217194
218195
@@ -225,14 +202,14 @@ static int io_popen (lua_State *L) { · 6 unmodified lines
225202 const char *mode = luaL_optstring(L, 2, "r");
226203 FILE **pf = newfile(L);
227204 *pf = lua_popen(L, filename, mode);
228− return (*pf == NULL) ? pushresult(L, 0, filename) : 1;
205+ return (*pf == NULL) ? luaL_fileresult(L, 0, filename) : 1;
229206 }
230207
231208
232209 static int io_tmpfile (lua_State *L) {
233210 FILE **pf = newfile(L);
234211 *pf = tmpfile();
235− return (*pf == NULL) ? pushresult(L, 0, NULL) : 1;
212+ return (*pf == NULL) ? luaL_fileresult(L, 0, NULL) : 1;
236213 }
237214
238215
@@ -305,7 +282,7 @@ static int io_lines (lua_State *L) { · 66 unmodified lines
305282 if (lua_isnone(L, 1)) lua_pushnil(L); /* at least one argument */
306283 if (lua_isnil(L, 1)) { /* no file name? */
307284 lua_rawgeti(L, lua_upvalueindex(1), IO_INPUT); /* get default input */
308− lua_replace(L, 1); /* put it at index 1 */
285+ lua_replace(L, 1); /* put it at index 1 */
309286 tofile(L); /* check that it's a valid file handle */
310287 toclose = 0; /* do not close it after iteration */
311288 }
@@ -315,7 +292,7 @@ static int io_lines (lua_State *L) { · 3 unmodified lines
315292 *pf = fopen(filename, "r");
316293 if (*pf == NULL)
317294 fileerror(L, 1, filename);
318− lua_replace(L, 1); /* put file at index 1 */
295+ lua_replace(L, 1); /* put file at index 1 */
319296 toclose = 1; /* close it after iteration */
320297 }
321298 aux_lines(L, toclose);
@@ -443,7 +420,7 @@ static int g_read (lua_State *L, FILE *f, int first) { · 121 unmodified lines
443420 }
444421 }
445422 if (ferror(f))
446− return pushresult(L, 0, NULL);
423+ return luaL_fileresult(L, 0, NULL);
447424 if (!success) {
448425 lua_pop(L, 1); /* remove last result */
449426 lua_pushnil(L); /* push nil instead */
@@ -507,7 +484,7 @@ static int g_write (lua_State *L, FILE *f, int arg) { · 57 unmodified lines
507484 }
508485 }
509486 if (status) return 1; /* file handle already on stack top */
510− else return pushresult(L, status, NULL);
487+ else return luaL_fileresult(L, status, NULL);
511488 }
512489
513490
@@ -517,7 +494,7 @@ static int io_write (lua_State *L) { · 3 unmodified lines
517494
518495
519496 static int f_write (lua_State *L) {
520− FILE * f = tofile(L);
497+ FILE * f = tofile(L);
521498 lua_pushvalue(L, 1); /* push file at the stack top (to be returned) */
522499 return g_write(L, f, 2);
523500 }
@@ -531,7 +508,7 @@ static int f_seek (lua_State *L) { · 7 unmodified lines
531508 long offset = luaL_optlong(L, 3, 0);
532509 op = fseek(f, offset, mode[op]);
533510 if (op)
534− return pushresult(L, 0, NULL); /* error */
511+ return luaL_fileresult(L, 0, NULL); /* error */
535512 else {
536513 lua_pushinteger(L, ftell(f));
537514 return 1;
@@ -546,18 +523,18 @@ static int f_setvbuf (lua_State *L) { · 8 unmodified lines
546523 int op = luaL_checkoption(L, 2, NULL, modenames);
547524 lua_Integer sz = luaL_optinteger(L, 3, LUAL_BUFFERSIZE);
548525 int res = setvbuf(f, NULL, mode[op], sz);
549− return pushresult(L, res == 0, NULL);
526+ return luaL_fileresult(L, res == 0, NULL);
550527 }
551528
552529
553530
554531 static int io_flush (lua_State *L) {
555− return pushresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
532+ return luaL_fileresult(L, fflush(getiofile(L, IO_OUTPUT)) == 0, NULL);
556533 }
557534
558535
559536 static int f_flush (lua_State *L) {
560− return pushresult(L, fflush(tofile(L)) == 0, NULL);
537+ return luaL_fileresult(L, fflush(tofile(L)) == 0, NULL);
561538 }
562539
563540
src/llex.c+13−17
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.c,v 2.41 2010/11/18 18:38:44 roberto Exp $
2+** $Id: llex.c,v 2.47 2011/05/03 15:51:16 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -35,7 +35,7 @@ · 29 unmodified lines
3535 /* ORDER RESERVED */
3636 static const char *const luaX_tokens [] = {
3737 "and", "break", "do", "else", "elseif",
38− "end", "false", "for", "function", "if",
38+ "end", "false", "for", "function", "goto", "if",
3939 "in", "local", "nil", "not", "or", "repeat",
4040 "return", "then", "true", "until", "while",
4141 "..", "...", "==", ">=", "<=", "~=", "<eof>",
@@ -67,7 +67,6 @@ void luaX_init (lua_State *L) { · 25 unmodified lines
6767 for (i=0; i<NUM_RESERVED; i++) {
6868 TString *ts = luaS_new(L, luaX_tokens[i]);
6969 luaS_fix(ts); /* reserved words are never collected */
70− lua_assert(strlen(luaX_tokens[i])+1 <= TOKEN_LEN);
7170 ts->tsv.reserved = cast_byte(i+1); /* reserved word */
7271 }
7372 }
@@ -127,7 +126,7 @@ TString *luaX_newstring (LexState *ls, const char *str, size_t l) { · 53 unmodified lines
127126 TValue *o; /* entry for `str' */
128127 TString *ts = luaS_newlstr(L, str, l); /* create new string */
129128 setsvalue2s(L, L->top++, ts); /* temporarily anchor it in stack */
130− o = luaH_setstr(L, ls->fs->h, ts);
129+ o = luaH_setstr(L, ls->fs->h, ts);
131130 if (ttisnil(o)) {
132131 setbvalue(o, 1); /* t[string] = true */
133132 luaC_checkGC(L);
@@ -152,9 +151,11 @@ static void inclinenumber (LexState *ls) { · 18 unmodified lines
152151 }
153152
154153
155−void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) {
154+void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source,
155+ int firstchar) {
156156 ls->decpoint = '.';
157157 ls->L = L;
158+ ls->current = firstchar;
158159 ls->lookahead.token = TK_EOS; /* no look-ahead token */
159160 ls->z = z;
160161 ls->fs = NULL;
@@ -164,7 +165,6 @@ void luaX_setinput (lua_State *L, LexState *ls, ZIO *z, TString *source) { · 3 unmodified lines
164165 ls->envn = luaS_new(L, LUA_ENV); /* create env name */
165166 luaS_fix(ls->envn); /* never collect this name */
166167 luaZ_resizebuffer(ls->L, ls->buff, LUA_MINBUFFER); /* initialize buffer */
167− next(ls); /* read first char */
168168 }
169169
170170
@@ -185,7 +185,7 @@ static int check_next (LexState *ls, const char *set) { · 14 unmodified lines
185185 }
186186
187187
188−/*
188+/*
189189 ** change all characters 'from' in buffer to 'to'
190190 */
191191 static void buffreplace (LexState *ls, char from, char to) {
@@ -200,6 +200,9 @@ static void buffreplace (LexState *ls, char from, char to) { · 8 unmodified lines
200200 #define getlocaledecpoint() (localeconv()->decimal_point[0])
201201 #endif
202202
203+
204+#define buff2d(b,e) luaO_str2d(luaZ_buffer(b), luaZ_bufflen(b) - 1, e)
205+
203206 /*
204207 ** in case of format error, try to change decimal point separator to
205208 ** the one defined in the current locale and check again
@@ -208,7 +211,7 @@ static void trydecpoint (LexState *ls, SemInfo *seminfo) { · 2 unmodified lines
208211 char old = ls->decpoint;
209212 ls->decpoint = getlocaledecpoint();
210213 buffreplace(ls, old, ls->decpoint); /* try new decimal separator */
211− if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) {
214+ if (!buff2d(ls->buff, &seminfo->r)) {
212215 /* format error with correct decimal point: no more options */
213216 buffreplace(ls, ls->decpoint, '.'); /* undo change (for error message) */
214217 lexerror(ls, "malformed number", TK_NUMBER);
@@ -226,7 +229,7 @@ static void read_numeral (LexState *ls, SemInfo *seminfo) { · 11 unmodified lines
226229 } while (lislalnum(ls->current) || ls->current == '.');
227230 save(ls, '\0');
228231 buffreplace(ls, '.', ls->decpoint); /* follow locale for decimal point */
229− if (!luaO_str2d(luaZ_buffer(ls->buff), &seminfo->r)) /* format error? */
232+ if (!buff2d(ls->buff, &seminfo->r)) /* format error? */
230233 trydecpoint(ls, seminfo); /* try to update decimal point separator */
231234 }
232235
@@ -283,13 +286,6 @@ static void read_long_string (LexState *ls, SemInfo *seminfo, int sep) { · 50 unmodified lines
283286 }
284287
285288
286−static int hexavalue (int c) {
287− if (lisdigit(c)) return c - '0';
288− else if (lisupper(c)) return c - 'A' + 10;
289− else return c - 'a' + 10;
290−}
291−
292−
293289 static int readhexaesc (LexState *ls) {
294290 int c1, c2 = EOZ;
295291 if (!lisxdigit(c1 = next(ls)) || !lisxdigit(c2 = next(ls))) {
@@ -299,7 +295,7 @@ static int readhexaesc (LexState *ls) { · 3 unmodified lines
299295 if (c2 != EOZ) save(ls, c2);
300296 lexerror(ls, "hexadecimal digit expected", TK_STRING);
301297 }
302− return (hexavalue(c1) << 4) + hexavalue(c2);
298+ return (luaO_hexavalue(c1) << 4) + luaO_hexavalue(c2);
303299 }
304300
305301
src/llex.h+7−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llex.h,v 1.65 2010/04/05 16:35:37 roberto Exp $
2+** $Id: llex.h,v 1.70 2011/05/03 15:51:16 roberto Exp $
33 ** Lexical Analyzer
44 ** See Copyright Notice in lua.h
55 */
@@ -13,8 +13,6 @@ · 7 unmodified lines
1313
1414 #define FIRST_RESERVED 257
1515
16−/* maximum length of a reserved word */
17−#define TOKEN_LEN (sizeof("function")/sizeof(char))
1816
1917
2018 /*
@@ -25,7 +23,7 @@ enum RESERVED { · 4 unmodified lines
2523 /* terminal symbols denoted by reserved words */
2624 TK_AND = FIRST_RESERVED, TK_BREAK,
2725 TK_DO, TK_ELSE, TK_ELSEIF, TK_END, TK_FALSE, TK_FOR, TK_FUNCTION,
28− TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
26+ TK_GOTO, TK_IF, TK_IN, TK_LOCAL, TK_NIL, TK_NOT, TK_OR, TK_REPEAT,
2927 TK_RETURN, TK_THEN, TK_TRUE, TK_UNTIL, TK_WHILE,
3028 /* other terminal symbols */
3129 TK_CONCAT, TK_DOTS, TK_EQ, TK_GE, TK_LE, TK_NE, TK_EOS,
@@ -48,17 +46,19 @@ typedef struct Token { · 16 unmodified lines
4846 } Token;
4947
5048
49+/* state of the lexer plus state of the parser when shared by all
50+ functions */
5151 typedef struct LexState {
5252 int current; /* current character (charint) */
5353 int linenumber; /* input line counter */
5454 int lastline; /* line of last token `consumed' */
5555 Token t; /* current token */
5656 Token lookahead; /* look ahead token */
57− struct FuncState *fs; /* `FuncState' is private to the parser */
57+ struct FuncState *fs; /* current function (parser) */
5858 struct lua_State *L;
5959 ZIO *z; /* input stream */
6060 Mbuffer *buff; /* buffer for tokens */
61− struct Varlist *varl; /* list of all active local variables */
61+ struct Dyndata *dyd; /* dynamic structures used by the parser */
6262 TString *source; /* current source name */
6363 TString *envn; /* environment variable name */
6464 char decpoint; /* locale decimal point */
@@ -67,7 +67,7 @@ typedef struct LexState { · 2 unmodified lines
6767
6868 LUAI_FUNC void luaX_init (lua_State *L);
6969 LUAI_FUNC void luaX_setinput (lua_State *L, LexState *ls, ZIO *z,
70− TString *source);
70+ TString *source, int firstchar);
7171 LUAI_FUNC TString *luaX_newstring (LexState *ls, const char *str, size_t l);
7272 LUAI_FUNC void luaX_next (LexState *ls);
7373 LUAI_FUNC int luaX_lookahead (LexState *ls);
src/llimits.h+15−10
@@ -1,5 +1,5 @@
11 /*
2−** $Id: llimits.h,v 1.84 2010/11/08 16:33:20 roberto Exp $
2+** $Id: llimits.h,v 1.89 2011/05/05 19:43:14 roberto Exp $
33 ** Limits, basic types, and some other `installation-dependent' definitions
44 ** See Copyright Notice in lua.h
55 */
@@ -59,9 +59,12 @@ typedef LUAI_UACNUMBER l_uacNumber; · 53 unmodified lines
5959 /* internal assertions for in-house debugging */
6060 #if defined(lua_assert)
6161 #define check_exp(c,e) (lua_assert(c), (e))
62+/* to avoid problems with conditions too long */
63+#define lua_longassert(c) { if (!(c)) lua_assert(0); }
6264 #else
6365 #define lua_assert(c) /* empty */
6466 #define check_exp(c,e) (e)
67+#define lua_longassert(c) /* empty */
6568 #endif
6669
6770 /*
@@ -87,6 +90,7 @@ typedef LUAI_UACNUMBER l_uacNumber; · 19 unmodified lines
8790 #define cast_byte(i) cast(lu_byte, (i))
8891 #define cast_num(i) cast(lua_Number, (i))
8992 #define cast_int(i) cast(int, (i))
93+#define cast_uchar(i) cast(unsigned char, (i))
9094
9195
9296 /*
@@ -173,6 +177,9 @@ typedef lu_int32 Instruction; · 80 unmodified lines
173177 ** lua_number2integer is a macro to convert lua_Number to lua_Integer.
174178 ** lua_number2unsigned is a macro to convert a lua_Number to a lua_Unsigned.
175179 ** lua_unsigned2number is a macro to convert a lua_Unsigned to a lua_Number.
180+** luai_hashnum is a macro to hash a lua_Number value into an integer.
181+** The hash must be deterministic and give reasonable values for
182+** both small and large values (outside the range of integers).
176183 */
177184
178185 #if defined(MS_ASMTRICK) /* { */
@@ -203,6 +210,10 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 24 unmodified lines
203210 volatile union luai_Cast u; u.l_d = (n) + 6755399441055744.0; \
204211 (i) = (t)u.l_p[LUA_IEEEENDIAN]; }
205212
213+#define luai_hashnum(i,n) \
214+ { volatile union luai_Cast u; u.l_d = (n) + 1.0; /* avoid -0 */ \
215+ (i) = u.l_p[0] + u.l_p[1]; } /* add double bits for his hash */
216+
206217 #define lua_number2int(i,n) lua_number2int32(i, n, int)
207218 #define lua_number2integer(i,n) lua_number2int32(i, n, lua_Integer)
208219 #define lua_number2unsigned(i,n) lua_number2int32(i, n, lua_Unsigned)
@@ -241,14 +252,8 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 32 unmodified lines
241252 #endif
242253
243254
244−/*
245−** luai_hashnum is a macro do hash a lua_Number value into an integer.
246−** The hash must be deterministic and give reasonable values for
247−** both small and large values (outside the range of integers).
248−** It is used only in ltable.c.
249−*/
250255
251−#if !defined(luai_hashnum) /* { */
256+#if (defined(ltable_c) || defined(luaall_c)) && !defined(luai_hashnum)
252257
253258 #include <float.h>
254259 #include <math.h>
@@ -257,7 +262,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 2 unmodified lines
257262 n = frexp(n, &e) * (lua_Number)(INT_MAX - DBL_MAX_EXP); \
258263 lua_number2int(i, n); i += e; }
259264
260−#endif /* } */
265+#endif
261266
262267
263268
@@ -275,7 +280,7 @@ union luai_Cast { double l_d; LUA_INT32 l_p[2]; }; · 11 unmodified lines
275280 #define condchangemem(L) condmovestack(L)
276281 #else
277282 #define condchangemem(L) \
278− ((void)(gcstopped(G(L)) || (luaC_fullgc(L, 0), 1)))
283+ ((void)(!(G(L)->gcrunning) || (luaC_fullgc(L, 0), 1)))
279284 #endif
280285
281286 #endif
src/lmem.c+5−9
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lmem.c,v 1.79 2010/05/05 18:49:56 roberto Exp $
2+** $Id: lmem.c,v 1.81 2010/12/20 19:40:07 roberto Exp $
33 ** Interface to Memory Manager
44 ** See Copyright Notice in lua.h
55 */
@@ -79,14 +79,14 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 73 unmodified lines
7979 size_t realosize = (block) ? osize : 0;
8080 lua_assert((realosize == 0) == (block == NULL));
8181 #if defined(HARDMEMTESTS)
82− if (nsize > realosize && !gcstopped(g))
82+ if (nsize > realosize && g->gcrunning)
8383 luaC_fullgc(L, 1); /* force a GC whenever possible */
8484 #endif
8585 newblock = (*g->frealloc)(g->ud, block, osize, nsize);
8686 if (newblock == NULL && nsize > 0) {
8787 api_check(L, nsize > realosize,
8888 "realloc cannot fail when shrinking a block");
89− if (!gcstopped(g)) {
89+ if (g->gcrunning) {
9090 luaC_fullgc(L, 1); /* try to free some memory... */
9191 newblock = (*g->frealloc)(g->ud, block, osize, nsize); /* try again */
9292 }
@@ -94,9 +94,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 1 unmodified line
9494 luaD_throw(L, LUA_ERRMEM);
9595 }
9696 lua_assert((nsize == 0) == (newblock == NULL));
97− g->totalbytes = (g->totalbytes - realosize) + nsize;
98− if (!gcstopped(g))
99− g->GCdebt += nsize; /* give some credit to garbage collector */
97+ g->GCdebt = (g->GCdebt + nsize) - realosize;
10098 #if defined(TRACEMEM)
10199 { /* auxiliary patch to monitor garbage collection.
102100 ** To plot, gnuplot with following command:
@@ -108,9 +106,7 @@ void *luaM_realloc_ (lua_State *L, void *block, size_t osize, size_t nsize) { · 5 unmodified lines
108106 if ((total % 200) == 0) {
109107 if (f == NULL) f = fopen(TRACEMEM, "w");
110108 fprintf(f, "%lu %u %d %d\n", total,
111− g->totalbytes,
112− gcstopped(g) ? 0 : g->GCdebt,
113− g->gcstate * 1000);
109+ g->totalbytes, g->GCdebt, g->gcstate * 1000);
114110 }
115111 }
116112 #endif
src/loadlib.c+43−30
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loadlib.c,v 1.94 2010/11/10 20:00:04 roberto Exp $
2+** $Id: loadlib.c,v 1.98 2011/04/08 19:17:36 roberto Exp $
33 ** Dynamic library loader for Lua
44 ** See Copyright Notice in lua.h
55 **
@@ -164,7 +164,7 @@ static void pusherror (lua_State *L) { · 158 unmodified lines
164164 int error = GetLastError();
165165 char buffer[128];
166166 if (FormatMessageA(FORMAT_MESSAGE_IGNORE_INSERTS | FORMAT_MESSAGE_FROM_SYSTEM,
167− NULL, error, 0, buffer, sizeof(buffer), NULL))
167+ NULL, error, 0, buffer, sizeof(buffer)/sizeof(char), NULL))
168168 lua_pushstring(L, buffer);
169169 else
170170 lua_pushfstring(L, "system error %d\n", error);
@@ -321,8 +321,10 @@ static const char *pushnexttemplate (lua_State *L, const char *path) { · 150 unmodified lines
321321
322322
323323 static const char *searchpath (lua_State *L, const char *name,
324− const char *path) {
325− name = luaL_gsub(L, name, ".", LUA_DIRSEP);
324+ const char *path,
325+ const char *sep) {
326+ if (*sep != '\0') /* non-empty separator? */
327+ name = luaL_gsub(L, name, sep, LUA_DIRSEP); /* replace it by proper one */
326328 lua_pushliteral(L, ""); /* error accumulator */
327329 while ((path = pushnexttemplate(L, path)) != NULL) {
328330 const char *filename = luaL_gsub(L, lua_tostring(L, -1),
@@ -339,7 +341,9 @@ static const char *searchpath (lua_State *L, const char *name, · 10 unmodified lines
339341
340342
341343 static int ll_searchpath (lua_State *L) {
342− const char *f = searchpath(L, luaL_checkstring(L, 1), luaL_checkstring(L, 2));
344+ const char *f = searchpath(L, luaL_checkstring(L, 1),
345+ luaL_checkstring(L, 2),
346+ luaL_optstring(L, 3, "."));
343347 if (f != NULL) return 1;
344348 else { /* error message is on top of the stack */
345349 lua_pushnil(L);
@@ -356,13 +360,19 @@ static const char *findfile (lua_State *L, const char *name, · 10 unmodified lines
356360 path = lua_tostring(L, -1);
357361 if (path == NULL)
358362 luaL_error(L, LUA_QL("package.%s") " must be a string", pname);
359− return searchpath(L, name, path);
363+ return searchpath(L, name, path, ".");
360364 }
361365
362366
363−static void loaderror (lua_State *L, const char *filename) {
364− luaL_error(L, "error loading module " LUA_QS " from file " LUA_QS ":\n\t%s",
365− lua_tostring(L, 1), filename, lua_tostring(L, -1));
367+static int checkload (lua_State *L, int stat, const char *filename) {
368+ if (stat) { /* module loaded successfully? */
369+ lua_pushstring(L, filename); /* will be 2nd argument to module */
370+ return 2; /* return open function and file name */
371+ }
372+ else
373+ return luaL_error(L, "error loading module " LUA_QS
374+ " from file " LUA_QS ":\n\t%s",
375+ lua_tostring(L, 1), filename, lua_tostring(L, -1));
366376 }
367377
368378
@@ -370,10 +380,8 @@ static int loader_Lua (lua_State *L) { · 1 unmodified line
370380 const char *filename;
371381 const char *name = luaL_checkstring(L, 1);
372382 filename = findfile(L, name, "path");
373− if (filename == NULL) return 1; /* library not found in this path */
374− if (luaL_loadfile(L, filename) != LUA_OK)
375− loaderror(L, filename);
376− return 1; /* library loaded successfully */
383+ if (filename == NULL) return 1; /* module not found in this path */
384+ return checkload(L, (luaL_loadfile(L, filename) == LUA_OK), filename);
377385 }
378386
379387
@@ -398,10 +406,8 @@ static int loadfunc (lua_State *L, const char *filename, const char *modname) { · 18 unmodified lines
398406 static int loader_C (lua_State *L) {
399407 const char *name = luaL_checkstring(L, 1);
400408 const char *filename = findfile(L, name, "cpath");
401− if (filename == NULL) return 1; /* library not found in this path */
402− if (loadfunc(L, filename, name) != 0)
403− loaderror(L, filename);
404− return 1; /* library loaded successfully */
409+ if (filename == NULL) return 1; /* module not found in this path */
410+ return checkload(L, (loadfunc(L, filename, name) == 0), filename);
405411 }
406412
407413
@@ -415,12 +421,16 @@ static int loader_Croot (lua_State *L) { · 7 unmodified lines
415421 filename = findfile(L, lua_tostring(L, -1), "cpath");
416422 if (filename == NULL) return 1; /* root not found */
417423 if ((stat = loadfunc(L, filename, name)) != 0) {
418− if (stat != ERRFUNC) loaderror(L, filename); /* real error */
419− lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
420− name, filename);
421− return 1; /* function not found */
424+ if (stat != ERRFUNC)
425+ return checkload(L, 0, filename); /* real error */
426+ else { /* open function not found */
427+ lua_pushfstring(L, "\n\tno module " LUA_QS " in file " LUA_QS,
428+ name, filename);
429+ return 1;
430+ }
422431 }
423− return 1;
432+ lua_pushstring(L, filename); /* will be 2nd argument to module */
433+ return 2;
424434 }
425435
426436
@@ -453,16 +463,19 @@ static int ll_require (lua_State *L) { · 26 unmodified lines
453463 luaL_error(L, "module " LUA_QS " not found:%s",
454464 name, lua_tostring(L, -2));
455465 lua_pushstring(L, name);
456− lua_call(L, 1, 1); /* call it */
457− if (lua_isfunction(L, -1)) /* did it find module? */
466+ lua_call(L, 1, 2); /* call it */
467+ if (lua_isfunction(L, -2)) /* did it find module? */
458468 break; /* module loaded successfully */
459− else if (lua_isstring(L, -1)) /* loader returned error message? */
460− lua_concat(L, 2); /* accumulate it */
469+ else if (lua_isstring(L, -2)) { /* loader returned error message? */
470+ lua_pop(L, 1); /* remove extra return */
471+ lua_concat(L, 2); /* accumulate error message */
472+ }
461473 else
462− lua_pop(L, 1);
474+ lua_pop(L, 2); /* remove both returns */
463475 }
464476 lua_pushstring(L, name); /* pass name as argument to module */
465− lua_call(L, 1, 1); /* run loaded module */
477+ lua_insert(L, -2); /* name is 1st argument (before search data) */
478+ lua_call(L, 2, 1); /* run loaded module */
466479 if (!lua_isnil(L, -1)) /* non-nil return? */
467480 lua_setfield(L, 2, name); /* _LOADED[name] = returned value */
468481 lua_getfield(L, 2, name);
@@ -639,10 +652,10 @@ LUAMOD_API int luaopen_package (lua_State *L) { · 170 unmodified lines
639652 LUA_EXEC_DIR "\n" LUA_IGMARK "\n");
640653 lua_setfield(L, -2, "config");
641654 /* set field `loaded' */
642− luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
655+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_LOADED");
643656 lua_setfield(L, -2, "loaded");
644657 /* set field `preload' */
645− luaL_findtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
658+ luaL_getsubtable(L, LUA_REGISTRYINDEX, "_PRELOAD");
646659 lua_setfield(L, -2, "preload");
647660 lua_pushglobaltable(L);
648661 lua_pushvalue(L, -2); /* set 'package' as upvalue for next lib */
src/lobject.c+86−38
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.c,v 2.43 2010/10/29 15:54:55 roberto Exp $
2+** $Id: lobject.c,v 2.49 2011/05/31 18:24:36 roberto Exp $
33 ** Some generic functions over Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -70,28 +70,6 @@ int luaO_ceillog2 (unsigned int x) { · 64 unmodified lines
7070 }
7171
7272
73−int luaO_rawequalObj (const TValue *t1, const TValue *t2) {
74− if (ttype(t1) != ttype(t2)) return 0;
75− else switch (ttype(t1)) {
76− case LUA_TNIL:
77− return 1;
78− case LUA_TNUMBER:
79− return luai_numeq(nvalue(t1), nvalue(t2));
80− case LUA_TBOOLEAN:
81− return bvalue(t1) == bvalue(t2); /* boolean true must be 1 !! */
82− case LUA_TLIGHTUSERDATA:
83− return pvalue(t1) == pvalue(t2);
84− case LUA_TSTRING:
85− return rawtsvalue(t1) == rawtsvalue(t2);
86− case LUA_TLCF:
87− return fvalue(t1) == fvalue(t2);
88− default:
89− lua_assert(iscollectable(t1));
90− return gcvalue(t1) == gcvalue(t2);
91− }
92−}
93−
94−
9573 lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) {
9674 switch (op) {
9775 case LUA_OPADD: return luai_numadd(NULL, v1, v2);
@@ -106,19 +84,87 @@ lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2) { · 8 unmodified lines
10684 }
10785
10886
109−static int checkend (const char *s, const char *endptr) {
110− if (endptr == s) return 0; /* no characters converted */
111− while (lisspace(cast(unsigned char, *endptr))) endptr++;
112− return (*endptr == '\0'); /* OK if no trailing characters */
87+int luaO_hexavalue (int c) {
88+ if (lisdigit(c)) return c - '0';
89+ else if (lisupper(c)) return c - 'A' + 10;
90+ else return c - 'a' + 10;
91+}
92+
93+
94+#if !defined(lua_strx2number)
95+
96+#include <math.h>
97+
98+
99+static int isneg (const char **s) {
100+ if (**s == '-') { (*s)++; return 1; }
101+ else if (**s == '+') (*s)++;
102+ return 0;
113103 }
114104
115105
116−int luaO_str2d (const char *s, lua_Number *result) {
106+static lua_Number readhexa (const char **s, lua_Number r, int *count) {
107+ while (lisxdigit(cast_uchar(**s))) { /* read integer part */
108+ r = (r * 16.0) + (double)luaO_hexavalue(*(*s)++);
109+ (*count)++;
110+ }
111+ return r;
112+}
113+
114+
115+/*
116+** convert an hexadecimal numeric string to a number, following
117+** C99 specification for 'strtod'
118+*/
119+static lua_Number lua_strx2number (const char *s, char **endptr) {
120+ lua_Number r = 0.0;
121+ int e = 0, i = 0;
122+ int neg = 0; /* 1 if number is negative */
123+ *endptr = cast(char *, s); /* nothing is valid yet */
124+ while (lisspace(cast_uchar(*s))) s++; /* skip initial spaces */
125+ neg = isneg(&s); /* check signal */
126+ if (!(*s == '0' && (*(s + 1) == 'x' || *(s + 1) == 'X'))) /* check '0x' */
127+ return 0.0; /* invalid format (no '0x') */
128+ s += 2; /* skip '0x' */
129+ r = readhexa(&s, r, &i); /* read integer part */
130+ if (*s == '.') {
131+ s++; /* skip dot */
132+ r = readhexa(&s, r, &e); /* read fractional part */
133+ }
134+ if (i == 0 && e == 0)
135+ return 0.0; /* invalid format (no digit) */
136+ e *= -4; /* each fractional digit divides value by 2^-4 */
137+ *endptr = cast(char *, s); /* valid up to here */
138+ if (*s == 'p' || *s == 'P') { /* exponent part? */
139+ int exp1 = 0;
140+ int neg1;
141+ s++; /* skip 'p' */
142+ neg1 = isneg(&s); /* signal */
143+ if (!lisdigit(cast_uchar(*s)))
144+ goto ret; /* must have at least one digit */
145+ while (lisdigit(cast_uchar(*s))) /* read exponent */
146+ exp1 = exp1 * 10 + *(s++) - '0';
147+ if (neg1) exp1 = -exp1;
148+ e += exp1;
149+ }
150+ *endptr = cast(char *, s); /* valid up to here */
151+ ret:
152+ if (neg) r = -r;
153+ return ldexp(r, e);
154+}
155+
156+#endif
157+
158+
159+int luaO_str2d (const char *s, size_t len, lua_Number *result) {
117160 char *endptr;
118− *result = lua_str2number(s, &endptr);
119− if (checkend(s, endptr)) return 1; /* conversion OK? */
120− *result = cast_num(strtoul(s, &endptr, 0)); /* try hexadecimal */
121− return checkend(s, endptr);
161+ if (strpbrk(s, "xX")) /* hexa? */
162+ *result = lua_strx2number(s, &endptr);
163+ else
164+ *result = lua_str2number(s, &endptr);
165+ if (endptr == s) return 0; /* nothing recognized */
166+ while (lisspace(cast_uchar(*endptr))) endptr++;
167+ return (endptr == s + len); /* OK if no trailing characters */
122168 }
123169
124170
@@ -196,19 +242,20 @@ const char *luaO_pushfstring (lua_State *L, const char *fmt, ...) { · 71 unmodified lines
196242 }
197243
198244
245+/* number of chars of a literal string without the ending \0 */
246+#define LL(x) (sizeof(x)/sizeof(char) - 1)
199247
200−#define LL(x) (sizeof(x) - 1)
201248 #define RETS "..."
202249 #define PRE "[string \""
203250 #define POS "\"]"
204251
205−#define addstr(a,b,l) ( memcpy(a,b,l), a += (l) )
252+#define addstr(a,b,l) ( memcpy(a,b,(l) * sizeof(char)), a += (l) )
206253
207254 void luaO_chunkid (char *out, const char *source, size_t bufflen) {
208255 size_t l = strlen(source);
209256 if (*source == '=') { /* 'literal' source */
210257 if (l <= bufflen) /* small enough? */
211− memcpy(out, source + 1, l);
258+ memcpy(out, source + 1, l * sizeof(char));
212259 else { /* truncate it */
213260 addstr(out, source + 1, bufflen - 1);
214261 *out = '\0';
@@ -216,11 +263,11 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { · 1 unmodified line
216263 }
217264 else if (*source == '@') { /* file name */
218265 if (l <= bufflen) /* small enough? */
219− memcpy(out, source + 1, l);
266+ memcpy(out, source + 1, l * sizeof(char));
220267 else { /* add '...' before rest of name */
221268 addstr(out, RETS, LL(RETS));
222269 bufflen -= LL(RETS);
223− memcpy(out, source + 1 + l - bufflen, bufflen);
270+ memcpy(out, source + 1 + l - bufflen, bufflen * sizeof(char));
224271 }
225272 }
226273 else { /* string; format as [string "source"] */
@@ -236,6 +283,7 @@ void luaO_chunkid (char *out, const char *source, size_t bufflen) { · 9 unmodified lines
236283 addstr(out, source, l);
237284 addstr(out, RETS, LL(RETS));
238285 }
239− memcpy(out, POS, LL(POS) + 1);
286+ memcpy(out, POS, (LL(POS) + 1) * sizeof(char));
240287 }
241288 }
289+
src/lobject.h+241−87
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lobject.h,v 2.42 2010/07/26 15:53:23 roberto Exp $
2+** $Id: lobject.h,v 2.60 2011/06/13 14:13:06 roberto Exp $
33 ** Type definitions for Lua objects
44 ** See Copyright Notice in lua.h
55 */
@@ -23,12 +23,38 @@ · 17 unmodified lines
2323 #define LUA_TUPVAL (LUA_NUMTAGS+1)
2424 #define LUA_TDEADKEY (LUA_NUMTAGS+2)
2525
26+/*
27+** number of all possible tags (including LUA_TNONE but excluding DEADKEY)
28+*/
29+#define LUA_TOTALTAGS (LUA_TUPVAL+2)
30+
31+
32+/*
33+** tags for Tagged Values have the following use of bits:
34+** bits 0-3: actual tag (a LUA_T* value)
35+** bits 4-5: variant bits
36+** bit 6: whether value is collectable
37+*/
2638
2739 /*
28−** Variant tag for light C functions (negative to be considered
29−** non collectable by 'iscollectable')
40+** LUA_TFUNCTION variants:
41+** 0 - Lua function
42+** 1 - light C function
43+** 2 - regular C function (closure)
3044 */
31−#define LUA_TLCF (~0x0F | LUA_TFUNCTION)
45+
46+/* Variant tags for functions */
47+#define LUA_TLCL (LUA_TFUNCTION | (0 << 4)) /* Lua closure */
48+#define LUA_TLCF (LUA_TFUNCTION | (1 << 4)) /* light C function */
49+#define LUA_TCCL (LUA_TFUNCTION | (2 << 4)) /* C closure */
50+
51+
52+/* Bit mark for collectable types */
53+#define BIT_ISCOLLECTABLE (1 << 6)
54+
55+/* mark a tag as collectable */
56+#define ctb(t) ((t) | BIT_ISCOLLECTABLE)
57+
3258
3359 /*
3460 ** Union of all collectable objects
@@ -55,13 +81,10 @@ typedef struct GCheader { · 20 unmodified lines
5581 /*
5682 ** Union of all Lua values
5783 */
58−typedef union {
59− GCObject *gc; /* collectable objects */
60− void *p; /* light userdata */
61− lua_Number n; /* numbers */
62− int b; /* booleans */
63− lua_CFunction f; /* light C functions */
64−} Value;
84+typedef union Value Value;
85+
86+
87+#define numfield lua_Number n; /* numbers */
6588
6689
6790
@@ -72,125 +95,143 @@ typedef union { · 4 unmodified lines
7295
7396 #define TValuefields Value value_; int tt_
7497
75−typedef struct lua_TValue {
76− TValuefields;
77−} TValue;
98+typedef struct lua_TValue TValue;
7899
79100
80101 /* macro defining a nil value */
81−#define NILCONSTANT {NULL}, LUA_TNIL
102+#define NILCONSTANT {NULL}, LUA_TNIL
82103
83104
84−/*
85−** type tag of a TValue
86−*/
87−#define ttype(o) ((o)->tt_)
105+#define val_(o) ((o)->value_)
106+#define num_(o) (val_(o).n)
88107
89108
90−/*
91−** type tag of a TValue with no variants
92−*/
93−#define ttypenv(o) (ttype(o) & 0x0F)
109+/* raw type tag of a TValue */
110+#define rttype(o) ((o)->tt_)
111+
112+/* type tag of a TValue (bits 0-3 for tags + variant bits 4-5) */
113+#define ttype(o) (rttype(o) & 0x3F)
114+
115+
116+/* type tag of a TValue with no variants (bits 0-3) */
117+#define ttypenv(o) (rttype(o) & 0x0F)
94118
95119
96120 /* Macros to test type */
97−#define ttisnil(o) (ttype(o) == LUA_TNIL)
98−#define ttisnumber(o) (ttype(o) == LUA_TNUMBER)
99−#define ttisstring(o) (ttype(o) == LUA_TSTRING)
100−#define ttistable(o) (ttype(o) == LUA_TTABLE)
101−#define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
102−#define ttisclosure(o) (ttype(o) == LUA_TFUNCTION)
103−#define ttislcf(o) (ttype(o) == LUA_TLCF)
104−#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN)
105−#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA)
106−#define ttisthread(o) (ttype(o) == LUA_TTHREAD)
107−#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA)
108−#define ttisdeadkey(o) (ttype(o) == LUA_TDEADKEY)
121+#define checktag(o,t) (rttype(o) == (t))
122+#define ttisnumber(o) checktag((o), LUA_TNUMBER)
123+#define ttisnil(o) checktag((o), LUA_TNIL)
124+#define ttisboolean(o) checktag((o), LUA_TBOOLEAN)
125+#define ttislightuserdata(o) checktag((o), LUA_TLIGHTUSERDATA)
126+#define ttisstring(o) checktag((o), ctb(LUA_TSTRING))
127+#define ttistable(o) checktag((o), ctb(LUA_TTABLE))
128+#define ttisfunction(o) (ttypenv(o) == LUA_TFUNCTION)
129+#define ttisclosure(o) ((rttype(o) & 0x1F) == LUA_TFUNCTION)
130+#define ttisCclosure(o) checktag((o), ctb(LUA_TCCL))
131+#define ttisLclosure(o) checktag((o), ctb(LUA_TLCL))
132+#define ttislcf(o) checktag((o), LUA_TLCF)
133+#define ttisuserdata(o) checktag((o), ctb(LUA_TUSERDATA))
134+#define ttisthread(o) checktag((o), ctb(LUA_TTHREAD))
135+#define ttisdeadkey(o) checktag((o), ctb(LUA_TDEADKEY))
136+
137+#define ttisequal(o1,o2) (rttype(o1) == rttype(o2))
109138
110139 /* Macros to access values */
111−#define gcvalue(o) check_exp(iscollectable(o), (o)->value_.gc)
112−#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value_.p)
113−#define nvalue(o) check_exp(ttisnumber(o), (o)->value_.n)
114−#define rawtsvalue(o) check_exp(ttisstring(o), &(o)->value_.gc->ts)
140+#define nvalue(o) check_exp(ttisnumber(o), num_(o))
141+#define gcvalue(o) check_exp(iscollectable(o), val_(o).gc)
142+#define pvalue(o) check_exp(ttislightuserdata(o), val_(o).p)
143+#define rawtsvalue(o) check_exp(ttisstring(o), &val_(o).gc->ts)
115144 #define tsvalue(o) (&rawtsvalue(o)->tsv)
116−#define rawuvalue(o) check_exp(ttisuserdata(o), &(o)->value_.gc->u)
145+#define rawuvalue(o) check_exp(ttisuserdata(o), &val_(o).gc->u)
117146 #define uvalue(o) (&rawuvalue(o)->uv)
118−#define clvalue(o) check_exp(ttisclosure(o), &(o)->value_.gc->cl)
119−#define fvalue(o) check_exp(ttislcf(o), (o)->value_.f)
120−#define hvalue(o) check_exp(ttistable(o), &(o)->value_.gc->h)
121−#define bvalue(o) check_exp(ttisboolean(o), (o)->value_.b)
122−#define thvalue(o) check_exp(ttisthread(o), &(o)->value_.gc->th)
147+#define clvalue(o) check_exp(ttisclosure(o), &val_(o).gc->cl)
148+#define clLvalue(o) check_exp(ttisLclosure(o), &val_(o).gc->cl.l)
149+#define clCvalue(o) check_exp(ttisCclosure(o), &val_(o).gc->cl.c)
150+#define fvalue(o) check_exp(ttislcf(o), val_(o).f)
151+#define hvalue(o) check_exp(ttistable(o), &val_(o).gc->h)
152+#define bvalue(o) check_exp(ttisboolean(o), val_(o).b)
153+#define thvalue(o) check_exp(ttisthread(o), &val_(o).gc->th)
123154
124155 #define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0))
125156
126157
127−#define iscollectable(o) (ttype(o) >= LUA_TSTRING)
158+#define iscollectable(o) (rttype(o) & BIT_ISCOLLECTABLE)
128159
129160
130161 /* Macros for internal tests */
131−#define righttt(obj) (ttype(obj) == gcvalue(obj)->gch.tt)
132−
133−#define checkconsistency(obj) lua_assert(!iscollectable(obj) || righttt(obj))
162+#define righttt(obj) (ttypenv(obj) == gcvalue(obj)->gch.tt)
134163
135164 #define checkliveness(g,obj) \
136− lua_assert(!iscollectable(obj) || (righttt(obj) && !isdead(g,gcvalue(obj))))
165+ lua_longassert(!iscollectable(obj) || \
166+ (righttt(obj) && !isdead(g,gcvalue(obj))))
137167
138168
139169 /* Macros to set values */
140−#define setnilvalue(obj) ((obj)->tt_=LUA_TNIL)
170+#define settt_(o,t) ((o)->tt_=(t))
141171
142172 #define setnvalue(obj,x) \
143− { TValue *i_o=(obj); i_o->value_.n=(x); i_o->tt_=LUA_TNUMBER; }
173+ { TValue *io=(obj); num_(io)=(x); settt_(io, LUA_TNUMBER); }
144174
145−#define setfvalue(obj,x) \
146− { TValue *i_o=(obj); i_o->value_.f=(x); i_o->tt_=LUA_TLCF; }
175+#define changenvalue(o,x) check_exp(ttisnumber(o), num_(o)=(x))
147176
148−#define changenvalue(o,x) check_exp((o)->tt_==LUA_TNUMBER, (o)->value_.n=(x))
177+#define setnilvalue(obj) settt_(obj, LUA_TNIL)
178+
179+#define setfvalue(obj,x) \
180+ { TValue *io=(obj); val_(io).f=(x); settt_(io, LUA_TLCF); }
149181
150182 #define setpvalue(obj,x) \
151− { TValue *i_o=(obj); i_o->value_.p=(x); i_o->tt_=LUA_TLIGHTUSERDATA; }
183+ { TValue *io=(obj); val_(io).p=(x); settt_(io, LUA_TLIGHTUSERDATA); }
152184
153185 #define setbvalue(obj,x) \
154− { TValue *i_o=(obj); i_o->value_.b=(x); i_o->tt_=LUA_TBOOLEAN; }
186+ { TValue *io=(obj); val_(io).b=(x); settt_(io, LUA_TBOOLEAN); }
187+
188+#define setgcovalue(L,obj,x) \
189+ { TValue *io=(obj); GCObject *i_g=(x); \
190+ val_(io).gc=i_g; settt_(io, ctb(gch(i_g)->tt)); }
155191
156192 #define setsvalue(L,obj,x) \
157− { TValue *i_o=(obj); \
158− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TSTRING; \
159− checkliveness(G(L),i_o); }
193+ { TValue *io=(obj); \
194+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TSTRING)); \
195+ checkliveness(G(L),io); }
160196
161197 #define setuvalue(L,obj,x) \
162− { TValue *i_o=(obj); \
163− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TUSERDATA; \
164− checkliveness(G(L),i_o); }
198+ { TValue *io=(obj); \
199+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TUSERDATA)); \
200+ checkliveness(G(L),io); }
165201
166202 #define setthvalue(L,obj,x) \
167− { TValue *i_o=(obj); \
168− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTHREAD; \
169− checkliveness(G(L),i_o); }
203+ { TValue *io=(obj); \
204+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTHREAD)); \
205+ checkliveness(G(L),io); }
206+
207+#define setclLvalue(L,obj,x) \
208+ { TValue *io=(obj); \
209+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TLCL)); \
210+ checkliveness(G(L),io); }
170211
171−#define setclvalue(L,obj,x) \
172− { TValue *i_o=(obj); \
173− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TFUNCTION; \
174− checkliveness(G(L),i_o); }
212+#define setclCvalue(L,obj,x) \
213+ { TValue *io=(obj); \
214+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TCCL)); \
215+ checkliveness(G(L),io); }
175216
176217 #define sethvalue(L,obj,x) \
177− { TValue *i_o=(obj); \
178− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TTABLE; \
179− checkliveness(G(L),i_o); }
218+ { TValue *io=(obj); \
219+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TTABLE)); \
220+ checkliveness(G(L),io); }
180221
181222 #define setptvalue(L,obj,x) \
182− { TValue *i_o=(obj); \
183− i_o->value_.gc=cast(GCObject *, (x)); i_o->tt_=LUA_TPROTO; \
184− checkliveness(G(L),i_o); }
223+ { TValue *io=(obj); \
224+ val_(io).gc=cast(GCObject *, (x)); settt_(io, ctb(LUA_TPROTO)); \
225+ checkliveness(G(L),io); }
185226
186−#define setdeadvalue(obj) ((obj)->tt_=LUA_TDEADKEY)
227+#define setdeadvalue(obj) settt_(obj, ctb(LUA_TDEADKEY))
187228
188229
189230
190231 #define setobj(L,obj1,obj2) \
191− { const TValue *o2=(obj2); TValue *o1=(obj1); \
192− o1->value_ = o2->value_; o1->tt_=o2->tt_; \
193− checkliveness(G(L),o1); }
232+ { const TValue *io2=(obj2); TValue *io1=(obj1); \
233+ io1->value_ = io2->value_; io1->tt_ = io2->tt_; \
234+ checkliveness(G(L),io1); }
194235
195236
196237 /*
@@ -214,9 +255,121 @@ typedef struct lua_TValue { · 17 unmodified lines
214255
215256
216257
258+
259+/*
260+** {======================================================
261+** NaN Trick
262+** =======================================================
263+*/
264+
265+#if defined(LUA_NANTRICKLE) || defined(LUA_NANTRICKBE)
266+
267+/*
268+** numbers are represented in the 'd_' field. All other values have the
269+** value (NNMARK | tag) in 'tt_'. A number with such pattern would be
270+** a "signaled NaN", which is never generated by regular operations by
271+** the CPU (nor by 'strtod')
272+*/
273+#if !defined(NNMARK)
274+#define NNMARK 0x7FF7A500
275+#endif
276+
277+#undef TValuefields
278+#if defined(LUA_NANTRICKLE)
279+/* little endian */
280+#define TValuefields \
281+ union { struct { Value v_; int tt_; } i; double d_; } u
282+#else
283+/* big endian */
284+#define TValuefields \
285+ union { struct { int tt_; Value v_; } i; double d_; } u
286+#endif
287+
288+#undef numfield
289+#define numfield /* no such field; numbers are the entire struct */
290+
291+/* basic check to distinguish numbers from non-numbers */
292+#undef ttisnumber
293+#define ttisnumber(o) (((o)->u.i.tt_ & 0x7fffff00) != NNMARK)
294+
295+#define tag2tt(t) (NNMARK | (t))
296+
297+#undef NILCONSTANT
298+#define NILCONSTANT {{{NULL}, tag2tt(LUA_TNIL)}}
299+
300+#undef val_
301+#define val_(o) ((o)->u.i.v_)
302+#undef num_
303+#define num_(o) ((o)->u.d_)
304+
305+#undef rttype
306+#define rttype(o) (ttisnumber(o) ? LUA_TNUMBER : (o)->u.i.tt_ & 0xff)
307+
308+#undef settt_
309+#define settt_(o,t) ((o)->u.i.tt_=tag2tt(t))
310+
311+#undef setnvalue
312+#define setnvalue(obj,x) \
313+ { TValue *io_=(obj); num_(io_)=(x); lua_assert(ttisnumber(io_)); }
314+
315+#undef setobj
316+#define setobj(L,obj1,obj2) \
317+ { const TValue *o2_=(obj2); TValue *o1_=(obj1); \
318+ o1_->u = o2_->u; \
319+ checkliveness(G(L),o1_); }
320+
321+
322+/*
323+** these redefinitions are not mandatory, but these forms are more efficient
324+*/
325+
326+#undef checktag
327+#define checktag(o,t) ((o)->u.i.tt_ == tag2tt(t))
328+
329+#undef ttisequal
330+#define ttisequal(o1,o2) \
331+ (ttisnumber(o1) ? ttisnumber(o2) : ((o1)->u.i.tt_ == (o2)->u.i.tt_))
332+
333+
334+
335+#define luai_checknum(L,o,c) { if (!ttisnumber(o)) c; }
336+
337+
338+#else
339+
340+#define luai_checknum(L,o,c) { /* empty */ }
341+
342+#endif
343+/* }====================================================== */
344+
345+
346+
347+/*
348+** {======================================================
349+** types and prototypes
350+** =======================================================
351+*/
352+
353+
354+union Value {
355+ GCObject *gc; /* collectable objects */
356+ void *p; /* light userdata */
357+ int b; /* booleans */
358+ lua_CFunction f; /* light C functions */
359+ numfield /* numbers */
360+};
361+
362+
363+struct lua_TValue {
364+ TValuefields;
365+};
366+
367+
217368 typedef TValue *StkId; /* index to stack elements */
218369
219370
371+
372+
220373 /*
221374 ** Header for string value; string bytes follow the end of this structure
222375 */
@@ -226,7 +379,7 @@ typedef union TString { · 3 unmodified lines
226379 CommonHeader;
227380 lu_byte reserved;
228381 unsigned int hash;
229− size_t len;
382+ size_t len; /* number of characters in string */
230383 } tsv;
231384 } TString;
232385
@@ -247,7 +400,7 @@ typedef union Udata { · 14 unmodified lines
247400 CommonHeader;
248401 struct Table *metatable;
249402 struct Table *env;
250− size_t len;
403+ size_t len; /* number of bytes */
251404 } uv;
252405 } Udata;
253406
@@ -346,9 +499,9 @@ typedef union Closure { · 92 unmodified lines
346499 } Closure;
347500
348501
349−#define isLfunction(o) (ttisclosure(o) && !clvalue(o)->c.isC)
502+#define isLfunction(o) ttisLclosure(o)
350503
351−#define getproto(o) (clvalue(o)->l.p)
504+#define getproto(o) (clLvalue(o)->p)
352505
353506
354507 /*
@@ -403,12 +556,13 @@ typedef struct Table { · 48 unmodified lines
403556
404557 LUAI_DDEC const TValue luaO_nilobject_;
405558
559+
406560 LUAI_FUNC int luaO_int2fb (unsigned int x);
407561 LUAI_FUNC int luaO_fb2int (int x);
408−LUAI_FUNC int luaO_ceillog2 (lu_int32 x);
562+LUAI_FUNC int luaO_ceillog2 (unsigned int x);
409563 LUAI_FUNC lua_Number luaO_arith (int op, lua_Number v1, lua_Number v2);
410−LUAI_FUNC int luaO_rawequalObj (const TValue *t1, const TValue *t2);
411−LUAI_FUNC int luaO_str2d (const char *s, lua_Number *result);
564+LUAI_FUNC int luaO_str2d (const char *s, size_t len, lua_Number *result);
565+LUAI_FUNC int luaO_hexavalue (int c);
412566 LUAI_FUNC const char *luaO_pushvfstring (lua_State *L, const char *fmt,
413567 va_list argp);
414568 LUAI_FUNC const char *luaO_pushfstring (lua_State *L, const char *fmt, ...);
src/lopcodes.c+5−5
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lopcodes.c,v 1.44 2010/10/13 16:45:54 roberto Exp $
2+** $Id: lopcodes.c,v 1.48 2011/04/19 16:22:13 roberto Exp $
33 ** See Copyright Notice in lua.h
44 */
55
@@ -16,6 +16,7 @@ · 10 unmodified lines
1616 LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = {
1717 "MOVE",
1818 "LOADK",
19+ "LOADKX",
1920 "LOADBOOL",
2021 "LOADNIL",
2122 "GETUPVAL",
@@ -50,7 +51,6 @@ LUAI_DDEF const char *const luaP_opnames[NUM_OPCODES+1] = { · 28 unmodified lines
5051 "TFORCALL",
5152 "TFORLOOP",
5253 "SETLIST",
53− "CLOSE",
5454 "CLOSURE",
5555 "VARARG",
5656 "EXTRAARG",
@@ -64,8 +64,9 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 7 unmodified lines
6464 /* T A B C mode opcode */
6565 opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_MOVE */
6666 ,opmode(0, 1, OpArgK, OpArgN, iABx) /* OP_LOADK */
67+ ,opmode(0, 1, OpArgN, OpArgN, iABx) /* OP_LOADKX */
6768 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_LOADBOOL */
68− ,opmode(0, 1, OpArgR, OpArgN, iABC) /* OP_LOADNIL */
69+ ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_LOADNIL */
6970 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_GETUPVAL */
7071 ,opmode(0, 1, OpArgU, OpArgK, iABC) /* OP_GETTABUP */
7172 ,opmode(0, 1, OpArgR, OpArgK, iABC) /* OP_GETTABLE */
@@ -88,7 +89,7 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 16 unmodified lines
8889 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_EQ */
8990 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LT */
9091 ,opmode(1, 0, OpArgK, OpArgK, iABC) /* OP_LE */
91− ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TEST */
92+ ,opmode(1, 0, OpArgN, OpArgU, iABC) /* OP_TEST */
9293 ,opmode(1, 1, OpArgR, OpArgU, iABC) /* OP_TESTSET */
9394 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_CALL */
9495 ,opmode(0, 1, OpArgU, OpArgU, iABC) /* OP_TAILCALL */
@@ -98,7 +99,6 @@ LUAI_DDEF const lu_byte luaP_opmodes[NUM_OPCODES] = { · 3 unmodified lines
9899 ,opmode(0, 0, OpArgN, OpArgU, iABC) /* OP_TFORCALL */
99100 ,opmode(0, 1, OpArgR, OpArgN, iAsBx) /* OP_TFORLOOP */
100101 ,opmode(0, 0, OpArgU, OpArgU, iABC) /* OP_SETLIST */
101− ,opmode(0, 0, OpArgN, OpArgN, iABC) /* OP_CLOSE */
102102 ,opmode(0, 1, OpArgU, OpArgN, iABx) /* OP_CLOSURE */
103103 ,opmode(0, 1, OpArgU, OpArgN, iABC) /* OP_VARARG */
104104 ,opmode(0, 0, OpArgU, OpArgU, iAx) /* OP_EXTRAARG */
src/lopcodes.h+6−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lopcodes.h,v 1.137 2010/10/25 12:24:55 roberto Exp $
2+** $Id: lopcodes.h,v 1.141 2011/04/19 16:22:13 roberto Exp $
33 ** Opcodes for Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -167,9 +167,10 @@ typedef enum { · 161 unmodified lines
167167 name args description
168168 ------------------------------------------------------------------------*/
169169 OP_MOVE,/* A B R(A) := R(B) */
170−OP_LOADK,/* A Bx R(A) := Kst(Bx - 1) */
170+OP_LOADK,/* A Bx R(A) := Kst(Bx) */
171+OP_LOADKX,/* A R(A) := Kst(extra arg) */
171172 OP_LOADBOOL,/* A B C R(A) := (Bool)B; if (C) pc++ */
172−OP_LOADNIL,/* A B R(A) := ... := R(B) := nil */
173+OP_LOADNIL,/* A B R(A), R(A+1), ..., R(A+B) := nil */
173174 OP_GETUPVAL,/* A B R(A) := UpValue[B] */
174175
175176 OP_GETTABUP,/* A B C R(A) := UpValue[B][RK(C)] */
@@ -195,8 +196,7 @@ OP_LEN,/* A B R(A) := length of R(B) */ · 19 unmodified lines
195196
196197 OP_CONCAT,/* A B C R(A) := R(B).. ... ..R(C) */
197198
198−OP_JMP,/* sBx pc+=sBx */
199−
199+OP_JMP,/* A sBx pc+=sBx; if (A) close all upvalues >= R(A) + 1 */
200200 OP_EQ,/* A B C if ((RK(B) == RK(C)) ~= A) then pc++ */
201201 OP_LT,/* A B C if ((RK(B) < RK(C)) ~= A) then pc++ */
202202 OP_LE,/* A B C if ((RK(B) <= RK(C)) ~= A) then pc++ */
@@ -217,7 +217,6 @@ OP_TFORLOOP,/* A sBx if R(A+1) ~= nil then { R(A)=R(A+1); pc += sBx }*/ · 14 unmodified lines
217217
218218 OP_SETLIST,/* A B C R(A)[(C-1)*FPF+i] := R(A+i), 1 <= i <= B */
219219
220−OP_CLOSE,/* A close all variables in the stack up to (>=) R(A)*/
221220 OP_CLOSURE,/* A Bx R(A) := closure(KPROTO[Bx]) */
222221
223222 OP_VARARG,/* A B R(A), R(A+1), ..., R(A+B-2) = vararg */
@@ -244,7 +243,7 @@ OP_EXTRAARG/* Ax extra (larger) argument for previous opcode */ · 20 unmodified lines
244243 (*) In OP_SETLIST, if (B == 0) then B = `top'; if (C == 0) then next
245244 'instruction' is EXTRAARG(real C).
246245
247− (*) In OP_LOADK, if (Bx == 0) then next 'instruction' is EXTRAARG(real Bx).
246+ (*) In OP_LOADKX, the next 'instruction' is always EXTRAARG.
248247
249248 (*) For comparisons, A specifies what condition the test should accept
250249 (true or false).
src/loslib.c+11−20
@@ -1,5 +1,5 @@
11 /*
2−** $Id: loslib.c,v 1.32 2010/10/05 12:18:03 roberto Exp $
2+** $Id: loslib.c,v 1.34 2011/03/03 16:34:46 roberto Exp $
33 ** Standard Operating System library
44 ** See Copyright Notice in lua.h
55 */
@@ -59,37 +59,28 @@ · 53 unmodified lines
5959
6060
6161
62−static int os_pushresult (lua_State *L, int i, const char *filename) {
63− int en = errno; /* calls to Lua API may change this value */
64− if (i) {
65− lua_pushboolean(L, 1);
66− return 1;
67− }
62+static int os_execute (lua_State *L) {
63+ const char *cmd = luaL_optstring(L, 1, NULL);
64+ int stat = system(cmd);
65+ if (cmd != NULL)
66+ return luaL_execresult(L, stat);
6867 else {
69− lua_pushnil(L);
70− lua_pushfstring(L, "%s: %s", filename, strerror(en));
71− lua_pushinteger(L, en);
72− return 3;
68+ lua_pushboolean(L, stat); /* true if there is a shell */
69+ return 1;
7370 }
7471 }
7572
7673
77−static int os_execute (lua_State *L) {
78− lua_pushinteger(L, system(luaL_optstring(L, 1, NULL)));
79− return 1;
80−}
81−
82−
8374 static int os_remove (lua_State *L) {
8475 const char *filename = luaL_checkstring(L, 1);
85− return os_pushresult(L, remove(filename) == 0, filename);
76+ return luaL_fileresult(L, remove(filename) == 0, filename);
8677 }
8778
8879
8980 static int os_rename (lua_State *L) {
9081 const char *fromname = luaL_checkstring(L, 1);
9182 const char *toname = luaL_checkstring(L, 2);
92− return os_pushresult(L, rename(fromname, toname) == 0, fromname);
83+ return luaL_fileresult(L, rename(fromname, toname) == 0, fromname);
9384 }
9485
9586
@@ -182,7 +173,7 @@ static const char *checkoption (lua_State *L, const char *conv, char *buff) { · 86 unmodified lines
182173 return conv; /* to avoid warnings */
183174 }
184175
185−
176+
186177 static int os_date (lua_State *L) {
187178 const char *s = luaL_optstring(L, 1, "%c");
188179 time_t t = luaL_opt(L, (time_t)luaL_checknumber, 2, time(NULL));
src/lparser.c+320−145
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.c,v 2.92 2010/09/07 19:21:39 roberto Exp $
2+** $Id: lparser.c,v 2.109 2011/05/02 17:33:01 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -41,10 +41,11 @@ · 35 unmodified lines
4141 */
4242 typedef struct BlockCnt {
4343 struct BlockCnt *previous; /* chain */
44− int breaklist; /* list of jumps out of this loop */
45− lu_byte nactvar; /* # active locals outside the breakable structure */
44+ int firstlabel; /* index of first label in this block */
45+ int firstgoto; /* index of first pending goto in this block */
46+ lu_byte nactvar; /* # active locals outside the block */
4647 lu_byte upval; /* true if some variable in the block is an upvalue */
47− lu_byte isbreakable; /* true if `block' is a loop */
48+ lu_byte isloop; /* true if `block' is a loop */
4849 } BlockCnt;
4950
5051
@@ -52,7 +53,7 @@ typedef struct BlockCnt { · 1 unmodified line
5253 /*
5354 ** prototypes for recursive non-terminal functions
5455 */
55−static void chunk (LexState *ls);
56+static void statement (LexState *ls);
5657 static void expr (LexState *ls, expdesc *v);
5758
5859
@@ -66,6 +67,13 @@ static void anchor_token (LexState *ls) { · 7 unmodified lines
6667 }
6768
6869
70+/* semantic error */
71+static void semerror (LexState *ls, const char *msg) {
72+ ls->t.token = 0; /* remove 'near to' from final message */
73+ luaX_syntaxerror(ls, msg);
74+}
75+
76+
6977 static void error_expected (LexState *ls, int token) {
7078 luaX_syntaxerror(ls,
7179 luaO_pushfstring(ls->L, "%s expected", luaX_token2str(ls, token)));
@@ -103,6 +111,7 @@ static void check (LexState *ls, int c) { · 31 unmodified lines
103111 error_expected(ls, c);
104112 }
105113
114+
106115 static void checknext (LexState *ls, int c) {
107116 check(ls, c);
108117 luaX_next(ls);
@@ -167,13 +176,13 @@ static int registerlocalvar (LexState *ls, TString *varname) { · 58 unmodified lines
167176
168177 static void new_localvar (LexState *ls, TString *name) {
169178 FuncState *fs = ls->fs;
170− Varlist *vl = ls->varl;
179+ Dyndata *dyd = ls->dyd;
171180 int reg = registerlocalvar(ls, name);
172− checklimit(fs, vl->nactvar + 1 - fs->firstlocal,
181+ checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal,
173182 MAXVARS, "local variables");
174− luaM_growvector(ls->L, vl->actvar, vl->nactvar + 1,
175− vl->actvarsize, vardesc, MAX_INT, "local variables");
176− vl->actvar[vl->nactvar++].idx = cast(unsigned short, reg);
183+ luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1,
184+ dyd->actvar.size, Vardesc, MAX_INT, "local variables");
185+ dyd->actvar.arr[dyd->actvar.n++].idx = cast(unsigned short, reg);
177186 }
178187
179188
@@ -186,7 +195,7 @@ static void new_localvarliteral_ (LexState *ls, const char *name, size_t sz) { · 6 unmodified lines
186195
187196
188197 static LocVar *getlocvar (FuncState *fs, int i) {
189− int idx = fs->ls->varl->actvar[fs->firstlocal + i].idx;
198+ int idx = fs->ls->dyd->actvar.arr[fs->firstlocal + i].idx;
190199 lua_assert(idx < fs->nlocvars);
191200 return &fs->f->locvars[idx];
192201 }
@@ -202,7 +211,7 @@ static void adjustlocalvars (LexState *ls, int nvars) { · 9 unmodified lines
202211
203212
204213 static void removevars (FuncState *fs, int tolevel) {
205− fs->ls->varl->nactvar -= (fs->nactvar - tolevel);
214+ fs->ls->dyd->actvar.n -= (fs->nactvar - tolevel);
206215 while (fs->nactvar > tolevel)
207216 getlocvar(fs, --fs->nactvar)->endpc = fs->pc;
208217 }
@@ -245,12 +254,12 @@ static int searchvar (FuncState *fs, TString *n) { · 36 unmodified lines
245254
246255 /*
247256 Mark block where variable at given level was defined
248− (to emit OP_CLOSE later).
257+ (to emit close instructions later).
249258 */
250259 static void markupval (FuncState *fs, int level) {
251260 BlockCnt *bl = fs->bl;
252− while (bl && bl->nactvar > level) bl = bl->previous;
253− if (bl) bl->upval = 1;
261+ while (bl->nactvar > level) bl = bl->previous;
262+ bl->upval = 1;
254263 }
255264
256265
@@ -327,10 +336,107 @@ static void enterlevel (LexState *ls) { · 70 unmodified lines
327336 #define leavelevel(ls) (G((ls)->L)->nCcalls--)
328337
329338
330−static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) {
331− bl->breaklist = NO_JUMP;
332− bl->isbreakable = isbreakable;
339+static void closegoto (LexState *ls, int g, Labeldesc *label) {
340+ int i;
341+ FuncState *fs = ls->fs;
342+ Labellist *gl = &ls->dyd->gt;
343+ Labeldesc *gt = &gl->arr[g];
344+ lua_assert(eqstr(gt->name, label->name));
345+ if (gt->nactvar < label->nactvar) {
346+ const char *msg = luaO_pushfstring(ls->L,
347+ "<goto %s> at line %d jumps into the scope of local " LUA_QS,
348+ getstr(gt->name), gt->line, getstr(getlocvar(fs, gt->nactvar)->varname));
349+ semerror(ls, msg);
350+ }
351+ luaK_patchlist(fs, gt->pc, label->pc);
352+ /* remove goto from pending list */
353+ for (i = g; i < gl->n - 1; i++)
354+ gl->arr[i] = gl->arr[i + 1];
355+ gl->n--;
356+}
357+
358+
359+/*
360+** try to close a goto with existing labels; this solves backward jumps
361+*/
362+static int findlabel (LexState *ls, int g) {
363+ int i;
364+ BlockCnt *bl = ls->fs->bl;
365+ Dyndata *dyd = ls->dyd;
366+ Labeldesc *gt = &dyd->gt.arr[g];
367+ /* check labels in current block for a match */
368+ for (i = bl->firstlabel; i < dyd->label.n; i++) {
369+ Labeldesc *lb = &dyd->label.arr[i];
370+ if (eqstr(lb->name, gt->name)) { /* correct label? */
371+ if (gt->nactvar > lb->nactvar &&
372+ (bl->upval || dyd->label.n > bl->firstlabel))
373+ luaK_patchclose(ls->fs, gt->pc, lb->nactvar);
374+ closegoto(ls, g, lb); /* close it */
375+ return 1;
376+ }
377+ }
378+ return 0; /* label not found; cannot close goto */
379+}
380+
381+
382+static int newlabelentry (LexState *ls, Labellist *l, TString *name,
383+ int line, int pc) {
384+ int n = l->n;
385+ luaM_growvector(ls->L, l->arr, n, l->size, Labeldesc, MAX_INT, "labels");
386+ l->arr[n].name = name;
387+ l->arr[n].line = line;
388+ l->arr[n].nactvar = ls->fs->nactvar;
389+ l->arr[n].pc = pc;
390+ l->n++;
391+ return n;
392+}
393+
394+
395+/*
396+** check whether new label 'lb' matches any pending gotos in current
397+** block; solves forward jumps
398+*/
399+static void findgotos (LexState *ls, Labeldesc *lb) {
400+ Labellist *gl = &ls->dyd->gt;
401+ int i = ls->fs->bl->firstgoto;
402+ while (i < gl->n) {
403+ if (eqstr(gl->arr[i].name, lb->name))
404+ closegoto(ls, i, lb);
405+ else
406+ i++;
407+ }
408+}
409+
410+
411+/*
412+** "export" pending gotos to outer level, to check them against
413+** outer labels; if the block being exited has upvalues, and
414+** the goto exists the scope of any variable (which can be the
415+** upvalue), close those variables being exited.
416+*/
417+static void movegotosout (FuncState *fs, BlockCnt *bl) {
418+ int i = bl->firstgoto;
419+ Labellist *gl = &fs->ls->dyd->gt;
420+ /* correct pending gotos to current block and try to close it
421+ with visible labels */
422+ while (i < gl->n) {
423+ Labeldesc *gt = &gl->arr[i];
424+ if (gt->nactvar > bl->nactvar) {
425+ if (bl->upval)
426+ luaK_patchclose(fs, gt->pc, bl->nactvar);
427+ gt->nactvar = bl->nactvar;
428+ }
429+ if (!findlabel(fs->ls, i))
430+ i++; /* move to next one */
431+ }
432+}
433+
434+
435+static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isloop) {
436+ bl->isloop = isloop;
333437 bl->nactvar = fs->nactvar;
438+ bl->firstlabel = fs->ls->dyd->label.n;
439+ bl->firstgoto = fs->ls->dyd->gt.n;
334440 bl->upval = 0;
335441 bl->previous = fs->bl;
336442 fs->bl = bl;
@@ -338,17 +444,48 @@ static void enterblock (FuncState *fs, BlockCnt *bl, lu_byte isbreakable) { · 1 unmodified line
338444 }
339445
340446
447+/*
448+** create a label named "break" to resolve break statements
449+*/
450+static void breaklabel (LexState *ls) {
451+ TString *n = luaS_new(ls->L, "break");
452+ int l = newlabelentry(ls, &ls->dyd->label, n, 0, ls->fs->pc);
453+ findgotos(ls, &ls->dyd->label.arr[l]);
454+}
455+
456+/*
457+** generates an error for an undefined 'goto'; choose appropriate
458+** message when label name is a reserved word (which can only be 'break')
459+*/
460+static void undefgoto (LexState *ls, Labeldesc *gt) {
461+ const char *msg = (gt->name->tsv.reserved > 0)
462+ ? "<%s> at line %d not inside a loop"
463+ : "label " LUA_QS " (<goto> at line %d) undefined";
464+ msg = luaO_pushfstring(ls->L, msg, getstr(gt->name), gt->line);
465+ semerror(ls, msg);
466+}
467+
468+
341469 static void leaveblock (FuncState *fs) {
342470 BlockCnt *bl = fs->bl;
471+ LexState *ls = fs->ls;
472+ if (bl->previous && bl->upval) {
473+ /* create a 'jump to here' to close upvalues */
474+ int j = luaK_jump(fs);
475+ luaK_patchclose(fs, j, bl->nactvar);
476+ luaK_patchtohere(fs, j);
477+ }
478+ if (bl->isloop)
479+ breaklabel(ls); /* close pending breaks */
343480 fs->bl = bl->previous;
344481 removevars(fs, bl->nactvar);
345− if (bl->upval)
346− luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
347− /* a block either controls scope or breaks (never both) */
348− lua_assert(!bl->isbreakable || !bl->upval);
349482 lua_assert(bl->nactvar == fs->nactvar);
350483 fs->freereg = fs->nactvar; /* free registers */
351− luaK_patchtohere(fs, bl->breaklist);
484+ ls->dyd->label.n = bl->firstlabel; /* remove local labels */
485+ if (bl->previous) /* inner block? */
486+ movegotosout(fs, bl); /* update pending gotos to outer block */
487+ else if (bl->firstgoto < ls->dyd->gt.n) /* pending gotos in outer block? */
488+ undefgoto(ls, &ls->dyd->gt.arr[bl->firstgoto]); /* error */
352489 }
353490
354491
@@ -368,10 +505,11 @@ static void codeclosure (LexState *ls, Proto *clp, expdesc *v) { · 13 unmodified lines
368505 f->p[fs->np++] = clp;
369506 luaC_objbarrier(ls->L, f, clp);
370507 init_exp(v, VRELOCABLE, luaK_codeABx(fs, OP_CLOSURE, 0, fs->np-1));
508+ luaK_exp2nextreg(fs, v); /* fix it at stack top (for GC) */
371509 }
372510
373511
374−static void open_func (LexState *ls, FuncState *fs) {
512+static void open_func (LexState *ls, FuncState *fs, BlockCnt *bl) {
375513 lua_State *L = ls->L;
376514 Proto *f;
377515 fs->prev = ls->fs; /* linked list of funcstates */
@@ -387,7 +525,7 @@ static void open_func (LexState *ls, FuncState *fs) { · 9 unmodified lines
387525 fs->nups = 0;
388526 fs->nlocvars = 0;
389527 fs->nactvar = 0;
390− fs->firstlocal = ls->varl->nactvar;
528+ fs->firstlocal = ls->dyd->actvar.n;
391529 fs->bl = NULL;
392530 f = luaF_newproto(L);
393531 fs->f = f;
@@ -400,6 +538,7 @@ static void open_func (LexState *ls, FuncState *fs) { · 6 unmodified lines
400538 /* anchor table of constants (to avoid being collected) */
401539 sethvalue2s(L, L->top, fs->h);
402540 incr_top(L);
541+ enterblock(fs, bl, 0);
403542 }
404543
405544
@@ -408,7 +547,7 @@ static void close_func (LexState *ls) { · 2 unmodified lines
408547 FuncState *fs = ls->fs;
409548 Proto *f = fs->f;
410549 luaK_ret(fs, 0, 0); /* final return */
411− removevars(fs, 0);
550+ leaveblock(fs);
412551 luaM_reallocvector(L, f->code, f->sizecode, fs->pc, Instruction);
413552 f->sizecode = fs->pc;
414553 luaM_reallocvector(L, f->lineinfo, f->sizelineinfo, fs->pc, int);
@@ -435,42 +574,49 @@ static void close_func (LexState *ls) { · 20 unmodified lines
435574 ** opens the main function, which is a regular vararg function with an
436575 ** upvalue named LUA_ENV
437576 */
438−static void open_mainfunc (LexState *ls, FuncState *fs) {
577+static void open_mainfunc (LexState *ls, FuncState *fs, BlockCnt *bl) {
439578 expdesc v;
440− open_func(ls, fs);
579+ open_func(ls, fs, bl);
441580 fs->f->is_vararg = 1; /* main function is always vararg */
442581 init_exp(&v, VLOCAL, 0);
443582 newupvalue(fs, ls->envn, &v); /* create environment upvalue */
444583 }
445584
446585
447−Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff, Varlist *varl,
448− const char *name) {
449− LexState lexstate;
450− FuncState funcstate;
451− TString *tname = luaS_new(L, name);
452− setsvalue2s(L, L->top, tname); /* push name to protect it */
453− incr_top(L);
454− lexstate.buff = buff;
455− lexstate.varl = varl;
456− luaX_setinput(L, &lexstate, z, tname);
457− open_mainfunc(&lexstate, &funcstate);
458− luaX_next(&lexstate); /* read first token */
459− chunk(&lexstate); /* read main chunk */
460− check(&lexstate, TK_EOS);
461− close_func(&lexstate);
462− L->top--; /* pop name */
463− lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
464− return funcstate.f;
465−}
466−
467−
468586
469587 /*============================================================*/
470588 /* GRAMMAR RULES */
471589 /*============================================================*/
472590
473591
592+/*
593+** check whether current token is in the follow set of a block.
594+** 'until' closes syntactical blocks, but do not close scope,
595+** so it handled in separate.
596+*/
597+static int block_follow (LexState *ls, int withuntil) {
598+ switch (ls->t.token) {
599+ case TK_ELSE: case TK_ELSEIF:
600+ case TK_END: case TK_EOS:
601+ return 1;
602+ case TK_UNTIL: return withuntil;
603+ default: return 0;
604+ }
605+}
606+
607+
608+static void statlist (LexState *ls) {
609+ /* statlist -> { stat [`;'] } */
610+ while (!block_follow(ls, 1)) {
611+ if (ls->t.token == TK_RETURN) {
612+ statement(ls);
613+ return; /* 'return' must be last statement */
614+ }
615+ statement(ls);
616+ }
617+}
618+
619+
474620 static void fieldsel (LexState *ls, expdesc *v) {
475621 /* fieldsel -> ['.' | ':'] NAME */
476622 FuncState *fs = ls->fs;
@@ -586,7 +732,7 @@ static void field (LexState *ls, struct ConsControl *cc) { · 109 unmodified lines
586732
587733
588734 static void constructor (LexState *ls, expdesc *t) {
589− /* constructor -> '{' [ field { sep field } [sep] ] '}'
735+ /* constructor -> '{' [ field { sep field } [sep] ] '}'
590736 sep -> ',' | ';' */
591737 FuncState *fs = ls->fs;
592738 int line = ls->linenumber;
@@ -596,7 +742,7 @@ static void constructor (LexState *ls, expdesc *t) { · 3 unmodified lines
596742 cc.t = t;
597743 init_exp(t, VRELOCABLE, pc);
598744 init_exp(&cc.v, VVOID, 0); /* no value (yet) */
599− luaK_exp2nextreg(ls->fs, t); /* fix it at stack top (for gc) */
745+ luaK_exp2nextreg(ls->fs, t); /* fix it at stack top */
600746 checknext(ls, '{');
601747 do {
602748 lua_assert(cc.v.k == VVOID || cc.tostore > 0);
@@ -643,19 +789,20 @@ static void parlist (LexState *ls) { · 40 unmodified lines
643789 }
644790
645791
646−static void body (LexState *ls, expdesc *e, int needself, int line) {
647− /* body -> `(' parlist `)' chunk END */
792+static void body (LexState *ls, expdesc *e, int ismethod, int line) {
793+ /* body -> `(' parlist `)' block END */
648794 FuncState new_fs;
649− open_func(ls, &new_fs);
795+ BlockCnt bl;
796+ open_func(ls, &new_fs, &bl);
650797 new_fs.f->linedefined = line;
651798 checknext(ls, '(');
652− if (needself) {
653− new_localvarliteral(ls, "self");
799+ if (ismethod) {
800+ new_localvarliteral(ls, "self"); /* create 'self' parameter */
654801 adjustlocalvars(ls, 1);
655802 }
656803 parlist(ls);
657804 checknext(ls, ')');
658− chunk(ls);
805+ statlist(ls);
659806 new_fs.f->lastlinedefined = ls->linenumber;
660807 check_match(ls, TK_END, TK_FUNCTION, line);
661808 codeclosure(ls, new_fs.f, e);
@@ -663,8 +810,8 @@ static void body (LexState *ls, expdesc *e, int needself, int line) { · 1 unmodified line
663810 }
664811
665812
666−static int explist1 (LexState *ls, expdesc *v) {
667− /* explist1 -> expr { `,' expr } */
813+static int explist (LexState *ls, expdesc *v) {
814+ /* explist -> expr { `,' expr } */
668815 int n = 1; /* at least one expression */
669816 expr(ls, v);
670817 while (testnext(ls, ',')) {
@@ -681,12 +828,12 @@ static void funcargs (LexState *ls, expdesc *f, int line) { · 10 unmodified lines
681828 expdesc args;
682829 int base, nparams;
683830 switch (ls->t.token) {
684− case '(': { /* funcargs -> `(' [ explist1 ] `)' */
831+ case '(': { /* funcargs -> `(' [ explist ] `)' */
685832 luaX_next(ls);
686833 if (ls->t.token == ')') /* arg list is empty? */
687834 args.k = VVOID;
688835 else {
689− explist1(ls, &args);
836+ explist(ls, &args);
690837 luaK_setmultret(fs, &args);
691838 }
692839 check_match(ls, ')', '(', line);
@@ -937,23 +1084,12 @@ static void expr (LexState *ls, expdesc *v) { · 244 unmodified lines
9371084 */
9381085
9391086
940−static int block_follow (int token) {
941− switch (token) {
942− case TK_ELSE: case TK_ELSEIF: case TK_END:
943− case TK_UNTIL: case TK_EOS:
944− return 1;
945− default: return 0;
946− }
947−}
948−
949−
9501087 static void block (LexState *ls) {
951− /* block -> chunk */
1088+ /* block -> statlist */
9521089 FuncState *fs = ls->fs;
9531090 BlockCnt bl;
9541091 enterblock(fs, &bl, 0);
955− chunk(ls);
956− lua_assert(bl.breaklist == NO_JUMP);
1092+ statlist(ls);
9571093 leaveblock(fs);
9581094 }
9591095
@@ -1012,10 +1148,10 @@ static void assignment (LexState *ls, struct LHS_assign *lh, int nvars) { · 52 unmodified lines
10121148 "variable names");
10131149 assignment(ls, &nv, nvars+1);
10141150 }
1015− else { /* assignment -> `=' explist1 */
1151+ else { /* assignment -> `=' explist */
10161152 int nexps;
10171153 checknext(ls, '=');
1018− nexps = explist1(ls, &e);
1154+ nexps = explist(ls, &e);
10191155 if (nexps != nvars) {
10201156 adjust_assign(ls, nvars, nexps, &e);
10211157 if (nexps > nvars)
@@ -1042,19 +1178,38 @@ static int cond (LexState *ls) { · 20 unmodified lines
10421178 }
10431179
10441180
1045−static void breakstat (LexState *ls) {
1181+static void gotostat (LexState *ls, TString *label, int line) {
1182+ /* create new entry for this goto */
1183+ int g = newlabelentry(ls, &ls->dyd->gt, label, line, luaK_jump(ls->fs));
1184+ findlabel(ls, g);
1185+}
1186+
1187+
1188+static void labelstat (LexState *ls, TString *label, int line) {
1189+ /* label -> '@' NAME ':' */
10461190 FuncState *fs = ls->fs;
1047− BlockCnt *bl = fs->bl;
1048− int upval = 0;
1049− while (bl && !bl->isbreakable) {
1050− upval |= bl->upval;
1051− bl = bl->previous;
1191+ Labellist *ll = &ls->dyd->label;
1192+ int l, i; /* index of new label being created */
1193+ /* check for repeated labels on the same block */
1194+ for (i = ls->fs->bl->firstlabel; i < ll->n; i++) {
1195+ if (eqstr(label, ll->arr[i].name)) {
1196+ const char *msg = luaO_pushfstring(ls->L,
1197+ "label " LUA_QS " already defined on line %d",
1198+ getstr(label), ll->arr[i].line);
1199+ semerror(ls, msg);
1200+ }
10521201 }
1053− if (!bl)
1054− luaX_syntaxerror(ls, "no loop to break");
1055− if (upval)
1056− luaK_codeABC(fs, OP_CLOSE, bl->nactvar, 0, 0);
1057− luaK_concat(fs, &bl->breaklist, luaK_jump(fs));
1202+ checknext(ls, ':'); /* skip colon */
1203+ /* create new entry for this label */
1204+ l = newlabelentry(ls, ll, label, line, fs->pc);
1205+ /* skip other no-op statements */
1206+ while (ls->t.token == ';' || ls->t.token == '@')
1207+ statement(ls);
1208+ if (block_follow(ls, 0)) { /* label is last no-op statement in the block? */
1209+ /* assume that locals are already out of scope */
1210+ ll->arr[l].nactvar = fs->bl->nactvar;
1211+ }
1212+ findgotos(ls, &ll->arr[l]);
10581213 }
10591214
10601215
@@ -1086,19 +1241,13 @@ static void repeatstat (LexState *ls, int line) { · 25 unmodified lines
10861241 enterblock(fs, &bl1, 1); /* loop block */
10871242 enterblock(fs, &bl2, 0); /* scope block */
10881243 luaX_next(ls); /* skip REPEAT */
1089− chunk(ls);
1244+ statlist(ls);
10901245 check_match(ls, TK_UNTIL, TK_REPEAT, line);
10911246 condexit = cond(ls); /* read condition (inside scope block) */
1092− if (!bl2.upval) { /* no upvalues? */
1093− leaveblock(fs); /* finish scope */
1094− luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
1095− }
1096− else { /* complete semantics when there are upvalues */
1097− breakstat(ls); /* if condition then break */
1098− luaK_patchtohere(ls->fs, condexit); /* else... */
1099− leaveblock(fs); /* finish scope... */
1100− luaK_jumpto(fs, repeat_init); /* and repeat */
1101− }
1247+ if (bl2.upval) /* upvalues? */
1248+ luaK_patchclose(fs, condexit, bl2.nactvar);
1249+ leaveblock(fs); /* finish scope */
1250+ luaK_patchlist(fs, condexit, repeat_init); /* close the loop */
11021251 leaveblock(fs); /* finish loop */
11031252 }
11041253
@@ -1163,7 +1312,7 @@ static void fornum (LexState *ls, TString *varname, int line) { · 58 unmodified lines
11631312
11641313
11651314 static void forlist (LexState *ls, TString *indexname) {
1166− /* forlist -> NAME {,NAME} IN explist1 forbody */
1315+ /* forlist -> NAME {,NAME} IN explist forbody */
11671316 FuncState *fs = ls->fs;
11681317 expdesc e;
11691318 int nvars = 4; /* gen, state, control, plus at least one declared var */
@@ -1181,7 +1330,7 @@ static void forlist (LexState *ls, TString *indexname) { · 11 unmodified lines
11811330 }
11821331 checknext(ls, TK_IN);
11831332 line = ls->linenumber;
1184− adjust_assign(ls, 3, explist1(ls, &e), &e);
1333+ adjust_assign(ls, 3, explist(ls, &e), &e);
11851334 luaK_checkstack(fs, 3); /* extra space to call generator */
11861335 forbody(ls, base, line, nvars - 3, 0);
11871336 }
@@ -1241,19 +1390,20 @@ static void ifstat (LexState *ls, int line) { · 53 unmodified lines
12411390
12421391
12431392 static void localfunc (LexState *ls) {
1244− expdesc v, b;
1393+ expdesc b;
12451394 FuncState *fs = ls->fs;
1246− new_localvar(ls, str_checkname(ls));
1247− init_exp(&v, VLOCAL, fs->freereg);
1248− luaK_reserveregs(fs, 1);
1249− adjustlocalvars(ls, 1);
1250− body(ls, &b, 0, ls->linenumber);
1251− luaK_storevar(fs, &v, &b);
1395+ int varidx = fs->nactvar; /* index of new local variable */
1396+ new_localvar(ls, str_checkname(ls)); /* new local variable */
1397+ adjustlocalvars(ls, 1); /* enter its scope */
1398+ body(ls, &b, 0, ls->linenumber); /* function created in next register */
1399+ lua_assert(b.k == VNONRELOC && b.u.ind.idx == varidx);
1400+ /* debug information will only see the variable after this point! */
1401+ getlocvar(fs, varidx)->startpc = fs->pc;
12521402 }
12531403
12541404
12551405 static void localstat (LexState *ls) {
1256− /* stat -> LOCAL NAME {`,' NAME} [`=' explist1] */
1406+ /* stat -> LOCAL NAME {`,' NAME} [`=' explist] */
12571407 int nvars = 0;
12581408 int nexps;
12591409 expdesc e;
@@ -1262,7 +1412,7 @@ static void localstat (LexState *ls) { · 2 unmodified lines
12621412 nvars++;
12631413 } while (testnext(ls, ','));
12641414 if (testnext(ls, '='))
1265− nexps = explist1(ls, &e);
1415+ nexps = explist(ls, &e);
12661416 else {
12671417 e.k = VVOID;
12681418 nexps = 0;
@@ -1274,25 +1424,25 @@ static void localstat (LexState *ls) { · 5 unmodified lines
12741424
12751425 static int funcname (LexState *ls, expdesc *v) {
12761426 /* funcname -> NAME {fieldsel} [`:' NAME] */
1277− int needself = 0;
1427+ int ismethod = 0;
12781428 singlevar(ls, v);
12791429 while (ls->t.token == '.')
12801430 fieldsel(ls, v);
12811431 if (ls->t.token == ':') {
1282− needself = 1;
1432+ ismethod = 1;
12831433 fieldsel(ls, v);
12841434 }
1285− return needself;
1435+ return ismethod;
12861436 }
12871437
12881438
12891439 static void funcstat (LexState *ls, int line) {
12901440 /* funcstat -> FUNCTION funcname body */
1291− int needself;
1441+ int ismethod;
12921442 expdesc v, b;
12931443 luaX_next(ls); /* skip FUNCTION */
1294− needself = funcname(ls, &v);
1295− body(ls, &b, needself, line);
1444+ ismethod = funcname(ls, &v);
1445+ body(ls, &b, ismethod, line);
12961446 luaK_storevar(ls->fs, &v, &b);
12971447 luaK_fixline(ls->fs, line); /* definition `happens' in the first line */
12981448 }
@@ -1313,14 +1463,14 @@ static void exprstat (LexState *ls) { · 14 unmodified lines
13131463
13141464
13151465 static void retstat (LexState *ls) {
1316− /* stat -> RETURN explist */
1466+ /* stat -> RETURN [explist] [';'] */
13171467 FuncState *fs = ls->fs;
13181468 expdesc e;
13191469 int first, nret; /* registers with returned values */
1320− if (block_follow(ls->t.token) || ls->t.token == ';')
1470+ if (block_follow(ls, 1) || ls->t.token == ';')
13211471 first = nret = 0; /* return no values */
13221472 else {
1323− nret = explist1(ls, &e); /* optional return values */
1473+ nret = explist(ls, &e); /* optional return values */
13241474 if (hasmultret(e.k)) {
13251475 luaK_setmultret(fs, &e);
13261476 if (e.k == VCALL && nret == 1) { /* tail call? */
@@ -1341,41 +1491,43 @@ static void retstat (LexState *ls) { · 14 unmodified lines
13411491 }
13421492 }
13431493 luaK_ret(fs, first, nret);
1494+ testnext(ls, ';'); /* skip optional semicolon */
13441495 }
13451496
13461497
1347−static int statement (LexState *ls) {
1498+static void statement (LexState *ls) {
13481499 int line = ls->linenumber; /* may be needed for error messages */
1500+ enterlevel(ls);
13491501 switch (ls->t.token) {
13501502 case ';': { /* stat -> ';' (empty statement) */
13511503 luaX_next(ls); /* skip ';' */
1352− return 0;
1504+ break;
13531505 }
13541506 case TK_IF: { /* stat -> ifstat */
13551507 ifstat(ls, line);
1356− return 0;
1508+ break;
13571509 }
13581510 case TK_WHILE: { /* stat -> whilestat */
13591511 whilestat(ls, line);
1360− return 0;
1512+ break;
13611513 }
13621514 case TK_DO: { /* stat -> DO block END */
13631515 luaX_next(ls); /* skip DO */
13641516 block(ls);
13651517 check_match(ls, TK_END, TK_DO, line);
1366− return 0;
1518+ break;
13671519 }
13681520 case TK_FOR: { /* stat -> forstat */
13691521 forstat(ls, line);
1370− return 0;
1522+ break;
13711523 }
13721524 case TK_REPEAT: { /* stat -> repeatstat */
13731525 repeatstat(ls, line);
1374− return 0;
1526+ break;
13751527 }
13761528 case TK_FUNCTION: { /* stat -> funcstat */
13771529 funcstat(ls, line);
1378− return 0;
1530+ break;
13791531 }
13801532 case TK_LOCAL: { /* stat -> localstat */
13811533 luaX_next(ls); /* skip LOCAL */
@@ -1383,39 +1535,62 @@ static int statement (LexState *ls) { · 1 unmodified line
13831535 localfunc(ls);
13841536 else
13851537 localstat(ls);
1386− return 0;
1538+ break;
1539+ }
1540+ case '@': { /* stat -> label */
1541+ luaX_next(ls); /* skip '@' */
1542+ labelstat(ls, str_checkname(ls), line);
1543+ break;
13871544 }
13881545 case TK_RETURN: { /* stat -> retstat */
13891546 luaX_next(ls); /* skip RETURN */
13901547 retstat(ls);
1391− return 1; /* must be last statement */
1548+ break;
13921549 }
13931550 case TK_BREAK: { /* stat -> breakstat */
13941551 luaX_next(ls); /* skip BREAK */
1395− breakstat(ls);
1396− return 1; /* must be last statement */
1552+ /* code it as "goto 'break'" */
1553+ gotostat(ls, luaS_new(ls->L, "break"), line);
1554+ break;
1555+ }
1556+ case TK_GOTO: { /* stat -> 'goto' NAME */
1557+ luaX_next(ls); /* skip GOTO */
1558+ gotostat(ls, str_checkname(ls), line);
1559+ break;
13971560 }
13981561 default: { /* stat -> func | assignment */
13991562 exprstat(ls);
1400− return 0;
1563+ break;
14011564 }
14021565 }
1566+ lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1567+ ls->fs->freereg >= ls->fs->nactvar);
1568+ ls->fs->freereg = ls->fs->nactvar; /* free registers */
1569+ leavelevel(ls);
14031570 }
14041571
1572+/* }====================================================================== */
14051573
1406−static void chunk (LexState *ls) {
1407− /* chunk -> { stat [`;'] } */
1408− int islast = 0;
1409− enterlevel(ls);
1410− while (!islast && !block_follow(ls->t.token)) {
1411− islast = statement(ls);
1412− if (islast)
1413− testnext(ls, ';');
1414− lua_assert(ls->fs->f->maxstacksize >= ls->fs->freereg &&
1415− ls->fs->freereg >= ls->fs->nactvar);
1416− ls->fs->freereg = ls->fs->nactvar; /* free registers */
1417− }
1418− leavelevel(ls);
1574+
1575+Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
1576+ Dyndata *dyd, const char *name, int firstchar) {
1577+ LexState lexstate;
1578+ FuncState funcstate;
1579+ BlockCnt bl;
1580+ TString *tname = luaS_new(L, name);
1581+ setsvalue2s(L, L->top, tname); /* push name to protect it */
1582+ incr_top(L);
1583+ lexstate.buff = buff;
1584+ lexstate.dyd = dyd;
1585+ dyd->actvar.n = dyd->gt.n = dyd->label.n = 0;
1586+ luaX_setinput(L, &lexstate, z, tname, firstchar);
1587+ open_mainfunc(&lexstate, &funcstate, &bl);
1588+ luaX_next(&lexstate); /* read first token */
1589+ statlist(&lexstate); /* main body */
1590+ check(&lexstate, TK_EOS);
1591+ close_func(&lexstate);
1592+ L->top--; /* pop name */
1593+ lua_assert(!funcstate.prev && funcstate.nups == 1 && !lexstate.fs);
1594+ return funcstate.f;
14191595 }
14201596
1421−/* }====================================================================== */
src/lparser.h+34−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lparser.h,v 1.65 2010/07/07 16:27:29 roberto Exp $
2+** $Id: lparser.h,v 1.68 2011/02/23 13:13:10 roberto Exp $
33 ** Lua Parser
44 ** See Copyright Notice in lua.h
55 */
@@ -53,19 +53,42 @@ typedef struct expdesc { · 47 unmodified lines
5353 } expdesc;
5454
5555
56−typedef struct vardesc {
57− unsigned short idx;
58−} vardesc;
56+/* description of active local variable */
57+typedef struct Vardesc {
58+ unsigned short idx; /* variable index in stack */
59+} Vardesc;
5960
6061
61−/* list of all active local variables */
62−typedef struct Varlist {
63− vardesc *actvar;
64− int nactvar;
65− int actvarsize;
66−} Varlist;
62+/* description of pending goto statements and label statements */
63+typedef struct Labeldesc {
64+ TString *name; /* label identifier */
65+ int pc; /* position in code */
66+ int line; /* line where it appeared */
67+ lu_byte nactvar; /* local level where it appears in current block */
68+} Labeldesc;
6769
6870
71+/* list of labels or gotos */
72+typedef struct Labellist {
73+ Labeldesc *arr; /* array */
74+ int n; /* number of entries in use */
75+ int size; /* array size */
76+} Labellist;
77+
78+
79+/* dynamic structures used by the parser */
80+typedef struct Dyndata {
81+ struct { /* list of active local variables */
82+ Vardesc *arr;
83+ int n;
84+ int size;
85+ } actvar;
86+ Labellist gt; /* list of pending gotos */
87+ Labellist label; /* list of active labels */
88+} Dyndata;
89+
90+
91+/* control of blocks */
6992 struct BlockCnt; /* defined in lparser.c */
7093
7194
@@ -91,7 +114,7 @@ typedef struct FuncState { · 19 unmodified lines
91114
92115
93116 LUAI_FUNC Proto *luaY_parser (lua_State *L, ZIO *z, Mbuffer *buff,
94− Varlist *varl, const char *name);
117+ Dyndata *dyd, const char *name, int firstchar);
95118
96119
97120 #endif
src/lstate.c+12−8
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.c,v 2.86 2010/09/03 14:14:01 roberto Exp $
2+** $Id: lstate.c,v 2.89 2010/12/20 19:40:07 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -65,11 +65,14 @@ typedef struct LG { · 59 unmodified lines
6565 #define fromstate(L) (cast(LX *, cast(lu_byte *, (L)) - offsetof(LX, l)))
6666
6767
68−
6968 /*
70−** maximum number of nested calls made by error-handling function
69+** set GCdebt to a new value keeping the value (totalbytes + GCdebt)
70+** invariant
7171 */
72−#define LUAI_EXTRACALLS 10
72+void luaE_setdebt (global_State *g, l_mem debt) {
73+ g->totalbytes -= (debt - g->GCdebt);
74+ g->GCdebt = debt;
75+}
7376
7477
7578 CallInfo *luaE_extendCI (lua_State *L) {
@@ -154,7 +157,7 @@ static void f_luaopen (lua_State *L, void *ud) { · 78 unmodified lines
154157 /* pre-create memory-error message */
155158 g->memerrmsg = luaS_newliteral(L, MEMERRMSG);
156159 luaS_fix(g->memerrmsg); /* it should never be collected */
157− g->GCdebt = 0;
160+ g->gcrunning = 1; /* allow gc */
158161 }
159162
160163
@@ -187,7 +190,7 @@ static void close_state (lua_State *L) { · 26 unmodified lines
187190 luaM_freearray(L, G(L)->strt.hash, G(L)->strt.size);
188191 luaZ_freebuffer(L, &g->buff);
189192 freestack(L);
190− lua_assert(g->totalbytes == sizeof(LG));
193+ lua_assert(gettotalbytes(g) == sizeof(LG));
191194 (*g->frealloc)(g->ud, fromstate(L), sizeof(LG), 0);
192195 }
193196
@@ -241,7 +244,7 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 47 unmodified lines
241244 g->mainthread = L;
242245 g->uvhead.u.l.prev = &g->uvhead;
243246 g->uvhead.u.l.next = &g->uvhead;
244− stopgc(g); /* no GC while building state */
247+ g->gcrunning = 0; /* no GC while building state */
245248 g->lastmajormem = 0;
246249 g->strt.size = 0;
247250 g->strt.nuse = 0;
@@ -252,11 +255,12 @@ LUA_API lua_State *lua_newstate (lua_Alloc f, void *ud) { · 4 unmodified lines
252255 g->version = lua_version(NULL);
253256 g->gcstate = GCSpause;
254257 g->allgc = NULL;
255− g->udgc = NULL;
258+ g->finobj = NULL;
256259 g->tobefnz = NULL;
257260 g->gray = g->grayagain = NULL;
258261 g->weak = g->ephemeron = g->allweak = NULL;
259262 g->totalbytes = sizeof(LG);
263+ g->GCdebt = 0;
260264 g->gcpause = LUAI_GCPAUSE;
261265 g->gcmajorinc = LUAI_GCMAJOR;
262266 g->gcstepmul = LUAI_GCMUL;
src/lstate.h+11−7
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstate.h,v 2.68 2010/10/29 17:52:46 roberto Exp $
2+** $Id: lstate.h,v 2.72 2011/06/02 19:31:40 roberto Exp $
33 ** Global State
44 ** See Copyright Notice in lua.h
55 */
@@ -32,9 +32,9 @@ · 26 unmodified lines
3232 ** when traversing the respective threads, but the thread may already be
3333 ** dead, while the upvalue is still accessible through closures.)
3434 **
35−** Userdata with finalizers are kept in the list g->udgc.
35+** Objects with finalizers are kept in the list g->finobj.
3636 **
37−** The list g->tobefnz links all userdata being finalized.
37+** The list g->tobefnz links all objects being finalized.
3838
3939 */
4040
@@ -104,7 +104,6 @@ typedef struct CallInfo { · 63 unmodified lines
104104 #define CIST_TAIL (1<<6) /* call was tail called */
105105
106106
107−#define ci_func(ci) (clvalue((ci)->func))
108107 #define isLua(ci) ((ci)->callstatus & CIST_LUA)
109108
110109
@@ -114,8 +113,8 @@ typedef struct CallInfo { · 3 unmodified lines
114113 typedef struct global_State {
115114 lua_Alloc frealloc; /* function to reallocate memory */
116115 void *ud; /* auxiliary data to `frealloc' */
117− lu_mem totalbytes; /* number of bytes currently allocated */
118− l_mem GCdebt; /* when positive, run a GC step */
116+ lu_mem totalbytes; /* number of bytes currently allocated - GCdebt */
117+ l_mem GCdebt; /* bytes allocated not yet compensated by the collector */
119118 lu_mem lastmajormem; /* memory in use after last major collection */
120119 stringtable strt; /* hash table for strings */
121120 TValue l_registry;
@@ -123,9 +122,10 @@ typedef struct global_State { · 1 unmodified line
123122 lu_byte currentwhite;
124123 lu_byte gcstate; /* state of garbage collector */
125124 lu_byte gckind; /* kind of GC running */
125+ lu_byte gcrunning; /* true if GC is running */
126126 int sweepstrgc; /* position of sweep in `strt' */
127127 GCObject *allgc; /* list of all collectable objects */
128− GCObject *udgc; /* list of collectable userdata with finalizers */
128+ GCObject *finobj; /* list of collectable objects with finalizers */
129129 GCObject **sweepgc; /* current position of sweep */
130130 GCObject *gray; /* list of gray objects */
131131 GCObject *grayagain; /* list of objects to be traversed atomically */
@@ -209,6 +209,10 @@ union GCObject { · 77 unmodified lines
209209 #define obj2gco(v) (cast(GCObject *, (v)))
210210
211211
212+/* actual number of total bytes allocated */
213+#define gettotalbytes(g) ((g)->totalbytes + (g)->GCdebt)
214+
215+LUAI_FUNC void luaE_setdebt (global_State *g, l_mem debt);
212216 LUAI_FUNC void luaE_freethread (lua_State *L, lua_State *L1);
213217 LUAI_FUNC CallInfo *luaE_extendCI (lua_State *L);
214218 LUAI_FUNC void luaE_freeCI (lua_State *L);
src/lstring.c+4−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstring.c,v 2.18 2010/05/10 18:23:45 roberto Exp $
2+** $Id: lstring.c,v 2.19 2011/05/03 16:01:57 roberto Exp $
33 ** String table (keeps all strings handled by Lua)
44 ** See Copyright Notice in lua.h
55 */
@@ -84,8 +84,9 @@ TString *luaS_newlstr (lua_State *L, const char *str, size_t l) { · 78 unmodified lines
8484 o != NULL;
8585 o = gch(o)->next) {
8686 TString *ts = rawgco2ts(o);
87− if (h == ts->tsv.hash && ts->tsv.len == l &&
88− (memcmp(str, getstr(ts), l) == 0)) {
87+ if (h == ts->tsv.hash &&
88+ ts->tsv.len == l &&
89+ (memcmp(str, getstr(ts), l * sizeof(char)) == 0)) {
8990 if (isdead(G(L), o)) /* string is dead (but was not collected yet)? */
9091 changewhite(o); /* resurrect it */
9192 return ts;
src/lstrlib.c+51−22
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lstrlib.c,v 1.159 2010/11/19 16:25:51 roberto Exp $
2+** $Id: lstrlib.c,v 1.168 2011/06/09 18:22:47 roberto Exp $
33 ** Standard library for string operations and pattern-matching
44 ** See Copyright Notice in lua.h
55 */
@@ -45,7 +45,7 @@ static int str_len (lua_State *L) { · 39 unmodified lines
4545 /* translate a relative string position: negative means back from end */
4646 static size_t posrelat (ptrdiff_t pos, size_t len) {
4747 if (pos >= 0) return (size_t)pos;
48− else if (pos == -pos || (size_t)-pos > len) return 0;
48+ else if (-(size_t)pos > len) return 0;
4949 else return len - ((size_t)-pos) + 1;
5050 }
5151
@@ -101,15 +101,29 @@ static int str_upper (lua_State *L) { · 49 unmodified lines
101101 return 1;
102102 }
103103
104+
105+/* reasonable limit to avoid arithmetic overflow */
106+#define MAXSIZE ((~(size_t)0) >> 1)
107+
104108 static int str_rep (lua_State *L) {
105− size_t l;
106− luaL_Buffer b;
109+ size_t l, lsep;
107110 const char *s = luaL_checklstring(L, 1, &l);
108111 int n = luaL_checkint(L, 2);
109− luaL_buffinit(L, &b);
110− while (n-- > 0)
111− luaL_addlstring(&b, s, l);
112− luaL_pushresult(&b);
112+ const char *sep = luaL_optlstring(L, 3, "", &lsep);
113+ if (n <= 0) lua_pushliteral(L, "");
114+ else if (l + lsep < l || l + lsep >= MAXSIZE / n) /* may overflow? */
115+ return luaL_error(L, "resulting string too large");
116+ else {
117+ size_t totallen = n * l + (n - 1) * lsep;
118+ luaL_Buffer b;
119+ char *p = luaL_buffinitsize(L, &b, totallen);
120+ while (n-- > 1) { /* first n-1 copies (followed by separator) */
121+ memcpy(p, s, l * sizeof(char)); p += l;
122+ memcpy(p, sep, lsep * sizeof(char)); p += lsep;
123+ }
124+ memcpy(p, s, l * sizeof(char)); /* last copy (not followed by separator) */
125+ luaL_pushresultsize(&b, totallen);
126+ }
113127 return 1;
114128 }
115129
@@ -125,7 +139,7 @@ static int str_byte (lua_State *L) { · 9 unmodified lines
125139 if (posi > pose) return 0; /* empty interval; return no values */
126140 n = (int)(pose - posi + 1);
127141 if (posi + n <= pose) /* overflow? */
128− luaL_error(L, "string slice too long");
142+ return luaL_error(L, "string slice too long");
129143 luaL_checkstack(L, n, "string slice too long");
130144 for (i=0; i<n; i++)
131145 lua_pushinteger(L, uchar(s[posi+i-1]));
@@ -161,7 +175,7 @@ static int str_dump (lua_State *L) { · 29 unmodified lines
161175 lua_settop(L, 1);
162176 luaL_buffinit(L,&b);
163177 if (lua_dump(L, writer, &b) != 0)
164− luaL_error(L, "unable to dump given function");
178+ return luaL_error(L, "unable to dump given function");
165179 luaL_pushresult(&b);
166180 return 1;
167181 }
@@ -510,7 +524,7 @@ static int nospecials (const char *p, size_t l) { · 342 unmodified lines
510524 size_t upto = 0;
511525 do {
512526 if (strpbrk(p + upto, SPECIALS))
513− return 0; /* pattern has a special character */
527+ return 0; /* pattern has a special character */
514528 upto += strlen(p + upto) + 1; /* may have more after \0 */
515529 } while (upto <= l);
516530 return 1; /* no special chars found */
@@ -729,7 +743,7 @@ static int str_gsub (lua_State *L) { · 212 unmodified lines
729743 ** the previous length
730744 */
731745 #if !defined(LUA_INTFRMLEN) /* { */
732−#if defined(LUA_USELONGLONG)
746+#if defined(LUA_USE_LONGLONG)
733747
734748 #define LUA_INTFRMLEN "ll"
735749 #define LUA_INTFRM_T long long
@@ -794,7 +808,7 @@ static void addquoted (lua_State *L, luaL_Buffer *b, int arg) { · 58 unmodified lines
794808 static const char *scanformat (lua_State *L, const char *strfrmt, char *form) {
795809 const char *p = strfrmt;
796810 while (*p != '\0' && strchr(FLAGS, *p) != NULL) p++; /* skip flags */
797− if ((size_t)(p - strfrmt) >= sizeof(FLAGS))
811+ if ((size_t)(p - strfrmt) >= sizeof(FLAGS)/sizeof(char))
798812 luaL_error(L, "invalid format (repeated flags)");
799813 if (isdigit(uchar(*p))) p++; /* skip width */
800814 if (isdigit(uchar(*p))) p++; /* (2 digits at most) */
@@ -806,7 +820,7 @@ static const char *scanformat (lua_State *L, const char *strfrmt, char *form) { · 5 unmodified lines
806820 if (isdigit(uchar(*p)))
807821 luaL_error(L, "invalid format (width or precision too long)");
808822 *(form++) = '%';
809− memcpy(form, strfrmt, p - strfrmt + 1);
823+ memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char));
810824 form += p - strfrmt + 1;
811825 *form = '\0';
812826 return p;
@@ -861,6 +875,9 @@ static int str_format (lua_State *L) { · 48 unmodified lines
861875 break;
862876 }
863877 case 'e': case 'E': case 'f':
878+#if defined(LUA_USE_AFORMAT)
879+ case 'a': case 'A':
880+#endif
864881 case 'g': case 'G': {
865882 addlenmod(form, LUA_FLTFRMLEN);
866883 nb = sprintf(buff, form, (LUA_FLTFRM_T)luaL_checknumber(L, arg));
@@ -872,16 +889,16 @@ static int str_format (lua_State *L) { · 5 unmodified lines
872889 }
873890 case 's': {
874891 size_t l;
875− const char *s = luaL_checklstring(L, arg, &l);
892+ const char *s = luaL_tolstring(L, arg, &l);
876893 if (!strchr(form, '.') && l >= 100) {
877894 /* no precision and string is too long to be formatted;
878895 keep original string */
879− lua_pushvalue(L, arg);
880896 luaL_addvalue(&b);
881897 break;
882898 }
883899 else {
884900 nb = sprintf(buff, form, s);
901+ lua_pop(L, 1); /* remove result from 'luaL_tolstring' */
885902 break;
886903 }
887904 }
@@ -900,6 +917,15 @@ static int str_format (lua_State *L) { · 12 unmodified lines
900917 /* }====================================================== */
901918
902919
920+static int noindex (lua_State *L) {
921+ const char *f = lua_tostring(L, 2);
922+ if (f)
923+ return luaL_error(L, "no field '%s' in strings", f);
924+ else
925+ return luaL_error(L, "no such field in strings");
926+}
927+
928+
903929 static const luaL_Reg strlib[] = {
904930 {"byte", str_byte},
905931 {"char", str_char},
@@ -915,19 +941,22 @@ static const luaL_Reg strlib[] = { · 9 unmodified lines
915941 {"reverse", str_reverse},
916942 {"sub", str_sub},
917943 {"upper", str_upper},
944+ {"__index", noindex},
918945 {NULL, NULL}
919946 };
920947
921948
922949 static void createmetatable (lua_State *L) {
923− lua_createtable(L, 0, 1); /* create metatable for strings */
950+ /* setmetatable("", {__index = string}) */
924951 lua_pushliteral(L, ""); /* dummy string */
925− lua_pushvalue(L, -2);
926− lua_setmetatable(L, -2); /* set string metatable */
952+ lua_createtable(L, 0, 1); /* create metatable for strings */
953+ lua_pushvalue(L, -3); /* set the string library... */
954+ lua_setfield(L, -2, "__index"); /* ...as the __index metamethod */
955+ lua_setmetatable(L, -2); /* set metatable for strings */
927956 lua_pop(L, 1); /* pop dummy string */
928− lua_pushvalue(L, -2); /* string library... */
929− lua_setfield(L, -2, "__index"); /* ...is the __index metamethod */
930− lua_pop(L, 1); /* pop metatable */
957+ /* setmetatable(string, string) */
958+ lua_pushvalue(L, -1); /* push string library */
959+ lua_setmetatable(L, -2); /* set it as its own metatable */
931960 }
932961
933962
src/ltable.c+10−8
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltable.c,v 2.53 2010/11/11 15:38:43 roberto Exp $
2+** $Id: ltable.c,v 2.59 2011/06/09 18:23:27 roberto Exp $
33 ** Lua tables (hash)
44 ** See Copyright Notice in lua.h
55 */
@@ -33,6 +33,7 @@ · 27 unmodified lines
3333 #include "lstate.h"
3434 #include "lstring.h"
3535 #include "ltable.h"
36+#include "lvm.h"
3637
3738
3839 /*
@@ -87,8 +88,9 @@ static Node *hashnum (const Table *t, lua_Number n) { · 48 unmodified lines
8788 int i;
8889 luai_hashnum(i, n);
8990 if (i < 0) {
90− i = -i; /* must be a positive value */
91− if (i < 0) i = 0; /* handle INT_MIN */
91+ if ((unsigned int)i == -(unsigned int)i)
92+ i = 0; /* handle INT_MIN */
93+ i = -i; /* must be a positive value */
9294 }
9395 return hashmod(t, i);
9496 }
@@ -148,8 +150,8 @@ static int findindex (lua_State *L, Table *t, StkId key) { · 53 unmodified lines
148150 Node *n = mainposition(t, key);
149151 do { /* check whether `key' is somewhere in the chain */
150152 /* key may be dead already, but it is ok to use it in `next' */
151− if (luaO_rawequalObj(gkey(n), key) ||
152− (ttype(gkey(n)) == LUA_TDEADKEY && iscollectable(key) &&
153+ if (luaV_rawequalobj(gkey(n), key) ||
154+ (ttisdeadkey(gkey(n)) && iscollectable(key) &&
153155 gcvalue(gkey(n)) == gcvalue(key))) {
154156 i = cast_int(n - gnode(t, 0)); /* key index in hash table */
155157 /* hash elements are numbered after array ones */
@@ -336,7 +338,7 @@ void luaH_resizearray (lua_State *L, Table *t, int nasize) { · 180 unmodified lines
336338
337339 static void rehash (lua_State *L, Table *t, const TValue *ek) {
338340 int nasize, na;
339− int nums[MAXBITS+1]; /* nums[i] = number of keys between 2^(i-1) and 2^i */
341+ int nums[MAXBITS+1]; /* nums[i] = number of keys with 2^(i-1) < k <= 2^i */
340342 int i;
341343 int totaluse;
342344 for (i=0; i<=MAXBITS; i++) nums[i] = 0; /* reset counts */
@@ -467,7 +469,7 @@ const TValue *luaH_getstr (Table *t, TString *key) { · 124 unmodified lines
467469 ** main search function
468470 */
469471 const TValue *luaH_get (Table *t, const TValue *key) {
470− switch (ttype(key)) {
472+ switch (ttypenv(key)) {
471473 case LUA_TNIL: return luaO_nilobject;
472474 case LUA_TSTRING: return luaH_getstr(t, rawtsvalue(key));
473475 case LUA_TNUMBER: {
@@ -481,7 +483,7 @@ const TValue *luaH_get (Table *t, const TValue *key) { · 7 unmodified lines
481483 default: {
482484 Node *n = mainposition(t, key);
483485 do { /* check whether `key' is somewhere in the chain */
484− if (luaO_rawequalObj(gkey(n), key))
486+ if (luaV_rawequalobj(gkey(n), key))
485487 return gval(n); /* that's it */
486488 else n = gnext(n);
487489 } while (n);
src/ltablib.c+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltablib.c,v 1.58 2010/11/23 17:21:14 roberto Exp $
2+** $Id: ltablib.c,v 1.59 2010/12/17 12:15:34 roberto Exp $
33 ** Library for Table Manipulation
44 ** See Copyright Notice in lua.h
55 */
@@ -17,7 +17,7 @@ · 11 unmodified lines
1717
1818
1919 #define aux_getn(L,n) \
20− (luaL_checktype(L, n, LUA_TTABLE), (int)lua_rawlen(L, n))
20+ (luaL_checktype(L, n, LUA_TTABLE), luaL_len(L, n))
2121
2222
2323 static int deprecatedfunc (lua_State *L) {
@@ -104,7 +104,7 @@ static int tconcat (lua_State *L) { · 80 unmodified lines
104104 const char *sep = luaL_optlstring(L, 2, "", &lsep);
105105 luaL_checktype(L, 1, LUA_TTABLE);
106106 i = luaL_optint(L, 3, 1);
107− last = luaL_opt(L, luaL_checkint, 4, (int)lua_rawlen(L, 1));
107+ last = luaL_opt(L, luaL_checkint, 4, luaL_len(L, 1));
108108 luaL_buffinit(L, &b);
109109 for (; i < last; i++) {
110110 addfield(L, &b, i);
@@ -143,7 +143,7 @@ static int unpack (lua_State *L) { · 32 unmodified lines
143143 int i, e, n;
144144 luaL_checktype(L, 1, LUA_TTABLE);
145145 i = luaL_optint(L, 2, 1);
146− e = luaL_opt(L, luaL_checkint, 3, (int)lua_rawlen(L, 1));
146+ e = luaL_opt(L, luaL_checkint, 3, luaL_len(L, 1));
147147 if (i > e) return 0; /* empty range */
148148 n = e - i + 1; /* number of elements */
149149 if (n <= 0 || !lua_checkstack(L, n)) /* n <= 0 means arith. overflow */
src/ltm.c+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.c,v 2.12 2010/04/13 20:48:12 roberto Exp $
2+** $Id: ltm.c,v 2.14 2011/06/02 19:31:40 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -21,11 +21,11 @@ · 15 unmodified lines
2121
2222 static const char udatatypename[] = "userdata";
2323
24−LUAI_DDEF const char *const luaT_typenames_[] = {
24+LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
2525 "no value",
2626 "nil", "boolean", udatatypename, "number",
2727 "string", "table", "function", udatatypename, "thread",
28− "proto", "upval"
28+ "proto", "upval" /* these last two cases are used for tests only */
2929 };
3030
3131
@@ -62,7 +62,7 @@ const TValue *luaT_gettm (Table *events, TMS event, TString *ename) { · 30 unmodified lines
6262
6363 const TValue *luaT_gettmbyobj (lua_State *L, const TValue *o, TMS event) {
6464 Table *mt;
65− switch (ttype(o)) {
65+ switch (ttypenv(o)) {
6666 case LUA_TTABLE:
6767 mt = hvalue(o)->metatable;
6868 break;
src/ltm.h+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: ltm.h,v 2.10 2010/04/13 20:48:12 roberto Exp $
2+** $Id: ltm.h,v 2.11 2011/02/28 17:32:10 roberto Exp $
33 ** Tag methods
44 ** See Copyright Notice in lua.h
55 */
@@ -46,7 +46,7 @@ typedef enum { · 40 unmodified lines
4646 #define ttypename(x) luaT_typenames_[(x) + 1]
4747 #define objtypename(x) ttypename(ttypenv(x))
4848
49−LUAI_DDEC const char *const luaT_typenames_[];
49+LUAI_DDEC const char *const luaT_typenames_[LUA_TOTALTAGS];
5050
5151
5252 LUAI_FUNC const TValue *luaT_gettm (Table *events, TMS event, TString *ename);
src/lua.c+10−11
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.c,v 1.194 2010/10/25 19:01:37 roberto Exp $
2+** $Id: lua.c,v 1.199 2011/05/26 16:09:40 roberto Exp $
33 ** Lua stand-alone interpreter
44 ** See Copyright Notice in lua.h
55 */
@@ -106,13 +106,11 @@ static void laction (int i) { · 100 unmodified lines
106106
107107
108108 static void print_usage (const char *badoption) {
109− if (badoption[1] == 'e' || badoption[1] == 'l') {
110− luai_writestringerror("%s: ", progname);
109+ luai_writestringerror("%s: ", progname);
110+ if (badoption[1] == 'e' || badoption[1] == 'l')
111111 luai_writestringerror("'%s' needs argument\n", badoption);
112− } else {
113− luai_writestringerror("%s: ", progname);
112+ else
114113 luai_writestringerror("unrecognized option '%s'\n", badoption);
115− }
116114 luai_writestringerror(
117115 "usage: %s [options] [script [args]]\n"
118116 "Available options are:\n"
@@ -185,7 +183,8 @@ static int docall (lua_State *L, int narg, int nres) { · 66 unmodified lines
185183
186184
187185 static void print_version (void) {
188− printf("%s\n", LUA_COPYRIGHT);
186+ luai_writestring(LUA_COPYRIGHT, sizeof(LUA_COPYRIGHT));
187+ luai_writeline();
189188 }
190189
191190
@@ -247,14 +246,14 @@ static const char *get_prompt (lua_State *L, int firstline) { · 55 unmodified lines
247246 }
248247
249248 /* mark in error messages for incomplete statements */
250−#define mark "<eof>"
251−#define marklen (sizeof(mark) - 1)
249+#define EOFMARK "<eof>"
250+#define marklen (sizeof(EOFMARK)/sizeof(char) - 1)
252251
253252 static int incomplete (lua_State *L, int status) {
254253 if (status == LUA_ERRSYNTAX) {
255254 size_t lmsg;
256255 const char *msg = lua_tolstring(L, -1, &lmsg);
257− if (lmsg >= marklen && strcmp(msg + lmsg - marklen, mark) == 0) {
256+ if (lmsg >= marklen && strcmp(msg + lmsg - marklen, EOFMARK) == 0) {
258257 lua_pop(L, 1);
259258 return 1;
260259 }
@@ -322,7 +321,7 @@ static void dotty (lua_State *L) { · 61 unmodified lines
322321 }
323322 }
324323 lua_settop(L, 0); /* clear stack */
325− luai_writestring("\n", 1);
324+ luai_writeline();
326325 progname = oldprogname;
327326 }
328327
src/lua.h+4−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lua.h,v 1.276 2010/10/26 19:32:19 roberto Exp $
2+** $Id: lua.h,v 1.277 2011/04/18 14:15:48 roberto Exp $
33 ** Lua - A Scripting Language
44 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
55 ** See Copyright Notice at the end of this file
@@ -18,12 +18,12 @@ · 12 unmodified lines
1818
1919 #define LUA_VERSION_MAJOR "5"
2020 #define LUA_VERSION_MINOR "2"
21−#define LUA_VERSION_RELEASE "0" " (alpha)"
21+#define LUA_VERSION_RELEASE "0" " (beta)"
2222
2323 #define LUA_VERSION "Lua " LUA_VERSION_MAJOR "." LUA_VERSION_MINOR
2424 #define LUA_RELEASE LUA_VERSION "." LUA_VERSION_RELEASE
2525 #define LUA_VERSION_NUM 502
26−#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2010 Lua.org, PUC-Rio"
26+#define LUA_COPYRIGHT LUA_RELEASE " Copyright (C) 1994-2011 Lua.org, PUC-Rio"
2727 #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo, W. Celes"
2828
2929
@@ -415,7 +415,7 @@ struct lua_Debug { · 385 unmodified lines
415415
416416
417417 /******************************************************************************
418−* Copyright (C) 1994-2010 Lua.org, PUC-Rio. All rights reserved.
418+* Copyright (C) 1994-2011 Lua.org, PUC-Rio. All rights reserved.
419419 *
420420 * Permission is hereby granted, free of charge, to any person obtaining
421421 * a copy of this software and associated documentation files (the
src/luac.c+17−12
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luac.c,v 1.65 2010/10/26 09:07:52 lhf Exp $
2+** $Id: luac.c,v 1.66 2011/05/10 01:08:57 lhf Exp $
33 ** Lua compiler (saves bytecodes to files; also list bytecodes)
44 ** See Copyright Notice in lua.h
55 */
@@ -143,9 +143,12 @@ static const Proto* combine(lua_State* L, int n) · 137 unmodified lines
143143 int i=n;
144144 if (lua_load(L,reader,&i,"=(" PROGNAME ")")!=LUA_OK) fatal(lua_tostring(L,-1));
145145 f=toproto(L,-1);
146− for (i=0; i<n; i++) f->p[i]=toproto(L,i-n-1);
146+ for (i=0; i<n; i++)
147+ {
148+ f->p[i]=toproto(L,i-n-1);
149+ if (f->p[i]->sizeupvalues>0) f->p[i]->upvalues[0].instack=0;
150+ }
147151 f->sizelineinfo=0;
148− f->sizeupvalues=0;
149152 return f;
150153 }
151154 }
@@ -200,7 +203,7 @@ int main(int argc, char* argv[]) · 48 unmodified lines
200203 }
201204
202205 /*
203−** $Id: print.c,v 1.66 2010/10/26 09:07:52 lhf Exp $
206+** $Id: print.c,v 1.67 2011/05/06 13:37:15 lhf Exp $
204207 ** print bytecodes
205208 ** See Copyright Notice in lua.h
206209 */
@@ -270,6 +273,7 @@ static void PrintConstant(const Proto* f, int i) · 63 unmodified lines
270273 }
271274
272275 #define UPVALNAME(x) ((f->upvalues[x].name) ? getstr(f->upvalues[x].name) : "-")
276+#define MYK(x) (-1-(x))
273277
274278 static void PrintCode(const Proto* f)
275279 {
@@ -293,23 +297,25 @@ static void PrintCode(const Proto* f) · 17 unmodified lines
293297 {
294298 case iABC:
295299 printf("%d",a);
296− if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (-1-INDEXK(b)) : b);
297− if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (-1-INDEXK(c)) : c);
300+ if (getBMode(o)!=OpArgN) printf(" %d",ISK(b) ? (MYK(INDEXK(b))) : b);
301+ if (getCMode(o)!=OpArgN) printf(" %d",ISK(c) ? (MYK(INDEXK(c))) : c);
298302 break;
299303 case iABx:
300− if (getBMode(o)==OpArgK) printf("%d %d",a,-bx); else printf("%d %d",a,bx);
304+ printf("%d",a);
305+ if (getBMode(o)==OpArgK) printf(" %d",MYK(bx));
306+ if (getBMode(o)==OpArgU) printf(" %d",bx);
301307 break;
302308 case iAsBx:
303− if (o==OP_JMP) printf("%d",sbx); else printf("%d %d",a,sbx);
309+ printf("%d %d",a,sbx);
304310 break;
305311 case iAx:
306− printf("%d",-1-ax);
312+ printf("%d",MYK(ax));
307313 break;
308314 }
309315 switch (o)
310316 {
311317 case OP_LOADK:
312− if (bx>0) { printf("\t; "); PrintConstant(f,bx-1); }
318+ printf("\t; "); PrintConstant(f,bx);
313319 break;
314320 case OP_GETUPVAL:
315321 case OP_SETUPVAL:
@@ -355,8 +361,7 @@ static void PrintCode(const Proto* f) · 39 unmodified lines
355361 printf("\t; %p",VOID(f->p[bx]));
356362 break;
357363 case OP_SETLIST:
358− if (c==0) printf("\t; %d",(int)code[++pc]);
359− else printf("\t; %d",c);
364+ if (c==0) printf("\t; %d",(int)code[++pc]); else printf("\t; %d",c);
360365 break;
361366 case OP_EXTRAARG:
362367 printf("\t; "); PrintConstant(f,ax);
src/luaconf.h+57−6
@@ -1,5 +1,5 @@
11 /*
2−** $Id: luaconf.h,v 1.151 2010/11/12 15:48:30 roberto Exp $
2+** $Id: luaconf.h,v 1.159 2011/06/13 14:13:06 roberto Exp $
33 ** Configuration file for Lua
44 ** See Copyright Notice in lua.h
55 */
@@ -35,6 +35,7 @@ · 29 unmodified lines
3535
3636 #if defined(LUA_WIN)
3737 #define LUA_DL_DLL
38+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
3839 #endif
3940
4041
@@ -43,12 +44,18 @@ · 2 unmodified lines
4344 #define LUA_USE_POSIX
4445 #define LUA_USE_DLOPEN /* needs an extra library: -ldl */
4546 #define LUA_USE_READLINE /* needs some extra libraries */
47+#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
48+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
49+#define LUA_USE_LONGLONG /* assume support for long long */
4650 #endif
4751
4852 #if defined(LUA_USE_MACOSX)
4953 #define LUA_USE_POSIX
5054 #define LUA_USE_DLOPEN /* does not need -ldl */
5155 #define LUA_USE_READLINE /* needs an extra library: -lreadline */
56+#define LUA_USE_STRTODHEX /* assume 'strtod' handles hexa formats */
57+#define LUA_USE_AFORMAT /* assume 'printf' handles 'aA' specifiers */
58+#define LUA_USE_LONGLONG /* assume support for long long */
5259 #endif
5360
5461
@@ -202,10 +209,11 @@ · 147 unmodified lines
202209
203210
204211 /*
205−@@ luai_writestring defines how 'print' prints its results.
212+@@ luai_writestring/luai_writeline define how 'print' prints its results.
206213 */
207214 #include <stdio.h>
208215 #define luai_writestring(s,l) fwrite((s), sizeof(char), (l), stdout)
216+#define luai_writeline() (luai_writestring("\n", 1), fflush(stdout))
209217
210218 /*
211219 @@ luai_writestringerror defines how to print error messages.
@@ -253,6 +261,12 @@ · 41 unmodified lines
253261 */
254262 #define LUA_COMPAT_LOG10
255263
264+/*
265+@@ LUA_COMPAT_LOADSTRING defines the function 'loadstring' in the base
266+** library. You can rewrite 'loadstring(s)' as 'load(s)'.
267+*/
268+#define LUA_COMPAT_LOADSTRING
269+
256270 /*
257271 @@ LUA_COMPAT_MAXN defines the function 'maxn' in the table library.
258272 */
@@ -371,14 +385,27 @@ · 112 unmodified lines
371385 @@ LUA_NUMBER_FMT is the format for writing numbers.
372386 @@ lua_number2str converts a number to a string.
373387 @@ LUAI_MAXNUMBER2STR is maximum size of previous conversion.
374−@@ lua_str2number converts a string to a number.
375388 */
376389 #define LUA_NUMBER_SCAN "%lf"
377390 #define LUA_NUMBER_FMT "%.14g"
378391 #define lua_number2str(s,n) sprintf((s), LUA_NUMBER_FMT, (n))
379392 #define LUAI_MAXNUMBER2STR 32 /* 16 digits, sign, point, and \0 */
393+
394+
395+/*
396+@@ lua_str2number converts a decimal numeric string to a number.
397+@@ lua_strx2number converts an hexadecimal numeric string to a number.
398+** In C99, 'strtod' do both conversions. C89, however, has no function
399+** to convert floating hexadecimal strings to numbers. For these
400+** systems, you can leave 'lua_strx2number' undefined and Lua will
401+** provide its own implementation.
402+*/
380403 #define lua_str2number(s,p) strtod((s), (p))
381404
405+#if defined(LUA_USE_STRTODHEX)
406+#define lua_strx2number(s,p) strtod((s), (p))
407+#endif
408+
382409
383410 /*
384411 @@ The luai_num* macros define the primitive operations over numbers.
@@ -438,11 +465,11 @@ · 53 unmodified lines
438465
439466 /*
440467 @@ LUA_IEEEENDIAN is the endianness of doubles in your machine
441−@@ (0 for little endian, 1 for big endian); if not defined, Lua will
442−@@ check it dynamically.
468+** (0 for little endian, 1 for big endian); if not defined, Lua will
469+** check it dynamically.
443470 */
444471 /* check for known architectures */
445−#if defined(__i386__) || defined(__i386) || defined(i386) || \
472+#if defined(__i386__) || defined(__i386) || defined(__X86__) || \
446473 defined (__x86_64)
447474 #define LUA_IEEEENDIAN 0
448475 #elif defined(__POWERPC__) || defined(__ppc__)
@@ -458,6 +485,30 @@ · 9 unmodified lines
458485 /* }================================================================== */
459486
460487
488+/*
489+@@ LUA_NANTRICKLE/LUA_NANTRICKBE controls the use of a trick to pack all
490+** types into a single double value, using NaN values to represent
491+** non-number values. The trick only works on 32-bit machines (ints and
492+** pointers are 32-bit values) with numbers represented as IEEE 754-2008
493+** doubles with conventional endianess (12345678 or 87654321), in CPUs
494+** that do not produce signaling NaN values (all NaNs are quiet).
495+*/
496+#if defined(LUA_CORE) /* { */
497+
498+#if defined(LUA_NUMBER_DOUBLE) && !defined(LUA_ANSI) /* { */
499+
500+/* little-endian architectures that satisfy those conditions */
501+#if defined(__i386__) || defined(__i386) || defined(__X86__)
502+
503+#define LUA_NANTRICKLE
504+
505+#endif
506+
507+#endif /* } */
508+
509+#endif /* } */
510+
511+
461512
462513
463514 /* =================================================================== */
src/lualib.h+2−2
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lualib.h,v 1.41 2010/10/25 14:32:36 roberto Exp $
2+** $Id: lualib.h,v 1.42 2011/05/25 14:12:28 roberto Exp $
33 ** Lua standard libraries
44 ** See Copyright Notice in lua.h
55 */
@@ -50,7 +50,7 @@ LUALIB_API void (luaL_openlibs) (lua_State *L); · 44 unmodified lines
5050
5151
5252
53−#ifndef lua_assert
53+#if !defined(lua_assert)
5454 #define lua_assert(x) ((void)0)
5555 #endif
5656
src/lundump.c+30−23
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.c,v 1.68 2010/10/26 00:23:46 lhf Exp $
2+** $Id: lundump.c,v 1.69 2011/05/06 13:35:17 lhf Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -38,6 +38,10 @@ static void error(LoadState* S, const char* why) · 32 unmodified lines
3838 #define LoadVar(S,x) LoadMem(S,&x,1,sizeof(x))
3939 #define LoadVector(S,b,n,size) LoadMem(S,b,n,size)
4040
41+#if !defined(luai_verifycode)
42+#define luai_verifycode(L,b,f) (f)
43+#endif
44+
4145 static void LoadBlock(LoadState* S, void* b, size_t size)
4246 {
4347 if (luaZ_read(S->Z,b,size)!=0) error(S,"corrupted");
@@ -54,6 +58,7 @@ static int LoadInt(LoadState* S) · 10 unmodified lines
5458 {
5559 int x;
5660 LoadVar(S,x);
61+ if (x<0) error(S,"corrupted");
5762 return x;
5863 }
5964
@@ -73,7 +78,7 @@ static TString* LoadString(LoadState* S) · 13 unmodified lines
7378 else
7479 {
7580 char* s=luaZ_openspace(S->L,S->b,size);
76− LoadBlock(S,s,size);
81+ LoadBlock(S,s,size*sizeof(char));
7782 return luaS_newlstr(S->L,s,size-1); /* remove trailing '\0' */
7883 }
7984 }
@@ -177,16 +182,17 @@ static Proto* LoadFunction(LoadState* S) · 97 unmodified lines
177182
178183 /* the code below must be consistent with the code in luaU_header */
179184 #define N0 LUAC_HEADERSIZE
180−#define N1 (sizeof(LUA_SIGNATURE)-1)
185+#define N1 (sizeof(LUA_SIGNATURE)-sizeof(char))
181186 #define N2 N1+2
182187 #define N3 N2+6
183188
184189 static void LoadHeader(LoadState* S)
185190 {
186− char h[LUAC_HEADERSIZE];
187− char s[LUAC_HEADERSIZE];
191+ lu_byte h[LUAC_HEADERSIZE];
192+ lu_byte s[LUAC_HEADERSIZE];
188193 luaU_header(h);
189− LoadBlock(S,s,LUAC_HEADERSIZE);
194+ memcpy(s,h,sizeof(char)); /* first char already read */
195+ LoadBlock(S,s+sizeof(char),LUAC_HEADERSIZE-sizeof(char));
190196 if (memcmp(h,s,N0)==0) return;
191197 if (memcmp(h,s,N1)!=0) error(S,"not a");
192198 if (memcmp(h,s,N2)!=0) error(S,"version mismatch in");
@@ -209,29 +215,30 @@ Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name) · 16 unmodified lines
209215 S.Z=Z;
210216 S.b=buff;
211217 LoadHeader(&S);
212− return LoadFunction(&S);
218+ return luai_verifycode(L,buff,LoadFunction(&S));
213219 }
214220
215−/* data to catch conversion errors */
216−#define TAIL "\x19\x93\r\n\x1a\n"
221+#define MYINT(s) (s[0]-'0')
222+#define VERSION MYINT(LUA_VERSION_MAJOR)*16+MYINT(LUA_VERSION_MINOR)
223+#define FORMAT 0 /* this is the official format */
217224
218225 /*
219226 * make header for precompiled chunks
220−* if you make any changes in the code below or in LUA_SIGNATURE in lua.h,
221−* be sure to update LoadHeader above and LUAC_HEADERSIZE in lundump.h
227+* if you change the code below be sure to update LoadHeader and FORMAT above
228+* and LUAC_HEADERSIZE in lundump.h
222229 */
223−void luaU_header (char* h)
230+void luaU_header (lu_byte* h)
224231 {
225232 int x=1;
226− memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-1);
227− h+=sizeof(LUA_SIGNATURE)-1;
228− *h++=(char)0x52; /* Lua 5.2 */
229− *h++=(char)0; /* the official format */
230− *h++=(char)*(char*)&x; /* endianness */
231− *h++=(char)sizeof(int);
232− *h++=(char)sizeof(size_t);
233− *h++=(char)sizeof(Instruction);
234− *h++=(char)sizeof(lua_Number);
235− *h++=(char)(((lua_Number)0.5)==0); /* is lua_Number integral? */
236− memcpy(h,TAIL,sizeof(TAIL)-1);
233+ memcpy(h,LUA_SIGNATURE,sizeof(LUA_SIGNATURE)-sizeof(char));
234+ h+=sizeof(LUA_SIGNATURE)-sizeof(char);
235+ *h++=cast_byte(VERSION);
236+ *h++=cast_byte(FORMAT);
237+ *h++=cast_byte(*(char*)&x); /* endianness */
238+ *h++=cast_byte(sizeof(int));
239+ *h++=cast_byte(sizeof(size_t));
240+ *h++=cast_byte(sizeof(Instruction));
241+ *h++=cast_byte(sizeof(lua_Number));
242+ *h++=cast_byte(((lua_Number)0.5)==0); /* is lua_Number integral? */
243+ memcpy(h,LUAC_TAIL,sizeof(LUAC_TAIL)-sizeof(char));
237244 }
src/lundump.h+7−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lundump.h,v 1.43 2010/10/26 00:23:46 lhf Exp $
2+** $Id: lundump.h,v 1.44 2011/05/06 13:35:17 lhf Exp $
33 ** load precompiled Lua chunks
44 ** See Copyright Notice in lua.h
55 */
@@ -14,12 +14,15 @@ · 8 unmodified lines
1414 LUAI_FUNC Proto* luaU_undump (lua_State* L, ZIO* Z, Mbuffer* buff, const char* name);
1515
1616 /* make header; from lundump.c */
17−LUAI_FUNC void luaU_header (char* h);
17+LUAI_FUNC void luaU_header (lu_byte* h);
1818
1919 /* dump one chunk; from ldump.c */
2020 LUAI_FUNC int luaU_dump (lua_State* L, const Proto* f, lua_Writer w, void* data, int strip);
2121
22−/* size of header of binary files */
23−#define LUAC_HEADERSIZE 18
22+/* data to catch conversion errors */
23+#define LUAC_TAIL "\x19\x93\r\n\x1a\n"
24+
25+/* size in bytes of header of binary files */
26+#define LUAC_HEADERSIZE (sizeof(LUA_SIGNATURE)-sizeof(char)+2+6+sizeof(LUAC_TAIL)-sizeof(char))
2427
2528 #endif
src/lvm.c+97−53
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.c,v 2.125 2010/10/29 17:52:46 roberto Exp $
2+** $Id: lvm.c,v 2.141 2011/06/09 18:24:22 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -35,7 +35,7 @@ · 29 unmodified lines
3535 const TValue *luaV_tonumber (const TValue *obj, TValue *n) {
3636 lua_Number num;
3737 if (ttisnumber(obj)) return obj;
38− if (ttisstring(obj) && luaO_str2d(svalue(obj), &num)) {
38+ if (ttisstring(obj) && luaO_str2d(svalue(obj), tsvalue(obj)->len, &num)) {
3939 setnvalue(n, num);
4040 return n;
4141 }
@@ -65,7 +65,7 @@ static void traceexec (lua_State *L) { · 23 unmodified lines
6565 luaD_hook(L, LUA_HOOKCOUNT, -1);
6666 }
6767 if (mask & LUA_MASKLINE) {
68− Proto *p = ci_func(ci)->l.p;
68+ Proto *p = ci_func(ci)->p;
6969 int npc = pcRel(ci->u.l.savedpc, p);
7070 int newline = getfuncline(p, npc);
7171 if (npc == 0 || /* call linehook when enter a new function, */
@@ -161,7 +161,6 @@ static int call_binTM (lua_State *L, const TValue *p1, const TValue *p2, · 89 unmodified lines
161161 if (ttisnil(tm))
162162 tm = luaT_gettmbyobj(L, p2, event); /* try second operand */
163163 if (ttisnil(tm)) return 0;
164− if (event == TM_UNM) p2 = luaO_nilobject;
165164 callTM(L, tm, p1, p2, res, 1);
166165 return 1;
167166 }
@@ -175,7 +174,7 @@ static const TValue *get_equalTM (lua_State *L, Table *mt1, Table *mt2, · 7 unmodified lines
175174 if (mt1 == mt2) return tm1; /* same metatables => same metamethods */
176175 tm2 = fasttm(L, mt2, event);
177176 if (tm2 == NULL) return NULL; /* no metamethod */
178− if (luaO_rawequalObj(tm1, tm2)) /* same metamethods? */
177+ if (luaV_rawequalobj(tm1, tm2)) /* same metamethods? */
179178 return tm1;
180179 return NULL;
181180 }
@@ -238,9 +237,12 @@ int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r) { · 56 unmodified lines
238237 }
239238
240239
241−int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) {
240+/*
241+** equality of Lua values. L == NULL means raw equality (no metamethods)
242+*/
243+int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2) {
242244 const TValue *tm;
243− lua_assert(ttype(t1) == ttype(t2));
245+ lua_assert(ttisequal(t1, t2));
244246 switch (ttype(t1)) {
245247 case LUA_TNIL: return 1;
246248 case LUA_TNUMBER: return luai_numeq(nvalue(t1), nvalue(t2));
@@ -250,15 +252,19 @@ int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2) { · 3 unmodified lines
250252 case LUA_TSTRING: return eqstr(rawtsvalue(t1), rawtsvalue(t2));
251253 case LUA_TUSERDATA: {
252254 if (uvalue(t1) == uvalue(t2)) return 1;
255+ else if (L == NULL) return 0;
253256 tm = get_equalTM(L, uvalue(t1)->metatable, uvalue(t2)->metatable, TM_EQ);
254257 break; /* will try TM */
255258 }
256259 case LUA_TTABLE: {
257260 if (hvalue(t1) == hvalue(t2)) return 1;
261+ else if (L == NULL) return 0;
258262 tm = get_equalTM(L, hvalue(t1)->metatable, hvalue(t2)->metatable, TM_EQ);
259263 break; /* will try TM */
260264 }
261− default: return gcvalue(t1) == gcvalue(t2);
265+ default:
266+ lua_assert(iscollectable(t1));
267+ return gcvalue(t1) == gcvalue(t2);
262268 }
263269 if (tm == NULL) return 0; /* no TM? */
264270 callTM(L, tm, t1, t2, L->top, 1); /* call TM */
@@ -286,18 +292,20 @@ void luaV_concat (lua_State *L, int total) { · 21 unmodified lines
286292 char *buffer;
287293 int i;
288294 /* collect total length */
289− for (n = 1; n < total && tostring(L, top-n-1); n++) {
290− size_t l = tsvalue(top-n-1)->len;
291− if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow");
295+ for (i = 1; i < total && tostring(L, top-i-1); i++) {
296+ size_t l = tsvalue(top-i-1)->len;
297+ if (l >= (MAX_SIZET/sizeof(char)) - tl)
298+ luaG_runerror(L, "string length overflow");
292299 tl += l;
293300 }
294301 buffer = luaZ_openspace(L, &G(L)->buff, tl);
295302 tl = 0;
296− for (i=n; i>0; i--) { /* concat all strings */
303+ n = i;
304+ do { /* concat all strings */
297305 size_t l = tsvalue(top-i)->len;
298− memcpy(buffer+tl, svalue(top-i), l);
306+ memcpy(buffer+tl, svalue(top-i), l * sizeof(char));
299307 tl += l;
300− }
308+ } while (--i > 0);
301309 setsvalue2s(L, top-n, luaS_newlstr(L, buffer, tl));
302310 }
303311 total -= n-1; /* got 'n' strings to create 1 new */
@@ -308,7 +316,7 @@ void luaV_concat (lua_State *L, int total) { · 4 unmodified lines
308316
309317 void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) {
310318 const TValue *tm;
311− switch (ttype(rb)) {
319+ switch (ttypenv(rb)) {
312320 case LUA_TTABLE: {
313321 Table *h = hvalue(rb);
314322 tm = fasttm(L, h->metatable, TM_LEN);
@@ -327,7 +335,7 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { · 12 unmodified lines
327335 break;
328336 }
329337 }
330− callTM(L, tm, rb, luaO_nilobject, ra, 1);
338+ callTM(L, tm, rb, rb, ra, 1);
331339 }
332340
333341
@@ -378,7 +386,7 @@ static void pushclosure (lua_State *L, Proto *p, UpVal **encup, StkId base, · 44 unmodified lines
378386 Upvaldesc *uv = p->upvalues;
379387 int i;
380388 Closure *ncl = luaF_newLclosure(L, p);
381− setclvalue(L, ra, ncl); /* anchor new closure in stack */
389+ setclLvalue(L, ra, ncl); /* anchor new closure in stack */
382390 for (i = 0; i < nup; i++) { /* fill in its upvalues */
383391 if (uv[i].instack) /* upvalue refers to local variable? */
384392 ncl->l.upvals[i] = luaF_findupval(L, base + uv[i].idx);
@@ -454,6 +462,11 @@ void luaV_finishOp (lua_State *L) { · 69 unmodified lines
454462 ** some macros for common tasks in `luaV_execute'
455463 */
456464
465+#if !defined luai_runtimecheck
466+#define luai_runtimecheck(L, c) /* void */
467+#endif
468+
469+
457470 #define RA(i) (base+GETARG_A(i))
458471 /* to be used after possible stack reallocation */
459472 #define RB(i) check_exp(getBMode(GET_OPCODE(i)) == OpArgR, base+GETARG_B(i))
@@ -466,12 +479,19 @@ void luaV_finishOp (lua_State *L) { · 6 unmodified lines
466479 (k + (GETARG_Bx(i) != 0 ? GETARG_Bx(i) - 1 : GETARG_Ax(*ci->u.l.savedpc++)))
467480
468481
469−#define dojump(i) (ci->u.l.savedpc += (i))
482+/* execute a jump instruction */
483+#define dojump(ci,i,e) \
484+ { int a = GETARG_A(i); \
485+ if (a > 0) luaF_close(L, ci->u.l.base + a - 1); \
486+ ci->u.l.savedpc += GETARG_sBx(i) + e; }
487+
488+/* for test instructions, execute the jump instruction that follows it */
489+#define donextjump(ci) { i = *ci->u.l.savedpc; dojump(ci, i, 1); }
470490
471491
472492 #define Protect(x) { {x;}; base = ci->u.l.base; }
473493
474−#define checkGC(L) Protect(luaC_checkGC(L); luai_threadyield(L);)
494+#define checkGC(L,c) Protect(luaC_condGC(L, c); luai_threadyield(L);)
475495
476496
477497 #define arith_op(op,tm) { \
@@ -493,9 +513,8 @@ void luaV_execute (lua_State *L) { · 15 unmodified lines
493513 TValue *k;
494514 StkId base;
495515 newframe: /* reentry point when frame changes (call/return) */
496− lua_assert(isLua(ci));
497516 lua_assert(ci == L->ci);
498− cl = &clvalue(ci->func)->l;
517+ cl = clLvalue(ci->func);
499518 k = cl->p->k;
500519 base = ci->u.l.base;
501520 /* main loop of interpreter */
@@ -515,7 +534,13 @@ void luaV_execute (lua_State *L) { · 13 unmodified lines
515534 setobjs2s(L, ra, RB(i));
516535 )
517536 vmcase(OP_LOADK,
518− TValue *rb = KBx(i);
537+ TValue *rb = k + GETARG_Bx(i);
538+ setobj2s(L, ra, rb);
539+ )
540+ vmcase(OP_LOADKX,
541+ TValue *rb;
542+ lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
543+ rb = k + GETARG_Ax(*ci->u.l.savedpc++);
519544 setobj2s(L, ra, rb);
520545 )
521546 vmcase(OP_LOADBOOL,
@@ -523,10 +548,10 @@ void luaV_execute (lua_State *L) { · 1 unmodified line
523548 if (GETARG_C(i)) ci->u.l.savedpc++; /* skip next instruction (if C) */
524549 )
525550 vmcase(OP_LOADNIL,
526− TValue *rb = RB(i);
551+ int b = GETARG_B(i);
527552 do {
528− setnilvalue(rb--);
529− } while (rb >= ra);
553+ setnilvalue(ra++);
554+ } while (b--);
530555 )
531556 vmcase(OP_GETUPVAL,
532557 int b = GETARG_B(i);
@@ -558,7 +583,11 @@ void luaV_execute (lua_State *L) { · 25 unmodified lines
558583 sethvalue(L, ra, t);
559584 if (b != 0 || c != 0)
560585 luaH_resize(L, t, luaO_fb2int(b), luaO_fb2int(c));
561− checkGC(L);
586+ checkGC(L,
587+ L->top = ra + 1; /* limit of live values */
588+ luaC_step(L);
589+ L->top = ci->top; /* restore top */
590+ )
562591 )
563592 vmcase(OP_SELF,
564593 StkId rb = RB(i);
@@ -594,7 +623,8 @@ void luaV_execute (lua_State *L) { · 29 unmodified lines
594623 }
595624 )
596625 vmcase(OP_NOT,
597− int res = l_isfalse(RB(i)); /* next assignment may change this value */
626+ TValue *rb = RB(i);
627+ int res = l_isfalse(rb); /* next assignment may change this value */
598628 setbvalue(ra, res);
599629 )
600630 vmcase(OP_LEN,
@@ -603,49 +633,61 @@ void luaV_execute (lua_State *L) { · 2 unmodified lines
603633 vmcase(OP_CONCAT,
604634 int b = GETARG_B(i);
605635 int c = GETARG_C(i);
636+ StkId rb;
606637 L->top = base + c + 1; /* mark the end of concat operands */
607− Protect(luaV_concat(L, c-b+1); checkGC(L);)
638+ Protect(luaV_concat(L, c - b + 1));
639+ ra = RA(i); /* 'luav_concat' may invoke TMs and move the stack */
640+ rb = b + base;
641+ setobjs2s(L, ra, rb);
642+ checkGC(L,
643+ L->top = (ra >= rb ? ra + 1 : rb); /* limit of live values */
644+ luaC_step(L);
645+ )
608646 L->top = ci->top; /* restore top */
609− setobjs2s(L, RA(i), base+b);
610647 )
611648 vmcase(OP_JMP,
612− dojump(GETARG_sBx(i));
649+ dojump(ci, i, 0);
613650 )
614651 vmcase(OP_EQ,
615652 TValue *rb = RKB(i);
616653 TValue *rc = RKC(i);
617654 Protect(
618− if (equalobj(L, rb, rc) == GETARG_A(i))
619− dojump(GETARG_sBx(*ci->u.l.savedpc));
655+ if (equalobj(L, rb, rc) != GETARG_A(i))
656+ ci->u.l.savedpc++;
657+ else
658+ donextjump(ci);
620659 )
621− ci->u.l.savedpc++;
622660 )
623661 vmcase(OP_LT,
624662 Protect(
625− if (luaV_lessthan(L, RKB(i), RKC(i)) == GETARG_A(i))
626− dojump(GETARG_sBx(*ci->u.l.savedpc));
663+ if (luaV_lessthan(L, RKB(i), RKC(i)) != GETARG_A(i))
664+ ci->u.l.savedpc++;
665+ else
666+ donextjump(ci);
627667 )
628− ci->u.l.savedpc++;
629668 )
630669 vmcase(OP_LE,
631670 Protect(
632− if (luaV_lessequal(L, RKB(i), RKC(i)) == GETARG_A(i))
633− dojump(GETARG_sBx(*ci->u.l.savedpc));
671+ if (luaV_lessequal(L, RKB(i), RKC(i)) != GETARG_A(i))
672+ ci->u.l.savedpc++;
673+ else
674+ donextjump(ci);
634675 )
635− ci->u.l.savedpc++;
636676 )
637677 vmcase(OP_TEST,
638− if (GETARG_C(i) ? !l_isfalse(ra) : l_isfalse(ra))
639− dojump(GETARG_sBx(*ci->u.l.savedpc));
640− ci->u.l.savedpc++;
678+ if (GETARG_C(i) ? l_isfalse(ra) : !l_isfalse(ra))
679+ ci->u.l.savedpc++;
680+ else
681+ donextjump(ci);
641682 )
642683 vmcase(OP_TESTSET,
643684 TValue *rb = RB(i);
644− if (GETARG_C(i) ? !l_isfalse(rb) : l_isfalse(rb)) {
685+ if (GETARG_C(i) ? l_isfalse(rb) : !l_isfalse(rb))
686+ ci->u.l.savedpc++;
687+ else {
645688 setobjs2s(L, ra, rb);
646− dojump(GETARG_sBx(*ci->u.l.savedpc));
689+ donextjump(ci);
647690 }
648− ci->u.l.savedpc++;
649691 )
650692 vmcase(OP_CALL,
651693 int b = GETARG_B(i);
@@ -711,7 +753,7 @@ void luaV_execute (lua_State *L) { · 59 unmodified lines
711753 lua_Number limit = nvalue(ra+1);
712754 if (luai_numlt(L, 0, step) ? luai_numle(L, idx, limit)
713755 : luai_numle(L, limit, idx)) {
714− dojump(GETARG_sBx(i)); /* jump back */
756+ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
715757 setnvalue(ra, idx); /* update internal index... */
716758 setnvalue(ra+3, idx); /* ...and external index */
717759 }
@@ -727,7 +769,7 @@ void luaV_execute (lua_State *L) { · 9 unmodified lines
727769 else if (!tonumber(pstep, ra+2))
728770 luaG_runerror(L, LUA_QL("for") " step must be a number");
729771 setnvalue(ra, luai_numsub(L, nvalue(ra), nvalue(pstep)));
730− dojump(GETARG_sBx(i));
772+ ci->u.l.savedpc += GETARG_sBx(i);
731773 )
732774 vmcase(OP_TFORCALL,
733775 StkId cb = ra + 3; /* call base */
@@ -746,7 +788,7 @@ void luaV_execute (lua_State *L) { · 12 unmodified lines
746788 l_tforloop:
747789 if (!ttisnil(ra + 1)) { /* continue loop? */
748790 setobjs2s(L, ra, ra + 1); /* save control variable */
749− dojump(GETARG_sBx(i)); /* jump back */
791+ ci->u.l.savedpc += GETARG_sBx(i); /* jump back */
750792 }
751793 )
752794 vmcase(OP_SETLIST,
@@ -759,6 +801,7 @@ void luaV_execute (lua_State *L) { · 6 unmodified lines
759801 lua_assert(GET_OPCODE(*ci->u.l.savedpc) == OP_EXTRAARG);
760802 c = GETARG_Ax(*ci->u.l.savedpc++);
761803 }
804+ luai_runtimecheck(L, ttistable(ra));
762805 h = hvalue(ra);
763806 last = ((c-1)*LFIELDS_PER_FLUSH) + n;
764807 if (last > h->sizearray) /* needs more space? */
@@ -770,17 +813,18 @@ void luaV_execute (lua_State *L) { · 5 unmodified lines
770813 }
771814 L->top = ci->top; /* correct top (in case of previous open call) */
772815 )
773− vmcase(OP_CLOSE,
774− luaF_close(L, ra);
775− )
776816 vmcase(OP_CLOSURE,
777817 Proto *p = cl->p->p[GETARG_Bx(i)];
778818 Closure *ncl = getcached(p, cl->upvals, base); /* cached closure */
779819 if (ncl == NULL) /* no match? */
780820 pushclosure(L, p, cl->upvals, base, ra); /* create a new one */
781821 else
782− setclvalue(L, ra, ncl); /* push cashed closure */
783− checkGC(L);
822+ setclLvalue(L, ra, ncl); /* push cashed closure */
823+ checkGC(L,
824+ L->top = ra + 1; /* limit of live values */
825+ luaC_step(L);
826+ L->top = ci->top; /* restore top */
827+ )
784828 )
785829 vmcase(OP_VARARG,
786830 int b = GETARG_B(i) - 1;
src/lvm.h+7−4
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lvm.h,v 2.14 2009/12/17 16:20:01 roberto Exp $
2+** $Id: lvm.h,v 2.17 2011/05/31 18:27:56 roberto Exp $
33 ** Lua virtual machine
44 ** See Copyright Notice in lua.h
55 */
@@ -17,12 +17,15 @@ · 11 unmodified lines
1717
1818 #define tonumber(o,n) (ttisnumber(o) || (((o) = luaV_tonumber(o,n)) != NULL))
1919
20−#define equalobj(L,o1,o2) \
21− (ttype(o1) == ttype(o2) && luaV_equalval_(L, o1, o2))
20+#define equalobj(L,o1,o2) (ttisequal(o1, o2) && luaV_equalobj_(L, o1, o2))
21+
22+#define luaV_rawequalobj(t1,t2) \
23+ (ttisequal(t1,t2) && luaV_equalobj_(NULL,t1,t2))
2224
2325
2426 /* not to called directly */
25−LUAI_FUNC int luaV_equalval_ (lua_State *L, const TValue *t1, const TValue *t2);
27+LUAI_FUNC int luaV_equalobj_ (lua_State *L, const TValue *t1, const TValue *t2);
28+
2629
2730 LUAI_FUNC int luaV_lessthan (lua_State *L, const TValue *l, const TValue *r);
2831 LUAI_FUNC int luaV_lessequal (lua_State *L, const TValue *l, const TValue *r);
src/lzio.c+12−18
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lzio.c,v 1.31 2005/06/03 20:15:29 roberto Exp $
2+** $Id: lzio.c,v 1.33 2011/02/23 13:13:10 roberto Exp $
33 ** a generic input stream interface
44 ** See Copyright Notice in lua.h
55 */
@@ -25,26 +25,14 @@ int luaZ_fill (ZIO *z) { · 19 unmodified lines
2525 lua_unlock(L);
2626 buff = z->reader(L, z->data, &size);
2727 lua_lock(L);
28− if (buff == NULL || size == 0) return EOZ;
29− z->n = size - 1;
28+ if (buff == NULL || size == 0)
29+ return EOZ;
30+ z->n = size - 1; /* discount char being returned */
3031 z->p = buff;
3132 return char2int(*(z->p++));
3233 }
3334
3435
35−int luaZ_lookahead (ZIO *z) {
36− if (z->n == 0) {
37− if (luaZ_fill(z) == EOZ)
38− return EOZ;
39− else {
40− z->n++; /* luaZ_fill removed first byte; put back it */
41− z->p--;
42− }
43− }
44− return char2int(*z->p);
45−}
46−
47−
4836 void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) {
4937 z->L = L;
5038 z->reader = reader;
@@ -58,8 +46,14 @@ void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader, void *data) { · 7 unmodified lines
5846 size_t luaZ_read (ZIO *z, void *b, size_t n) {
5947 while (n) {
6048 size_t m;
61− if (luaZ_lookahead(z) == EOZ)
62− return n; /* return number of missing bytes */
49+ if (z->n == 0) { /* no bytes in buffer? */
50+ if (luaZ_fill(z) == EOZ) /* try to read more */
51+ return n; /* no more input; return number of missing bytes */
52+ else {
53+ z->n++; /* luaZ_fill consumed first byte; put it back */
54+ z->p--;
55+ }
56+ }
6357 m = (n <= z->n) ? n : z->n; /* min. between n and z->n */
6458 memcpy(b, z->p, m);
6559 z->n -= m;
src/lzio.h+2−3
@@ -1,5 +1,5 @@
11 /*
2−** $Id: lzio.h,v 1.22 2009/05/18 17:26:25 roberto Exp $
2+** $Id: lzio.h,v 1.24 2011/02/23 13:13:10 roberto Exp $
33 ** Buffered streams
44 ** See Copyright Notice in lua.h
55 */
@@ -50,7 +50,6 @@ LUAI_FUNC char *luaZ_openspace (lua_State *L, Mbuffer *buff, size_t n); · 44 unmodified lines
5050 LUAI_FUNC void luaZ_init (lua_State *L, ZIO *z, lua_Reader reader,
5151 void *data);
5252 LUAI_FUNC size_t luaZ_read (ZIO* z, void* b, size_t n); /* read next n bytes */
53−LUAI_FUNC int luaZ_lookahead (ZIO *z);
5453
5554
5655
@@ -59,7 +58,7 @@ LUAI_FUNC int luaZ_lookahead (ZIO *z); · 2 unmodified lines
5958 struct Zio {
6059 size_t n; /* bytes still unread */
6160 const char *p; /* current position in buffer */
62− lua_Reader reader;
61+ lua_Reader reader; /* reader function */
6362 void* data; /* additional data */
6463 lua_State *L; /* Lua state (for reader) */
6564 };

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t