all accesses to TObjects done through macros

This commit is contained in:
Roberto Ierusalimschy
2001-01-18 13:59:09 -02:00
parent 619edfd9e4
commit f2c451d745
10 changed files with 173 additions and 185 deletions

62
lapi.c
View File

@@ -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 ** Lua API
** See Copyright Notice in lua.h ** 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) { TObject *luaA_index (lua_State *L, int index) {
return Index(L, 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) { void luaA_pushobject (lua_State *L, const TObject *o) {
*L->top = *o; setobj(L->top, o);
incr_top; 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) { LUA_API void lua_remove (lua_State *L, int index) {
StkId p = luaA_index(L, index); StkId p = luaA_index(L, index);
while (++p < L->top) *(p-1) = *p; while (++p < L->top) setobj(p-1, p);
L->top--; 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) { LUA_API void lua_insert (lua_State *L, int index) {
StkId p = luaA_index(L, index); StkId p = luaA_index(L, index);
StkId q; StkId q;
for (q = L->top; q>p; q--) for (q = L->top; q>p; q--) setobj(q, q-1);
*q = *(q-1); setobj(p, L->top);
*p = *L->top;
} }
LUA_API void lua_pushvalue (lua_State *L, int index) { 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); 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) { LUA_API void lua_pushnil (lua_State *L) {
ttype(L->top) = LUA_TNIL; setnilvalue(L->top);
api_incr_top(L); api_incr_top(L);
} }
LUA_API void lua_pushnumber (lua_State *L, lua_Number n) { LUA_API void lua_pushnumber (lua_State *L, lua_Number n) {
nvalue(L->top) = n; setnvalue(L->top, n);
ttype(L->top) = LUA_TNUMBER;
api_incr_top(L); api_incr_top(L);
} }
LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) { LUA_API void lua_pushlstring (lua_State *L, const char *s, size_t len) {
tsvalue(L->top) = luaS_newlstr(L, s, len); setsvalue(L->top, luaS_newlstr(L, s, len));
ttype(L->top) = LUA_TSTRING;
api_incr_top(L); api_incr_top(L);
} }
@@ -236,8 +232,7 @@ LUA_API void lua_pushusertag (lua_State *L, void *u, int tag) {
/* ORDER LUA_T */ /* ORDER LUA_T */
if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag))) if (!(tag == LUA_ANYTAG || tag == LUA_TUSERDATA || validtag(tag)))
luaO_verror(L, "invalid tag for a userdata (%d)", tag); luaO_verror(L, "invalid tag for a userdata (%d)", tag);
tsvalue(L->top) = luaS_createudata(L, u, tag); setuvalue(L->top, luaS_createudata(L, u, tag));
ttype(L->top) = LUA_TUSERDATA;
api_incr_top(L); 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) { LUA_API void lua_getglobal (lua_State *L, const char *name) {
StkId top = L->top; StkId top = L->top;
*top = *luaV_getglobal(L, luaS_new(L, name)); setobj(top, luaV_getglobal(L, luaS_new(L, name)));
L->top = top; L->top = top;
api_incr_top(L); 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) { LUA_API void lua_gettable (lua_State *L, int index) {
StkId t = Index(L, index); StkId t = Index(L, index);
StkId top = L->top; 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 */ 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) { LUA_API void lua_rawget (lua_State *L, int index) {
StkId t = Index(L, index); StkId t = Index(L, index);
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); 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) { LUA_API void lua_rawgeti (lua_State *L, int index, int n) {
StkId o = Index(L, index); StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); 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); api_incr_top(L);
} }
LUA_API void lua_getglobals (lua_State *L) { LUA_API void lua_getglobals (lua_State *L) {
hvalue(L->top) = L->gt; sethvalue(L->top, L->gt);
ttype(L->top) = LUA_TTABLE;
api_incr_top(L); api_incr_top(L);
} }
LUA_API int lua_getref (lua_State *L, int ref) { LUA_API int lua_getref (lua_State *L, int ref) {
if (ref == LUA_REFNIL) if (ref == LUA_REFNIL) {
ttype(L->top) = LUA_TNIL; setnilvalue(L->top);
}
else if (0 <= ref && ref < L->nref && else if (0 <= ref && ref < L->nref &&
(L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) (L->refArray[ref].st == LOCK || L->refArray[ref].st == HOLD)) {
*L->top = L->refArray[ref].o; setobj(L->top, &L->refArray[ref].o);
}
else else
return 0; return 0;
api_incr_top(L); 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) { LUA_API void lua_newtable (lua_State *L) {
hvalue(L->top) = luaH_new(L, 0); sethvalue(L->top, luaH_new(L, 0));
ttype(L->top) = LUA_TTABLE;
api_incr_top(L); 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) { LUA_API void lua_rawset (lua_State *L, int index) {
StkId t = Index(L, index); StkId t = Index(L, index);
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); 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; 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) { LUA_API void lua_rawseti (lua_State *L, int index, int n) {
StkId o = Index(L, index); StkId o = Index(L, index);
LUA_ASSERT(ttype(o) == LUA_TTABLE, "table expected"); 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--; L->top--;
} }
@@ -364,7 +359,7 @@ LUA_API int lua_ref (lua_State *L, int lock) {
MAX_INT, "reference table overflow"); MAX_INT, "reference table overflow");
ref = L->nref++; 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->refArray[ref].st = lock ? LOCK : HOLD;
} }
L->top--; L->top--;
@@ -442,8 +437,8 @@ LUA_API int lua_next (lua_State *L, int index) {
LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected"); LUA_ASSERT(ttype(t) == LUA_TTABLE, "table expected");
n = luaH_next(L, hvalue(t), luaA_index(L, -1)); n = luaH_next(L, hvalue(t), luaA_index(L, -1));
if (n) { if (n) {
*(L->top-1) = *key(n); setobj(L->top-1, key(n));
*L->top = *val(n); setobj(L->top, val(n));
api_incr_top(L); api_incr_top(L);
return 1; 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) { LUA_API void *lua_newuserdata (lua_State *L, size_t size) {
TString *ts = luaS_newudata(L, size, NULL); TString *ts = luaS_newudata(L, size, NULL);
tsvalue(L->top) = ts; setuvalue(L->top, ts);
ttype(L->top) = LUA_TUSERDATA;
api_incr_top(L); api_incr_top(L);
return ts->u.d.value; return ts->u.d.value;
} }

View File

@@ -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 ** Debug Interface
** See Copyright Notice in lua.h ** 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) { static void setnormalized (TObject *d, const TObject *s) {
if (ttype(s) == LUA_TMARK) { if (ttype(s) == LUA_TMARK) {
clvalue(d) = infovalue(s)->func; setclvalue(d, infovalue(s)->func);
ttype(d) = LUA_TFUNCTION;
} }
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) { static StkId aux_stackedfunction (lua_State *L, int level, StkId top) {
int i; int i;
for (i = (top-1) - L->stack; i>=0; 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) if (level == 0)
return L->stack+i; return L->stack+i;
level--; 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? */ if (!fp) return NULL; /* `f' is not a Lua function? */
name = luaF_getlocalname(fp, n, currentpc(f)); name = luaF_getlocalname(fp, n, currentpc(f));
if (!name || name[0] == '(') return NULL; /* `(' starts private locals */ if (!name || name[0] == '(') return NULL; /* `(' starts private locals */
*((f+1)+(n-1)) = *L->top; setobj((f+1)+(n-1), L->top);
return name; return name;
} }

25
ldo.c
View File

@@ -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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -74,7 +74,7 @@ void luaD_adjusttop (lua_State *L, StkId base, int extra) {
else { else {
luaD_checkstack(L, diff); luaD_checkstack(L, diff);
while (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) { static void luaD_openstack (lua_State *L, StkId pos) {
int i = L->top-pos; int i = L->top-pos;
while (i--) pos[i+1] = pos[i]; while (i--) setobj(pos+i+1, pos+i);
incr_top; 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 */ L->Cbase = base; /* new base for C function */
luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */ luaD_checkstack(L, nup+LUA_MINSTACK); /* ensure minimum stack size */
for (n=0; n<nup; n++) /* copy upvalues as extra arguments */ for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
*(L->top++) = cl->upvalue[n]; setobj(L->top++, &cl->upvalue[n]);
n = (*cl->f.c)(L); /* do the actual call */ n = (*cl->f.c)(L); /* do the actual call */
L->Cbase = old_Cbase; /* restore old C base */ L->Cbase = old_Cbase; /* restore old C base */
return L->top - n; /* return index of first result */ 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) { void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults) {
StkId base = L->top - nParams; StkId base = L->top - nParams;
luaD_openstack(L, base); luaD_openstack(L, base);
clvalue(base) = f; setclvalue(base, f);
ttype(base) = LUA_TFUNCTION;
luaD_call(L, base, nResults); luaD_call(L, base, nResults);
} }
@@ -166,13 +165,11 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
if (tm == NULL) if (tm == NULL)
luaG_typeerror(L, func, "call"); luaG_typeerror(L, func, "call");
luaD_openstack(L, func); luaD_openstack(L, func);
clvalue(func) = tm; /* tag method is the new function to be called */ setclvalue(func, tm); /* tag method is the new function to be called */
ttype(func) = LUA_TFUNCTION;
} }
cl = clvalue(func); cl = clvalue(func);
ci.func = cl; ci.func = cl;
infovalue(func) = &ci; setivalue(func, &ci);
ttype(func) = LUA_TMARK;
callhook = L->callhook; callhook = L->callhook;
if (callhook) if (callhook)
luaD_callHook(L, func, callhook, "call"); 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) */ /* move results to `func' (to erase parameters and function) */
if (nResults == LUA_MULTRET) { if (nResults == LUA_MULTRET) {
while (firstResult < L->top) /* copy all results */ while (firstResult < L->top) /* copy all results */
*func++ = *firstResult++; setobj(func++, firstResult++);
L->top = func; L->top = func;
} }
else { /* copy at most `nResults' */ else { /* copy at most `nResults' */
for (; nResults > 0 && firstResult < L->top; nResults--) for (; nResults > 0 && firstResult < L->top; nResults--)
*func++ = *firstResult++; setobj(func++, firstResult++);
L->top = func; L->top = func;
for (; nResults > 0; nResults--) { /* if there are not enough results */ 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 */ incr_top; /* must check stack space */
} }
} }
@@ -334,7 +331,7 @@ struct lua_longjmp {
static void message (lua_State *L, const char *s) { static void message (lua_State *L, const char *s) {
const TObject *em = luaH_getstr(L->gt, luaS_newliteral(L, LUA_ERRORMESSAGE)); const TObject *em = luaH_getstr(L->gt, luaS_newliteral(L, LUA_ERRORMESSAGE));
if (ttype(em) == LUA_TFUNCTION) { if (ttype(em) == LUA_TFUNCTION) {
*L->top = *em; setobj(L->top, em);
incr_top; incr_top;
lua_pushstring(L, s); lua_pushstring(L, s);
luaD_call(L, L->top-2, 0); luaD_call(L, L->top-2, 0);

20
lgc.c
View File

@@ -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 ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -145,7 +145,7 @@ static void markall (lua_State *L) {
static int hasmark (const TObject *o) { static int hasmark (const TObject *o) {
/* valid only for locked objects */ /* valid only for locked objects */
switch (o->ttype) { switch (ttype(o)) {
case LUA_TSTRING: case LUA_TUSERDATA: case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(o)->marked; return tsvalue(o)->marked;
case LUA_TTABLE: case LUA_TTABLE:
@@ -290,15 +290,14 @@ static void checkMbuffer (lua_State *L) {
} }
static void callgcTM (lua_State *L, const TObject *o) { static void callgcTM (lua_State *L, const TObject *obj) {
Closure *tm = luaT_gettmbyObj(L, o, TM_GC); Closure *tm = luaT_gettmbyObj(L, obj, TM_GC);
if (tm != NULL) { if (tm != NULL) {
int oldah = L->allowhooks; int oldah = L->allowhooks;
L->allowhooks = 0; /* stop debug hooks during GC tag methods */ L->allowhooks = 0; /* stop debug hooks during GC tag methods */
luaD_checkstack(L, 2); luaD_checkstack(L, 2);
clvalue(L->top) = tm; setclvalue(L->top, tm);
ttype(L->top) = LUA_TFUNCTION; setobj(L->top+1, obj);
*(L->top+1) = *o;
L->top += 2; L->top += 2;
luaD_call(L, L->top-2, 0); luaD_call(L, L->top-2, 0);
L->allowhooks = oldah; /* restore hooks */ L->allowhooks = oldah; /* restore hooks */
@@ -308,15 +307,14 @@ static void callgcTM (lua_State *L, const TObject *o) {
static void callgcTMudata (lua_State *L) { static void callgcTMudata (lua_State *L) {
int tag; int tag;
TObject o;
ttype(&o) = LUA_TUSERDATA;
L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */ L->GCthreshold = 2*L->nblocks; /* avoid GC during tag methods */
for (tag=L->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */ for (tag=L->ntag-1; tag>=0; tag--) { /* for each tag (in reverse order) */
TString *udata; TString *udata;
while ((udata = L->TMtable[tag].collected) != NULL) { while ((udata = L->TMtable[tag].collected) != NULL) {
TObject obj;
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */ L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
tsvalue(&o) = udata; setuvalue(&obj, udata);
callgcTM(L, &o); callgcTM(L, &obj);
luaM_free(L, udata, sizeudata(udata->len)); luaM_free(L, udata, sizeudata(udata->len));
} }
} }

View File

@@ -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 ** Standard I/O (and system) library
** See Copyright Notice in lua.h ** 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) { 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 ? */ if (p != NULL) { /* is `f' a userdata ? */
int ftag = lua_tag(L, f); int ftag = lua_tag(L, f);
if (ftag == ctrl->iotag) /* does it have the correct tag? */ if (ftag == ctrl->iotag) /* does it have the correct tag? */
return (FILE *)p; return p;
else if (ftag == ctrl->closedtag) else if (ftag == ctrl->closedtag)
lua_error(L, "cannot access a closed file"); lua_error(L, "cannot access a closed file");
/* else go through */ /* else go through */
@@ -496,7 +496,7 @@ static int getfield (lua_State *L, const char *key, int d) {
lua_pushstring(L, key); lua_pushstring(L, key);
lua_rawget(L, -2); lua_rawget(L, -2);
if (lua_isnumber(L, -1)) if (lua_isnumber(L, -1))
res = lua_tonumber(L, -1); res = (int)lua_tonumber(L, -1);
else { else {
if (d == -2) if (d == -2)
luaL_verror(L, "field `%.20s' missing in date table", key); luaL_verror(L, "field `%.20s' missing in date table", key);

View File

@@ -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 ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -40,32 +40,61 @@
/* check whether `t' is a mark */ /* 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 { typedef union {
struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */ void *v;
struct Closure *cl; /* LUA_TFUNCTION */
struct Hash *a; /* LUA_TTABLE */
struct CallInfo *i; /* LUA_TLMARK */
lua_Number n; /* LUA_TNUMBER */ lua_Number n; /* LUA_TNUMBER */
} Value; } Value;
typedef struct lua_TObject {
int tt;
Value value;
} TObject;
/* Macros to access values */ /* Macros to access values */
#define ttype(o) ((o)->ttype) #define ttype(o) ((o)->tt)
#define nvalue(o) ((o)->value.n) #define nvalue(o) ((o)->value.n)
#define tsvalue(o) ((o)->value.ts) #define tsvalue(o) ((struct TString *)(o)->value.v)
#define clvalue(o) ((o)->value.cl) #define clvalue(o) ((struct Closure *)(o)->value.v)
#define hvalue(o) ((o)->value.a) #define hvalue(o) ((struct Hash *)(o)->value.v)
#define infovalue(o) ((o)->value.i) #define infovalue(o) ((struct CallInfo *)(o)->value.v)
#define svalue(o) (tsvalue(o)->str) #define svalue(o) (tsvalue(o)->str)
typedef struct lua_TObject { /* Macros to set values */
int ttype; #define setnvalue(obj,x) \
Value value; { TObject *o=(obj); o->tt=LUA_TNUMBER; o->value.n=(x); }
} TObject;
#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; }
/* /*

View File

@@ -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) ** Lua tables (hash)
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -143,8 +143,7 @@ void luaH_remove (Hash *t, TObject *key) {
return; /* give up; (to avoid overflow) */ return; /* give up; (to avoid overflow) */
n += t->size; n += t->size;
} }
ttype(key) = LUA_TNUMBER; setnvalue(key, n);
nvalue(key) = n;
LUA_ASSERT(luaH_mainposition(t, key) == mp, "cannot change hash"); 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"); lua_error(L, "table overflow");
t->node = luaM_newvector(L, size, Node); t->node = luaM_newvector(L, size, Node);
for (i=0; i<(int)size; i++) { 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->node[i].next = NULL;
} }
t->size = size; t->size = size;
@@ -212,7 +212,7 @@ static void rehash (lua_State *L, Hash *t) {
for (i=0; i<oldsize; i++) { for (i=0; i<oldsize; i++) {
Node *old = nold+i; Node *old = nold+i;
if (ttype(&old->val) != LUA_TNIL) if (ttype(&old->val) != 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 */ 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 = n;
} }
} }
mp->key = *key; setobj(&mp->key, key);
for (;;) { /* correct `firstfree' */ for (;;) { /* correct `firstfree' */
if (ttype(&t->firstfree->key) == LUA_TNIL) if (ttype(&t->firstfree->key) == LUA_TNIL)
return &mp->val; /* OK; table still has a free place */ 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; else n = n->next;
} while (n); } while (n);
/* `key' not found; must insert it */ /* `key' not found; must insert it */
ttype(&kobj) = LUA_TNUMBER; setnvalue(&kobj, key);
nvalue(&kobj) = key;
return newkey(L, t, mp, &kobj); return newkey(L, t, mp, &kobj);
} }
@@ -295,8 +294,7 @@ TObject *luaH_setstr (lua_State *L, Hash *t, TString *key) {
else n = n->next; else n = n->next;
} while (n); } while (n);
/* `key' not found; must insert it */ /* `key' not found; must insert it */
ttype(&kobj) = LUA_TSTRING; setsvalue(&kobj, key);
tsvalue(&kobj) = key;
return newkey(L, t, mp, &kobj); return newkey(L, t, mp, &kobj);
} }

View File

@@ -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 ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -226,8 +226,7 @@ static int string_query (lua_State *L) {
TString *ts; TString *ts;
int n = 0; int n = 0;
for (ts = tb->hash[s]; ts; ts = ts->nexthash) { for (ts = tb->hash[s]; ts; ts = ts->nexthash) {
ttype(L->top) = LUA_TSTRING; setsvalue(L->top, ts);
tsvalue(L->top) = ts;
incr_top; incr_top;
n++; n++;
} }

7
ltm.c
View File

@@ -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 ** Tag methods
** See Copyright Notice in lua.h ** 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); e = luaI_checkevent(L, event, t);
checktag(L, t); checktag(L, t);
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) { if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
clvalue(L->top) = luaT_gettm(L, t, e); setclvalue(L->top, luaT_gettm(L, t, e));
ttype(L->top) = LUA_TFUNCTION;
} }
else else
ttype(L->top) = LUA_TNIL; setnilvalue(L->top);
incr_top; incr_top;
} }

143
lvm.c
View File

@@ -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 ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -57,8 +57,7 @@ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
else { else {
char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */ char s[32]; /* 16 digits, sign, point and \0 (+ some extra...) */
lua_number2str(s, nvalue(obj)); /* convert `s' to number */ lua_number2str(s, nvalue(obj)); /* convert `s' to number */
tsvalue(obj) = luaS_new(L, s); setsvalue(obj, luaS_new(L, s));
ttype(obj) = LUA_TSTRING;
return 0; return 0;
} }
} }
@@ -89,9 +88,8 @@ static Closure *luaV_closure (lua_State *L, int nelems) {
Closure *c = luaF_newclosure(L, nelems); Closure *c = luaF_newclosure(L, nelems);
L->top -= nelems; L->top -= nelems;
while (nelems--) while (nelems--)
c->upvalue[nelems] = *(L->top+nelems); setobj(&c->upvalue[nelems], L->top+nelems);
clvalue(L->top) = c; setclvalue(L->top, c);
ttype(L->top) = LUA_TFUNCTION;
incr_top; incr_top;
return c; return c;
} }
@@ -133,10 +131,9 @@ const TObject *luaV_gettable (lua_State *L, StkId t) {
} }
if (tm != NULL) { /* is there a tag method? */ if (tm != NULL) { /* is there a tag method? */
luaD_checkstack(L, 2); luaD_checkstack(L, 2);
*(L->top+1) = *(L->top-1); /* key */ setobj(L->top+1, L->top-1); /* key */
*L->top = *t; /* table */ setobj(L->top, t); /* table */
clvalue(L->top-1) = tm; /* tag method */ setclvalue(L->top-1, tm); /* tag method */
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 2; L->top += 2;
luaD_call(L, L->top - 3, 1); luaD_call(L, L->top - 3, 1);
return L->top - 1; /* call result */ return L->top - 1; /* call result */
@@ -155,17 +152,17 @@ void luaV_settable (lua_State *L, StkId t, StkId key) {
int tg; int tg;
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */ if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */ ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
luaT_gettm(L, tg, TM_SETTABLE) == NULL)) /* or no TM? */ luaT_gettm(L, tg, TM_SETTABLE) == NULL)) { /* or no TM? */
*luaH_set(L, hvalue(t), key) = *(L->top-1); /* do a primitive set */ setobj(luaH_set(L, hvalue(t), key), L->top-1); /* do a primitive set */
}
else { /* try a `settable' tag method */ else { /* try a `settable' tag method */
Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE); Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE);
if (tm != NULL) { if (tm != NULL) {
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); setobj(L->top+2, L->top-1);
*(L->top+1) = *key; setobj(L->top+1, key);
*(L->top) = *t; setobj(L->top, t);
clvalue(L->top-1) = tm; setclvalue(L->top-1, tm);
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 3; L->top += 3;
luaD_call(L, L->top - 4, 0); /* call `settable' tag method */ 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 */ return value; /* default behavior */
else { /* tag method */ else { /* tag method */
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
clvalue(L->top) = tm; setclvalue(L->top, tm);
ttype(L->top) = LUA_TFUNCTION; setsvalue(L->top+1, s); /* global name */
tsvalue(L->top+1) = s; /* global name */ setobj(L->top+2, value);
ttype(L->top+1) = LUA_TSTRING;
*(L->top+2) = *value;
L->top += 3; L->top += 3;
luaD_call(L, L->top - 3, 1); luaD_call(L, L->top - 3, 1);
return L->top - 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) { void luaV_setglobal (lua_State *L, TString *s) {
TObject *oldvalue = luaH_setstr(L, L->gt, s); TObject *oldvalue = luaH_setstr(L, L->gt, s);
Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL); Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
if (tm == NULL) /* no tag methods? */ if (tm == NULL) { /* no tag methods? */
*oldvalue = *(L->top - 1); /* raw set */ setobj(oldvalue, L->top - 1); /* raw set */
}
else { /* call tag method */ else { /* call tag method */
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); /* new value */ setobj(L->top+2, L->top-1); /* new value */
*(L->top+1) = *oldvalue; setobj(L->top+1, oldvalue); /* old value */
ttype(L->top) = LUA_TSTRING; setsvalue(L->top, s); /* var name */
tsvalue(L->top) = s; setclvalue(L->top-1, tm); /* tag method */
clvalue(L->top-1) = tm;
ttype(L->top-1) = LUA_TFUNCTION;
L->top += 3; L->top += 3;
luaD_call(L, L->top - 4, 0); 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)); return luaV_strlessthan(tsvalue(l), tsvalue(r));
else { /* call TM */ else { /* call TM */
luaD_checkstack(L, 2); luaD_checkstack(L, 2);
*top++ = *l; setobj(top++, l);
*top++ = *r; setobj(top++, r);
if (!call_binTM(L, top, TM_LT)) if (!call_binTM(L, top, TM_LT))
luaG_ordererror(L, top-2); luaG_ordererror(L, top-2);
L->top--; L->top--;
@@ -301,7 +295,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
memcpy(buffer+tl, tsvalue(top-i)->str, l); memcpy(buffer+tl, tsvalue(top-i)->str, l);
tl += 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 */ total -= n-1; /* got `n' strings to create 1 new */
top -= n-1; 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) { static void luaV_pack (lua_State *L, StkId firstelem) {
TObject *nf;
int i; int i;
Hash *htab = luaH_new(L, 0); Hash *htab = luaH_new(L, 0);
for (i=0; firstelem+i<L->top; i++) for (i=0; firstelem+i<L->top; i++)
*luaH_setnum(L, htab, i+1) = *(firstelem+i); setobj(luaH_setnum(L, htab, i+1), firstelem+i);
/* store counter in field `n' */ /* store counter in field `n' */
nf = luaH_setstr(L, htab, luaS_newliteral(L, "n")); setnvalue(luaH_setstr(L, htab, luaS_newliteral(L, "n")), i);
ttype(nf) = LUA_TNUMBER;
nvalue(nf) = i;
L->top = firstelem; /* remove elements from the stack */ L->top = firstelem; /* remove elements from the stack */
ttype(L->top) = LUA_TTABLE; sethvalue(L->top, htab);
hvalue(L->top) = htab;
incr_top; incr_top;
} }
@@ -381,7 +371,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
int n = GETARG_U(i); int n = GETARG_U(i);
LUA_ASSERT(n>0, "invalid argument"); LUA_ASSERT(n>0, "invalid argument");
do { do {
ttype(top++) = LUA_TNIL; setnilvalue(top++);
} while (--n > 0); } while (--n > 0);
break; break;
} }
@@ -390,88 +380,74 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_PUSHINT: { case OP_PUSHINT: {
ttype(top) = LUA_TNUMBER; setnvalue(top++, (lua_Number)GETARG_S(i));
nvalue(top) = (lua_Number)GETARG_S(i);
top++;
break; break;
} }
case OP_PUSHSTRING: { case OP_PUSHSTRING: {
ttype(top) = LUA_TSTRING; setsvalue(top++, kstr[GETARG_U(i)]);
tsvalue(top) = kstr[GETARG_U(i)];
top++;
break; break;
} }
case OP_PUSHNUM: { case OP_PUSHNUM: {
ttype(top) = LUA_TNUMBER; setnvalue(top++, tf->knum[GETARG_U(i)]);
nvalue(top) = tf->knum[GETARG_U(i)];
top++;
break; break;
} }
case OP_PUSHNEGNUM: { case OP_PUSHNEGNUM: {
ttype(top) = LUA_TNUMBER; setnvalue(top++, -tf->knum[GETARG_U(i)]);
nvalue(top) = -tf->knum[GETARG_U(i)];
top++;
break; break;
} }
case OP_PUSHUPVALUE: { case OP_PUSHUPVALUE: {
*top++ = cl->upvalue[GETARG_U(i)]; setobj(top++, &cl->upvalue[GETARG_U(i)]);
break; break;
} }
case OP_GETLOCAL: { case OP_GETLOCAL: {
*top++ = *(base+GETARG_U(i)); setobj(top++, base+GETARG_U(i));
break; break;
} }
case OP_GETGLOBAL: { case OP_GETGLOBAL: {
L->top = top; L->top = top;
*top = *luaV_getglobal(L, kstr[GETARG_U(i)]); setobj(top++, luaV_getglobal(L, kstr[GETARG_U(i)]));
top++;
break; break;
} }
case OP_GETTABLE: { case OP_GETTABLE: {
L->top = top; L->top = top;
top--; top--;
*(top-1) = *luaV_gettable(L, top-1); setobj(top-1, luaV_gettable(L, top-1));
break; break;
} }
case OP_GETDOTTED: { case OP_GETDOTTED: {
ttype(top) = LUA_TSTRING; setsvalue(top, kstr[GETARG_U(i)]);
tsvalue(top) = kstr[GETARG_U(i)];
L->top = top+1; L->top = top+1;
*(top-1) = *luaV_gettable(L, top-1); setobj(top-1, luaV_gettable(L, top-1));
break; break;
} }
case OP_GETINDEXED: { case OP_GETINDEXED: {
*top = *(base+GETARG_U(i)); setobj(top, base+GETARG_U(i));
L->top = top+1; L->top = top+1;
*(top-1) = *luaV_gettable(L, top-1); setobj(top-1, luaV_gettable(L, top-1));
break; break;
} }
case OP_PUSHSELF: { case OP_PUSHSELF: {
TObject receiver; TObject receiver;
receiver = *(top-1); setobj(&receiver, top-1);
ttype(top) = LUA_TSTRING; setsvalue(top++, kstr[GETARG_U(i)]);
tsvalue(top++) = kstr[GETARG_U(i)];
L->top = top; L->top = top;
*(top-2) = *luaV_gettable(L, top-2); setobj(top-2, luaV_gettable(L, top-2));
*(top-1) = receiver; setobj(top-1, &receiver);
break; break;
} }
case OP_CREATETABLE: { case OP_CREATETABLE: {
L->top = top; L->top = top;
luaC_checkGC(L); luaC_checkGC(L);
hvalue(top) = luaH_new(L, GETARG_U(i)); sethvalue(top++, luaH_new(L, GETARG_U(i)));
ttype(top) = LUA_TTABLE;
top++;
break; break;
} }
case OP_SETLOCAL: { case OP_SETLOCAL: {
*(base+GETARG_U(i)) = *(--top); setobj(base+GETARG_U(i), --top);
break; break;
} }
case OP_SETGLOBAL: { case OP_SETGLOBAL: {
L->top = top; L->top = top--;
luaV_setglobal(L, kstr[GETARG_U(i)]); luaV_setglobal(L, kstr[GETARG_U(i)]);
top--;
break; break;
} }
case OP_SETTABLE: { case OP_SETTABLE: {
@@ -487,7 +463,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
Hash *arr = hvalue(top-n-1); Hash *arr = hvalue(top-n-1);
L->top = top-n; /* final value of `top' (in case of errors) */ L->top = top-n; /* final value of `top' (in case of errors) */
for (; n; n--) for (; n; n--)
*luaH_setnum(L, arr, n+aux) = *(--top); setobj(luaH_setnum(L, arr, n+aux), --top);
break; break;
} }
case OP_SETMAP: { 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) */ L->top = finaltop; /* final value of `top' (in case of errors) */
for (; n; n--) { for (; n; n--) {
top-=2; top-=2;
*luaH_set(L, arr, top) = *(top+1); setobj(luaH_set(L, arr, top), top+1);
} }
break; break;
} }
@@ -511,8 +487,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_ADDI: { case OP_ADDI: {
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = LUA_TNUMBER; setnvalue(top, (lua_Number)GETARG_S(i));
nvalue(top) = (lua_Number)GETARG_S(i);
call_arith(L, top+1, TM_ADD); call_arith(L, top+1, TM_ADD);
} }
else else
@@ -559,7 +534,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_MINUS: { case OP_MINUS: {
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = LUA_TNIL; setnilvalue(top);
call_arith(L, top+1, TM_UNM); call_arith(L, top+1, TM_UNM);
} }
else else
@@ -625,7 +600,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_PUSHNILJMP: { case OP_PUSHNILJMP: {
ttype(top++) = LUA_TNIL; setnilvalue(top++);
pc++; pc++;
break; break;
} }
@@ -669,8 +644,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
else { else {
top += 2; /* index,value */ top += 2; /* index,value */
*(top-2) = *key(node); setobj(top-2, key(node));
*(top-1) = *val(node); setobj(top-1, val(node));
} }
break; break;
} }
@@ -681,8 +656,8 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
if (node == NULL) /* end loop? */ if (node == NULL) /* end loop? */
top -= 3; /* remove table, key, and value */ top -= 3; /* remove table, key, and value */
else { else {
*(top-2) = *key(node); setobj(top-2, key(node));
*(top-1) = *val(node); setobj(top-1, val(node));
dojump(pc, i); /* repeat loop */ dojump(pc, i); /* repeat loop */
} }
break; break;