From a68385b1043a2041390f65ce5cf9ed434af5e95d Mon Sep 17 00:00:00 2001 From: pixel Date: Wed, 25 Jul 2007 16:54:32 +0000 Subject: Upgrading to Lua-5.0.3 --- lib/lua/include/lua.h | 8 +++--- lib/lua/includes/lfunc.h | 9 +++++- lib/lua/src/LuaLib/lbaselib.c | 4 ++- lib/lua/src/LuaLib/liolib.c | 66 +++++++++++++++++++++++++------------------ lib/lua/src/lapi.c | 6 ++-- lib/lua/src/lcode.c | 52 ++++++++++++++++++---------------- lib/lua/src/lfunc.c | 10 +------ lib/lua/src/lgc.c | 23 +++++++++------ lib/lua/src/lvm.c | 14 ++++----- 9 files changed, 108 insertions(+), 84 deletions(-) diff --git a/lib/lua/include/lua.h b/lib/lua/include/lua.h index 9a28d9e..8a9caf1 100644 --- a/lib/lua/include/lua.h +++ b/lib/lua/include/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.9 2006-02-09 16:59:55 pixel Exp $ +** $Id: lua.h,v 1.10 2007-07-25 16:54:32 pixel Exp $ ** Lua - An Extensible Extension Language ** Tecgraf: Computer Graphics Technology Group, PUC-Rio, Brazil ** http://www.lua.org mailto:info@lua.org @@ -17,8 +17,8 @@ extern "C" { #endif -#define LUA_VERSION "Lua 5.0.2" -#define LUA_COPYRIGHT "Copyright (C) 1994-2004 Tecgraf, PUC-Rio" +#define LUA_VERSION "Lua 5.0.3" +#define LUA_COPYRIGHT "Copyright (C) 1994-2006 Tecgraf, PUC-Rio" #define LUA_AUTHORS "R. Ierusalimschy, L. H. de Figueiredo & W. Celes" @@ -380,7 +380,7 @@ struct lua_Debug { /****************************************************************************** -* Copyright (C) 1994-2003 Tecgraf, PUC-Rio. All rights reserved. +* Copyright (C) 1994-2006 Tecgraf, PUC-Rio. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the diff --git a/lib/lua/includes/lfunc.h b/lib/lua/includes/lfunc.h index 6af41b7..df8f1c9 100644 --- a/lib/lua/includes/lfunc.h +++ b/lib/lua/includes/lfunc.h @@ -1,5 +1,5 @@ /* -** $Id: lfunc.h,v 1.4 2004-11-27 21:46:06 pixel Exp $ +** $Id: lfunc.h,v 1.5 2007-07-25 16:54:32 pixel Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -11,6 +11,13 @@ #include "lobject.h" +#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ + cast(int, sizeof(TObject)*((n)-1))) + +#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ + cast(int, sizeof(TObject *)*((n)-1))) + + Proto *luaF_newproto (lua_State *L); Closure *luaF_newCclosure (lua_State *L, int nelems); Closure *luaF_newLclosure (lua_State *L, int nelems, TObject *e); diff --git a/lib/lua/src/LuaLib/lbaselib.c b/lib/lua/src/LuaLib/lbaselib.c index f9c76f1..9a173a4 100644 --- a/lib/lua/src/LuaLib/lbaselib.c +++ b/lib/lua/src/LuaLib/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.6 2007-05-31 13:26:52 pixel Exp $ +** $Id: lbaselib.c,v 1.7 2007-07-25 16:54:33 pixel Exp $ ** Basic library ** See Copyright Notice in lua.h */ @@ -173,6 +173,7 @@ static int luaB_rawequal (lua_State *L) { static int luaB_rawget (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 2); + lua_settop(L, 2); lua_rawget(L, 1); return 1; } @@ -181,6 +182,7 @@ static int luaB_rawset (lua_State *L) { luaL_checktype(L, 1, LUA_TTABLE); luaL_checkany(L, 2); luaL_checkany(L, 3); + lua_settop(L, 3); lua_rawset(L, 1); return 1; } diff --git a/lib/lua/src/LuaLib/liolib.c b/lib/lua/src/LuaLib/liolib.c index 29c4821..bd3b2ed 100644 --- a/lib/lua/src/LuaLib/liolib.c +++ b/lib/lua/src/LuaLib/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.5 2004-12-27 19:52:23 pixel Exp $ +** $Id: liolib.c,v 1.6 2007-07-25 16:54:33 pixel Exp $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -20,6 +20,12 @@ #include "lualib.h" +typedef struct FileHandle { + FILE *f; + int ispipe; +} FileHandle; + + /* ** by default, gcc does not get `tmpname' @@ -86,17 +92,17 @@ static int pushresult (lua_State *L, int i, const char *filename) { } -static FILE **topfile (lua_State *L, int findex) { - FILE **f = (FILE **)luaL_checkudata(L, findex, FILEHANDLE); - if (f == NULL) luaL_argerror(L, findex, "bad file"); - return f; +static FileHandle *topfile (lua_State *L, int findex) { + FileHandle *fh = (FileHandle *)luaL_checkudata(L, findex, FILEHANDLE); + if (fh == NULL) luaL_argerror(L, findex, "bad file"); + return fh; } static int io_type (lua_State *L) { - FILE **f = (FILE **)luaL_checkudata(L, 1, FILEHANDLE); - if (f == NULL) lua_pushnil(L); - else if (*f == NULL) + FileHandle *fh = (FileHandle *)luaL_checkudata(L, 1, FILEHANDLE); + if (fh == NULL) lua_pushnil(L); + else if (fh->f == NULL) lua_pushliteral(L, "closed file"); else lua_pushliteral(L, "file"); @@ -104,26 +110,31 @@ static int io_type (lua_State *L) { } -static FILE *tofile (lua_State *L, int findex) { - FILE **f = topfile(L, findex); - if (*f == NULL) +#define tofile(L,i) (tofileh(L,i)->f) + +static FileHandle *tofileh (lua_State *L, int findex) { + FileHandle *fh = topfile(L, findex); + if (fh->f == NULL) luaL_error(L, "attempt to use a closed file"); - return *f; + return fh; } +#define newfile(L) (&(newfileh(L)->f)) + /* ** When creating file handles, always creates a `closed' file handle ** before opening the actual file; so, if there is a memory error, the ** file is not left opened. */ -static FILE **newfile (lua_State *L) { - FILE **pf = (FILE **)lua_newuserdata(L, sizeof(FILE *)); - *pf = NULL; /* file handle is currently `closed' */ +static FileHandle *newfileh (lua_State *L) { + FileHandle *fh = (FileHandle *)lua_newuserdata(L, sizeof(FileHandle)); + fh->f = NULL; /* file handle is currently `closed' */ + fh->ispipe = 0; luaL_getmetatable(L, FILEHANDLE); lua_setmetatable(L, -2); - return pf; + return fh; } @@ -145,13 +156,13 @@ static void registerfile (lua_State *L, FILE *f, const char *name, static int aux_close (lua_State *L) { - FILE *f = tofile(L, 1); + FileHandle *fh = tofileh(L, 1); + FILE *f = fh->f; if (f == stdin || f == stdout || f == stderr) return 0; /* file cannot be closed */ else { - int ok = (pclose(f) != -1) || (fclose(f) == 0); - if (ok) - *(FILE **)lua_touserdata(L, 1) = NULL; /* mark file as closed */ + int ok = fh->ispipe ? (pclose(f) != -1) : (fclose(f) == 0); + fh->f = NULL; /* mark file as closed */ return ok; } } @@ -167,8 +178,8 @@ static int io_close (lua_State *L) { static int io_gc (lua_State *L) { - FILE **f = topfile(L, 1); - if (*f != NULL) /* ignore closed files */ + FileHandle *fh = topfile(L, 1); + if (fh->f != NULL) /* ignore closed files */ aux_close(L); return 0; } @@ -176,8 +187,8 @@ static int io_gc (lua_State *L) { static int io_tostring (lua_State *L) { char buff[128]; - FILE **f = topfile(L, 1); - if (*f == NULL) + FileHandle *fh = topfile(L, 1); + if (fh->f == NULL) strcpy(buff, "closed"); else sprintf(buff, "%p", lua_touserdata(L, 1)); @@ -202,9 +213,10 @@ static int io_popen (lua_State *L) { #else const char *filename = luaL_checkstring(L, 1); const char *mode = luaL_optstring(L, 2, "r"); - FILE **pf = newfile(L); - *pf = popen(filename, mode); - return (*pf == NULL) ? pushresult(L, 0, filename) : 1; + FileHandle *fh = newfileh(L); + fh->f = popen(filename, mode); + fh->ispipe = 1; + return (fh->f == NULL) ? pushresult(L, 0, filename) : 1; #endif } diff --git a/lib/lua/src/lapi.c b/lib/lua/src/lapi.c index 038d5bf..7298b87 100644 --- a/lib/lua/src/lapi.c +++ b/lib/lua/src/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.4 2004-11-27 21:46:07 pixel Exp $ +** $Id: lapi.c,v 1.5 2007-07-25 16:54:32 pixel Exp $ ** Lua API ** See Copyright Notice in lua.h */ @@ -879,13 +879,13 @@ static const char *aux_upvalue (lua_State *L, int funcindex, int n, if (!ttisfunction(fi)) return NULL; f = clvalue(fi); if (f->c.isC) { - if (n > f->c.nupvalues) return NULL; + if (!(1 <= n && n <= f->c.nupvalues)) return NULL; *val = &f->c.upvalue[n-1]; return ""; } else { Proto *p = f->l.p; - if (n > p->sizeupvalues) return NULL; + if (!(1 <= n && n <= p->sizeupvalues)) return NULL; *val = f->l.upvals[n-1]->v; return getstr(p->upvalues[n-1]); } diff --git a/lib/lua/src/lcode.c b/lib/lua/src/lcode.c index 0b2f6b1..f393e7c 100644 --- a/lib/lua/src/lcode.c +++ b/lib/lua/src/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.4 2004-11-27 21:46:07 pixel Exp $ +** $Id: lcode.c,v 1.5 2007-07-25 16:54:32 pixel Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -102,7 +102,10 @@ static Instruction *getjumpcontrol (FuncState *fs, int pc) { static int need_value (FuncState *fs, int list, int cond) { for (; list != NO_JUMP; list = luaK_getjump(fs, list)) { Instruction i = *getjumpcontrol(fs, list); - if (GET_OPCODE(i) != OP_TEST || GETARG_C(i) != cond) return 1; + if (GET_OPCODE(i) != OP_TEST || + GETARG_A(i) != NO_REG || + GETARG_C(i) != cond) + return 1; } return 0; /* not found */ } @@ -114,34 +117,33 @@ static void patchtestreg (Instruction *i, int reg) { } -static void luaK_patchlistaux (FuncState *fs, int list, - int ttarget, int treg, int ftarget, int freg, int dtarget) { +static void removevalues (FuncState *fs, int list) { + for (; list != NO_JUMP; list = luaK_getjump(fs, list)) { + Instruction *i = getjumpcontrol(fs, list); + if (GET_OPCODE(*i) == OP_TEST) + patchtestreg(i, NO_REG); + } +} + + +static void luaK_patchlistaux (FuncState *fs, int list, int vtarget, int reg, + int dtarget) { while (list != NO_JUMP) { int next = luaK_getjump(fs, list); Instruction *i = getjumpcontrol(fs, list); - if (GET_OPCODE(*i) != OP_TEST) { - lua_assert(dtarget != NO_JUMP); - luaK_fixjump(fs, list, dtarget); /* jump to default target */ - } - else { - if (GETARG_C(*i)) { - lua_assert(ttarget != NO_JUMP); - patchtestreg(i, treg); - luaK_fixjump(fs, list, ttarget); - } - else { - lua_assert(ftarget != NO_JUMP); - patchtestreg(i, freg); - luaK_fixjump(fs, list, ftarget); - } + if (GET_OPCODE(*i) == OP_TEST && GETARG_A(*i) == NO_REG) { + patchtestreg(i, reg); + luaK_fixjump(fs, list, vtarget); } + else + luaK_fixjump(fs, list, dtarget); /* jump to default target */ list = next; } } static void luaK_dischargejpc (FuncState *fs) { - luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc, NO_REG, fs->pc); + luaK_patchlistaux(fs, fs->jpc, fs->pc, NO_REG, fs->pc); fs->jpc = NO_JUMP; } @@ -151,7 +153,7 @@ void luaK_patchlist (FuncState *fs, int list, int target) { luaK_patchtohere(fs, list); else { lua_assert(target < fs->pc); - luaK_patchlistaux(fs, list, target, NO_REG, target, NO_REG, target); + luaK_patchlistaux(fs, list, target, NO_REG, target); } } @@ -354,8 +356,8 @@ static void luaK_exp2reg (FuncState *fs, expdesc *e, int reg) { luaK_patchtohere(fs, fj); } final = luaK_getlabel(fs); - luaK_patchlistaux(fs, e->f, p_f, NO_REG, final, reg, p_f); - luaK_patchlistaux(fs, e->t, final, reg, p_t, NO_REG, p_t); + luaK_patchlistaux(fs, e->f, final, reg, p_f); + luaK_patchlistaux(fs, e->t, final, reg, p_t); } e->f = e->t = NO_JUMP; e->info = reg; @@ -473,7 +475,7 @@ static int jumponcond (FuncState *fs, expdesc *e, int cond) { Instruction ie = getcode(fs, e); if (GET_OPCODE(ie) == OP_NOT) { fs->pc--; /* remove previous OP_NOT */ - return luaK_condjump(fs, OP_TEST, NO_REG, GETARG_B(ie), !cond); + return luaK_condjump(fs, OP_TEST, GETARG_B(ie), GETARG_B(ie), !cond); } /* else go through */ } @@ -564,6 +566,8 @@ static void codenot (FuncState *fs, expdesc *e) { } /* interchange true and false lists */ { int temp = e->f; e->f = e->t; e->t = temp; } + removevalues(fs, e->f); + removevalues(fs, e->t); } diff --git a/lib/lua/src/lfunc.c b/lib/lua/src/lfunc.c index 7dc41e2..45a60cb 100644 --- a/lib/lua/src/lfunc.c +++ b/lib/lua/src/lfunc.c @@ -1,5 +1,5 @@ /* -** $Id: lfunc.c,v 1.4 2004-11-27 21:46:07 pixel Exp $ +** $Id: lfunc.c,v 1.5 2007-07-25 16:54:32 pixel Exp $ ** Auxiliary functions to manipulate prototypes and closures ** See Copyright Notice in lua.h */ @@ -18,14 +18,6 @@ #include "lstate.h" -#define sizeCclosure(n) (cast(int, sizeof(CClosure)) + \ - cast(int, sizeof(TObject)*((n)-1))) - -#define sizeLclosure(n) (cast(int, sizeof(LClosure)) + \ - cast(int, sizeof(TObject *)*((n)-1))) - - - Closure *luaF_newCclosure (lua_State *L, int nelems) { Closure *c = cast(Closure *, luaM_malloc(L, sizeCclosure(nelems))); luaC_link(L, valtogco(c), LUA_TFUNCTION); diff --git a/lib/lua/src/lgc.c b/lib/lua/src/lgc.c index dc939c8..dbc022d 100644 --- a/lib/lua/src/lgc.c +++ b/lib/lua/src/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.5 2004-12-27 19:52:23 pixel Exp $ +** $Id: lgc.c,v 1.6 2007-07-25 16:54:32 pixel Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -218,10 +218,8 @@ static void traverseclosure (GCState *st, Closure *cl) { markvalue(st, cl->l.p); for (i=0; il.nupvalues; i++) { /* mark its upvalues */ UpVal *u = cl->l.upvals[i]; - if (!u->marked) { - markobject(st, &u->value); - u->marked = 1; - } + markobject(st, u->v); + u->marked = 1; } } } @@ -258,36 +256,45 @@ static void traversestack (GCState *st, lua_State *L1) { } -static void propagatemarks (GCState *st) { +static lu_mem propagatemarks (GCState *st) { + lu_mem mf = 0; while (st->tmark) { /* traverse marked objects */ switch (st->tmark->gch.tt) { case LUA_TTABLE: { Table *h = gcotoh(st->tmark); st->tmark = h->gclist; traversetable(st, h); + mf += sizeof(Table) + sizeof(TObject) * h->sizearray + + sizeof(Node) * sizenode(h); break; } case LUA_TFUNCTION: { Closure *cl = gcotocl(st->tmark); st->tmark = cl->c.gclist; traverseclosure(st, cl); + mf += (cl->c.isC) ? sizeCclosure(cl->c.nupvalues) : + sizeLclosure(cl->l.nupvalues); break; } case LUA_TTHREAD: { lua_State *th = gcototh(st->tmark); st->tmark = th->gclist; traversestack(st, th); + mf += sizeof(lua_State) + sizeof(TObject) * th->stacksize + + sizeof(CallInfo) * th->size_ci; break; } case LUA_TPROTO: { Proto *p = gcotop(st->tmark); st->tmark = p->gclist; traverseproto(st, p); + /* do not need 'mf' for this case (cannot happen inside a udata) */ break; } default: lua_assert(0); } } + return mf; } @@ -368,7 +375,7 @@ static int sweeplist (lua_State *L, GCObject **p, int limit) { GCObject *curr; int count = 0; /* number of collected items */ while ((curr = *p) != NULL) { - if (curr->gch.marked > limit) { + if ((curr->gch.marked & ~(KEYWEAK | VALUEWEAK)) > limit) { unmark(curr); p = &curr->gch.next; } @@ -470,7 +477,7 @@ static size_t mark (lua_State *L) { st.wv = NULL; deadmem = luaC_separateudata(L); /* separate userdata to be preserved */ marktmu(&st); /* mark `preserved' userdata */ - propagatemarks(&st); /* remark, to propagate `preserveness' */ + deadmem += propagatemarks(&st); /* remark, to propagate `preserveness' */ cleartablekeys(wkv); /* `propagatemarks' may resuscitate some weak tables; clear them too */ cleartablekeys(st.wk); diff --git a/lib/lua/src/lvm.c b/lib/lua/src/lvm.c index 5c6620a..5103140 100644 --- a/lib/lua/src/lvm.c +++ b/lib/lua/src/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.9 2006-02-09 16:57:30 pixel Exp $ +** $Id: lvm.c,v 1.10 2007-07-25 16:54:32 pixel Exp $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -341,15 +341,15 @@ void luaV_concat (lua_State *L, int total, int last) { luaG_concaterror(L, top-2, top-1); } else if (tsvalue(top-1)->tsv.len > 0) { /* if len=0, do nothing */ /* at least two string values; get as many as possible */ - lu_mem tl = cast(lu_mem, tsvalue(top-1)->tsv.len) + - cast(lu_mem, tsvalue(top-2)->tsv.len); + size_t tl = tsvalue(top-1)->tsv.len; char *buffer; int i; - while (n < total && tostring(L, top-n-1)) { /* collect total length */ - tl += tsvalue(top-n-1)->tsv.len; - n++; + /* collect total length */ + for (n = 1; n < total && tostring(L, top-n-1); n++) { + size_t l = tsvalue(top-n-1)->tsv.len; + if (l >= MAX_SIZET - tl) luaG_runerror(L, "string length overflow"); + tl += l; } - if (tl > MAX_SIZET) luaG_runerror(L, "string size overflow"); buffer = luaZ_openspace(L, &G(L)->buff, tl); tl = 0; for (i=n; i>0; i--) { /* concat all strings */ -- cgit v1.2.3