From 5037196f6fddb828056578b1c0d352cdef672d6a Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 5 Aug 2002 11:50:39 -0300 Subject: [PATCH] new macros `ttis*' --- lapi.c | 29 ++++++++++++++++++----------- lcode.c | 6 +++--- lgc.c | 14 +++++++------- lobject.h | 29 +++++++++++++++++++---------- ltable.c | 42 +++++++++++++++++++++--------------------- ltm.c | 4 ++-- 6 files changed, 70 insertions(+), 54 deletions(-) diff --git a/lapi.c b/lapi.c index ebb16a38..7d1831c8 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.204 2002/06/26 19:28:44 roberto Exp roberto $ +** $Id: lapi.c,v 1.205 2002/07/17 16:25:13 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -266,7 +266,7 @@ LUA_API const char *lua_tostring (lua_State *L, int index) { StkId o = luaA_indexAcceptable(L, index); if (o == NULL) return NULL; - else if (ttype(o) == LUA_TSTRING) + else if (ttisstring(o)) return svalue(o); else { const char *s; @@ -282,7 +282,7 @@ LUA_API size_t lua_strlen (lua_State *L, int index) { StkId o = luaA_indexAcceptable(L, index); if (o == NULL) return 0; - else if (ttype(o) == LUA_TSTRING) + else if (ttisstring(o)) return tsvalue(o)->tsv.len; else { size_t l; @@ -439,7 +439,7 @@ LUA_API void lua_rawget (lua_State *L, int index) { StkId t; lua_lock(L); t = luaA_index(L, index); - api_check(L, ttype(t) == LUA_TTABLE); + api_check(L, ttistable(t)); setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); lua_unlock(L); } @@ -449,7 +449,7 @@ LUA_API void lua_rawgeti (lua_State *L, int index, int n) { StkId o; lua_lock(L); o = luaA_index(L, index); - api_check(L, ttype(o) == LUA_TTABLE); + api_check(L, ttistable(o)); setobj(L->top, luaH_getnum(hvalue(o), n)); api_incr_top(L); lua_unlock(L); @@ -536,7 +536,7 @@ LUA_API void lua_rawset (lua_State *L, int index) { lua_lock(L); api_checknelems(L, 2); t = luaA_index(L, index); - api_check(L, ttype(t) == LUA_TTABLE); + api_check(L, ttistable(t)); setobj(luaH_set(L, hvalue(t), L->top-2), L->top-1); L->top -= 2; lua_unlock(L); @@ -548,7 +548,7 @@ LUA_API void lua_rawseti (lua_State *L, int index, int n) { lua_lock(L); api_checknelems(L, 1); o = luaA_index(L, index); - api_check(L, ttype(o) == LUA_TTABLE); + api_check(L, ttistable(o)); setobj(luaH_setnum(L, hvalue(o), n), L->top-1); L->top--; lua_unlock(L); @@ -561,8 +561,8 @@ LUA_API int lua_setmetatable (lua_State *L, int objindex) { lua_lock(L); api_checknelems(L, 1); obj = luaA_index(L, objindex); - mt = (ttype(L->top - 1) != LUA_TNIL) ? L->top - 1 : defaultmeta(L); - api_check(L, ttype(mt) == LUA_TTABLE); + mt = (!ttisnil(L->top - 1)) ? L->top - 1 : defaultmeta(L); + api_check(L, ttistable(mt)); switch (ttype(obj)) { case LUA_TTABLE: { hvalue(obj)->metatable = hvalue(mt); @@ -589,7 +589,7 @@ LUA_API int lua_setglobals (lua_State *L, int level) { api_checknelems(L, 1); f = getfunc(L, level); L->top--; - api_check(L, ttype(L->top) == LUA_TTABLE); + api_check(L, ttistable(L->top)); if (f) f->g = *(L->top); lua_unlock(L); return (f != NULL); @@ -619,6 +619,13 @@ LUA_API int lua_pcall (lua_State *L, int nargs, int nresults) { } +LUA_API void lua_pcallreset (lua_State *L) { + lua_lock(L); + luaD_resetprotection(L); /* reset error handler */ + lua_unlock(L); +} + + LUA_API int lua_load (lua_State *L, lua_Chunkreader reader, void *data, const char *chunkname) { ZIO z; @@ -688,7 +695,7 @@ LUA_API int lua_next (lua_State *L, int index) { int more; lua_lock(L); t = luaA_index(L, index); - api_check(L, ttype(t) == LUA_TTABLE); + api_check(L, ttistable(t)); more = luaH_next(L, hvalue(t), L->top - 1); if (more) { api_incr_top(L); diff --git a/lcode.c b/lcode.c index eadaef52..cd976cf0 100644 --- a/lcode.c +++ b/lcode.c @@ -1,5 +1,5 @@ /* -** $Id: lcode.c,v 1.107 2002/06/12 14:51:31 roberto Exp roberto $ +** $Id: lcode.c,v 1.108 2002/06/13 13:39:55 roberto Exp $ ** Code generator for Lua ** See Copyright Notice in lua.h */ @@ -206,7 +206,7 @@ static void freeexp (FuncState *fs, expdesc *e) { static int addk (FuncState *fs, TObject *k, TObject *v) { const TObject *index = luaH_get(fs->h, k); - if (ttype(index) == LUA_TNUMBER) { + if (ttisnumber(index)) { lua_assert(luaO_rawequalObj(&fs->f->k[cast(int, nvalue(index))], v)); return cast(int, nvalue(index)); } @@ -573,7 +573,7 @@ void luaK_indexed (FuncState *fs, expdesc *t, expdesc *k) { void luaK_prefix (FuncState *fs, UnOpr op, expdesc *e) { if (op == OPR_MINUS) { luaK_exp2val(fs, e); - if (e->k == VK && ttype(&fs->f->k[e->info]) == LUA_TNUMBER) + if (e->k == VK && ttisnumber(&fs->f->k[e->info])) e->info = luaK_numberK(fs, -nvalue(&fs->f->k[e->info])); else { luaK_exp2anyreg(fs, e); diff --git a/lgc.c b/lgc.c index 47513769..3b2c3fa4 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.142 2002/07/08 18:21:33 roberto Exp roberto $ +** $Id: lgc.c,v 1.143 2002/07/17 16:25:13 roberto Exp $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -71,7 +71,7 @@ static void protomark (Proto *f) { f->marked = 1; strmark(f->source); for (i=0; isizek; i++) { - if (ttype(f->k+i) == LUA_TSTRING) + if (ttisstring(f->k+i)) strmark(tsvalue(f->k+i)); } for (i=0; isizep; i++) @@ -148,7 +148,7 @@ static void checkstacksizes (lua_State *L, StkId max) { if (4*used < L->size_ci && 2*BASIC_CI_SIZE < L->size_ci) luaD_reallocCI(L, L->size_ci/2); /* still big enough... */ used = max - L->stack; /* part of stack in use */ - if (4*used < L->stacksize && 2*BASIC_STACK_SIZE < L->stacksize) + if (4*used < L->stacksize && 2*(BASIC_STACK_SIZE+EXTRA_STACK) < L->stacksize) luaD_reallocstack(L, L->stacksize/2); /* still big enough... */ } @@ -158,7 +158,7 @@ static void markstacks (GCState *st) { do { /* for each thread */ StkId o, lim; CallInfo *ci; - if (L1->base_ci == NULL) { /* incomplete state? */ + if (ttisnil(defaultmeta(L1))) { /* incomplete state? */ lua_assert(L1 != st->L); L1 = L1->next; luaE_closethread(st->L, L1->previous); /* collect it */ @@ -227,7 +227,7 @@ static void traversetable (GCState *st, Table *h) { marktable(st, h->metatable); lua_assert(h->lsizenode || h->node == G(st->L)->dummynode); mode = fasttm(st->L, h->metatable, TM_MODE); - if (mode && ttype(mode) == LUA_TSTRING) { /* weak table? */ + if (mode && ttisstring(mode)) { /* weak table? */ h->mark = st->toclear; /* must be cleared after GC, ... */ st->toclear = h; /* ...put in the appropriate list */ weakkey = (strchr(svalue(mode), 'k') != NULL); @@ -243,8 +243,8 @@ static void traversetable (GCState *st, Table *h) { i = sizenode(h); while (i--) { Node *n = node(h, i); - if (ttype(val(n)) != LUA_TNIL) { - lua_assert(ttype(key(n)) != LUA_TNIL); + if (!ttisnil(val(n))) { + lua_assert(!ttisnil(key(n))); if (!weakkey) markobject(st, key(n)); if (!weakvalue) markobject(st, val(n)); } diff --git a/lobject.h b/lobject.h index 0e800c16..e3dc6f95 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.139 2002/07/01 17:06:58 roberto Exp roberto $ +** $Id: lobject.h,v 1.140 2002/07/17 16:25:13 roberto Exp $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -33,18 +33,27 @@ typedef struct lua_TObject { } TObject; +/* Macros to test type */ +#define ttisnil(o) (ttype(o) == LUA_TNIL) +#define ttisnumber(o) (ttype(o) == LUA_TNUMBER) +#define ttisstring(o) (ttype(o) == LUA_TSTRING) +#define ttistable(o) (ttype(o) == LUA_TTABLE) +#define ttisfunction(o) (ttype(o) == LUA_TFUNCTION) +#define ttisboolean(o) (ttype(o) == LUA_TBOOLEAN) +#define ttisuserdata(o) (ttype(o) == LUA_TUSERDATA) +#define ttislightuserdata(o) (ttype(o) == LUA_TLIGHTUSERDATA) + /* Macros to access values */ #define ttype(o) ((o)->tt) -#define pvalue(o) check_exp(ttype(o)==LUA_TLIGHTUSERDATA, (o)->value.p) -#define nvalue(o) check_exp(ttype(o)==LUA_TNUMBER, (o)->value.n) -#define tsvalue(o) check_exp(ttype(o)==LUA_TSTRING, (o)->value.ts) -#define uvalue(o) check_exp(ttype(o)==LUA_TUSERDATA, (o)->value.u) -#define clvalue(o) check_exp(ttype(o)==LUA_TFUNCTION, (o)->value.cl) -#define hvalue(o) check_exp(ttype(o)==LUA_TTABLE, (o)->value.h) -#define bvalue(o) check_exp(ttype(o)==LUA_TBOOLEAN, (o)->value.b) +#define pvalue(o) check_exp(ttislightuserdata(o), (o)->value.p) +#define nvalue(o) check_exp(ttisnumber(o), (o)->value.n) +#define tsvalue(o) check_exp(ttisstring(o), (o)->value.ts) +#define uvalue(o) check_exp(ttisuserdata(o), (o)->value.u) +#define clvalue(o) check_exp(ttisfunction(o), (o)->value.cl) +#define hvalue(o) check_exp(ttistable(o), (o)->value.h) +#define bvalue(o) check_exp(ttisboolean(o), (o)->value.b) -#define l_isfalse(o) (ttype(o) == LUA_TNIL || \ - (ttype(o) == LUA_TBOOLEAN && bvalue(o) == 0)) +#define l_isfalse(o) (ttisnil(o) || (ttisboolean(o) && bvalue(o) == 0)) /* Macros to set values */ #define setnvalue(obj,x) \ diff --git a/ltable.c b/ltable.c index 2951def0..576c615e 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.113 2002/07/02 17:54:23 roberto Exp roberto $ +** $Id: ltable.c,v 1.114 2002/07/17 16:25:13 roberto Exp $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -99,7 +99,7 @@ Node *luaH_mainposition (const Table *t, const TObject *key) { ** the array part of the table, -1 otherwise. */ static int arrayindex (const TObject *key) { - if (ttype(key) == LUA_TNUMBER) { + if (ttisnumber(key)) { int k; lua_number2int(k, (nvalue(key))); if (cast(lua_Number, k) == nvalue(key) && k >= 1 && !toobig(k)) @@ -116,7 +116,7 @@ static int arrayindex (const TObject *key) { */ static int luaH_index (lua_State *L, Table *t, const TObject *key) { int i; - if (ttype(key) == LUA_TNIL) return -1; /* first iteration */ + if (ttisnil(key)) return -1; /* first iteration */ i = arrayindex(key); if (0 <= i && i <= t->sizearray) { /* is `key' inside array part? */ return i-1; /* yes; that's the index (corrected to C) */ @@ -135,14 +135,14 @@ static int luaH_index (lua_State *L, Table *t, const TObject *key) { int luaH_next (lua_State *L, Table *t, TObject *key) { int i = luaH_index(L, t, key); /* find original element */ for (i++; i < t->sizearray; i++) { /* try first array part */ - if (ttype(&t->array[i]) != LUA_TNIL) { /* a non-nil value? */ + if (!ttisnil(&t->array[i])) { /* a non-nil value? */ setnvalue(key, i+1); setobj(key+1, &t->array[i]); return 1; } } for (i -= t->sizearray; i < sizenode(t); i++) { /* then hash part */ - if (ttype(val(node(t, i))) != LUA_TNIL) { /* a non-nil value? */ + if (!ttisnil(val(node(t, i)))) { /* a non-nil value? */ setobj(key, key(node(t, i))); setobj(key+1, val(node(t, i))); return 1; @@ -192,7 +192,7 @@ static void numuse (const Table *t, int *narray, int *nhash) { int from = to/2; if (to > t->sizearray) to = t->sizearray; for (; from < to; from++) - if (ttype(&t->array[from]) != LUA_TNIL) { + if (!ttisnil(&t->array[from])) { nums[i]++; totaluse++; } @@ -201,7 +201,7 @@ static void numuse (const Table *t, int *narray, int *nhash) { /* count elements in hash part */ i = sizenode(t); while (i--) { - if (ttype(val(&t->node[i])) != LUA_TNIL) { + if (!ttisnil(val(&t->node[i]))) { int k = arrayindex(key(&t->node[i])); if (k >= 0) { /* is `key' an appropriate array index? */ nums[luaO_log2(k-1)+1]++; /* count as such */ @@ -230,8 +230,8 @@ static void setnodevector (lua_State *L, Table *t, int lsize) { luaG_runerror(L, "table overflow"); if (lsize == 0) { /* no elements to hash part? */ t->node = G(L)->dummynode; /* use common `dummynode' */ - lua_assert(ttype(key(t->node)) == LUA_TNIL); /* assert invariants: */ - lua_assert(ttype(val(t->node)) == LUA_TNIL); + lua_assert(ttisnil(key(t->node))); /* assert invariants: */ + lua_assert(ttisnil(val(t->node))); lua_assert(t->node->next == NULL); /* (`dummynode' must be empty) */ } else { @@ -272,7 +272,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { t->sizearray = nasize; /* re-insert elements from vanishing slice */ for (i=nasize; iarray[i]) != LUA_TNIL) + if (!ttisnil(&t->array[i])) setobj(luaH_setnum(L, t, i+1), &t->array[i]); } /* shrink array */ @@ -281,7 +281,7 @@ static void resize (lua_State *L, Table *t, int nasize, int nhsize) { /* re-insert elements in hash part */ for (i = twoto(oldhsize) - 1; i >= 0; i--) { Node *old = nold+i; - if (ttype(val(old)) != LUA_TNIL) + if (!ttisnil(val(old))) setobj(luaH_set(L, t, key(old)), val(old)); } if (oldhsize) @@ -342,7 +342,7 @@ void luaH_remove (Table *t, Node *e) { else { if (e->next != NULL) ?? } - lua_assert(ttype(val(node)) == LUA_TNIL); + lua_assert(ttisnil(val(node))); setnilvalue(key(e)); /* clear node `e' */ e->next = NULL; } @@ -359,7 +359,7 @@ void luaH_remove (Table *t, Node *e) { static TObject *newkey (lua_State *L, Table *t, const TObject *key) { TObject *val; Node *mp = luaH_mainposition(t, key); - if (ttype(val(mp)) != LUA_TNIL) { /* main position is not free? */ + if (!ttisnil(val(mp))) { /* main position is not free? */ Node *othern = luaH_mainposition(t, key(mp)); /* `mp' of colliding node */ Node *n = t->firstfree; /* get a free place */ if (othern != mp) { /* is colliding node out of its main position? */ @@ -378,9 +378,9 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { } } setobj(key(mp), key); - lua_assert(ttype(val(mp)) == LUA_TNIL); + lua_assert(ttisnil(val(mp))); for (;;) { /* correct `firstfree' */ - if (ttype(key(t->firstfree)) == LUA_TNIL) + if (ttisnil(key(t->firstfree))) return val(mp); /* OK; table still has a free place */ else if (t->firstfree == t->node) break; /* cannot decrement from here */ else (t->firstfree)--; @@ -389,7 +389,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { setbvalue(val(mp), 0); /* avoid new key being removed */ rehash(L, t); /* grow table */ val = cast(TObject *, luaH_get(t, key)); /* get new position */ - lua_assert(ttype(val) == LUA_TBOOLEAN); + lua_assert(ttisboolean(val)); setnilvalue(val); return val; } @@ -399,7 +399,7 @@ static TObject *newkey (lua_State *L, Table *t, const TObject *key) { ** generic search function */ static const TObject *luaH_getany (Table *t, const TObject *key) { - if (ttype(key) == LUA_TNIL) return &luaO_nilobject; + if (ttisnil(key)) return &luaO_nilobject; else { Node *n = luaH_mainposition(t, key); do { /* check whether `key' is somewhere in the chain */ @@ -420,7 +420,7 @@ const TObject *luaH_getnum (Table *t, int key) { else { Node *n = hashnum(t, key); do { /* check whether `key' is somewhere in the chain */ - if (ttype(key(n)) == LUA_TNUMBER && nvalue(key(n)) == (lua_Number)key) + if (ttisnumber(key(n)) && nvalue(key(n)) == (lua_Number)key) return val(n); /* that's it */ else n = n->next; } while (n); @@ -435,7 +435,7 @@ const TObject *luaH_getnum (Table *t, int key) { const TObject *luaH_getstr (Table *t, TString *key) { Node *n = hashstr(t, key); do { /* check whether `key' is somewhere in the chain */ - if (ttype(key(n)) == LUA_TSTRING && tsvalue(key(n)) == key) + if (ttisstring(key(n)) && tsvalue(key(n)) == key) return val(n); /* that's it */ else n = n->next; } while (n); @@ -467,8 +467,8 @@ TObject *luaH_set (lua_State *L, Table *t, const TObject *key) { if (p != &luaO_nilobject) return cast(TObject *, p); else { - if (ttype(key) == LUA_TNIL) luaG_runerror(L, "table index is nil"); - else if (ttype(key) == LUA_TNUMBER && nvalue(key) != nvalue(key)) + if (ttisnil(key)) luaG_runerror(L, "table index is nil"); + else if (ttisnumber(key) && nvalue(key) != nvalue(key)) luaG_runerror(L, "table index is NaN"); return newkey(L, t, key); } diff --git a/ltm.c b/ltm.c index d9a8dcac..70fd9807 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.97 2002/06/25 19:17:22 roberto Exp roberto $ +** $Id: ltm.c,v 1.98 2002/07/17 16:25:13 roberto Exp $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -47,7 +47,7 @@ void luaT_init (lua_State *L) { const TObject *luaT_gettm (Table *events, TMS event, TString *ename) { const TObject *tm = luaH_getstr(events, ename); lua_assert(event <= TM_MODE); - if (ttype(tm) == LUA_TNIL) { /* no tag method? */ + if (ttisnil(tm)) { /* no tag method? */ events->flags |= (1u<