diff --git a/lapi.c b/lapi.c index 61302603..9c5bda01 100644 --- a/lapi.c +++ b/lapi.c @@ -1,5 +1,5 @@ /* -** $Id: lapi.c,v 1.115 2001/01/10 17:41:50 roberto Exp roberto $ +** $Id: lapi.c,v 1.116 2001/01/10 18:56:11 roberto Exp roberto $ ** Lua API ** See Copyright Notice in lua.h */ @@ -33,7 +33,6 @@ const char lua_ident[] = "$Lua: " LUA_VERSION " " LUA_COPYRIGHT " $\n" - TObject *luaA_index (lua_State *L, int index) { return Index(L, index); } @@ -50,7 +49,7 @@ static TObject *luaA_indexAcceptable (lua_State *L, int index) { void luaA_pushobject (lua_State *L, const TObject *o) { - *L->top = *o; + setobj(L->top, o); incr_top; } @@ -80,7 +79,7 @@ LUA_API void lua_settop (lua_State *L, int index) { LUA_API void lua_remove (lua_State *L, int index) { StkId p = luaA_index(L, index); - while (++p < L->top) *(p-1) = *p; + while (++p < L->top) setobj(p-1, p); L->top--; } @@ -88,14 +87,13 @@ LUA_API void lua_remove (lua_State *L, int index) { LUA_API void lua_insert (lua_State *L, int index) { StkId p = luaA_index(L, index); StkId q; - for (q = L->top; q>p; q--) - *q = *(q-1); - *p = *L->top; + for (q = L->top; q>p; q--) setobj(q, q-1); + setobj(p, L->top); } LUA_API void lua_pushvalue (lua_State *L, int index) { - *L->top = *luaA_index(L, index); + setobj(L->top, luaA_index(L, index)); api_incr_top(L); } @@ -200,21 +198,19 @@ LUA_API const void *lua_topointer (lua_State *L, int index) { LUA_API void lua_pushnil (lua_State *L) { - ttype(L->top) = LUA_TNIL; + setnilvalue(L->top); api_incr_top(L); } LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { - nvalue(L->top) = n; - ttype(L->top) = LUA_TNUMBER; + setnvalue(L->top, n); api_incr_top(L); } LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { - tsvalue(L->top) = luaS_newlstr(L, s, len); - ttype(L->top) = LUA_TSTRING; + setsvalue(L->top, luaS_newlstr(L, s, len)); api_incr_top(L); } @@ -236,8 +232,7 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */ 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) = LUA_TUSERDATA; + setuvalue(L->top, luaS_createudata(L, u, tag)); api_incr_top(L); } @@ -250,7 +245,7 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) { LUA_API void lua_getglobal (lua_State *L, const char *name) { StkId top = L->top; - *top = *luaV_getglobal(L, luaS_new(L, name)); + setobj(top, luaV_getglobal(L, luaS_new(L, name))); L->top = top; api_incr_top(L); } @@ -259,7 +254,7 @@ LUA_API void lua_getglobal (lua_State *L, const char *name) { LUA_API void lua_gettable (lua_State *L, int index) { StkId t = Index(L, index); StkId top = L->top; - *(top-1) = *luaV_gettable(L, t); + setobj(top-1, luaV_gettable(L, t)); L->top = top; /* tag method may change top */ } @@ -267,31 +262,32 @@ LUA_API void lua_gettable (lua_State *L, int index) { LUA_API void lua_rawget (lua_State *L, int index) { StkId t = Index(L, index); LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); - *(L->top - 1) = *luaH_get(hvalue(t), L->top - 1); + setobj(L->top - 1, luaH_get(hvalue(t), L->top - 1)); } LUA_API void lua_rawgeti (lua_State *L, int index, int n) { StkId o = Index(L, index); LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); - *L->top = *luaH_getnum(hvalue(o), n); + setobj(L->top, luaH_getnum(hvalue(o), n)); api_incr_top(L); } LUA_API void lua_getglobals (lua_State *L) { - hvalue(L->top) = L->gt; - ttype(L->top) = LUA_TTABLE; + sethvalue(L->top, L->gt); api_incr_top(L); } LUA_API int lua_getref (lua_State *L, int ref) { - if (ref == LUA_REFNIL) - ttype(L->top) = LUA_TNIL; + if (ref == LUA_REFNIL) { + setnilvalue(L->top); + } else if (0 <= ref && ref < L->nref && - (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) - *L->top = L->refArray[ref].o; + (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) { + setobj(L->top, &L->refArray[ref].o); + } else return 0; api_incr_top(L); @@ -300,8 +296,7 @@ LUA_API int lua_getref (lua_State *L, int ref) { LUA_API void lua_newtable (lua_State *L) { - hvalue(L->top) = luaH_new(L, 0); - ttype(L->top) = LUA_TTABLE; + sethvalue(L->top, luaH_new(L, 0)); api_incr_top(L); } @@ -330,7 +325,7 @@ LUA_API void lua_settable (lua_State *L, int index) { LUA_API void lua_rawset (lua_State *L, int index) { StkId t = Index(L, index); LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); - *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1); + setobj(luaH_set(L, hvalue(t), L->top-2), (L->top-1)); L->top -= 2; } @@ -338,7 +333,7 @@ LUA_API void lua_rawset (lua_State *L, int index) { LUA_API void lua_rawseti (lua_State *L, int index, int n) { StkId o = Index(L, index); LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); - *luaH_setnum(L, hvalue(o), n) = *(L->top-1); + setobj(luaH_setnum(L, hvalue(o), n), (L->top-1)); L->top--; } @@ -364,7 +359,7 @@ LUA_API int lua_ref (lua_State *L, int lock) { MAX_INT, "reference table overflow"); ref = L->nref++; } - L->refArray[ref].o = *(L->top-1); + setobj(&L->refArray[ref].o, L->top-1); L->refArray[ref].st = lock ? LOCK : HOLD; } L->top--; @@ -442,8 +437,8 @@ LUA_API int lua_next (lua_State *L, int index) { LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); n = luaH_next(L, hvalue(t), luaA_index(L, -1)); if (n) { - *(L->top-1) = *key(n); - *L->top = *val(n); + setobj(L->top-1, key(n)); + setobj(L->top, val(n)); api_incr_top(L); return 1; } @@ -485,8 +480,7 @@ LUA_API void lua_concat (lua_State *L, int n) { LUA_API void *lua_newuserdata (lua_State *L, size_t size) { TString *ts = luaS_newudata(L, size, NULL); - tsvalue(L->top) = ts; - ttype(L->top) = LUA_TUSERDATA; + setuvalue(L->top, ts); api_incr_top(L); return ts->u.d.value; } diff --git a/ldebug.c b/ldebug.c index b3cc80d8..c0969359 100644 --- a/ldebug.c +++ b/ldebug.c @@ -1,5 +1,5 @@ /* -** $Id: ldebug.c,v 1.51 2000/11/30 18:50:47 roberto Exp roberto $ +** $Id: ldebug.c,v 1.52 2000/12/26 18:46:09 roberto Exp roberto $ ** Debug Interface ** See Copyright Notice in lua.h */ @@ -29,10 +29,9 @@ static const char *getfuncname (lua_State *L, StkId f, const char **name); static void setnormalized (TObject *d, const TObject *s) { if (ttype(s) == LUA_TMARK) { - clvalue(d) = infovalue(s)->func; - ttype(d) = LUA_TFUNCTION; + setclvalue(d, infovalue(s)->func); } - else *d = *s; + else setobj(d, s); } @@ -58,7 +57,7 @@ LUA_API lua_Hook lua_setlinehook (lua_State *L, lua_Hook func) { static StkId aux_stackedfunction (lua_State *L, int level, StkId top) { int i; for (i = (top-1) - L->stack; i>=0; i--) { - if (is_T_MARK(L->stack[i].ttype)) { + if (is_T_MARK(&L->stack[i])) { if (level == 0) return L->stack+i; level--; @@ -168,7 +167,7 @@ LUA_API const char *lua_setlocal (lua_State *L, const lua_Debug *ar, int n) { if (!fp) return NULL; /* `f' is not a Lua function? */ name = luaF_getlocalname(fp, n, currentpc(f)); if (!name || name[0] == '(') return NULL; /* `(' starts private locals */ - *((f+1)+(n-1)) = *L->top; + setobj((f+1)+(n-1), L->top); return name; } diff --git a/ldo.c b/ldo.c index 46c397ea..7586cdf3 100644 --- a/ldo.c +++ b/ldo.c @@ -1,5 +1,5 @@ /* -** $Id: ldo.c,v 1.112 2001/01/10 16:58:11 roberto Exp roberto $ +** $Id: ldo.c,v 1.113 2001/01/10 18:56:11 roberto Exp roberto $ ** Stack and Call structure of Lua ** See Copyright Notice in lua.h */ @@ -74,7 +74,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) { else { luaD_checkstack(L, diff); while (diff--) - ttype(L->top++) = LUA_TNIL; + setnilvalue(L->top++); } } @@ -84,7 +84,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) { */ static void luaD_openstack (lua_State *L, StkId pos) { int i = L->top-pos; - while (i--) pos[i+1] = pos[i]; + while (i--) setobj(pos+i+1, pos+i); incr_top; } @@ -132,7 +132,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { L->Cbase = base; /* new base for C function */ luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ for (n=0; ntop++) = cl->upvalue[n]; + setobj(L->top++, &cl->upvalue[n]); n = (*cl->f.c)(L); /* do the actual call */ L->Cbase = old_Cbase; /* restore old C base */ return L->top - n; /* return index of first result */ @@ -142,8 +142,7 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) { void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) { StkId base = L->top - nParams; luaD_openstack(L, base); - clvalue(base) = f; - ttype(base) = LUA_TFUNCTION; + setclvalue(base, f); luaD_call(L, base, nResults); } @@ -166,13 +165,11 @@ void luaD_call (lua_State *L, StkId func, int nResults) { if (tm == NULL) luaG_typeerror(L, func, "call"); luaD_openstack(L, func); - clvalue(func) = tm; /* tag method is the new function to be called */ - ttype(func) = LUA_TFUNCTION; + setclvalue(func, tm); /* tag method is the new function to be called */ } cl = clvalue(func); ci.func = cl; - infovalue(func) = &ci; - ttype(func) = LUA_TMARK; + setivalue(func, &ci); callhook = L->callhook; if (callhook) luaD_callHook(L, func, callhook, "call"); @@ -184,15 +181,15 @@ void luaD_call (lua_State *L, StkId func, int nResults) { /* move results to `func' (to erase parameters and function) */ if (nResults == LUA_MULTRET) { while (firstResult < L->top) /* copy all results */ - *func++ = *firstResult++; + setobj(func++, firstResult++); L->top = func; } else { /* copy at most `nResults' */ for (; nResults > 0 && firstResult < L->top; nResults--) - *func++ = *firstResult++; + setobj(func++, firstResult++); L->top = func; for (; nResults > 0; nResults--) { /* if there are not enough results */ - ttype(L->top) = LUA_TNIL; /* adjust the stack */ + setnilvalue(L->top); /* adjust the stack */ incr_top; /* must check stack space */ } } @@ -334,7 +331,7 @@ struct lua_longjmp { static void message (lua_State *L, const char *s) { const TObject *em = luaH_getstr(L->gt, luaS_newliteral(L, LUA_ERRORMESSAGE)); if (ttype(em) == LUA_TFUNCTION) { - *L->top = *em; + setobj(L->top, em); incr_top; lua_pushstring(L, s); luaD_call(L, L->top-2, 0); diff --git a/lgc.c b/lgc.c index 8c63570a..90630cd7 100644 --- a/lgc.c +++ b/lgc.c @@ -1,5 +1,5 @@ /* -** $Id: lgc.c,v 1.74 2000/12/26 18:46:09 roberto Exp roberto $ +** $Id: lgc.c,v 1.75 2000/12/28 12:55:41 roberto Exp roberto $ ** Garbage Collector ** See Copyright Notice in lua.h */ @@ -145,7 +145,7 @@ static void markall (lua_State *L) { static int hasmark (const TObject *o) { /* valid only for locked objects */ - switch (o->ttype) { + switch (ttype(o)) { case LUA_TSTRING: case LUA_TUSERDATA: return tsvalue(o)->marked; case LUA_TTABLE: @@ -290,15 +290,14 @@ static void checkMbuffer (lua_State *L) { } -static void callgcTM (lua_State *L, const TObject *o) { - Closure *tm = luaT_gettmbyObj(L, o, TM_GC); +static void callgcTM (lua_State *L, const TObject *obj) { + Closure *tm = luaT_gettmbyObj(L, obj, TM_GC); if (tm != NULL) { int oldah = L->allowhooks; L->allowhooks = 0; /* stop debug hooks during GC tag methods */ luaD_checkstack(L, 2); - clvalue(L->top) = tm; - ttype(L->top) = LUA_TFUNCTION; - *(L->top+1) = *o; + setclvalue(L->top, tm); + setobj(L->top+1, obj); L->top += 2; luaD_call(L, L->top-2, 0); L->allowhooks = oldah; /* restore hooks */ @@ -308,15 +307,14 @@ static void callgcTM (lua_State *L, const TObject *o) { static void callgcTMudata (lua_State *L) { int tag; - TObject o; - ttype(&o) = LUA_TUSERDATA; L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */ for (tag=L->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */ TString *udata; while ((udata = L->TMtable[tag].collected) != NULL) { + TObject obj; L->TMtable[tag].collected = udata->nexthash; /* remove it from list */ - tsvalue(&o) = udata; - callgcTM(L, &o); + setuvalue(&obj, udata); + callgcTM(L, &obj); luaM_free(L, udata, sizeudata(udata->len)); } } diff --git a/liolib.c b/liolib.c index a2d96ca0..419cbf83 100644 --- a/liolib.c +++ b/liolib.c @@ -1,5 +1,5 @@ /* -** $Id: liolib.c,v 1.97 2001/01/10 16:58:11 roberto Exp roberto $ +** $Id: liolib.c,v 1.98 2001/01/11 18:59:03 roberto Exp roberto $ ** Standard I/O (and system) library ** See Copyright Notice in lua.h */ @@ -87,11 +87,11 @@ static int pushresult (lua_State *L, int i) { static FILE *gethandle (lua_State *L, IOCtrl *ctrl, int f) { - void *p = lua_touserdata(L, f); + FILE *p = (FILE *)lua_touserdata(L, f); if (p != NULL) { /* is `f' a userdata ? */ int ftag = lua_tag(L, f); if (ftag == ctrl->iotag) /* does it have the correct tag? */ - return (FILE *)p; + return p; else if (ftag == ctrl->closedtag) lua_error(L, "cannot access a closed file"); /* else go through */ @@ -496,7 +496,7 @@ static int getfield (lua_State *L, const char *key, int d) { lua_pushstring(L, key); lua_rawget(L, -2); if (lua_isnumber(L, -1)) - res = lua_tonumber(L, -1); + res = (int)lua_tonumber(L, -1); else { if (d == -2) luaL_verror(L, "field `%.20s' missing in date table", key); diff --git a/lobject.h b/lobject.h index 903833f0..ec27403c 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.84 2000/12/04 18:33:40 roberto Exp roberto $ +** $Id: lobject.h,v 1.85 2000/12/28 12:55:41 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -40,32 +40,61 @@ /* check whether `t' is a mark */ -#define is_T_MARK(t) ((t) == LUA_TMARK) +#define is_T_MARK(t) (ttype(t) == LUA_TMARK) typedef union { - struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */ - struct Closure *cl; /* LUA_TFUNCTION */ - struct Hash *a; /* LUA_TTABLE */ - struct CallInfo *i; /* LUA_TLMARK */ + void *v; lua_Number n; /* LUA_TNUMBER */ } Value; +typedef struct lua_TObject { + int tt; + Value value; +} TObject; + + /* Macros to access values */ -#define ttype(o) ((o)->ttype) +#define ttype(o) ((o)->tt) #define nvalue(o) ((o)->value.n) -#define tsvalue(o) ((o)->value.ts) -#define clvalue(o) ((o)->value.cl) -#define hvalue(o) ((o)->value.a) -#define infovalue(o) ((o)->value.i) +#define tsvalue(o) ((struct TString *)(o)->value.v) +#define clvalue(o) ((struct Closure *)(o)->value.v) +#define hvalue(o) ((struct Hash *)(o)->value.v) +#define infovalue(o) ((struct CallInfo *)(o)->value.v) #define svalue(o) (tsvalue(o)->str) -typedef struct lua_TObject { - int ttype; - Value value; -} TObject; +/* Macros to set values */ +#define setnvalue(obj,x) \ + { TObject *o=(obj); o->tt=LUA_TNUMBER; o->value.n=(x); } + +#define setsvalue(obj,x) \ + { TObject *o=(obj); struct TString *v=(x); \ + o->tt=LUA_TSTRING; o->value.v=v; } + +#define setuvalue(obj,x) \ + { TObject *o=(obj); struct TString *v=(x); \ + o->tt=LUA_TUSERDATA; o->value.v=v; } + +#define setclvalue(obj,x) \ + { TObject *o=(obj); struct Closure *v=(x); \ + o->tt=LUA_TFUNCTION; o->value.v=v; } + +#define sethvalue(obj,x) \ + { TObject *o=(obj); struct Hash *v=(x); \ + o->tt=LUA_TTABLE; o->value.v=v; } + +#define setivalue(obj,x) \ + { TObject *o=(obj); struct CallInfo *v=(x); \ + o->tt=LUA_TMARK; o->value.v=v; } + +#define setnilvalue(obj) { (obj)->tt=LUA_TNIL; } + +#define setobj(obj1,obj2) \ + { TObject *o1=(obj1); const TObject *o2=(obj2); \ + o1->tt=o2->tt; o1->value = o2->value; } + /* diff --git a/ltable.c b/ltable.c index ba1ccbe8..f8eb931b 100644 --- a/ltable.c +++ b/ltable.c @@ -1,5 +1,5 @@ /* -** $Id: ltable.c,v 1.62 2000/12/28 12:55:41 roberto Exp roberto $ +** $Id: ltable.c,v 1.63 2001/01/10 18:56:11 roberto Exp roberto $ ** Lua tables (hash) ** See Copyright Notice in lua.h */ @@ -143,8 +143,7 @@ void luaH_remove (Hash *t, TObject *key) { return; /* give up; (to avoid overflow) */ n += t->size; } - ttype(key) = LUA_TNUMBER; - nvalue(key) = n; + setnvalue(key, n); LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash"); } } @@ -156,7 +155,8 @@ static void setnodevector (lua_State *L, Hash *t, luint32 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) = LUA_TNIL; + setnilvalue(&t->node[i].key); + setnilvalue(&t->node[i].val); t->node[i].next = NULL; } t->size = size; @@ -212,7 +212,7 @@ static void rehash (lua_State *L, Hash *t) { for (i=0; ival) != LUA_TNIL) - *luaH_set(L, t, &old->key) = old->val; + setobj(luaH_set(L, t, &old->key), &old->val); } luaM_freearray(L, nold, oldsize, Node); /* free old array */ } @@ -243,7 +243,7 @@ static TObject *newkey (lua_State *L, Hash *t, Node *mp, const TObject *key) { mp = n; } } - mp->key = *key; + setobj(&mp->key, key); for (;;) { /* correct `firstfree' */ if (ttype(&t->firstfree->key) == LUA_TNIL) return &mp->val; /* OK; table still has a free place */ @@ -279,8 +279,7 @@ TObject *luaH_setnum (lua_State *L, Hash *t, lua_Number key) { else n = n->next; } while (n); /* `key' not found; must insert it */ - ttype(&kobj) = LUA_TNUMBER; - nvalue(&kobj) = key; + setnvalue(&kobj, key); return newkey(L, t, mp, &kobj); } @@ -295,8 +294,7 @@ TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) { else n = n->next; } while (n); /* `key' not found; must insert it */ - ttype(&kobj) = LUA_TSTRING; - tsvalue(&kobj) = key; + setsvalue(&kobj, key); return newkey(L, t, mp, &kobj); } diff --git a/ltests.c b/ltests.c index 97c63dfb..b767e2ca 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.55 2000/12/28 12:55:41 roberto Exp roberto $ +** $Id: ltests.c,v 1.56 2001/01/15 16:13:24 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -226,8 +226,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) = LUA_TSTRING; - tsvalue(L->top) = ts; + setsvalue(L->top, ts); incr_top; n++; } diff --git a/ltm.c b/ltm.c index 78cebeb2..fa13cbfd 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.58 2000/12/26 18:46:09 roberto Exp roberto $ +** $Id: ltm.c,v 1.59 2000/12/28 12:55:41 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -129,11 +129,10 @@ LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) { e = luaI_checkevent(L, event, t); checktag(L, t); if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) { - clvalue(L->top) = luaT_gettm(L, t, e); - ttype(L->top) = LUA_TFUNCTION; + setclvalue(L->top, luaT_gettm(L, t, e)); } else - ttype(L->top) = LUA_TNIL; + setnilvalue(L->top); incr_top; } diff --git a/lvm.c b/lvm.c index a589dd2c..fe2e8be2 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.152 2001/01/11 18:59:32 roberto Exp roberto $ +** $Id: lvm.c,v 1.153 2001/01/15 16:13:24 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -57,8 +57,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ 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) = LUA_TSTRING; + setsvalue(obj, luaS_new(L, s)); return 0; } } @@ -89,9 +88,8 @@ 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); - clvalue(L->top) = c; - ttype(L->top) = LUA_TFUNCTION; + setobj(&c->upvalue[nelems], L->top+nelems); + setclvalue(L->top, c); incr_top; return c; } @@ -133,10 +131,9 @@ const TObject *luaV_gettable (lua_State *L, StkId t) { } if (tm != NULL) { /* is there a tag method? */ luaD_checkstack(L, 2); - *(L->top+1) = *(L->top-1); /* key */ - *L->top = *t; /* table */ - clvalue(L->top-1) = tm; /* tag method */ - ttype(L->top-1) = LUA_TFUNCTION; + setobj(L->top+1, L->top-1); /* key */ + setobj(L->top, t); /* table */ + setclvalue(L->top-1, tm); /* tag method */ L->top += 2; luaD_call(L, L->top - 3, 1); return L->top - 1; /* call result */ @@ -155,17 +152,17 @@ void luaV_settable (lua_State *L, StkId t, StkId key) { int tg; if (ttype(t) == LUA_TTABLE && /* `t' is a table? */ ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */ - luaT_gettm(L, tg, TM_SETTABLE) == NULL)) /* or no TM? */ - *luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */ + luaT_gettm(L, tg, TM_SETTABLE) == NULL)) { /* or no TM? */ + setobj(luaH_set(L, hvalue(t), key), L->top-1); /* do a primitive set */ + } else { /* try a `settable' tag method */ Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE); if (tm != NULL) { luaD_checkstack(L, 3); - *(L->top+2) = *(L->top-1); - *(L->top+1) = *key; - *(L->top) = *t; - clvalue(L->top-1) = tm; - ttype(L->top-1) = LUA_TFUNCTION; + setobj(L->top+2, L->top-1); + setobj(L->top+1, key); + setobj(L->top, t); + setclvalue(L->top-1, tm); L->top += 3; luaD_call(L, L->top - 4, 0); /* call `settable' tag method */ } @@ -182,11 +179,9 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) { return value; /* default behavior */ else { /* tag method */ luaD_checkstack(L, 3); - clvalue(L->top) = tm; - ttype(L->top) = LUA_TFUNCTION; - tsvalue(L->top+1) = s; /* global name */ - ttype(L->top+1) = LUA_TSTRING; - *(L->top+2) = *value; + setclvalue(L->top, tm); + setsvalue(L->top+1, s); /* global name */ + setobj(L->top+2, value); L->top += 3; luaD_call(L, L->top - 3, 1); return L->top - 1; @@ -197,16 +192,15 @@ const TObject *luaV_getglobal (lua_State *L, TString *s) { void luaV_setglobal (lua_State *L, TString *s) { TObject *oldvalue = luaH_setstr(L, L->gt, s); Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL); - if (tm == NULL) /* no tag methods? */ - *oldvalue = *(L->top - 1); /* raw set */ + if (tm == NULL) { /* no tag methods? */ + setobj(oldvalue, L->top - 1); /* raw set */ + } else { /* call tag method */ luaD_checkstack(L, 3); - *(L->top+2) = *(L->top-1); /* new value */ - *(L->top+1) = *oldvalue; - ttype(L->top) = LUA_TSTRING; - tsvalue(L->top) = s; - clvalue(L->top-1) = tm; - ttype(L->top-1) = LUA_TFUNCTION; + setobj(L->top+2, L->top-1); /* new value */ + setobj(L->top+1, oldvalue); /* old value */ + setsvalue(L->top, s); /* var name */ + setclvalue(L->top-1, tm); /* tag method */ L->top += 3; luaD_call(L, L->top - 4, 0); } @@ -266,8 +260,8 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top) return luaV_strlessthan(tsvalue(l), tsvalue(r)); else { /* call TM */ luaD_checkstack(L, 2); - *top++ = *l; - *top++ = *r; + setobj(top++, l); + setobj(top++, r); if (!call_binTM(L, top, TM_LT)) luaG_ordererror(L, top-2); L->top--; @@ -301,7 +295,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) { memcpy(buffer+tl, tsvalue(top-i)->str, l); tl += l; } - tsvalue(top-n) = luaS_newlstr(L, buffer, tl); + setsvalue(top-n, luaS_newlstr(L, buffer, tl)); } total -= n-1; /* got `n' strings to create 1 new */ top -= n-1; @@ -310,18 +304,14 @@ void luaV_strconc (lua_State *L, int total, StkId top) { static void luaV_pack (lua_State *L, StkId firstelem) { - TObject *nf; int i; Hash *htab = luaH_new(L, 0); for (i=0; firstelem+itop; i++) - *luaH_setnum(L, htab, i+1) = *(firstelem+i); + setobj(luaH_setnum(L, htab, i+1), firstelem+i); /* store counter in field `n' */ - nf = luaH_setstr(L, htab, luaS_newliteral(L, "n")); - ttype(nf) = LUA_TNUMBER; - nvalue(nf) = i; + setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), i); L->top = firstelem; /* remove elements from the stack */ - ttype(L->top) = LUA_TTABLE; - hvalue(L->top) = htab; + sethvalue(L->top, htab); incr_top; } @@ -381,7 +371,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++) = LUA_TNIL; + setnilvalue(top++); } while (--n > 0); break; } @@ -390,88 +380,74 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_PUSHINT: { - ttype(top) = LUA_TNUMBER; - nvalue(top) = (lua_Number)GETARG_S(i); - top++; + setnvalue(top++, (lua_Number)GETARG_S(i)); break; } case OP_PUSHSTRING: { - ttype(top) = LUA_TSTRING; - tsvalue(top) = kstr[GETARG_U(i)]; - top++; + setsvalue(top++, kstr[GETARG_U(i)]); break; } case OP_PUSHNUM: { - ttype(top) = LUA_TNUMBER; - nvalue(top) = tf->knum[GETARG_U(i)]; - top++; + setnvalue(top++, tf->knum[GETARG_U(i)]); break; } case OP_PUSHNEGNUM: { - ttype(top) = LUA_TNUMBER; - nvalue(top) = -tf->knum[GETARG_U(i)]; - top++; + setnvalue(top++, -tf->knum[GETARG_U(i)]); break; } case OP_PUSHUPVALUE: { - *top++ = cl->upvalue[GETARG_U(i)]; + setobj(top++, &cl->upvalue[GETARG_U(i)]); break; } case OP_GETLOCAL: { - *top++ = *(base+GETARG_U(i)); + setobj(top++, base+GETARG_U(i)); break; } case OP_GETGLOBAL: { L->top = top; - *top = *luaV_getglobal(L, kstr[GETARG_U(i)]); - top++; + setobj(top++, luaV_getglobal(L, kstr[GETARG_U(i)])); break; } case OP_GETTABLE: { L->top = top; top--; - *(top-1) = *luaV_gettable(L, top-1); + setobj(top-1, luaV_gettable(L, top-1)); break; } case OP_GETDOTTED: { - ttype(top) = LUA_TSTRING; - tsvalue(top) = kstr[GETARG_U(i)]; + setsvalue(top, kstr[GETARG_U(i)]); L->top = top+1; - *(top-1) = *luaV_gettable(L, top-1); + setobj(top-1, luaV_gettable(L, top-1)); break; } case OP_GETINDEXED: { - *top = *(base+GETARG_U(i)); + setobj(top, base+GETARG_U(i)); L->top = top+1; - *(top-1) = *luaV_gettable(L, top-1); + setobj(top-1, luaV_gettable(L, top-1)); break; } case OP_PUSHSELF: { TObject receiver; - receiver = *(top-1); - ttype(top) = LUA_TSTRING; - tsvalue(top++) = kstr[GETARG_U(i)]; + setobj(&receiver, top-1); + setsvalue(top++, kstr[GETARG_U(i)]); L->top = top; - *(top-2) = *luaV_gettable(L, top-2); - *(top-1) = receiver; + setobj(top-2, luaV_gettable(L, top-2)); + setobj(top-1, &receiver); break; } case OP_CREATETABLE: { L->top = top; luaC_checkGC(L); - hvalue(top) = luaH_new(L, GETARG_U(i)); - ttype(top) = LUA_TTABLE; - top++; + sethvalue(top++, luaH_new(L, GETARG_U(i))); break; } case OP_SETLOCAL: { - *(base+GETARG_U(i)) = *(--top); + setobj(base+GETARG_U(i), --top); break; } case OP_SETGLOBAL: { - L->top = top; + L->top = top--; luaV_setglobal(L, kstr[GETARG_U(i)]); - top--; break; } case OP_SETTABLE: { @@ -487,7 +463,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { Hash *arr = hvalue(top-n-1); L->top = top-n; /* final value of `top' (in case of errors) */ for (; n; n--) - *luaH_setnum(L, arr, n+aux) = *(--top); + setobj(luaH_setnum(L, arr, n+aux), --top); break; } case OP_SETMAP: { @@ -497,7 +473,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { L->top = finaltop; /* final value of `top' (in case of errors) */ for (; n; n--) { top-=2; - *luaH_set(L, arr, top) = *(top+1); + setobj(luaH_set(L, arr, top), top+1); } break; } @@ -511,8 +487,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_ADDI: { if (tonumber(top-1)) { - ttype(top) = LUA_TNUMBER; - nvalue(top) = (lua_Number)GETARG_S(i); + setnvalue(top, (lua_Number)GETARG_S(i)); call_arith(L, top+1, TM_ADD); } else @@ -559,7 +534,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } case OP_MINUS: { if (tonumber(top-1)) { - ttype(top) = LUA_TNIL; + setnilvalue(top); call_arith(L, top+1, TM_UNM); } else @@ -625,7 +600,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { break; } case OP_PUSHNILJMP: { - ttype(top++) = LUA_TNIL; + setnilvalue(top++); pc++; break; } @@ -669,8 +644,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { } else { top += 2; /* index,value */ - *(top-2) = *key(node); - *(top-1) = *val(node); + setobj(top-2, key(node)); + setobj(top-1, val(node)); } break; } @@ -681,8 +656,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) { if (node == NULL) /* end loop? */ top -= 3; /* remove table, key, and value */ else { - *(top-2) = *key(node); - *(top-1) = *val(node); + setobj(top-2, key(node)); + setobj(top-1, val(node)); dojump(pc, i); /* repeat loop */ } break;