new definition for types-tags

This commit is contained in:
Roberto Ierusalimschy 2000-10-05 09:14:08 -03:00
parent cd2ddaded9
commit 001f2bdd0e
18 changed files with 308 additions and 332 deletions

77
lapi.c
View File

@ -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 ** Lua API
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -116,20 +116,18 @@ void lua_pushvalue (lua_State *L, int index) {
return ((test) ? (value) : (default)); } return ((test) ? (value) : (default)); }
lua_Type lua_type (lua_State *L, int index) { int lua_type (lua_State *L, int index) {
btest(L, index, luaO_type(o), LUA_NOVALUE); btest(L, index, ttype(o), LUA_TNONE);
} }
const char *lua_typename (lua_State *L, lua_Type t) { const char *lua_typename (lua_State *L, int t) {
static const char *const names[] = {
"NO VALUE", "userdata", "number", "string", "table", "function", "nil"
};
UNUSED(L); UNUSED(L);
return names[(int)t]; return luaO_typenames[t];
} }
int lua_iscfunction (lua_State *L, int index) { 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) { 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) { 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); 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) { 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) { 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) { 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) { 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) { 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) { 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) { const void *lua_topointer (lua_State *L, int index) {
StkId o = Index(L, index); StkId o = Index(L, index);
switch (ttype(o)) { switch (ttype(o)) {
case TAG_TABLE: case LUA_TTABLE:
return hvalue(o); return hvalue(o);
case TAG_CCLOSURE: case TAG_LCLOSURE: case LUA_TFUNCTION:
return clvalue(o); return clvalue(o);
default: return NULL; default: return NULL;
} }
@ -206,13 +199,13 @@ const void *lua_topointer (lua_State *L, int index) {
void lua_pushnil (lua_State *L) { void lua_pushnil (lua_State *L) {
ttype(L->top) = TAG_NIL; ttype(L->top) = LUA_TNIL;
api_incr_top(L); api_incr_top(L);
} }
void lua_pushnumber (lua_State *L, double n) { void lua_pushnumber (lua_State *L, double n) {
ttype(L->top) = TAG_NUMBER; ttype(L->top) = LUA_TNUMBER;
nvalue(L->top) = n; nvalue(L->top) = n;
api_incr_top(L); 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) { void lua_pushlstring (lua_State *L, const char *s, size_t len) {
tsvalue(L->top) = luaS_newlstr(L, s, len); tsvalue(L->top) = luaS_newlstr(L, s, len);
ttype(L->top) = TAG_STRING; ttype(L->top) = LUA_TSTRING;
api_incr_top(L); 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 */ 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); luaO_verror(L, "invalid tag for a userdata (%d)", tag);
tsvalue(L->top) = luaS_createudata(L, u, tag); tsvalue(L->top) = luaS_createudata(L, u, tag);
ttype(L->top) = TAG_USERDATA; ttype(L->top) = LUA_TUSERDATA;
api_incr_top(L); api_incr_top(L);
} }
@ -271,14 +264,14 @@ void lua_gettable (lua_State *L, int index) {
void lua_rawget (lua_State *L, int index) { void lua_rawget (lua_State *L, int index) {
StkId t = Index(L, 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); *(L->top - 1) = *luaH_get(L, hvalue(t), L->top - 1);
} }
void lua_rawgeti (lua_State *L, int index, int n) { void lua_rawgeti (lua_State *L, int index, int n) {
StkId o = Index(L, index); 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); *L->top = *luaH_getnum(hvalue(o), n);
api_incr_top(L); api_incr_top(L);
} }
@ -286,14 +279,14 @@ void lua_rawgeti (lua_State *L, int index, int n) {
void lua_getglobals (lua_State *L) { void lua_getglobals (lua_State *L) {
hvalue(L->top) = L->gt; hvalue(L->top) = L->gt;
ttype(L->top) = TAG_TABLE; ttype(L->top) = LUA_TTABLE;
api_incr_top(L); api_incr_top(L);
} }
int lua_getref (lua_State *L, int ref) { int lua_getref (lua_State *L, int ref) {
if (ref == LUA_REFNIL) if (ref == LUA_REFNIL)
ttype(L->top) = TAG_NIL; ttype(L->top) = LUA_TNIL;
else if (0 <= ref && ref < L->refSize && else if (0 <= ref && ref < L->refSize &&
(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; *L->top = L->refArray[ref].o;
@ -306,7 +299,7 @@ int lua_getref (lua_State *L, int ref) {
void lua_newtable (lua_State *L) { void lua_newtable (lua_State *L) {
hvalue(L->top) = luaH_new(L, 0); hvalue(L->top) = luaH_new(L, 0);
ttype(L->top) = TAG_TABLE; ttype(L->top) = LUA_TTABLE;
api_incr_top(L); api_incr_top(L);
} }
@ -334,7 +327,7 @@ void lua_settable (lua_State *L, int index) {
void lua_rawset (lua_State *L, int index) { void lua_rawset (lua_State *L, int index) {
StkId t = Index(L, 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); *luaH_set(L, hvalue(t), L->top-2) = *(L->top-1);
L->top -= 2; 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) { void lua_rawseti (lua_State *L, int index, int n) {
StkId o = Index(L, index); 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); *luaH_setint(L, hvalue(o), n) = *(L->top-1);
L->top--; L->top--;
} }
@ -350,14 +343,14 @@ void lua_rawseti (lua_State *L, int index, int n) {
void lua_setglobals (lua_State *L) { void lua_setglobals (lua_State *L) {
StkId newtable = --L->top; StkId newtable = --L->top;
LUA_ASSERT(ttype(newtable) == TAG_TABLE, "table expected"); LUA_ASSERT(ttype(newtable) == LUA_TTABLE, "table expected");
L->gt = hvalue(newtable); L->gt = hvalue(newtable);
} }
int lua_ref (lua_State *L, int lock) { int lua_ref (lua_State *L, int lock) {
int ref; int ref;
if (ttype(L->top-1) == TAG_NIL) if (ttype(L->top-1) == LUA_TNIL)
ref = LUA_REFNIL; ref = LUA_REFNIL;
else { else {
if (L->refFree != NONEXT) { /* is there a free place? */ 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) { void lua_settag (lua_State *L, int tag) {
luaT_realtag(L, tag); luaT_realtag(L, tag);
switch (ttype(L->top-1)) { switch (ttype(L->top-1)) {
case TAG_TABLE: case LUA_TTABLE:
hvalue(L->top-1)->htag = tag; hvalue(L->top-1)->htag = tag;
break; break;
case TAG_USERDATA: case LUA_TUSERDATA:
tsvalue(L->top-1)->u.d.tag = tag; tsvalue(L->top-1)->u.d.tag = tag;
break; break;
default: default:
luaO_verror(L, "cannot change the tag of a %.20s", luaO_verror(L, "cannot change the tag of a %.20s",
luaO_typename(L, L->top-1)); luaO_typename(L->top-1));
} }
L->top--; L->top--;
} }
@ -446,7 +439,7 @@ void lua_unref (lua_State *L, int ref) {
int lua_next (lua_State *L, int index) { int lua_next (lua_State *L, int index) {
StkId t = Index(L, index); StkId t = Index(L, index);
Node *n; 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)); n = luaH_next(L, hvalue(t), Index(L, -1));
if (n) { if (n) {
*(L->top-1) = *key(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) { int lua_getn (lua_State *L, int index) {
Hash *h = hvalue(Index(L, index)); Hash *h = hvalue(Index(L, index));
const TObject *value = luaH_getstr(h, luaS_new(L, "n")); /* value = h.n */ 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); return (int)nvalue(value);
else { else {
Number max = 0; Number max = 0;
int i = h->size; int i = h->size;
Node *n = h->node; Node *n = h->node;
while (i--) { while (i--) {
if (ttype(key(n)) == TAG_NUMBER && if (ttype(key(n)) == LUA_TNUMBER &&
ttype(val(n)) != TAG_NIL && ttype(val(n)) != LUA_TNIL &&
nvalue(key(n)) > max) nvalue(key(n)) > max)
max = nvalue(key(n)); max = nvalue(key(n));
n++; n++;

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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]; char buff[100];
const char *rt = lua_typename(L, lua_type(L, narg)); const char *rt = lua_typename(L, lua_type(L, narg));
if (*rt == 'N') rt = "no value"; 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) if (lua_type(L, narg) != t)
type_error(L, narg, t); type_error(L, narg, t);
} }
void luaL_checkany (lua_State *L, int narg) { 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"); luaL_argerror(L, narg, "value expected");
} }

View File

@ -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 ** Auxiliary functions for building Lua libraries
** See Copyright Notice in lua.h ** 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); double luaL_opt_number (lua_State *L, int numArg, double def);
void luaL_checkstack (lua_State *L, int space, const char *msg); 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_checkany (lua_State *L, int narg);
void luaL_verror (lua_State *L, const char *fmt, ...); void luaL_verror (lua_State *L, const char *fmt, ...);

View File

@ -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 ** Basic library
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -322,7 +322,7 @@ static int luaB_tostring (lua_State *L) {
case LUA_TNIL: case LUA_TNIL:
lua_pushstring(L, "nil"); lua_pushstring(L, "nil");
return 1; return 1;
case LUA_NOVALUE: default:
luaL_argerror(L, 1, "value expected"); luaL_argerror(L, 1, "value expected");
} }
lua_pushstring(L, buff); lua_pushstring(L, buff);

View File

@ -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 ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -23,23 +23,21 @@
#include "luadebug.h" #include "luadebug.h"
static const char *getfuncname (lua_State *L, StkId f, const char **name); 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) {
switch (s->ttype) { if (ttype(s) == LUA_TMARK) {
case TAG_CMARK: { clvalue(d) = infovalue(s)->func;
clvalue(d) = clvalue(s); ttype(d) = LUA_TFUNCTION;
ttype(d) = TAG_CCLOSURE;
break;
}
case TAG_LMARK: {
clvalue(d) = infovalue(s)->func;
ttype(d) = TAG_LCLOSURE;
break;
}
default: *d = *s;
} }
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) { static int lua_nups (StkId f) {
switch (ttype(f)) { switch (ttype(f)) {
case TAG_LCLOSURE: case TAG_CCLOSURE: case TAG_CMARK: case LUA_TFUNCTION:
return clvalue(f)->nupvalues; return clvalue(f)->nupvalues;
case TAG_LMARK: case LUA_TMARK:
return infovalue(f)->func->nupvalues; return infovalue(f)->func->nupvalues;
default: default:
return 0; return 0;
@ -125,13 +123,13 @@ int luaG_getline (int *lineinfo, int pc, int refline, int *prefi) {
static int lua_currentpc (StkId f) { static int lua_currentpc (StkId f) {
CallInfo *ci = infovalue(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; return (*ci->pc - ci->func->f.l->code) - 1;
} }
static int lua_currentline (StkId f) { static int lua_currentline (StkId f) {
if (ttype(f) != TAG_LMARK) if (!isLmark(f))
return -1; /* only active lua functions have current-line information */ return -1; /* only active lua functions have current-line information */
else { else {
CallInfo *ci = infovalue(f); CallInfo *ci = infovalue(f);
@ -143,7 +141,7 @@ static int lua_currentline (StkId f) {
static Proto *getluaproto (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)) { switch (ttype(func)) {
case TAG_LCLOSURE: case LUA_TFUNCTION:
infoLproto(ar, clvalue(func)->f.l); cl = clvalue(func);
break; break;
case TAG_LMARK: case LUA_TMARK:
infoLproto(ar, infovalue(func)->func->f.l); cl = infovalue(func)->func;
break;
case TAG_CCLOSURE: case TAG_CMARK:
ar->source = "(C)";
ar->linedefined = -1;
ar->what = "C";
break; break;
default: 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)); luaO_chunkid(ar->short_src, ar->source, sizeof(ar->short_src));
if (ar->linedefined == 0) if (ar->linedefined == 0)
ar->what = "main"; ar->what = "main";
@ -218,7 +219,7 @@ static const char *travglobals (lua_State *L, const TObject *o) {
int i; int i;
for (i=0; i<g->size; i++) { for (i=0; i<g->size; i++) {
if (luaO_equalObj(o, val(node(g, 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 tsvalue(key(node(g, i)))->str;
} }
return NULL; return NULL;
@ -250,7 +251,7 @@ int lua_getinfo (lua_State *L, const char *what, lua_Debug *ar) {
for (; *what; what++) { for (; *what; what++) {
switch (*what) { switch (*what) {
case 'S': { case 'S': {
lua_funcinfo(ar, func); lua_funcinfo(L, ar, func);
break; break;
} }
case 'l': { 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) { static const char *getobjname (lua_State *L, StkId obj, const char **name) {
StkId func = aux_stackedfunction(L, 0, obj); StkId func = aux_stackedfunction(L, 0, obj);
if (func == NULL || ttype(func) != TAG_LMARK) if (!isLmark(func))
return NULL; /* not a Lua function */ return NULL; /* not an active Lua function */
else { else {
Proto *p = infovalue(func)->func->f.l; Proto *p = infovalue(func)->func->f.l;
int pc = lua_currentpc(func); 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) { static const char *getfuncname (lua_State *L, StkId f, const char **name) {
StkId func = aux_stackedfunction(L, 0, f); /* calling function */ StkId func = aux_stackedfunction(L, 0, f); /* calling function */
if (func == NULL || ttype(func) != TAG_LMARK) if (!isLmark(func))
return NULL; /* not a Lua function */ return NULL; /* not an active Lua function */
else { else {
Proto *p = infovalue(func)->func->f.l; Proto *p = infovalue(func)->func->f.l;
int pc = lua_currentpc(func); 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) { void luaG_typeerror (lua_State *L, StkId o, const char *op) {
const char *name; const char *name;
const char *kind = getobjname(L, o, &name); const char *kind = getobjname(L, o, &name);
const char *t = luaO_typename(L, o); const char *t = luaO_typename(o);
if (kind) if (kind)
luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)", luaO_verror(L, "attempt to %.30s %.20s `%.40s' (a %.10s value)",
op, kind, name, t); 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++; if (ttype(p1) == t) p1++;
LUA_ASSERT(ttype(p1) != t, "must be an error"); LUA_ASSERT(ttype(p1) != t, "must be an error");
luaG_typeerror(L, p1, op); 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) { void luaG_ordererror (lua_State *L, StkId top) {
const char *t1 = luaO_typename(L, top-2); const char *t1 = luaO_typename(top-2);
const char *t2 = luaO_typename(L, top-1); const char *t2 = luaO_typename(top-1);
if (t1[2] == t2[2]) if (t1[2] == t2[2])
luaO_verror(L, "attempt to compare two %.10s values", t1); luaO_verror(L, "attempt to compare two %.10s values", t1);
else else

View File

@ -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 ** Auxiliary functions from Debug Interface module
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,7 +13,7 @@
void luaG_typeerror (lua_State *L, StkId o, const char *op); 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); int luaG_getline (int *lineinfo, int pc, int refline, int *refi);
void luaG_ordererror (lua_State *L, StkId top); void luaG_ordererror (lua_State *L, StkId top);

56
ldo.c
View File

@ -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 ** Stack and Call structure of Lua
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -73,7 +73,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++) = 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) { static void dohook (lua_State *L, lua_Debug *ar, lua_Hook hook) {
StkId old_Cbase = L->Cbase; StkId old_Cbase = L->Cbase;
StkId old_top = L->Cbase = L->top; 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 */ L->allowhooks = 0; /* cannot call hooks inside a hook */
(*hook)(L, ar); (*hook)(L, ar);
LUA_ASSERT(L->allowhooks == 0, "invalid allow"); 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; StkId old_Cbase = L->Cbase;
int n; int n;
L->Cbase = base; /* new base for C function */ 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; n<nup; n++) /* copy upvalues as extra arguments */ for (n=0; n<nup; n++) /* copy upvalues as extra arguments */
*(L->top++) = cl->upvalue[n]; *(L->top++) = cl->upvalue[n];
if (callhook) 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) { void luaD_call (lua_State *L, StkId func, int nResults) {
StkId firstResult; StkId firstResult;
retry: /* for `function' tag method */ CallInfo ci;
switch (ttype(func)) { Closure *cl;
case TAG_LCLOSURE: { if (ttype(func) != LUA_TFUNCTION) {
CallInfo ci; /* `func' is not a function; check the `function' tag method */
ci.func = clvalue(func); const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
infovalue(func) = &ci; if (ttype(im) == LUA_TNIL)
ttype(func) = TAG_LMARK; luaG_typeerror(L, func, "call");
firstResult = luaV_execute(L, ci.func, func+1); luaD_openstack(L, func);
LUA_ASSERT(ttype(func) == TAG_LMARK, "invalid tag"); *func = *im; /* tag method is the new function to be called */
break; LUA_ASSERT(ttype(func) == LUA_TFUNCTION, "invalid tag method");
}
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 */
}
} }
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 */ /* adjust the number of results */
if (nResults == LUA_MULTRET) if (nResults == LUA_MULTRET)
nResults = L->top - firstResult; nResults = L->top - firstResult;
@ -250,7 +244,7 @@ static int protectedparser (lua_State *L, ZIO *z, int bin) {
old_blocks = L->nblocks; old_blocks = L->nblocks;
status = luaD_runprotected(L, f_parser, &p); status = luaD_runprotected(L, f_parser, &p);
if (status == 0) { 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); L->GCthreshold += (L->nblocks - old_blocks);
} }
else if (status == LUA_ERRRUN) /* an error occurred: correct error code */ 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) { static void message (lua_State *L, const char *s) {
const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE); const TObject *em = luaH_getglobal(L, LUA_ERRORMESSAGE);
if (luaO_type(em) == LUA_TFUNCTION) { if (ttype(em) == LUA_TFUNCTION) {
*L->top = *em; *L->top = *em;
incr_top; incr_top;
lua_pushstring(L, s); lua_pushstring(L, s);

60
lgc.c
View File

@ -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 ** Garbage Collector
** See Copyright Notice in lua.h ** 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 */ /* 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)) { switch (ttype(o)) {
case TAG_USERDATA: case TAG_STRING: case LUA_TUSERDATA: case LUA_TSTRING:
strmark(tsvalue(o)); strmark(tsvalue(o));
break; 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))) { if (!ismarked(hvalue(o))) {
hvalue(o)->mark = st->tmark; /* chain it in list of marked */ hvalue(o)->mark = st->tmark; /* chain it in list of marked */
st->tmark = hvalue(o); st->tmark = hvalue(o);
} }
break; 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 */ default: break; /* numbers, etc */
} }
return 0;
} }
@ -131,8 +128,8 @@ static void markall (lua_State *L) {
st.tmark = h->mark; /* remove it from list */ st.tmark = h->mark; /* remove it from list */
for (i=0; i<h->size; i++) { for (i=0; i<h->size; i++) {
Node *n = node(h, i); Node *n = node(h, i);
if (ttype(key(n)) != TAG_NIL) { if (ttype(key(n)) != LUA_TNIL) {
if (ttype(val(n)) == TAG_NIL) if (ttype(val(n)) == LUA_TNIL)
luaH_remove(h, key(n)); /* dead element; try to remove it */ luaH_remove(h, key(n)); /* dead element; try to remove it */
markobject(&st, &n->key); markobject(&st, &n->key);
markobject(&st, &n->val); markobject(&st, &n->val);
@ -147,11 +144,11 @@ 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 (o->ttype) {
case TAG_STRING: case TAG_USERDATA: case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(o)->marked; return tsvalue(o)->marked;
case TAG_TABLE: case LUA_TTABLE:
return ismarked(hvalue(o)); return ismarked(hvalue(o));
case TAG_LCLOSURE: case TAG_CCLOSURE: case LUA_TFUNCTION:
return ismarked(clvalue(o)); return ismarked(clvalue(o));
default: /* number */ default: /* number */
return 1; return 1;
@ -271,7 +268,6 @@ static void collectudata (lua_State *L, int all) {
} }
else { /* collect */ else { /* collect */
int tag = next->u.d.tag; int tag = next->u.d.tag;
if (tag > L->last_tag) tag = TAG_USERDATA;
*p = next->nexthash; *p = next->nexthash;
next->nexthash = L->IMtable[tag].collected; /* chain udata */ next->nexthash = L->IMtable[tag].collected; /* chain udata */
L->IMtable[tag].collected = next; L->IMtable[tag].collected = next;
@ -297,7 +293,7 @@ static void checkMbuffer (lua_State *L) {
static void callgcTM (lua_State *L, const TObject *o) { static void callgcTM (lua_State *L, const TObject *o) {
const TObject *im = luaT_getimbyObj(L, o, IM_GC); const TObject *im = luaT_getimbyObj(L, o, IM_GC);
if (ttype(im) != TAG_NIL) { if (ttype(im) != LUA_TNIL) {
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);
@ -313,7 +309,7 @@ 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; TObject o;
ttype(&o) = TAG_USERDATA; 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->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */ for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
TString *udata; TString *udata;

View File

@ -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 ** Some generic functions over Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -18,16 +18,15 @@
const lua_Type luaO_typearr[] = { /* ORDER LUA_T */ const TObject luaO_nilobject = {LUA_TNIL, {NULL}};
LUA_TUSERDATA, LUA_TNUMBER, LUA_TSTRING, LUA_TTABLE,
LUA_TFUNCTION, LUA_TFUNCTION, LUA_TNIL
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) ** 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) { int luaO_equalObj (const TObject *t1, const TObject *t2) {
if (ttype(t1) != ttype(t2)) return 0; if (ttype(t1) != ttype(t2)) return 0;
switch (ttype(t1)) { switch (ttype(t1)) {
case TAG_NUMBER: case LUA_TNUMBER:
return nvalue(t1) == nvalue(t2); return nvalue(t1) == nvalue(t2);
case TAG_STRING: case TAG_USERDATA: case LUA_TSTRING: case LUA_TUSERDATA:
return tsvalue(t1) == tsvalue(t2); return tsvalue(t1) == tsvalue(t2);
case TAG_TABLE: case LUA_TTABLE:
return hvalue(t1) == hvalue(t2); return hvalue(t1) == hvalue(t2);
case TAG_CCLOSURE: case TAG_LCLOSURE: case LUA_TFUNCTION:
return clvalue(t1) == clvalue(t2); return clvalue(t1) == clvalue(t2);
default: default:
LUA_ASSERT(ttype(t1) == TAG_NIL, "invalid type"); LUA_ASSERT(ttype(t1) == LUA_TNIL, "invalid type");
return 1; /* TAG_NIL */ 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 */ /* this function needs to handle only '%d' and '%.XXs' formats */
void luaO_verror (lua_State *L, const char *fmt, ...) { void luaO_verror (lua_State *L, const char *fmt, ...) {
va_list argp; va_list argp;
char buff[600]; /* to hold formated message */ char buff[600]; /* to hold formatted message */
va_start(argp, fmt); va_start(argp, fmt);
vsprintf(buff, fmt, argp); vsprintf(buff, fmt, argp);
va_end(argp); va_end(argp);

View File

@ -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 ** Type definitions for Lua objects
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -31,41 +31,24 @@
#endif #endif
/* /* mark for closures active in the stack */
** Lua TYPES #define LUA_TMARK 6
** 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 */
TAG_LMARK, /* mark for Lua closures */
TAG_CMARK /* mark for C closures */
} lua_Tag;
/* tags for values visible from Lua == first user-created 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 */
** check whether `t' is a mark #define is_T_MARK(t) ((t) == LUA_TMARK)
*/
#define is_T_MARK(t) ((t) == TAG_LMARK || (t) == TAG_CMARK)
typedef union { typedef union {
struct TString *ts; /* TAG_STRING, TAG_USERDATA */ struct TString *ts; /* LUA_TSTRING, LUA_TUSERDATA */
struct Closure *cl; /* TAG_[CL]CLOSURE, TAG_CMARK */ struct Closure *cl; /* LUA_TFUNCTION */
struct Hash *a; /* TAG_TABLE */ struct Hash *a; /* LUA_TTABLE */
struct CallInfo *i; /* TAG_LMARK */ struct CallInfo *i; /* LUA_TLMARK */
Number n; /* TAG_NUMBER */ Number n; /* LUA_TNUMBER */
} Value; } Value;
@ -80,7 +63,7 @@ typedef union {
typedef struct lua_TObject { typedef struct lua_TObject {
lua_Tag ttype; int ttype;
Value value; Value value;
} TObject; } TObject;
@ -150,11 +133,15 @@ typedef struct Closure {
} f; } f;
struct Closure *next; struct Closure *next;
struct Closure *mark; /* marked closures (point to itself when not marked) */ 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]; TObject upvalue[1];
} Closure; } Closure;
#define iscfunction(o) (ttype(o) == LUA_TFUNCTION && clvalue(o)->isC)
typedef struct Node { typedef struct Node {
TObject key; TObject key;
TObject val; TObject val;
@ -189,12 +176,12 @@ typedef struct CallInfo {
} CallInfo; } CallInfo;
extern const lua_Type luaO_typearr[];
extern const TObject luaO_nilobject; 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); lint32 luaO_power2 (lint32 n);
char *luaO_openspace (lua_State *L, size_t n); char *luaO_openspace (lua_State *L, size_t n);

View File

@ -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 ** Standard library for string operations and pattern-matching
** See Copyright Notice in lua.h ** 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, '"'); 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 #define MAX_ITEM 512
/* maximum size of each format specification (such as '%-099.99d') */ /* maximum size of each format specification (such as '%-099.99d') */
#define MAX_FORMAT 20 #define MAX_FORMAT 20

View File

@ -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) ** Lua tables (hash)
** See Copyright Notice in lua.h ** 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) { Node *luaH_mainposition (const Hash *t, const TObject *key) {
unsigned long h; unsigned long h;
switch (ttype(key)) { switch (ttype(key)) {
case TAG_NUMBER: case LUA_TNUMBER:
h = (unsigned long)(long)nvalue(key); h = (unsigned long)(long)nvalue(key);
break; break;
case TAG_STRING: case LUA_TSTRING:
h = tsvalue(key)->u.s.hash; h = tsvalue(key)->u.s.hash;
break; break;
case TAG_USERDATA: case LUA_TUSERDATA:
h = IntPoint(tsvalue(key)); h = IntPoint(tsvalue(key));
break; break;
case TAG_TABLE: case LUA_TTABLE:
h = IntPoint(hvalue(key)); h = IntPoint(hvalue(key));
break; break;
case TAG_LCLOSURE: case TAG_CCLOSURE: case LUA_TFUNCTION:
h = IntPoint(clvalue(key)); h = IntPoint(clvalue(key));
break; break;
default: 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) { const TObject *luaH_getnum (const Hash *t, Number key) {
Node *n = &t->node[(unsigned long)(long)key&(t->size-1)]; Node *n = &t->node[(unsigned long)(long)key&(t->size-1)];
do { do {
if (ttype(&n->key) == TAG_NUMBER && nvalue(&n->key) == key) if (ttype(&n->key) == LUA_TNUMBER && nvalue(&n->key) == key)
return &n->val; return &n->val;
n = n->next; n = n->next;
} while (n); } while (n);
@ -96,7 +96,7 @@ const TObject *luaH_getnum (const Hash *t, Number key) {
const TObject *luaH_getstr (const Hash *t, TString *key) { const TObject *luaH_getstr (const Hash *t, TString *key) {
Node *n = &t->node[key->u.s.hash&(t->size-1)]; Node *n = &t->node[key->u.s.hash&(t->size-1)];
do { do {
if (ttype(&n->key) == TAG_STRING && tsvalue(&n->key) == key) if (ttype(&n->key) == LUA_TSTRING && tsvalue(&n->key) == key)
return &n->val; return &n->val;
n = n->next; n = n->next;
} while (n); } 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) { const TObject *luaH_get (lua_State *L, const Hash *t, const TObject *key) {
switch (ttype(key)) { switch (ttype(key)) {
case TAG_NUMBER: return luaH_getnum(t, nvalue(key)); case LUA_TNUMBER: return luaH_getnum(t, nvalue(key));
case TAG_STRING: return luaH_getstr(t, tsvalue(key)); case LUA_TSTRING: return luaH_getstr(t, tsvalue(key));
default: return luaH_getany(L, t, 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) { Node *luaH_next (lua_State *L, const Hash *t, const TObject *key) {
int i; int i;
if (ttype(key) == TAG_NIL) if (ttype(key) == LUA_TNIL)
i = 0; /* first iteration */ i = 0; /* first iteration */
else { else {
const TObject *v = luaH_get(L, t, key); 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 (; i<t->size; i++) { for (; i<t->size; i++) {
Node *n = node(t, i); Node *n = node(t, i);
if (ttype(val(n)) != TAG_NIL) if (ttype(val(n)) != LUA_TNIL)
return n; return n;
} }
return NULL; /* no more elements */ 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. ** hash, change `key' for a number with the same hash.
*/ */
void luaH_remove (Hash *t, TObject *key) { void luaH_remove (Hash *t, TObject *key) {
if (ttype(key) == TAG_NUMBER || if (ttype(key) == LUA_TNUMBER ||
(ttype(key) == TAG_STRING && tsvalue(key)->u.s.len <= 30)) (ttype(key) == LUA_TSTRING && tsvalue(key)->u.s.len <= 30))
return; /* do not remove numbers nor small strings */ return; /* do not remove numbers nor small strings */
else { else {
/* try to find a number `n' with the same hash as `key' */ /* 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) */ return; /* give up; (to avoid overflow) */
n += t->size; n += t->size;
} }
ttype(key) = TAG_NUMBER; ttype(key) = LUA_TNUMBER;
nvalue(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");
} }
@ -164,7 +164,7 @@ static void setnodevector (lua_State *L, Hash *t, lint32 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) = TAG_NIL; ttype(&t->node[i].key) = ttype(&t->node[i].val) = LUA_TNIL;
t->node[i].next = NULL; t->node[i].next = NULL;
} }
L->nblocks += gcsize(L, size) - gcsize(L, t->size); L->nblocks += gcsize(L, size) - gcsize(L, t->size);
@ -200,7 +200,7 @@ static int numuse (const Hash *t) {
int realuse = 0; int realuse = 0;
int i; int i;
for (i=0; i<size; i++) { for (i=0; i<size; i++) {
if (ttype(&v[i].val) != TAG_NIL) if (ttype(&v[i].val) != LUA_TNIL)
realuse++; realuse++;
} }
return realuse; return realuse;
@ -222,7 +222,7 @@ static void rehash (lua_State *L, Hash *t) {
setnodevector(L, t, oldsize); setnodevector(L, t, oldsize);
for (i=0; i<oldsize; i++) { for (i=0; i<oldsize; i++) {
Node *old = nold+i; Node *old = nold+i;
if (ttype(&old->val) != TAG_NIL) if (ttype(&old->val) != LUA_TNIL)
*luaH_set(L, t, &old->key) = old->val; *luaH_set(L, t, &old->key) = old->val;
} }
luaM_free(L, nold); /* free old array */ 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; else n = n->next;
} while (n); } while (n);
/* `key' not found; must insert it */ /* `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 */ Node *othern; /* main position of colliding node */
n = t->firstfree; /* get a free place */ n = t->firstfree; /* get a free place */
/* is colliding node out of its main position? (can only happens if /* 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; mp->key = *key;
for (;;) { /* correct `firstfree' */ 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 */ return &mp->val; /* OK; table still has a free place */
else if (t->firstfree == t->node) break; /* cannot decrement from here */ else if (t->firstfree == t->node) break; /* cannot decrement from here */
else (t->firstfree)--; 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 *luaH_setint (lua_State *L, Hash *t, int key) {
TObject index; TObject index;
ttype(&index) = TAG_NUMBER; ttype(&index) = LUA_TNUMBER;
nvalue(&index) = key; nvalue(&index) = key;
return luaH_set(L, t, &index); 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) { void luaH_setstrnum (lua_State *L, Hash *t, TString *key, Number val) {
TObject *value, index; TObject *value, index;
ttype(&index) = TAG_STRING; ttype(&index) = LUA_TSTRING;
tsvalue(&index) = key; tsvalue(&index) = key;
value = luaH_set(L, t, &index); value = luaH_set(L, t, &index);
ttype(value) = TAG_NUMBER; ttype(value) = LUA_TNUMBER;
nvalue(value) = val; nvalue(value) = val;
} }

View File

@ -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 ** Internal Module for Debugging of the Lua Implementation
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -93,7 +93,8 @@ static int listcode (lua_State *L) {
int pc; int pc;
Proto *p; Proto *p;
int res; 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; p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L); lua_newtable(L);
setnameval(L, "maxstack", p->maxstacksize); setnameval(L, "maxstack", p->maxstacksize);
@ -111,7 +112,8 @@ static int listcode (lua_State *L) {
static int liststrings (lua_State *L) { static int liststrings (lua_State *L) {
Proto *p; Proto *p;
int i; 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; p = clvalue(luaA_index(L, 1))->f.l;
lua_newtable(L); lua_newtable(L);
for (i=0; i<p->nkstr; i++) { for (i=0; i<p->nkstr; i++) {
@ -128,7 +130,8 @@ static int listlocals (lua_State *L) {
int pc = luaL_check_int(L, 2) - 1; int pc = luaL_check_int(L, 2) - 1;
int i = 0; int i = 0;
const char *name; 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; p = clvalue(luaA_index(L, 1))->f.l;
while ((name = luaF_getlocalname(p, ++i, pc)) != NULL) while ((name = luaF_getlocalname(p, ++i, pc)) != NULL)
lua_pushstring(L, name); lua_pushstring(L, name);
@ -177,7 +180,7 @@ static int mem_query (lua_State *L) {
static int hash_query (lua_State *L) { static int hash_query (lua_State *L) {
if (lua_isnull(L, 2)) { 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); lua_pushnumber(L, tsvalue(luaA_index(L, 1))->u.s.hash);
} }
else { else {
@ -226,7 +229,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) = TAG_STRING; ttype(L->top) = LUA_TSTRING;
tsvalue(L->top) = ts; tsvalue(L->top) = ts;
incr_top; incr_top;
n++; n++;

49
ltm.c
View File

@ -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 ** Tag methods
** See Copyright Notice in lua.h ** 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); int e = findevent(name);
if (e >= IM_N) if (e >= IM_N)
luaO_verror(L, "event `%.50s' is deprecated", name); 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"); luaO_verror(L, "event `gc' for tables is deprecated");
if (e < 0) if (e < 0)
luaO_verror(L, "`%.50s' is not a valid event name", name); 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 * 'placeholder' for "default" fallbacks
*/ */
/* ORDER LUA_T, ORDER IM */ /* ORDER LUA_T, ORDER IM */
static const char luaT_validevents[NUM_TAGS][IM_N] = { 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, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* TAG_NUMBER */ {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, 0, 0, 0, 0, 0, 1}, /* TAG_STRING */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
{0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* TAG_TABLE */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1}, /* LUA_TSTRING */
{1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0}, /* TAG_LCLOSURE */ {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}, /* TAG_CCLOSURE */ {1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0} /* LUA_TFUNCTION */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
}; };
int luaT_validevent (int t, int e) { /* ORDER LUA_T */ 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) { static void init_entry (lua_State *L, int tag) {
int i; int i;
for (i=0; i<IM_N; i++) for (i=0; i<IM_N; i++)
ttype(luaT_getim(L, tag, i)) = TAG_NIL; ttype(luaT_getim(L, tag, i)) = LUA_TNIL;
L->IMtable[tag].collected = NULL; L->IMtable[tag].collected = NULL;
} }
@ -100,7 +99,7 @@ static void checktag (lua_State *L, int tag) {
} }
void luaT_realtag (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); 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) { int luaT_tag (const TObject *o) {
lua_Tag t = ttype(o); int t = ttype(o);
switch (t) { switch (t) {
case TAG_USERDATA: { case LUA_TUSERDATA: return tsvalue(o)->u.d.tag;
int tag = tsvalue(o)->u.d.tag; case LUA_TTABLE: return hvalue(o)->htag;
if (tag > L->last_tag) default: return t;
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;;
} }
} }
@ -142,7 +133,7 @@ void lua_gettagmethod (lua_State *L, int t, const char *event) {
if (luaT_validevent(t, e)) if (luaT_validevent(t, e))
*L->top = *luaT_getim(L, t,e); *L->top = *luaT_getim(L, t,e);
else else
ttype(L->top) = TAG_NIL; ttype(L->top) = LUA_TNIL;
incr_top; incr_top;
} }
@ -156,8 +147,8 @@ void lua_settagmethod (lua_State *L, int t, const char *event) {
checktag(L, t); checktag(L, t);
if (!luaT_validevent(t, e)) if (!luaT_validevent(t, e))
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
luaT_eventname[e], lua_typename(L, luaO_tag2type(t)), luaT_eventname[e], luaO_typenames[t],
(t == TAG_TABLE || t == TAG_USERDATA) ? " with default tag" (t == LUA_TTABLE || t == LUA_TUSERDATA) ? " with default tag"
: ""); : "");
temp = *(L->top - 1); temp = *(L->top - 1);
*(L->top - 1) = *luaT_getim(L, t,e); *(L->top - 1) = *luaT_getim(L, t,e);

9
ltm.h
View File

@ -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 ** Tag methods
** See Copyright Notice in lua.h ** 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_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[]; extern const char *const luaT_eventname[];
void luaT_init (lua_State *L); void luaT_init (lua_State *L);
void luaT_realtag (lua_State *L, int tag); 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 */ int luaT_validevent (int t, int e); /* used by compatibility module */

23
lua.h
View File

@ -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 ** Lua - An Extensible Extension Language
** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
** e-mail: lua@tecgraf.puc-rio.br ** e-mail: lua@tecgraf.puc-rio.br
@ -47,11 +47,17 @@ typedef struct lua_State lua_State;
typedef int (*lua_CFunction) (lua_State *L); typedef int (*lua_CFunction) (lua_State *L);
/*
** types returned by `lua_type'
*/
#define LUA_TNONE (-1)
typedef enum lua_Type { #define LUA_TUSERDATA 0
LUA_NOVALUE, LUA_TUSERDATA, LUA_TNUMBER, LUA_TSTRING, #define LUA_TNIL 1
LUA_TTABLE, LUA_TFUNCTION, LUA_TNIL #define LUA_TNUMBER 2
} lua_Type; #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) ** access functions (stack -> C)
*/ */
lua_Type lua_type (lua_State *L, int index); int lua_type (lua_State *L, int index);
const char *lua_typename (lua_State *L, lua_Type t); const char *lua_typename (lua_State *L, int t);
int lua_isnumber (lua_State *L, int index); int lua_isnumber (lua_State *L, int index);
int lua_isstring (lua_State *L, int index); int lua_isstring (lua_State *L, int index);
int lua_iscfunction (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_istable(L,n) (lua_type(L,n) == LUA_TTABLE)
#define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA) #define lua_isuserdata(L,n) (lua_type(L,n) == LUA_TUSERDATA)
#define lua_isnil(L,n) (lua_type(L,n) == LUA_TNIL) #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 #endif
@ -224,3 +230,4 @@ void lua_concat (lua_State *L, int n);
* *
* This implementation contains no third-party code. * This implementation contains no third-party code.
******************************************************************************/ ******************************************************************************/

112
lvm.c
View File

@ -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 ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -40,25 +40,25 @@
int luaV_tonumber (TObject *obj) { int luaV_tonumber (TObject *obj) {
if (ttype(obj) != TAG_STRING) if (ttype(obj) != LUA_TSTRING)
return 1; return 1;
else { else {
if (!luaO_str2d(svalue(obj), &nvalue(obj))) if (!luaO_str2d(svalue(obj), &nvalue(obj)))
return 2; return 2;
ttype(obj) = TAG_NUMBER; ttype(obj) = LUA_TNUMBER;
return 0; return 0;
} }
} }
int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */ int luaV_tostring (lua_State *L, TObject *obj) { /* LUA_NUMBER */
if (ttype(obj) != TAG_NUMBER) if (ttype(obj) != LUA_TNUMBER)
return 1; return 1;
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); tsvalue(obj) = luaS_new(L, s);
ttype(obj) = TAG_STRING; ttype(obj) = LUA_TSTRING;
return 0; 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); Closure *c = luaF_newclosure(L, nelems);
L->top -= nelems; L->top -= nelems;
while (nelems--) while (nelems--)
c->upvalue[nelems] = *(L->top+nelems); c->upvalue[nelems] = *(L->top+nelems);
ttype(L->top) = t;
clvalue(L->top) = c; clvalue(L->top) = c;
ttype(L->top) = LUA_TFUNCTION;
incr_top; incr_top;
return c; return c;
} }
void luaV_Cclosure (lua_State *L, lua_CFunction c, int nelems) { 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->f.c = c;
cl->isC = 1;
} }
void luaV_Lclosure (lua_State *L, Proto *l, int nelems) { 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->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 *luaV_gettable (lua_State *L, StkId t) {
const TObject *im; const TObject *im;
int tg; int tg;
if (ttype(t) == TAG_TABLE && /* `t' is a table? */ if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
ttype(luaT_getim(L, tg, IM_GETTABLE)) == TAG_NIL)) { /* or no TM? */ ttype(luaT_getim(L, tg, IM_GETTABLE)) == LUA_TNIL)) { /* or no TM? */
/* do a primitive get */ /* do a primitive get */
const TObject *h = luaH_get(L, hvalue(t), L->top-1); const TObject *h = luaH_get(L, hvalue(t), L->top-1);
/* result is no nil or there is no `index' tag method? */ /* result is no nil or there is no `index' tag method? */
if (ttype(h) != TAG_NIL || if (ttype(h) != LUA_TNIL ||
(ttype(im=luaT_getim(L, tg, IM_INDEX)) == TAG_NIL)) (ttype(im=luaT_getim(L, tg, IM_INDEX)) == LUA_TNIL))
return h; /* return result */ return h; /* return result */
/* else call `index' tag method */ /* else call `index' tag method */
} }
else { /* try a 'gettable' TM */ else { /* try a `gettable' tag method */
im = luaT_getimbyObj(L, t, IM_GETTABLE); 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); luaD_checkstack(L, 2);
*(L->top+1) = *(L->top-1); /* key */ *(L->top+1) = *(L->top-1); /* key */
*L->top = *t; /* table */ *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) { void luaV_settable (lua_State *L, StkId t, StkId key) {
int tg; int tg;
if (ttype(t) == TAG_TABLE && /* `t' is a table? */ if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
((tg = hvalue(t)->htag) == TAG_TABLE || /* with default tag? */ ((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
ttype(luaT_getim(L, tg, IM_SETTABLE)) == TAG_NIL)) /* or no TM? */ 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 */ *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 */
const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE); const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
if (ttype(im) != TAG_NIL) { if (ttype(im) != LUA_TNIL) {
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); *(L->top+2) = *(L->top-1);
*(L->top+1) = *key; *(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 *luaV_getglobal (lua_State *L, TString *s) {
const TObject *value = luaH_getstr(L->gt, s); const TObject *value = luaH_getstr(L->gt, s);
const TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL); 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 */ return value; /* default behavior */
else { /* tag method */ else { /* tag method */
luaD_checkstack(L, 3); luaD_checkstack(L, 3);
*L->top = *im; *L->top = *im;
ttype(L->top+1) = TAG_STRING; ttype(L->top+1) = LUA_TSTRING;
tsvalue(L->top+1) = s; /* global name */ tsvalue(L->top+1) = s; /* global name */
*(L->top+2) = *value; *(L->top+2) = *value;
L->top += 3; L->top += 3;
@ -193,14 +195,14 @@ 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) {
const TObject *oldvalue = luaH_getstr(L->gt, s); const TObject *oldvalue = luaH_getstr(L->gt, s);
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL); 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) { if (oldvalue != &luaO_nilobject) {
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */ /* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
*(TObject *)oldvalue = *(L->top - 1); *(TObject *)oldvalue = *(L->top - 1);
} }
else { else {
TObject key; TObject key;
ttype(&key) = TAG_STRING; ttype(&key) = LUA_TSTRING;
tsvalue(&key) = s; tsvalue(&key) = s;
*luaH_set(L, L->gt, &key) = *(L->top - 1); *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); luaD_checkstack(L, 3);
*(L->top+2) = *(L->top-1); /* new value */ *(L->top+2) = *(L->top-1); /* new value */
*(L->top+1) = *oldvalue; *(L->top+1) = *oldvalue;
ttype(L->top) = TAG_STRING; ttype(L->top) = LUA_TSTRING;
tsvalue(L->top) = s; tsvalue(L->top) = s;
*(L->top-1) = *im; *(L->top-1) = *im;
L->top += 3; L->top += 3;
@ -222,11 +224,11 @@ static int call_binTM (lua_State *L, StkId top, IMS event) {
/* try first operand */ /* try first operand */
const TObject *im = luaT_getimbyObj(L, top-2, event); const TObject *im = luaT_getimbyObj(L, top-2, event);
L->top = top; L->top = top;
if (ttype(im) == TAG_NIL) { if (ttype(im) == LUA_TNIL) {
im = luaT_getimbyObj(L, top-1, event); /* try second operand */ 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 */ im = luaT_getim(L, 0, event); /* try a `global' method */
if (ttype(im) == TAG_NIL) if (ttype(im) == LUA_TNIL)
return 0; /* error */ 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) { static void call_arith (lua_State *L, StkId top, IMS event) {
if (!call_binTM(L, top, 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) { 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)); 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); return (luaV_strcomp(tsvalue(l), tsvalue(r)) < 0);
else { /* call TM */ else { /* call TM */
luaD_checkstack(L, 2); 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)) if (!call_binTM(L, top, IM_LT))
luaG_ordererror(L, top-2); luaG_ordererror(L, top-2);
L->top--; 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) */ int n = 2; /* number of elements handled in this pass (at least 2) */
if (tostring(L, top-2) || tostring(L, top-1)) { if (tostring(L, top-2) || tostring(L, top-1)) {
if (!call_binTM(L, top, IM_CONCAT)) 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 */ else if (tsvalue(top-1)->u.s.len > 0) { /* if len=0, do nothing */
/* at least two string values; get as many as possible */ /* 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' */ /* store counter in field `n' */
luaH_setstrnum(L, htab, luaS_new(L, "n"), i); luaH_setstrnum(L, htab, luaS_new(L, "n"), i);
L->top = firstelem; /* remove elements from the stack */ L->top = firstelem; /* remove elements from the stack */
ttype(L->top) = TAG_TABLE; ttype(L->top) = LUA_TTABLE;
hvalue(L->top) = htab; hvalue(L->top) = htab;
incr_top; incr_top;
} }
@ -393,7 +395,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++) = TAG_NIL; ttype(top++) = LUA_TNIL;
} while (--n > 0); } while (--n > 0);
break; break;
} }
@ -402,25 +404,25 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_PUSHINT: { case OP_PUSHINT: {
ttype(top) = TAG_NUMBER; ttype(top) = LUA_TNUMBER;
nvalue(top) = (Number)GETARG_S(i); nvalue(top) = (Number)GETARG_S(i);
top++; top++;
break; break;
} }
case OP_PUSHSTRING: { case OP_PUSHSTRING: {
ttype(top) = TAG_STRING; ttype(top) = LUA_TSTRING;
tsvalue(top) = kstr[GETARG_U(i)]; tsvalue(top) = kstr[GETARG_U(i)];
top++; top++;
break; break;
} }
case OP_PUSHNUM: { case OP_PUSHNUM: {
ttype(top) = TAG_NUMBER; ttype(top) = LUA_TNUMBER;
nvalue(top) = tf->knum[GETARG_U(i)]; nvalue(top) = tf->knum[GETARG_U(i)];
top++; top++;
break; break;
} }
case OP_PUSHNEGNUM: { case OP_PUSHNEGNUM: {
ttype(top) = TAG_NUMBER; ttype(top) = LUA_TNUMBER;
nvalue(top) = -tf->knum[GETARG_U(i)]; nvalue(top) = -tf->knum[GETARG_U(i)];
top++; top++;
break; break;
@ -446,7 +448,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_GETDOTTED: { case OP_GETDOTTED: {
ttype(top) = TAG_STRING; ttype(top) = LUA_TSTRING;
tsvalue(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); *(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: { case OP_PUSHSELF: {
TObject receiver; TObject receiver;
receiver = *(top-1); receiver = *(top-1);
ttype(top) = TAG_STRING; ttype(top) = LUA_TSTRING;
tsvalue(top++) = kstr[GETARG_U(i)]; tsvalue(top++) = kstr[GETARG_U(i)];
L->top = top; L->top = top;
*(top-2) = *luaV_gettable(L, top-2); *(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; L->top = top;
luaC_checkGC(L); luaC_checkGC(L);
hvalue(top) = luaH_new(L, GETARG_U(i)); hvalue(top) = luaH_new(L, GETARG_U(i));
ttype(top) = TAG_TABLE; ttype(top) = LUA_TTABLE;
top++; top++;
break; break;
} }
@ -523,7 +525,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) = TAG_NUMBER; ttype(top) = LUA_TNUMBER;
nvalue(top) = (Number)GETARG_S(i); nvalue(top) = (Number)GETARG_S(i);
call_arith(L, top+1, IM_ADD); 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: { case OP_MINUS: {
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = TAG_NIL; ttype(top) = LUA_TNIL;
call_arith(L, top+1, IM_UNM); call_arith(L, top+1, IM_UNM);
} }
else else
@ -580,7 +582,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_NOT: { case OP_NOT: {
ttype(top-1) = 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; nvalue(top-1) = 1;
break; break;
} }
@ -615,20 +617,20 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_JMPT: { case OP_JMPT: {
if (ttype(--top) != TAG_NIL) dojump(pc, i); if (ttype(--top) != LUA_TNIL) dojump(pc, i);
break; break;
} }
case OP_JMPF: { case OP_JMPF: {
if (ttype(--top) == TAG_NIL) dojump(pc, i); if (ttype(--top) == LUA_TNIL) dojump(pc, i);
break; break;
} }
case OP_JMPONT: { case OP_JMPONT: {
if (ttype(top-1) == TAG_NIL) top--; if (ttype(top-1) == LUA_TNIL) top--;
else dojump(pc, i); else dojump(pc, i);
break; break;
} }
case OP_JMPONF: { case OP_JMPONF: {
if (ttype(top-1) != TAG_NIL) top--; if (ttype(top-1) != LUA_TNIL) top--;
else dojump(pc, i); else dojump(pc, i);
break; break;
} }
@ -637,7 +639,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_PUSHNILJMP: { case OP_PUSHNILJMP: {
ttype(top++) = TAG_NIL; ttype(top++) = LUA_TNIL;
pc++; pc++;
break; break;
} }
@ -657,9 +659,9 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
break; break;
} }
case OP_FORLOOP: { case OP_FORLOOP: {
LUA_ASSERT(ttype(top-1) == TAG_NUMBER, "invalid step"); LUA_ASSERT(ttype(top-1) == LUA_TNUMBER, "invalid step");
LUA_ASSERT(ttype(top-2) == TAG_NUMBER, "invalid limit"); LUA_ASSERT(ttype(top-2) == LUA_TNUMBER, "invalid limit");
if (ttype(top-3) != TAG_NUMBER) if (ttype(top-3) != LUA_TNUMBER)
lua_error(L, "`for' index must be a number"); lua_error(L, "`for' index must be a number");
nvalue(top-3) += nvalue(top-1); /* increment index */ nvalue(top-3) += nvalue(top-1); /* increment index */
if (nvalue(top-1) > 0 ? if (nvalue(top-1) > 0 ?
@ -672,7 +674,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_LFORPREP: { case OP_LFORPREP: {
Node *node; Node *node;
if (ttype(top-1) != TAG_TABLE) if (ttype(top-1) != LUA_TTABLE)
lua_error(L, "`for' table must be a table"); lua_error(L, "`for' table must be a table");
node = luaH_next(L, hvalue(top-1), &luaO_nilobject); node = luaH_next(L, hvalue(top-1), &luaO_nilobject);
if (node == NULL) { /* `empty' loop? */ if (node == NULL) { /* `empty' loop? */
@ -688,7 +690,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_LFORLOOP: { case OP_LFORLOOP: {
Node *node; 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); node = luaH_next(L, hvalue(top-3), top-2);
if (node == NULL) /* end loop? */ if (node == NULL) /* end loop? */
top -= 3; /* remove table, key, and value */ top -= 3; /* remove table, key, and value */

6
lvm.h
View File

@ -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 ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@ -13,8 +13,8 @@
#include "ltm.h" #include "ltm.h"
#define tonumber(o) ((ttype(o) != TAG_NUMBER) && (luaV_tonumber(o) != 0)) #define tonumber(o) ((ttype(o) != LUA_TNUMBER) && (luaV_tonumber(o) != 0))
#define tostring(L,o) ((ttype(o) != TAG_STRING) && (luaV_tostring(L, o) != 0)) #define tostring(L,o) ((ttype(o) != LUA_TSTRING) && (luaV_tostring(L, o) != 0))
int luaV_tonumber (TObject *obj); int luaV_tonumber (TObject *obj);