diff --git a/lapi.c b/lapi.c index 7715c5a2..e9f9078e 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.103 2000/10/02 20:10:55 roberto Exp roberto $ +** $Id: lapi.c,v 1.104 2000/10/03 14:27:44 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -116,20 +116,18 @@ void lua_pushvalue (lua_State *L, int index) { return ((test) ? (value) : (default)); } -lua_Type lua_type (lua_State *L, int index) { - btest(L, index, luaO_type(o), LUA_NOVALUE); +int lua_type (lua_State *L, int index) { + btest(L, index, ttype(o), LUA_TNONE); } -const char *lua_typename (lua_State *L, lua_Type t) { - static const char *const names[] = { - "NO VALUE", "userdata", "number", "string", "table", "function", "nil" - }; +const char *lua_typename (lua_State *L, int t) { UNUSED(L); - return names[(int)t]; + return luaO_typenames[t]; } + int lua_iscfunction (lua_State *L, int index) { - btest(L, index, (ttype(o) == TAG_CCLOSURE), 0); + btest(L, index, iscfunction(o), 0); } int lua_isnumber (lua_State *L, int index) { @@ -137,18 +135,13 @@ int lua_isnumber (lua_State *L, int index) { } int lua_isstring (lua_State *L, int index) { - lua_Type t = lua_type(L, index); + int t = lua_type(L, index); return (t == LUA_TSTRING || t == LUA_TNUMBER); } -static int auxtag (const TObject *o) { -return ((ttype(o) == TAG_USERDATA) ? tsvalue(o)->u.d.tag : - (ttype(o) == TAG_TABLE) ? hvalue(o)->htag : (int)ttype(o)); -} - int lua_tag (lua_State *L, int index) { - btest(L, index, auxtag(o), LUA_NOTAG); + btest(L, index, luaT_tag(o), LUA_NOTAG); } int lua_equal (lua_State *L, int index1, int index2) { @@ -168,7 +161,7 @@ int lua_lessthan (lua_State *L, int index1, int index2) { double lua_tonumber (lua_State *L, int index) { - access(L, index, (tonumber(o) == 0), 0.0, nvalue(o)); + access(L, index, (tonumber(o) == 0), 0, nvalue(o)); } const char *lua_tostring (lua_State *L, int index) { @@ -180,19 +173,19 @@ size_t lua_strlen (lua_State *L, int index) { } lua_CFunction lua_tocfunction (lua_State *L, int index) { - access(L, index, (ttype(o) == TAG_CCLOSURE), NULL, clvalue(o)->f.c); + access(L, index, iscfunction(o), NULL, clvalue(o)->f.c); } void *lua_touserdata (lua_State *L, int index) { - access(L, index, (ttype(o) == TAG_USERDATA), NULL, tsvalue(o)->u.d.value); + access(L, index, (ttype(o) == LUA_TUSERDATA), NULL, tsvalue(o)->u.d.value); } const void *lua_topointer (lua_State *L, int index) { StkId o = Index(L, index); switch (ttype(o)) { - case TAG_TABLE: + case LUA_TTABLE: return hvalue(o); - case TAG_CCLOSURE: case TAG_LCLOSURE: + case LUA_TFUNCTION: return clvalue(o); default: return NULL; } @@ -206,13 +199,13 @@ const void *lua_topointer (lua_State *L, int index) { void lua_pushnil (lua_State *L) { - ttype(L->top) = TAG_NIL; + ttype(L->top) = LUA_TNIL; api_incr_top(L); } void lua_pushnumber (lua_State *L, double n) { - ttype(L->top) = TAG_NUMBER; + ttype(L->top) = LUA_TNUMBER; nvalue(L->top) = n; api_incr_top(L); } @@ -220,7 +213,7 @@ void lua_pushnumber (lua_State *L, double n) { void lua_pushlstring (lua_State *L, const char *s, size_t len) { tsvalue(L->top) = luaS_newlstr(L, s, len); - ttype(L->top) = TAG_STRING; + ttype(L->top) = LUA_TSTRING; api_incr_top(L); } @@ -239,10 +232,10 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) { void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */ - if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS) + if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag))) luaO_verror(L, "invalid tag for a userdata (%d)", tag); tsvalue(L->top) = luaS_createudata(L, u, tag); - ttype(L->top) = TAG_USERDATA; + ttype(L->top) = LUA_TUSERDATA; api_incr_top(L); } @@ -271,14 +264,14 @@ void lua_gettable (lua_State *L, int index) { void lua_rawget (lua_State *L, int index) { StkId t = Index(L, index); - LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1); } void lua_rawgeti (lua_State *L, int index, int n) { StkId o = Index(L, index); - LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); *L->top = *luaH_getnum(hvalue(o), n); api_incr_top(L); } @@ -286,14 +279,14 @@ void lua_rawgeti (lua_State *L, int index, int n) { void lua_getglobals (lua_State *L) { hvalue(L->top) = L->gt; - ttype(L->top) = TAG_TABLE; + ttype(L->top) = LUA_TTABLE; api_incr_top(L); } int lua_getref (lua_State *L, int ref) { if (ref == LUA_REFNIL) - ttype(L->top) = TAG_NIL; + ttype(L->top) = LUA_TNIL; else if (0 <= ref && ref < L->refSize && (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) *L->top = L->refArray[ref].o; @@ -306,7 +299,7 @@ int lua_getref (lua_State *L, int ref) { void lua_newtable (lua_State *L) { hvalue(L->top) = luaH_new(L, 0); - ttype(L->top) = TAG_TABLE; + ttype(L->top) = LUA_TTABLE; api_incr_top(L); } @@ -334,7 +327,7 @@ void lua_settable (lua_State *L, int index) { void lua_rawset (lua_State *L, int index) { StkId t = Index(L, index); - LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1); L->top -= 2; } @@ -342,7 +335,7 @@ void lua_rawset (lua_State *L, int index) { void lua_rawseti (lua_State *L, int index, int n) { StkId o = Index(L, index); - LUA_ASSERT(ttype(o) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); *luaH_setint(L, hvalue(o), n) = *(L->top-1); L->top--; } @@ -350,14 +343,14 @@ void lua_rawseti (lua_State *L, int index, int n) { void lua_setglobals (lua_State *L) { StkId newtable = --L->top; - LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected"); L->gt = hvalue(newtable); } int lua_ref (lua_State *L, int lock) { int ref; - if (ttype(L->top-1) == TAG_NIL) + if (ttype(L->top-1) == LUA_TNIL) ref = LUA_REFNIL; else { if (L->refFree != NONEXT) { /* is there a free place? */ @@ -420,15 +413,15 @@ void lua_setgcthreshold (lua_State *L, int newthreshold) { void lua_settag (lua_State *L, int tag) { luaT_realtag(L, tag); switch (ttype(L->top-1)) { - case TAG_TABLE: + case LUA_TTABLE: hvalue(L->top-1)->htag = tag; break; - case TAG_USERDATA: + case LUA_TUSERDATA: tsvalue(L->top-1)->u.d.tag = tag; break; default: luaO_verror(L, "cannot change the tag of a %.20s", - luaO_typename(L, L->top-1)); + luaO_typename(L->top-1)); } L->top--; } @@ -446,7 +439,7 @@ void lua_unref (lua_State *L, int ref) { int lua_next (lua_State *L, int index) { StkId t = Index(L, index); Node *n; - LUA_ASSERT(ttype(t) == TAG_TABLE, "table expected"); + LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); n = luaH_next(L, hvalue(t), Index(L, -1)); if (n) { *(L->top-1) = *key(n); @@ -464,15 +457,15 @@ int lua_next (lua_State *L, int index) { int lua_getn (lua_State *L, int index) { Hash *h = hvalue(Index(L, index)); const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ - if (ttype(value) == TAG_NUMBER) + if (ttype(value) == LUA_TNUMBER) return (int)nvalue(value); else { Number max = 0; int i = h->size; Node *n = h->node; while (i--) { - if (ttype(key(n)) == TAG_NUMBER && - ttype(val(n)) != TAG_NIL && + if (ttype(key(n)) == LUA_TNUMBER && + ttype(val(n)) != LUA_TNIL && nvalue(key(n)) > max) max = nvalue(key(n)); n++; diff --git a/lauxlib.c b/lauxlib.c index d6398795..433c24df 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.37 2000/09/29 12:40:56 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.38 2000/10/02 20:10:55 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -40,7 +40,7 @@ void luaL_argerror (lua_State *L, int narg, const char *extramsg) { } -static void type_error (lua_State *L, int narg, lua_Type t) { +static void type_error (lua_State *L, int narg, int t) { char buff[100]; const char *rt = lua_typename(L, lua_type(L, narg)); if (*rt == 'N') rt = "no value"; @@ -55,14 +55,14 @@ void luaL_checkstack (lua_State *L, int space, const char *mes) { } -void luaL_checktype(lua_State *L, int narg, lua_Type t) { +void luaL_checktype(lua_State *L, int narg, int t) { if (lua_type(L, narg) != t) type_error(L, narg, t); } void luaL_checkany (lua_State *L, int narg) { - if (lua_type(L, narg) == LUA_NOVALUE) + if (lua_type(L, narg) == LUA_TNONE) luaL_argerror(L, narg, "value expected"); } diff --git a/lauxlib.h b/lauxlib.h index 54949d86..d94b9bae 100644 --- a/lauxlib.h +++ b/lauxlib.h @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.h,v 1.25 2000/09/12 13:48:22 roberto Exp roberto $ +** $Id: lauxlib.h,v 1.26 2000/10/02 20:10:55 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -30,7 +30,7 @@ double luaL_check_number (lua_State *L, int numArg); double luaL_opt_number (lua_State *L, int numArg, double def); void luaL_checkstack (lua_State *L, int space, const char *msg); -void luaL_checktype (lua_State *L, int narg, lua_Type t); +void luaL_checktype (lua_State *L, int narg, int t); void luaL_checkany (lua_State *L, int narg); void luaL_verror (lua_State *L, const char *fmt, ...); diff --git a/lbaselib.c b/lbaselib.c index 9c1e6f0b..b66cf490 100644 --- a/lbaselib.c +++ b/lbaselib.c @@ -1,5 +1,5 @@ /* -** $Id: lbaselib.c,v 1.7 2000/10/02 14:47:43 roberto Exp roberto $ +** $Id: lbaselib.c,v 1.8 2000/10/02 20:10:55 roberto Exp roberto $ ** Basic library ** See Copyright Notice in lua.h */ @@ -322,7 +322,7 @@ static int luaB_tostring (lua_State *L) { case LUA_TNIL: lua_pushstring(L, "nil"); return 1; - case LUA_NOVALUE: + default: luaL_argerror(L, 1, "value expected"); } lua_pushstring(L, buff); diff --git a/ldebug.c b/ldebug.c index cec9f191..8a9a2ca6 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.42 2000/09/18 19:39:49 roberto Exp roberto $ +** $Id: ldebug.c,v 1.43 2000/10/02 20:10:55 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -23,23 +23,21 @@ #include "luadebug.h" + static const char *getfuncname (lua_State *L, StkId f, const char **name); static void setnormalized (TObject *d, const TObject *s) { - switch (s->ttype) { - case TAG_CMARK: { - clvalue(d) = clvalue(s); - ttype(d) = TAG_CCLOSURE; - break; - } - case TAG_LMARK: { - clvalue(d) = infovalue(s)->func; - ttype(d) = TAG_LCLOSURE; - break; - } - default: *d = *s; + if (ttype(s) == LUA_TMARK) { + clvalue(d) = infovalue(s)->func; + ttype(d) = LUA_TFUNCTION; } + else *d = *s; +} + + +static int isLmark (StkId o) { + return (o && ttype(o) == LUA_TMARK && !infovalue(o)->func->isC); } @@ -82,9 +80,9 @@ int lua_getstack (lua_State *L, int level, lua_Debug *ar) { static int lua_nups (StkId f) { switch (ttype(f)) { - case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK: + case LUA_TFUNCTION: return clvalue(f)->nupvalues; - case TAG_LMARK: + case LUA_TMARK: return infovalue(f)->func->nupvalues; default: return 0; @@ -125,13 +123,13 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) { static int lua_currentpc (StkId f) { CallInfo *ci = infovalue(f); - LUA_ASSERT(ttype(f) == TAG_LMARK, "function has no pc"); + LUA_ASSERT(isLmark(f), "function has no pc"); return (*ci->pc - ci->func->f.l->code) - 1; } static int lua_currentline (StkId f) { - if (ttype(f) != TAG_LMARK) + if (!isLmark(f)) return -1; /* only active lua functions have current-line information */ else { CallInfo *ci = infovalue(f); @@ -143,7 +141,7 @@ static int lua_currentline (StkId f) { static Proto *getluaproto (StkId f) { - return (ttype(f) == TAG_LMARK) ? infovalue(f)->func->f.l : NULL; + return (isLmark(f) ? infovalue(f)->func->f.l : NULL); } @@ -179,22 +177,25 @@ static void infoLproto (lua_Debug *ar, Proto *f) { } -static void lua_funcinfo (lua_Debug *ar, StkId func) { +static void lua_funcinfo (lua_State *L, lua_Debug *ar, StkId func) { + Closure *cl = NULL; switch (ttype(func)) { - case TAG_LCLOSURE: - infoLproto(ar, clvalue(func)->f.l); + case LUA_TFUNCTION: + cl = clvalue(func); break; - case TAG_LMARK: - infoLproto(ar, infovalue(func)->func->f.l); - break; - case TAG_CCLOSURE: case TAG_CMARK: - ar->source = "(C)"; - ar->linedefined = -1; - ar->what = "C"; + case LUA_TMARK: + cl = infovalue(func)->func; break; default: - LUA_INTERNALERROR("invalid `func' value"); + lua_error(L, "value for `lua_getinfo' is not a function"); } + if (cl->isC) { + ar->source = "(C)"; + ar->linedefined = -1; + ar->what = "C"; + } + else + infoLproto(ar, cl->f.l); luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src)); if (ar->linedefined == 0) ar->what = "main"; @@ -218,7 +219,7 @@ static const char *travglobals (lua_State *L, const TObject *o) { int i; for (i=0; isize; i++) { if (luaO_equalObj(o, val(node(g, i))) && - ttype(key(node(g, i))) == TAG_STRING) + ttype(key(node(g, i))) == LUA_TSTRING) return tsvalue(key(node(g, i)))->str; } return NULL; @@ -250,7 +251,7 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) { for (; *what; what++) { switch (*what) { case 'S': { - lua_funcinfo(ar, func); + lua_funcinfo(L, ar, func); break; } case 'l': { @@ -377,8 +378,8 @@ static Instruction luaG_symbexec (const Proto *pt, int lastpc, int stackpos) { static const char *getobjname (lua_State *L, StkId obj, const char **name) { StkId func = aux_stackedfunction(L, 0, obj); - if (func == NULL || ttype(func) != TAG_LMARK) - return NULL; /* not a Lua function */ + if (!isLmark(func)) + return NULL; /* not an active Lua function */ else { Proto *p = infovalue(func)->func->f.l; int pc = lua_currentpc(func); @@ -409,8 +410,8 @@ static const char *getobjname (lua_State *L, StkId obj, const char **name) { static const char *getfuncname (lua_State *L, StkId f, const char **name) { StkId func = aux_stackedfunction(L, 0, f); /* calling function */ - if (func == NULL || ttype(func) != TAG_LMARK) - return NULL; /* not a Lua function */ + if (!isLmark(func)) + return NULL; /* not an active Lua function */ else { Proto *p = infovalue(func)->func->f.l; int pc = lua_currentpc(func); @@ -433,7 +434,7 @@ static const char *getfuncname (lua_State *L, StkId f, const char **name) { void luaG_typeerror (lua_State *L, StkId o, const char *op) { const char *name; const char *kind = getobjname(L, o, &name); - const char *t = luaO_typename(L, o); + const char *t = luaO_typename(o); if (kind) luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)", op, kind, name, t); @@ -442,7 +443,7 @@ void luaG_typeerror (lua_State *L, StkId o, const char *op) { } -void luaG_binerror (lua_State *L, StkId p1, lua_Tag t, const char *op) { +void luaG_binerror (lua_State *L, StkId p1, int t, const char *op) { if (ttype(p1) == t) p1++; LUA_ASSERT(ttype(p1) != t, "must be an error"); luaG_typeerror(L, p1, op); @@ -450,8 +451,8 @@ void luaG_binerror (lua_State *L, StkId p1, lua_Tag t, const char *op) { void luaG_ordererror (lua_State *L, StkId top) { - const char *t1 = luaO_typename(L, top-2); - const char *t2 = luaO_typename(L, top-1); + const char *t1 = luaO_typename(top-2); + const char *t2 = luaO_typename(top-1); if (t1[2] == t2[2]) luaO_verror(L, "attempt to compare two %.10s values", t1); else diff --git a/ldebug.h b/ldebug.h index 94baa17b..dd217513 100644 --- a/ldebug.h +++ b/ldebug.h @@ -1,5 +1,5 @@ /* -** $Id: ldebug.h,v 1.5 2000/08/11 16:17:28 roberto Exp roberto $ +** $Id: ldebug.h,v 1.6 2000/10/02 20:10:55 roberto Exp roberto $ ** Auxiliary functions from Debug Interface module ** See Copyright Notice in lua.h */ @@ -13,7 +13,7 @@ void luaG_typeerror (lua_State *L, StkId o, const char *op); -void luaG_binerror (lua_State *L, StkId p1, lua_Tag t, const char *op); +void luaG_binerror (lua_State *L, StkId p1, int t, const char *op); int luaG_getline (int *lineinfo, int pc, int refline, int *refi); void luaG_ordererror (lua_State *L, StkId top); diff --git a/ldo.c b/ldo.c index f9f1d113..281ef22e 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.100 2000/10/02 20:10:55 roberto Exp roberto $ +** $Id: ldo.c,v 1.101 2000/10/04 12:16:08 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -73,7 +73,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) { else { luaD_checkstack(L, diff); while (diff--) - ttype(L->top++) = TAG_NIL; + ttype(L->top++) = LUA_TNIL; } } @@ -91,7 +91,7 @@ static void luaD_openstack (lua_State *L, StkId pos) { static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) { StkId old_Cbase = L->Cbase; StkId old_top = L->Cbase = L->top; - luaD_checkstack(L, LUA_MINSTACK); /* assures minimum stack size */ + luaD_checkstack(L, LUA_MINSTACK); /* ensure minimum stack size */ L->allowhooks = 0; /* cannot call hooks inside a hook */ (*hook)(L, ar); LUA_ASSERT(L->allowhooks == 0, "invalid allow"); @@ -129,7 +129,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { StkId old_Cbase = L->Cbase; int n; L->Cbase = base; /* new base for C function */ - luaD_checkstack(L, nup+LUA_MINSTACK); /* assures minimum stack size */ + luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ for (n=0; ntop++) = cl->upvalue[n]; if (callhook) @@ -159,32 +159,26 @@ void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults) { */ void luaD_call (lua_State *L, StkId func, int nResults) { StkId firstResult; - retry: /* for `function' tag method */ - switch (ttype(func)) { - case TAG_LCLOSURE: { - CallInfo ci; - ci.func = clvalue(func); - infovalue(func) = &ci; - ttype(func) = TAG_LMARK; - firstResult = luaV_execute(L, ci.func, func+1); - LUA_ASSERT(ttype(func) == TAG_LMARK, "invalid tag"); - break; - } - case TAG_CCLOSURE: { - ttype(func) = TAG_CMARK; - firstResult = callCclosure(L, clvalue(func), func+1); - LUA_ASSERT(ttype(func) == TAG_CMARK, "invalid tag"); - break; - } - default: { /* `func' is not a function; check the `function' tag method */ - const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION); - if (ttype(im) == TAG_NIL) - luaG_typeerror(L, func, "call"); - luaD_openstack(L, func); - *func = *im; /* tag method is the new function to be called */ - goto retry; /* retry the call */ - } + CallInfo ci; + Closure *cl; + if (ttype(func) != LUA_TFUNCTION) { + /* `func' is not a function; check the `function' tag method */ + const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION); + if (ttype(im) == LUA_TNIL) + luaG_typeerror(L, func, "call"); + luaD_openstack(L, func); + *func = *im; /* tag method is the new function to be called */ + LUA_ASSERT(ttype(func) == LUA_TFUNCTION, "invalid tag method"); } + cl = clvalue(func); + ci.func = cl; + infovalue(func) = &ci; + ttype(func) = LUA_TMARK; + if (cl->isC) + firstResult = callCclosure(L, cl, func+1); + else + firstResult = luaV_execute(L, cl, func+1); + LUA_ASSERT(ttype(func) == LUA_TMARK, "invalid tag"); /* adjust the number of results */ if (nResults == LUA_MULTRET) nResults = L->top - firstResult; @@ -250,7 +244,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) { old_blocks = L->nblocks; status = luaD_runprotected(L, f_parser, &p); if (status == 0) { - /* add new memory to threshould (as it probably will stay) */ + /* add new memory to threshold (as it probably will stay) */ L->GCthreshold += (L->nblocks - old_blocks); } else if (status == LUA_ERRRUN) /* an error occurred: correct error code */ @@ -331,7 +325,7 @@ struct lua_longjmp { static void message (lua_State *L, const char *s) { const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE); - if (luaO_type(em) == LUA_TFUNCTION) { + if (ttype(em) == LUA_TFUNCTION) { *L->top = *em; incr_top; lua_pushstring(L, s); diff --git a/lgc.c b/lgc.c index 4ff8bf3c..1aca4e20 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.68 2000/09/29 12:42:13 roberto Exp roberto $ +** $Id: lgc.c,v 1.69 2000/10/02 14:47:43 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -24,7 +24,7 @@ typedef struct GCState { -static int markobject (GCState *st, TObject *o); +static void markobject (GCState *st, TObject *o); /* mark a string; marks larger than 1 cannot be changed */ @@ -73,39 +73,36 @@ static void marktagmethods (lua_State *L, GCState *st) { } -static int markobject (GCState *st, TObject *o) { +static void markclosure (GCState *st, Closure *cl) { + if (!ismarked(cl)) { + if (!cl->isC) + protomark(cl->f.l); + cl->mark = st->cmark; /* chain it for later traversal */ + st->cmark = cl; + } +} + + +static void markobject (GCState *st, TObject *o) { switch (ttype(o)) { - case TAG_USERDATA: case TAG_STRING: + case LUA_TUSERDATA: case LUA_TSTRING: strmark(tsvalue(o)); break; - case TAG_TABLE: { + case LUA_TMARK: + markclosure(st, infovalue(o)->func); + break; + case LUA_TFUNCTION: + markclosure(st, clvalue(o)); + break; + case LUA_TTABLE: { if (!ismarked(hvalue(o))) { hvalue(o)->mark = st->tmark; /* chain it in list of marked */ st->tmark = hvalue(o); } break; } - case TAG_LMARK: { - Closure *cl = infovalue(o)->func; - if (!ismarked(cl)) { - protomark(cl->f.l); - cl->mark = st->cmark; /* chain it for later traversal */ - st->cmark = cl; - } - break; - } - case TAG_LCLOSURE: - protomark(clvalue(o)->f.l); - /* go through */ - case TAG_CCLOSURE: case TAG_CMARK: - if (!ismarked(clvalue(o))) { - clvalue(o)->mark = st->cmark; /* chain it for later traversal */ - st->cmark = clvalue(o); - } - break; default: break; /* numbers, etc */ } - return 0; } @@ -131,8 +128,8 @@ static void markall (lua_State *L) { st.tmark = h->mark; /* remove it from list */ for (i=0; isize; i++) { Node *n = node(h, i); - if (ttype(key(n)) != TAG_NIL) { - if (ttype(val(n)) == TAG_NIL) + if (ttype(key(n)) != LUA_TNIL) { + if (ttype(val(n)) == LUA_TNIL) luaH_remove(h, key(n)); /* dead element; try to remove it */ markobject(&st, &n->key); markobject(&st, &n->val); @@ -147,11 +144,11 @@ static void markall (lua_State *L) { static int hasmark (const TObject *o) { /* valid only for locked objects */ switch (o->ttype) { - case TAG_STRING: case TAG_USERDATA: + case LUA_TSTRING: case LUA_TUSERDATA: return tsvalue(o)->marked; - case TAG_TABLE: + case LUA_TTABLE: return ismarked(hvalue(o)); - case TAG_LCLOSURE: case TAG_CCLOSURE: + case LUA_TFUNCTION: return ismarked(clvalue(o)); default: /* number */ return 1; @@ -271,7 +268,6 @@ static void collectudata (lua_State *L, int all) { } else { /* collect */ int tag = next->u.d.tag; - if (tag > L->last_tag) tag = TAG_USERDATA; *p = next->nexthash; next->nexthash = L->IMtable[tag].collected; /* chain udata */ L->IMtable[tag].collected = next; @@ -297,7 +293,7 @@ static void checkMbuffer (lua_State *L) { static void callgcTM (lua_State *L, const TObject *o) { const TObject *im = luaT_getimbyObj(L, o, IM_GC); - if (ttype(im) != TAG_NIL) { + if (ttype(im) != LUA_TNIL) { int oldah = L->allowhooks; L->allowhooks = 0; /* stop debug hooks during GC tag methods */ luaD_checkstack(L, 2); @@ -313,7 +309,7 @@ static void callgcTM (lua_State *L, const TObject *o) { static void callgcTMudata (lua_State *L) { int tag; TObject o; - ttype(&o) = TAG_USERDATA; + ttype(&o) = LUA_TUSERDATA; L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */ for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */ TString *udata; diff --git a/lobject.c b/lobject.c index 8f10ee03..de79dcdc 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.50 2000/10/02 20:10:55 roberto Exp roberto $ +** $Id: lobject.c,v 1.51 2000/10/03 14:03:21 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -18,16 +18,15 @@ -const lua_Type luaO_typearr[] = { /* ORDER LUA_T */ - LUA_TUSERDATA, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE, - LUA_TFUNCTION, LUA_TFUNCTION, LUA_TNIL +const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; + + +const char *const luaO_typenames[] = { + "userdata", "nil", "number", "string", "table", "function" }; -const TObject luaO_nilobject = {TAG_NIL, {NULL}}; - - /* ** returns smaller power of 2 larger than `n' (minimum is MINPOWER2) */ @@ -41,17 +40,17 @@ lint32 luaO_power2 (lint32 n) { int luaO_equalObj (const TObject *t1, const TObject *t2) { if (ttype(t1) != ttype(t2)) return 0; switch (ttype(t1)) { - case TAG_NUMBER: + case LUA_TNUMBER: return nvalue(t1) == nvalue(t2); - case TAG_STRING: case TAG_USERDATA: + case LUA_TSTRING: case LUA_TUSERDATA: return tsvalue(t1) == tsvalue(t2); - case TAG_TABLE: + case LUA_TTABLE: return hvalue(t1) == hvalue(t2); - case TAG_CCLOSURE: case TAG_LCLOSURE: + case LUA_TFUNCTION: return clvalue(t1) == clvalue(t2); default: - LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type"); - return 1; /* TAG_NIL */ + LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type"); + return 1; /* LUA_TNIL */ } } @@ -80,7 +79,7 @@ int luaO_str2d (const char *s, Number *result) { /* LUA_NUMBER */ /* this function needs to handle only '%d' and '%.XXs' formats */ void luaO_verror (lua_State *L, const char *fmt, ...) { va_list argp; - char buff[600]; /* to hold formated message */ + char buff[600]; /* to hold formatted message */ va_start(argp, fmt); vsprintf(buff, fmt, argp); va_end(argp); diff --git a/lobject.h b/lobject.h index d6f667f4..dd7adeda 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.77 2000/09/29 12:42:13 roberto Exp roberto $ +** $Id: lobject.h,v 1.78 2000/10/02 20:10:55 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -31,41 +31,24 @@ #endif -/* -** Lua TYPES -** WARNING: if you change the order of this enumeration, -** grep "ORDER LUA_T" -*/ -typedef enum { - TAG_USERDATA = 0, /* default tag for userdata */ - TAG_NUMBER, /* fixed tag for numbers */ - TAG_STRING, /* fixed tag for strings */ - TAG_TABLE, /* default tag for tables */ - TAG_LCLOSURE, /* fixed tag for Lua closures */ - TAG_CCLOSURE, /* fixed tag for C closures */ - TAG_NIL, /* last "pre-defined" tag */ +/* mark for closures active in the stack */ +#define LUA_TMARK 6 - TAG_LMARK, /* mark for Lua closures */ - TAG_CMARK /* mark for C closures */ - -} lua_Tag; /* tags for values visible from Lua == first user-created tag */ -#define NUM_TAGS 7 +#define NUM_TAGS 6 -/* -** check whether `t' is a mark -*/ -#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK) +/* check whether `t' is a mark */ +#define is_T_MARK(t) ((t) == LUA_TMARK) typedef union { - struct TString *ts; /* TAG_STRING, TAG_USERDATA */ - struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */ - struct Hash *a; /* TAG_TABLE */ - struct CallInfo *i; /* TAG_LMARK */ - Number n; /* TAG_NUMBER */ + struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */ + struct Closure *cl; /* LUA_TFUNCTION */ + struct Hash *a; /* LUA_TTABLE */ + struct CallInfo *i; /* LUA_TLMARK */ + Number n; /* LUA_TNUMBER */ } Value; @@ -80,7 +63,7 @@ typedef union { typedef struct lua_TObject { - lua_Tag ttype; + int ttype; Value value; } TObject; @@ -150,11 +133,15 @@ typedef struct Closure { } f; struct Closure *next; struct Closure *mark; /* marked closures (point to itself when not marked) */ - int nupvalues; + short isC; /* 0 for Lua functions, 1 for C functions */ + short nupvalues; TObject upvalue[1]; } Closure; +#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC) + + typedef struct Node { TObject key; TObject val; @@ -189,12 +176,12 @@ typedef struct CallInfo { } CallInfo; -extern const lua_Type luaO_typearr[]; extern const TObject luaO_nilobject; +extern const char *const luaO_typenames[]; + + +#define luaO_typename(o) (luaO_typenames[ttype(o)]) -#define luaO_tag2type(t) (luaO_typearr[(int)(t)]) -#define luaO_type(o) (luaO_tag2type(ttype(o))) -#define luaO_typename(L, o) (lua_typename(L, luaO_type(o))) lint32 luaO_power2 (lint32 n); char *luaO_openspace (lua_State *L, size_t n); diff --git a/lstrlib.c b/lstrlib.c index 52caae20..c79a4011 100644 --- a/lstrlib.c +++ b/lstrlib.c @@ -1,5 +1,5 @@ /* -** $Id: lstrlib.c,v 1.52 2000/09/11 17:38:42 roberto Exp roberto $ +** $Id: lstrlib.c,v 1.53 2000/09/14 14:09:31 roberto Exp roberto $ ** Standard library for string operations and pattern-matching ** See Copyright Notice in lua.h */ @@ -525,7 +525,7 @@ static void luaI_addquoted (lua_State *L, luaL_Buffer *b, int arg) { luaL_putchar(b, '"'); } -/* maximum size of each formated item (> len(format('%99.99f', -1e308))) */ +/* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */ #define MAX_ITEM 512 /* maximum size of each format specification (such as '%-099.99d') */ #define MAX_FORMAT 20 diff --git a/ltable.c b/ltable.c index 7a732e53..0e0fe772 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.55 2000/09/11 20:29:27 roberto Exp roberto $ +** $Id: ltable.c,v 1.56 2000/09/29 12:42:13 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -31,7 +31,7 @@ -#define TagDefault TAG_TABLE +#define TagDefault LUA_TTABLE @@ -42,19 +42,19 @@ Node *luaH_mainposition (const Hash *t, const TObject *key) { unsigned long h; switch (ttype(key)) { - case TAG_NUMBER: + case LUA_TNUMBER: h = (unsigned long)(long)nvalue(key); break; - case TAG_STRING: + case LUA_TSTRING: h = tsvalue(key)->u.s.hash; break; - case TAG_USERDATA: + case LUA_TUSERDATA: h = IntPoint(tsvalue(key)); break; - case TAG_TABLE: + case LUA_TTABLE: h = IntPoint(hvalue(key)); break; - case TAG_LCLOSURE: case TAG_CCLOSURE: + case LUA_TFUNCTION: h = IntPoint(clvalue(key)); break; default: @@ -84,7 +84,7 @@ static const TObject *luaH_getany (lua_State *L, const Hash *t, const TObject *luaH_getnum (const Hash *t, Number key) { Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; do { - if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key) + if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key) return &n->val; n = n->next; } while (n); @@ -96,7 +96,7 @@ const TObject *luaH_getnum (const Hash *t, Number key) { const TObject *luaH_getstr (const Hash *t, TString *key) { Node *n = &t->node[key->u.s.hash&(t->size-1)]; do { - if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key) + if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key) return &n->val; n = n->next; } while (n); @@ -106,8 +106,8 @@ const TObject *luaH_getstr (const Hash *t, TString *key) { const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { switch (ttype(key)) { - case TAG_NUMBER: return luaH_getnum(t, nvalue(key)); - case TAG_STRING: return luaH_getstr(t, tsvalue(key)); + case LUA_TNUMBER: return luaH_getnum(t, nvalue(key)); + case LUA_TSTRING: return luaH_getstr(t, tsvalue(key)); default: return luaH_getany(L, t, key); } } @@ -115,7 +115,7 @@ const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) { Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) { int i; - if (ttype(key) == TAG_NIL) + if (ttype(key) == LUA_TNIL) i = 0; /* first iteration */ else { const TObject *v = luaH_get(L, t, key); @@ -126,7 +126,7 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) { } for (; isize; i++) { Node *n = node(t, i); - if (ttype(val(n)) != TAG_NIL) + if (ttype(val(n)) != LUA_TNIL) return n; } return NULL; /* no more elements */ @@ -138,8 +138,8 @@ Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) { ** hash, change `key' for a number with the same hash. */ void luaH_remove (Hash *t, TObject *key) { - if (ttype(key) == TAG_NUMBER || - (ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30)) + if (ttype(key) == LUA_TNUMBER || + (ttype(key) == LUA_TSTRING && tsvalue(key)->u.s.len <= 30)) return; /* do not remove numbers nor small strings */ else { /* try to find a number `n' with the same hash as `key' */ @@ -151,7 +151,7 @@ void luaH_remove (Hash *t, TObject *key) { return; /* give up; (to avoid overflow) */ n += t->size; } - ttype(key) = TAG_NUMBER; + ttype(key) = LUA_TNUMBER; nvalue(key) = n; LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash"); } @@ -164,7 +164,7 @@ static void setnodevector (lua_State *L, Hash *t, lint32 size) { lua_error(L, "table overflow"); t->node = luaM_newvector(L, size, Node); for (i=0; i<(int)size; i++) { - ttype(&t->node[i].key) = ttype(&t->node[i].val) = TAG_NIL; + ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL; t->node[i].next = NULL; } L->nblocks += gcsize(L, size) - gcsize(L, t->size); @@ -200,7 +200,7 @@ static int numuse (const Hash *t) { int realuse = 0; int i; for (i=0; ival) != TAG_NIL) + if (ttype(&old->val) != LUA_TNIL) *luaH_set(L, t, &old->key) = old->val; } luaM_free(L, nold); /* free old array */ @@ -248,7 +248,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { else n = n->next; } while (n); /* `key' not found; must insert it */ - if (ttype(&mp->key) != TAG_NIL) { /* main position is not free? */ + if (ttype(&mp->key) != LUA_TNIL) { /* main position is not free? */ Node *othern; /* main position of colliding node */ n = t->firstfree; /* get a free place */ /* is colliding node out of its main position? (can only happens if @@ -269,7 +269,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { } mp->key = *key; for (;;) { /* correct `firstfree' */ - if (ttype(&t->firstfree->key) == TAG_NIL) + if (ttype(&t->firstfree->key) == LUA_TNIL) return &mp->val; /* OK; table still has a free place */ else if (t->firstfree == t->node) break; /* cannot decrement from here */ else (t->firstfree)--; @@ -281,7 +281,7 @@ TObject *luaH_set (lua_State *L, Hash *t, const TObject *key) { TObject *luaH_setint (lua_State *L, Hash *t, int key) { TObject index; - ttype(&index) = TAG_NUMBER; + ttype(&index) = LUA_TNUMBER; nvalue(&index) = key; return luaH_set(L, t, &index); } @@ -289,10 +289,10 @@ TObject *luaH_setint (lua_State *L, Hash *t, int key) { void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) { TObject *value, index; - ttype(&index) = TAG_STRING; + ttype(&index) = LUA_TSTRING; tsvalue(&index) = key; value = luaH_set(L, t, &index); - ttype(value) = TAG_NUMBER; + ttype(value) = LUA_TNUMBER; nvalue(value) = val; } diff --git a/ltests.c b/ltests.c index 70cbd78f..5ceebcb5 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.46 2000/10/02 14:47:43 roberto Exp roberto $ +** $Id: ltests.c,v 1.47 2000/10/02 20:10:55 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -93,7 +93,8 @@ static int listcode (lua_State *L) { int pc; Proto *p; int res; - luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected"); + luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); p = clvalue(luaA_index(L, 1))->f.l; lua_newtable(L); setnameval(L, "maxstack", p->maxstacksize); @@ -111,7 +112,8 @@ static int listcode (lua_State *L) { static int liststrings (lua_State *L) { Proto *p; int i; - luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected"); + luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); p = clvalue(luaA_index(L, 1))->f.l; lua_newtable(L); for (i=0; inkstr; i++) { @@ -128,7 +130,8 @@ static int listlocals (lua_State *L) { int pc = luaL_check_int(L, 2) - 1; int i = 0; const char *name; - luaL_arg_check(L, lua_tag(L, 1) == TAG_LCLOSURE, 1, "Lua function expected"); + luaL_arg_check(L, lua_isfunction(L, 1) && !lua_iscfunction(L, 1), + 1, "Lua function expected"); p = clvalue(luaA_index(L, 1))->f.l; while ((name = luaF_getlocalname(p, ++i, pc)) != NULL) lua_pushstring(L, name); @@ -177,7 +180,7 @@ static int mem_query (lua_State *L) { static int hash_query (lua_State *L) { if (lua_isnull(L, 2)) { - luaL_arg_check(L, lua_tag(L, 1) == TAG_STRING, 1, "string expected"); + luaL_arg_check(L, lua_tag(L, 1) == LUA_TSTRING, 1, "string expected"); lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash); } else { @@ -226,7 +229,7 @@ static int string_query (lua_State *L) { TString *ts; int n = 0; for (ts = tb->hash[s]; ts; ts = ts->nexthash) { - ttype(L->top) = TAG_STRING; + ttype(L->top) = LUA_TSTRING; tsvalue(L->top) = ts; incr_top; n++; diff --git a/ltm.c b/ltm.c index b009e8b4..1dfc8a72 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.51 2000/10/02 20:10:55 roberto Exp roberto $ +** $Id: ltm.c,v 1.52 2000/10/03 14:27:44 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -38,7 +38,7 @@ static int luaI_checkevent (lua_State *L, const char *name, int t) { int e = findevent(name); if (e >= IM_N) luaO_verror(L, "event `%.50s' is deprecated", name); - if (e == IM_GC && t == TAG_TABLE) + if (e == IM_GC && t == LUA_TTABLE) luaO_verror(L, "event `gc' for tables is deprecated"); if (e < 0) luaO_verror(L, "`%.50s' is not a valid event name", name); @@ -47,29 +47,28 @@ static int luaI_checkevent (lua_State *L, const char *name, int t) { -/* events in TAG_NIL are all allowed, since this is used as a +/* events in LUA_TNIL are all allowed, since this is used as a * 'placeholder' for "default" fallbacks */ /* ORDER LUA_T, ORDER IM */ static const char luaT_validevents[NUM_TAGS][IM_N] = { - {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_USERDATA */ - {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */ - {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */ - {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */ - {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */ - {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_CCLOSURE */ - {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */ + {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */ + {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */ + {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */ + {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */ + {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TTABLE */ + {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */ }; int luaT_validevent (int t, int e) { /* ORDER LUA_T */ - return (t > TAG_NIL) ? 1 : luaT_validevents[t][e]; + return (t >= NUM_TAGS) ? 1 : luaT_validevents[t][e]; } static void init_entry (lua_State *L, int tag) { int i; for (i=0; iIMtable[tag].collected = NULL; } @@ -100,7 +99,7 @@ static void checktag (lua_State *L, int tag) { } void luaT_realtag (lua_State *L, int tag) { - if (!(NUM_TAGS <= tag && tag <= L->last_tag)) + if (!validtag(tag)) luaO_verror(L, "tag %d was not created by `newtag'", tag); } @@ -117,20 +116,12 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) { } -const TObject *luaT_gettagmethods (lua_State *L, const TObject *o) { - lua_Tag t = ttype(o); +int luaT_tag (const TObject *o) { + int t = ttype(o); switch (t) { - case TAG_USERDATA: { - int tag = tsvalue(o)->u.d.tag; - if (tag > L->last_tag) - return L->IMtable[TAG_USERDATA].int_method; - else - return L->IMtable[tag].int_method; - } - case TAG_TABLE: - return L->IMtable[hvalue(o)->htag].int_method; - default: - return L->IMtable[(int)t].int_method;; + case LUA_TUSERDATA: return tsvalue(o)->u.d.tag; + case LUA_TTABLE: return hvalue(o)->htag; + default: return t; } } @@ -142,7 +133,7 @@ void lua_gettagmethod (lua_State *L, int t, const char *event) { if (luaT_validevent(t, e)) *L->top = *luaT_getim(L, t,e); else - ttype(L->top) = TAG_NIL; + ttype(L->top) = LUA_TNIL; incr_top; } @@ -156,8 +147,8 @@ void lua_settagmethod (lua_State *L, int t, const char *event) { checktag(L, t); if (!luaT_validevent(t, e)) luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", - luaT_eventname[e], lua_typename(L, luaO_tag2type(t)), - (t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag" + luaT_eventname[e], luaO_typenames[t], + (t == LUA_TTABLE || t == LUA_TUSERDATA) ? " with default tag" : ""); temp = *(L->top - 1); *(L->top - 1) = *luaT_getim(L, t,e); diff --git a/ltm.h b/ltm.h index 22974dc5..c6de9e36 100644 --- a/ltm.h +++ b/ltm.h @@ -1,5 +1,5 @@ /* -** $Id: ltm.h,v 1.15 2000/09/05 19:33:32 roberto Exp roberto $ +** $Id: ltm.h,v 1.16 2000/10/03 14:27:44 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -42,14 +42,17 @@ struct IM { #define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event]) -#define luaT_getimbyObj(L,o,e) (&luaT_gettagmethods((L),(o))[e]) +#define luaT_getimbyObj(L,o,e) (luaT_getim((L),luaT_tag(o),(e))) + + +#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag) extern const char *const luaT_eventname[]; void luaT_init (lua_State *L); void luaT_realtag (lua_State *L, int tag); -const TObject *luaT_gettagmethods (lua_State *L, const TObject *o); +int luaT_tag (const TObject *o); int luaT_validevent (int t, int e); /* used by compatibility module */ diff --git a/lua.h b/lua.h index 5257ecc9..0855c45a 100644 --- a/lua.h +++ b/lua.h @@ -1,5 +1,5 @@ /* -** $Id: lua.h,v 1.71 2000/10/02 14:47:43 roberto Exp roberto $ +** $Id: lua.h,v 1.72 2000/10/02 20:10:55 roberto Exp roberto $ ** Lua - An Extensible Extension Language ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** e-mail: lua@tecgraf.puc-rio.br @@ -47,11 +47,17 @@ typedef struct lua_State lua_State; typedef int (*lua_CFunction) (lua_State *L); +/* +** types returned by `lua_type' +*/ +#define LUA_TNONE (-1) -typedef enum lua_Type { - LUA_NOVALUE, LUA_TUSERDATA, LUA_TNUMBER, LUA_TSTRING, - LUA_TTABLE, LUA_TFUNCTION, LUA_TNIL -} lua_Type; +#define LUA_TUSERDATA 0 +#define LUA_TNIL 1 +#define LUA_TNUMBER 2 +#define LUA_TSTRING 3 +#define LUA_TTABLE 4 +#define LUA_TFUNCTION 5 @@ -77,8 +83,8 @@ int lua_stackspace (lua_State *L); ** access functions (stack -> C) */ -lua_Type lua_type (lua_State *L, int index); -const char *lua_typename (lua_State *L, lua_Type t); +int lua_type (lua_State *L, int index); +const char *lua_typename (lua_State *L, int t); int lua_isnumber (lua_State *L, int index); int lua_isstring (lua_State *L, int index); int lua_iscfunction (lua_State *L, int index); @@ -184,7 +190,7 @@ void lua_concat (lua_State *L, int n); #define lua_istable(L,n) (lua_type(L,n) == LUA_TTABLE) #define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA) #define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL) -#define lua_isnull(L,n) (lua_type(L,n) == LUA_NOVALUE) +#define lua_isnull(L,n) (lua_type(L,n) == LUA_TNONE) #endif @@ -224,3 +230,4 @@ void lua_concat (lua_State *L, int n); * * This implementation contains no third-party code. ******************************************************************************/ + diff --git a/lvm.c b/lvm.c index 98b87ce3..7b33be14 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.141 2000/10/03 14:27:44 roberto Exp roberto $ +** $Id: lvm.c,v 1.142 2000/10/04 12:16:08 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -40,25 +40,25 @@ int luaV_tonumber (TObject *obj) { - if (ttype(obj) != TAG_STRING) + if (ttype(obj) != LUA_TSTRING) return 1; else { if (!luaO_str2d(svalue(obj), &nvalue(obj))) return 2; - ttype(obj) = TAG_NUMBER; + ttype(obj) = LUA_TNUMBER; return 0; } } int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ - if (ttype(obj) != TAG_NUMBER) + if (ttype(obj) != LUA_TNUMBER) return 1; else { char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ lua_number2str(s, nvalue(obj)); /* convert `s' to number */ tsvalue(obj) = luaS_new(L, s); - ttype(obj) = TAG_STRING; + ttype(obj) = LUA_TSTRING; return 0; } } @@ -85,27 +85,29 @@ static void traceexec (lua_State *L, StkId base, StkId top, lua_Hook linehook) { } -static Closure *luaV_closure (lua_State *L, lua_Tag t, int nelems) { +static Closure *luaV_closure (lua_State *L, int nelems) { Closure *c = luaF_newclosure(L, nelems); L->top -= nelems; while (nelems--) c->upvalue[nelems] = *(L->top+nelems); - ttype(L->top) = t; clvalue(L->top) = c; + ttype(L->top) = LUA_TFUNCTION; incr_top; return c; } void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) { - Closure *cl = luaV_closure(L, TAG_CCLOSURE, nelems); + Closure *cl = luaV_closure(L, nelems); cl->f.c = c; + cl->isC = 1; } void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { - Closure *cl = luaV_closure(L, TAG_LCLOSURE, nelems); + Closure *cl = luaV_closure(L, nelems); cl->f.l = l; + cl->isC = 0; } @@ -116,21 +118,21 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { const TObject *luaV_gettable (lua_State *L, StkId t) { const TObject *im; int tg; - if (ttype(t) == TAG_TABLE && /* `t' is a table? */ - ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ - ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */ + if (ttype(t) == LUA_TTABLE && /* `t' is a table? */ + ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */ + ttype(luaT_getim(L, tg, IM_GETTABLE)) == LUA_TNIL)) { /* or no TM? */ /* do a primitive get */ const TObject *h = luaH_get(L, hvalue(t), L->top-1); /* result is no nil or there is no `index' tag method? */ - if (ttype(h) != TAG_NIL || - (ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL)) + if (ttype(h) != LUA_TNIL || + (ttype(im=luaT_getim(L, tg, IM_INDEX)) == LUA_TNIL)) return h; /* return result */ /* else call `index' tag method */ } - else { /* try a 'gettable' TM */ + else { /* try a `gettable' tag method */ im = luaT_getimbyObj(L, t, IM_GETTABLE); } - if (ttype(im) != TAG_NIL) { /* is there a tag method? */ + if (ttype(im) != LUA_TNIL) { /* is there a tag method? */ luaD_checkstack(L, 2); *(L->top+1) = *(L->top-1); /* key */ *L->top = *t; /* table */ @@ -151,13 +153,13 @@ const TObject *luaV_gettable (lua_State *L, StkId t) { */ void luaV_settable (lua_State *L, StkId t, StkId key) { int tg; - if (ttype(t) == TAG_TABLE && /* `t' is a table? */ - ((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ - ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */ + if (ttype(t) == LUA_TTABLE && /* `t' is a table? */ + ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */ + ttype(luaT_getim(L, tg, IM_SETTABLE)) == LUA_TNIL)) /* or no TM? */ *luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */ else { /* try a `settable' tag method */ const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE); - if (ttype(im) != TAG_NIL) { + if (ttype(im) != LUA_TNIL) { luaD_checkstack(L, 3); *(L->top+2) = *(L->top-1); *(L->top+1) = *key; @@ -175,12 +177,12 @@ void luaV_settable (lua_State *L, StkId t, StkId key) { const TObject *luaV_getglobal (lua_State *L, TString *s) { const TObject *value = luaH_getstr(L->gt, s); const TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); - if (ttype(im) == TAG_NIL) /* is there a tag method? */ + if (ttype(im) == LUA_TNIL) /* is there a tag method? */ return value; /* default behavior */ else { /* tag method */ luaD_checkstack(L, 3); *L->top = *im; - ttype(L->top+1) = TAG_STRING; + ttype(L->top+1) = LUA_TSTRING; tsvalue(L->top+1) = s; /* global name */ *(L->top+2) = *value; L->top += 3; @@ -193,14 +195,14 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) { void luaV_setglobal (lua_State *L, TString *s) { const TObject *oldvalue = luaH_getstr(L->gt, s); const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); - if (ttype(im) == TAG_NIL) { /* is there a tag method? */ + if (ttype(im) == LUA_TNIL) { /* is there a tag method? */ if (oldvalue != &luaO_nilobject) { /* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */ *(TObject *)oldvalue = *(L->top - 1); } else { TObject key; - ttype(&key) = TAG_STRING; + ttype(&key) = LUA_TSTRING; tsvalue(&key) = s; *luaH_set(L, L->gt, &key) = *(L->top - 1); } @@ -209,7 +211,7 @@ void luaV_setglobal (lua_State *L, TString *s) { luaD_checkstack(L, 3); *(L->top+2) = *(L->top-1); /* new value */ *(L->top+1) = *oldvalue; - ttype(L->top) = TAG_STRING; + ttype(L->top) = LUA_TSTRING; tsvalue(L->top) = s; *(L->top-1) = *im; L->top += 3; @@ -222,11 +224,11 @@ static int call_binTM (lua_State *L, StkId top, IMS event) { /* try first operand */ const TObject *im = luaT_getimbyObj(L, top-2, event); L->top = top; - if (ttype(im) == TAG_NIL) { + if (ttype(im) == LUA_TNIL) { im = luaT_getimbyObj(L, top-1, event); /* try second operand */ - if (ttype(im) == TAG_NIL) { + if (ttype(im) == LUA_TNIL) { im = luaT_getim(L, 0, event); /* try a `global' method */ - if (ttype(im) == TAG_NIL) + if (ttype(im) == LUA_TNIL) return 0; /* error */ } } @@ -238,7 +240,7 @@ static int call_binTM (lua_State *L, StkId top, IMS event) { static void call_arith (lua_State *L, StkId top, IMS event) { if (!call_binTM(L, top, event)) - luaG_binerror(L, top-2, TAG_NUMBER, "perform arithmetic on"); + luaG_binerror(L, top-2, LUA_TNUMBER, "perform arithmetic on"); } @@ -265,9 +267,9 @@ static int luaV_strcomp (const TString *ls, const TString *rs) { int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) { - if (ttype(l) == TAG_NUMBER && ttype(r) == TAG_NUMBER) + if (ttype(l) == LUA_TNUMBER && ttype(r) == LUA_TNUMBER) return (nvalue(l) < nvalue(r)); - else if (ttype(l) == TAG_STRING && ttype(r) == TAG_STRING) + else if (ttype(l) == LUA_TSTRING && ttype(r) == LUA_TSTRING) return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0); else { /* call TM */ luaD_checkstack(L, 2); @@ -276,7 +278,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) if (!call_binTM(L, top, IM_LT)) luaG_ordererror(L, top-2); L->top--; - return (ttype(L->top) != TAG_NIL); + return (ttype(L->top) != LUA_TNIL); } } @@ -286,7 +288,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) { int n = 2; /* number of elements handled in this pass (at least 2) */ if (tostring(L, top-2) || tostring(L, top-1)) { if (!call_binTM(L, top, IM_CONCAT)) - luaG_binerror(L, top-2, TAG_STRING, "concat"); + luaG_binerror(L, top-2, LUA_TSTRING, "concat"); } else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */ /* at least two string values; get as many as possible */ @@ -322,7 +324,7 @@ static void luaV_pack (lua_State *L, StkId firstelem) { /* store counter in field `n' */ luaH_setstrnum(L, htab, luaS_new(L, "n"), i); L->top = firstelem; /* remove elements from the stack */ - ttype(L->top) = TAG_TABLE; + ttype(L->top) = LUA_TTABLE; hvalue(L->top) = htab; incr_top; } @@ -393,7 +395,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { int n = GETARG_U(i); LUA_ASSERT(n>0, "invalid argument"); do { - ttype(top++) = TAG_NIL; + ttype(top++) = LUA_TNIL; } while (--n > 0); break; } @@ -402,25 +404,25 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_PUSHINT: { - ttype(top) = TAG_NUMBER; + ttype(top) = LUA_TNUMBER; nvalue(top) = (Number)GETARG_S(i); top++; break; } case OP_PUSHSTRING: { - ttype(top) = TAG_STRING; + ttype(top) = LUA_TSTRING; tsvalue(top) = kstr[GETARG_U(i)]; top++; break; } case OP_PUSHNUM: { - ttype(top) = TAG_NUMBER; + ttype(top) = LUA_TNUMBER; nvalue(top) = tf->knum[GETARG_U(i)]; top++; break; } case OP_PUSHNEGNUM: { - ttype(top) = TAG_NUMBER; + ttype(top) = LUA_TNUMBER; nvalue(top) = -tf->knum[GETARG_U(i)]; top++; break; @@ -446,7 +448,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_GETDOTTED: { - ttype(top) = TAG_STRING; + ttype(top) = LUA_TSTRING; tsvalue(top) = kstr[GETARG_U(i)]; L->top = top+1; *(top-1) = *luaV_gettable(L, top-1); @@ -461,7 +463,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { case OP_PUSHSELF: { TObject receiver; receiver = *(top-1); - ttype(top) = TAG_STRING; + ttype(top) = LUA_TSTRING; tsvalue(top++) = kstr[GETARG_U(i)]; L->top = top; *(top-2) = *luaV_gettable(L, top-2); @@ -472,7 +474,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { L->top = top; luaC_checkGC(L); hvalue(top) = luaH_new(L, GETARG_U(i)); - ttype(top) = TAG_TABLE; + ttype(top) = LUA_TTABLE; top++; break; } @@ -523,7 +525,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_ADDI: { if (tonumber(top-1)) { - ttype(top) = TAG_NUMBER; + ttype(top) = LUA_TNUMBER; nvalue(top) = (Number)GETARG_S(i); call_arith(L, top+1, IM_ADD); } @@ -571,7 +573,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_MINUS: { if (tonumber(top-1)) { - ttype(top) = TAG_NIL; + ttype(top) = LUA_TNIL; call_arith(L, top+1, IM_UNM); } else @@ -580,7 +582,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_NOT: { ttype(top-1) = - (ttype(top-1) == TAG_NIL) ? TAG_NUMBER : TAG_NIL; + (ttype(top-1) == LUA_TNIL) ? LUA_TNUMBER : LUA_TNIL; nvalue(top-1) = 1; break; } @@ -615,20 +617,20 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_JMPT: { - if (ttype(--top) != TAG_NIL) dojump(pc, i); + if (ttype(--top) != LUA_TNIL) dojump(pc, i); break; } case OP_JMPF: { - if (ttype(--top) == TAG_NIL) dojump(pc, i); + if (ttype(--top) == LUA_TNIL) dojump(pc, i); break; } case OP_JMPONT: { - if (ttype(top-1) == TAG_NIL) top--; + if (ttype(top-1) == LUA_TNIL) top--; else dojump(pc, i); break; } case OP_JMPONF: { - if (ttype(top-1) != TAG_NIL) top--; + if (ttype(top-1) != LUA_TNIL) top--; else dojump(pc, i); break; } @@ -637,7 +639,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_PUSHNILJMP: { - ttype(top++) = TAG_NIL; + ttype(top++) = LUA_TNIL; pc++; break; } @@ -657,9 +659,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_FORLOOP: { - LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step"); - LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit"); - if (ttype(top-3) != TAG_NUMBER) + LUA_ASSERT(ttype(top-1) == LUA_TNUMBER, "invalid step"); + LUA_ASSERT(ttype(top-2) == LUA_TNUMBER, "invalid limit"); + if (ttype(top-3) != LUA_TNUMBER) lua_error(L, "`for' index must be a number"); nvalue(top-3) += nvalue(top-1); /* increment index */ if (nvalue(top-1) > 0 ? @@ -672,7 +674,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_LFORPREP: { Node *node; - if (ttype(top-1) != TAG_TABLE) + if (ttype(top-1) != LUA_TTABLE) lua_error(L, "`for' table must be a table"); node = luaH_next(L, hvalue(top-1), &luaO_nilobject); if (node == NULL) { /* `empty' loop? */ @@ -688,7 +690,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_LFORLOOP: { Node *node; - LUA_ASSERT(ttype(top-3) == TAG_TABLE, "invalid table"); + LUA_ASSERT(ttype(top-3) == LUA_TTABLE, "invalid table"); node = luaH_next(L, hvalue(top-3), top-2); if (node == NULL) /* end loop? */ top -= 3; /* remove table, key, and value */ diff --git a/lvm.h b/lvm.h index e74eaf30..f43150b1 100644 --- a/lvm.h +++ b/lvm.h @@ -1,5 +1,5 @@ /* -** $Id: lvm.h,v 1.25 2000/08/31 21:02:55 roberto Exp roberto $ +** $Id: lvm.h,v 1.26 2000/09/05 19:33:32 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -13,8 +13,8 @@ #include "ltm.h" -#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0)) -#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0)) +#define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0)) +#define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0)) int luaV_tonumber (TObject *obj);