tag methods are always functions, so don't need to store a whole object

This commit is contained in:
Roberto Ierusalimschy
2000-10-05 10:00:17 -03:00
parent 001f2bdd0e
commit 046a3d6173
10 changed files with 159 additions and 130 deletions

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldebug.c,v 1.43 2000/10/02 20:10:55 roberto Exp roberto $ ** $Id: ldebug.c,v 1.44 2000/10/05 12:14:08 roberto Exp roberto $
** Debug Interface ** Debug Interface
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -203,12 +203,14 @@ static void lua_funcinfo (lua_State *L, lua_Debug *ar, StkId func) {
static const char *travtagmethods (lua_State *L, const TObject *o) { static const char *travtagmethods (lua_State *L, const TObject *o) {
int e; if (ttype(o) == LUA_TFUNCTION) {
for (e=0; e<IM_N; e++) { int e;
int t; for (e=0; e<TM_N; e++) {
for (t=0; t<=L->last_tag; t++) int t;
if (luaO_equalObj(o, luaT_getim(L, t,e))) for (t=0; t<=L->last_tag; t++)
return luaT_eventname[e]; if (clvalue(o) == luaT_gettm(L, t, e))
return luaT_eventname[e];
}
} }
return NULL; return NULL;
} }

15
ldo.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldo.c,v 1.101 2000/10/04 12:16:08 roberto Exp roberto $ ** $Id: ldo.c,v 1.102 2000/10/05 12:14: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
*/ */
@@ -142,10 +142,11 @@ static StkId callCclosure (lua_State *L, const struct Closure *cl, StkId base) {
} }
void luaD_callTM (lua_State *L, const TObject *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);
*base = *f; clvalue(base) = f;
ttype(base) = LUA_TFUNCTION;
luaD_call(L, base, nResults); luaD_call(L, base, nResults);
} }
@@ -163,12 +164,12 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
Closure *cl; Closure *cl;
if (ttype(func) != LUA_TFUNCTION) { if (ttype(func) != LUA_TFUNCTION) {
/* `func' is not a function; check the `function' tag method */ /* `func' is not a function; check the `function' tag method */
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION); Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
if (ttype(im) == LUA_TNIL) if (tm == NULL)
luaG_typeerror(L, func, "call"); luaG_typeerror(L, func, "call");
luaD_openstack(L, func); luaD_openstack(L, func);
*func = *im; /* tag method is the new function to be called */ clvalue(func) = tm; /* tag method is the new function to be called */
LUA_ASSERT(ttype(func) == LUA_TFUNCTION, "invalid tag method"); ttype(func) = LUA_TFUNCTION;
} }
cl = clvalue(func); cl = clvalue(func);
ci.func = cl; ci.func = cl;

4
ldo.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ldo.h,v 1.25 2000/09/25 16:22:42 roberto Exp roberto $ ** $Id: ldo.h,v 1.26 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
*/ */
@@ -25,7 +25,7 @@ void luaD_lineHook (lua_State *L, StkId func, int line, lua_Hook linehook);
void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook, void luaD_callHook (lua_State *L, StkId func, lua_Hook callhook,
const char *event); const char *event);
void luaD_call (lua_State *L, StkId func, int nResults); void luaD_call (lua_State *L, StkId func, int nResults);
void luaD_callTM (lua_State *L, const TObject *f, int nParams, int nResults); void luaD_callTM (lua_State *L, Closure *f, int nParams, int nResults);
void luaD_checkstack (lua_State *L, int n); void luaD_checkstack (lua_State *L, int n);
void luaD_breakrun (lua_State *L, int errcode); void luaD_breakrun (lua_State *L, int errcode);

39
lgc.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lgc.c,v 1.69 2000/10/02 14:47:43 roberto Exp roberto $ ** $Id: lgc.c,v 1.70 2000/10/05 12:14:08 roberto Exp roberto $
** Garbage Collector ** Garbage Collector
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -63,16 +63,6 @@ static void marklock (lua_State *L, GCState *st) {
} }
static void marktagmethods (lua_State *L, GCState *st) {
int e;
for (e=0; e<IM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++)
markobject(st, luaT_getim(L, t,e));
}
}
static void markclosure (GCState *st, Closure *cl) { static void markclosure (GCState *st, Closure *cl) {
if (!ismarked(cl)) { if (!ismarked(cl)) {
if (!cl->isC) if (!cl->isC)
@@ -83,6 +73,18 @@ static void markclosure (GCState *st, Closure *cl) {
} }
static void marktagmethods (lua_State *L, GCState *st) {
int e;
for (e=0; e<TM_N; e++) {
int t;
for (t=0; t<=L->last_tag; t++) {
Closure *cl = luaT_gettm(L, t, e);
if (cl) markclosure(st, cl);
}
}
}
static void markobject (GCState *st, TObject *o) { static void markobject (GCState *st, TObject *o) {
switch (ttype(o)) { switch (ttype(o)) {
case LUA_TUSERDATA: case LUA_TSTRING: case LUA_TUSERDATA: case LUA_TSTRING:
@@ -269,8 +271,8 @@ static void collectudata (lua_State *L, int all) {
else { /* collect */ else { /* collect */
int tag = next->u.d.tag; int tag = next->u.d.tag;
*p = next->nexthash; *p = next->nexthash;
next->nexthash = L->IMtable[tag].collected; /* chain udata */ next->nexthash = L->TMtable[tag].collected; /* chain udata */
L->IMtable[tag].collected = next; L->TMtable[tag].collected = next;
L->nblocks -= gcsizeudata; L->nblocks -= gcsizeudata;
L->udt.nuse--; L->udt.nuse--;
} }
@@ -292,12 +294,13 @@ 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); Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
if (ttype(im) != LUA_TNIL) { 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);
*(L->top) = *im; clvalue(L->top) = tm;
ttype(L->top) = LUA_TFUNCTION;
*(L->top+1) = *o; *(L->top+1) = *o;
L->top += 2; L->top += 2;
luaD_call(L, L->top-2, 0); luaD_call(L, L->top-2, 0);
@@ -313,8 +316,8 @@ static void callgcTMudata (lua_State *L) {
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;
while ((udata = L->IMtable[tag].collected) != NULL) { while ((udata = L->TMtable[tag].collected) != NULL) {
L->IMtable[tag].collected = udata->nexthash; /* remove it from list */ L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
tsvalue(&o) = udata; tsvalue(&o) = udata;
callgcTM(L, &o); callgcTM(L, &o);
luaM_free(L, udata); luaM_free(L, udata);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.c,v 1.41 2000/09/25 16:22:42 roberto Exp roberto $ ** $Id: lstate.c,v 1.42 2000/09/29 12:42:13 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -73,7 +73,7 @@ lua_State *lua_open (int stacksize) {
L->rootproto = NULL; L->rootproto = NULL;
L->rootcl = NULL; L->rootcl = NULL;
L->roottable = NULL; L->roottable = NULL;
L->IMtable = NULL; L->TMtable = NULL;
L->last_tag = -1; L->last_tag = -1;
L->refArray = NULL; L->refArray = NULL;
L->refSize = 0; L->refSize = 0;
@@ -103,8 +103,8 @@ void lua_close (lua_State *L) {
if (L->stack) if (L->stack)
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject); L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
luaM_free(L, L->stack); luaM_free(L, L->stack);
L->nblocks -= (L->last_tag+1)*sizeof(struct IM); L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
luaM_free(L, L->IMtable); luaM_free(L, L->TMtable);
L->nblocks -= (L->refSize)*sizeof(struct Ref); L->nblocks -= (L->refSize)*sizeof(struct Ref);
luaM_free(L, L->refArray); luaM_free(L, L->refArray);
L->nblocks -= (L->Mbuffsize)*sizeof(char); L->nblocks -= (L->Mbuffsize)*sizeof(char);

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lstate.h,v 1.39 2000/09/25 16:22:42 roberto Exp roberto $ ** $Id: lstate.h,v 1.40 2000/09/29 12:42:13 roberto Exp roberto $
** Global State ** Global State
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -32,6 +32,7 @@ struct Ref {
struct lua_longjmp; /* defined in ldo.c */ struct lua_longjmp; /* defined in ldo.c */
struct TM; /* defined in ltm.h */
typedef struct stringtable { typedef struct stringtable {
@@ -59,8 +60,8 @@ struct lua_State {
stringtable strt; /* hash table for strings */ stringtable strt; /* hash table for strings */
stringtable udt; /* hash table for udata */ stringtable udt; /* hash table for udata */
Hash *gt; /* table for globals */ Hash *gt; /* table for globals */
struct IM *IMtable; /* table for tag methods */ struct TM *TMtable; /* table for tag methods */
int last_tag; /* last used tag in IMtable */ int last_tag; /* last used tag in TMtable */
struct Ref *refArray; /* locked objects */ struct Ref *refArray; /* locked objects */
int refSize; /* size of refArray */ int refSize; /* size of refArray */
int refFree; /* list of free positions in refArray */ int refFree; /* list of free positions in refArray */

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltests.c,v 1.47 2000/10/02 20:10:55 roberto Exp roberto $ ** $Id: ltests.c,v 1.48 2000/10/05 12:14:08 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
*/ */
@@ -438,6 +438,16 @@ static int testC (lua_State *L) {
else if EQ("dostring") { else if EQ("dostring") {
lua_dostring(L, luaL_check_string(L, getnum)); lua_dostring(L, luaL_check_string(L, getnum));
} }
else if EQ("settagmethod") {
int tag = getnum;
const char *event = getname;
lua_settagmethod(L, tag, event);
}
else if EQ("gettagmethod") {
int tag = getnum;
const char *event = getname;
lua_gettagmethod(L, tag, event);
}
else if EQ("type") { else if EQ("type") {
lua_pushstring(L, lua_typename(L, lua_type(L, getnum))); lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
} }

63
ltm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltm.c,v 1.52 2000/10/03 14:27:44 roberto Exp roberto $ ** $Id: ltm.c,v 1.53 2000/10/05 12:14:08 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -17,7 +17,7 @@
#include "ltm.h" #include "ltm.h"
const char *const luaT_eventname[] = { /* ORDER IM */ const char *const luaT_eventname[] = { /* ORDER TM */
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub", "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function", "mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
"le", "gt", "ge", /* deprecated options!! */ "le", "gt", "ge", /* deprecated options!! */
@@ -36,9 +36,9 @@ static int findevent (const char *name) {
static int luaI_checkevent (lua_State *L, const char *name, int t) { 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 >= TM_N)
luaO_verror(L, "event `%.50s' is deprecated", name); luaO_verror(L, "event `%.50s' is deprecated", name);
if (e == IM_GC && t == LUA_TTABLE) if (e == TM_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);
@@ -50,8 +50,8 @@ static int luaI_checkevent (lua_State *L, const char *name, int t) {
/* events in LUA_TNIL 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 TM */
static const char luaT_validevents[NUM_TAGS][IM_N] = { static const char luaT_validevents[NUM_TAGS][TM_N] = {
{1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */ {1, 1, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1}, /* LUA_TUSERDATA */
{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */ {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}, /* LUA_TNIL */
{1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */ {1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1}, /* LUA_TNUMBER */
@@ -67,16 +67,16 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
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<TM_N; i++)
ttype(luaT_getim(L, tag, i)) = LUA_TNIL; luaT_gettm(L, tag, i) = NULL;
L->IMtable[tag].collected = NULL; L->TMtable[tag].collected = NULL;
} }
void luaT_init (lua_State *L) { void luaT_init (lua_State *L) {
int t; int t;
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT); luaM_growvector(L, L->TMtable, 0, NUM_TAGS, struct TM, "", MAX_INT);
L->nblocks += NUM_TAGS*sizeof(struct IM); L->nblocks += NUM_TAGS*sizeof(struct TM);
L->last_tag = NUM_TAGS-1; L->last_tag = NUM_TAGS-1;
for (t=0; t<=L->last_tag; t++) for (t=0; t<=L->last_tag; t++)
init_entry(L, t); init_entry(L, t);
@@ -84,9 +84,9 @@ void luaT_init (lua_State *L) {
int lua_newtag (lua_State *L) { int lua_newtag (lua_State *L) {
luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, luaM_growvector(L, L->TMtable, L->last_tag, 1, struct TM,
"tag table overflow", MAX_INT); "tag table overflow", MAX_INT);
L->nblocks += sizeof(struct IM); L->nblocks += sizeof(struct TM);
L->last_tag++; L->last_tag++;
init_entry(L, L->last_tag); init_entry(L, L->last_tag);
return L->last_tag; return L->last_tag;
@@ -108,9 +108,9 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
int e; int e;
checktag(L, tagto); checktag(L, tagto);
checktag(L, tagfrom); checktag(L, tagfrom);
for (e=0; e<IM_N; e++) { for (e=0; e<TM_N; e++) {
if (luaT_validevent(tagto, e)) if (luaT_validevent(tagto, e))
*luaT_getim(L, tagto, e) = *luaT_getim(L, tagfrom, e); luaT_gettm(L, tagto, e) = luaT_gettm(L, tagfrom, e);
} }
return tagto; return tagto;
} }
@@ -130,8 +130,10 @@ void lua_gettagmethod (lua_State *L, int t, const char *event) {
int e; int e;
e = luaI_checkevent(L, event, t); e = luaI_checkevent(L, event, t);
checktag(L, t); checktag(L, t);
if (luaT_validevent(t, e)) if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
*L->top = *luaT_getim(L, t,e); clvalue(L->top) = luaT_gettm(L, t, e);
ttype(L->top) = LUA_TFUNCTION;
}
else else
ttype(L->top) = LUA_TNIL; ttype(L->top) = LUA_TNIL;
incr_top; incr_top;
@@ -139,19 +141,26 @@ void lua_gettagmethod (lua_State *L, int t, const char *event) {
void lua_settagmethod (lua_State *L, int t, const char *event) { void lua_settagmethod (lua_State *L, int t, const char *event) {
TObject temp; Closure *oldtm;
int e; int e = luaI_checkevent(L, event, t);
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
"function or nil expected");
e = luaI_checkevent(L, event, t);
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], luaO_typenames[t], luaT_eventname[e], luaO_typenames[t],
(t == LUA_TTABLE || t == LUA_TUSERDATA) ? " with default tag" (t == LUA_TTABLE || t == LUA_TUSERDATA) ?
: ""); " with default tag" : "");
temp = *(L->top - 1); oldtm = luaT_gettm(L, t, e);
*(L->top - 1) = *luaT_getim(L, t,e); switch (ttype(L->top - 1)) {
*luaT_getim(L, t, e) = temp; case LUA_TNIL:
luaT_gettm(L, t, e) = NULL;
break;
case LUA_TFUNCTION:
luaT_gettm(L, t, e) = clvalue(L->top - 1);
break;
default:
lua_error(L, "tag method must be a function (or nil)");
}
clvalue(L->top - 1) = oldtm;
ttype(L->top - 1) = (oldtm ? LUA_TFUNCTION : LUA_TNIL);
} }

48
ltm.h
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: ltm.h,v 1.16 2000/10/03 14:27:44 roberto Exp roberto $ ** $Id: ltm.h,v 1.17 2000/10/05 12:14:08 roberto Exp roberto $
** Tag methods ** Tag methods
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -13,36 +13,36 @@
/* /*
* WARNING: if you change the order of this enumeration, * WARNING: if you change the order of this enumeration,
* grep "ORDER IM" * grep "ORDER TM"
*/ */
typedef enum { typedef enum {
IM_GETTABLE = 0, TM_GETTABLE = 0,
IM_SETTABLE, TM_SETTABLE,
IM_INDEX, TM_INDEX,
IM_GETGLOBAL, TM_GETGLOBAL,
IM_SETGLOBAL, TM_SETGLOBAL,
IM_ADD, TM_ADD,
IM_SUB, TM_SUB,
IM_MUL, TM_MUL,
IM_DIV, TM_DIV,
IM_POW, TM_POW,
IM_UNM, TM_UNM,
IM_LT, TM_LT,
IM_CONCAT, TM_CONCAT,
IM_GC, TM_GC,
IM_FUNCTION, TM_FUNCTION,
IM_N /* number of elements in the enum */ TM_N /* number of elements in the enum */
} IMS; } TMS;
struct IM { struct TM {
TObject int_method[IM_N]; Closure *method[TM_N];
TString *collected; /* list of G. collected udata with this tag */ TString *collected; /* list of garbage-collected udata with this tag */
}; };
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event]) #define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
#define luaT_getimbyObj(L,o,e) (luaT_getim((L),luaT_tag(o),(e))) #define luaT_gettmbyObj(L,o,e) (luaT_gettm((L),luaT_tag(o),(e)))
#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag) #define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag)

77
lvm.c
View File

@@ -1,5 +1,5 @@
/* /*
** $Id: lvm.c,v 1.142 2000/10/04 12:16:08 roberto Exp roberto $ ** $Id: lvm.c,v 1.143 2000/10/05 12:14:08 roberto Exp roberto $
** Lua virtual machine ** Lua virtual machine
** See Copyright Notice in lua.h ** See Copyright Notice in lua.h
*/ */
@@ -116,27 +116,27 @@ void luaV_Lclosure (lua_State *L, Proto *l, int nelems) {
** Receives the table at `t' and the key at top. ** Receives the table at `t' and the key at top.
*/ */
const TObject *luaV_gettable (lua_State *L, StkId t) { const TObject *luaV_gettable (lua_State *L, StkId t) {
const TObject *im; Closure *tm;
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? */
ttype(luaT_getim(L, tg, IM_GETTABLE)) == LUA_TNIL)) { /* or no TM? */ luaT_gettm(L, tg, TM_GETTABLE) == NULL)) { /* 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) != LUA_TNIL || if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL))
(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' tag method */ else { /* try a `gettable' tag method */
im = luaT_getimbyObj(L, t, IM_GETTABLE); tm = luaT_gettmbyObj(L, t, TM_GETTABLE);
} }
if (ttype(im) != LUA_TNIL) { /* 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 */ *(L->top+1) = *(L->top-1); /* key */
*L->top = *t; /* table */ *L->top = *t; /* table */
*(L->top-1) = *im; /* tag method */ clvalue(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,16 +155,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? */
ttype(luaT_getim(L, tg, IM_SETTABLE)) == LUA_TNIL)) /* 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 */ *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); Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE);
if (ttype(im) != LUA_TNIL) { if (tm != NULL) {
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;
*(L->top) = *t; *(L->top) = *t;
*(L->top-1) = *im; clvalue(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 */
} }
@@ -176,14 +177,15 @@ 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); Closure *tm = luaT_gettmbyObj(L, value, TM_GETGLOBAL);
if (ttype(im) == LUA_TNIL) /* is there a tag method? */ if (tm == NULL) /* 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; clvalue(L->top) = tm;
ttype(L->top+1) = LUA_TSTRING; ttype(L->top) = LUA_TFUNCTION;
tsvalue(L->top+1) = s; /* global name */ tsvalue(L->top+1) = s; /* global name */
ttype(L->top+1) = LUA_TSTRING;
*(L->top+2) = *value; *(L->top+2) = *value;
L->top += 3; L->top += 3;
luaD_call(L, L->top - 3, 1); luaD_call(L, L->top - 3, 1);
@@ -194,8 +196,8 @@ 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); Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
if (ttype(im) == LUA_TNIL) { /* is there a tag method? */ if (tm == NULL) { /* 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);
@@ -213,32 +215,33 @@ void luaV_setglobal (lua_State *L, TString *s) {
*(L->top+1) = *oldvalue; *(L->top+1) = *oldvalue;
ttype(L->top) = LUA_TSTRING; ttype(L->top) = LUA_TSTRING;
tsvalue(L->top) = s; tsvalue(L->top) = s;
*(L->top-1) = *im; 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);
} }
} }
static int call_binTM (lua_State *L, StkId top, IMS event) { static int call_binTM (lua_State *L, StkId top, TMS event) {
/* try first operand */ /* try first operand */
const TObject *im = luaT_getimbyObj(L, top-2, event); Closure *tm = luaT_gettmbyObj(L, top-2, event);
L->top = top; L->top = top;
if (ttype(im) == LUA_TNIL) { if (tm == NULL) {
im = luaT_getimbyObj(L, top-1, event); /* try second operand */ tm = luaT_gettmbyObj(L, top-1, event); /* try second operand */
if (ttype(im) == LUA_TNIL) { if (tm == NULL) {
im = luaT_getim(L, 0, event); /* try a `global' method */ tm = luaT_gettm(L, 0, event); /* try a `global' method */
if (ttype(im) == LUA_TNIL) if (tm == NULL)
return 0; /* error */ return 0; /* error */
} }
} }
lua_pushstring(L, luaT_eventname[event]); lua_pushstring(L, luaT_eventname[event]);
luaD_callTM(L, im, 3, 1); luaD_callTM(L, tm, 3, 1);
return 1; return 1;
} }
static void call_arith (lua_State *L, StkId top, IMS event) { static void call_arith (lua_State *L, StkId top, TMS event) {
if (!call_binTM(L, top, event)) if (!call_binTM(L, top, event))
luaG_binerror(L, top-2, LUA_TNUMBER, "perform arithmetic on"); luaG_binerror(L, top-2, LUA_TNUMBER, "perform arithmetic on");
} }
@@ -275,7 +278,7 @@ int luaV_lessthan (lua_State *L, const TObject *l, const TObject *r, StkId top)
luaD_checkstack(L, 2); luaD_checkstack(L, 2);
*top++ = *l; *top++ = *l;
*top++ = *r; *top++ = *r;
if (!call_binTM(L, top, IM_LT)) if (!call_binTM(L, top, TM_LT))
luaG_ordererror(L, top-2); luaG_ordererror(L, top-2);
L->top--; L->top--;
return (ttype(L->top) != LUA_TNIL); return (ttype(L->top) != LUA_TNIL);
@@ -287,7 +290,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
do { do {
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, TM_CONCAT))
luaG_binerror(L, top-2, LUA_TSTRING, "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 */
@@ -517,7 +520,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_ADD: { case OP_ADD: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_ADD); call_arith(L, top, TM_ADD);
else else
nvalue(top-2) += nvalue(top-1); nvalue(top-2) += nvalue(top-1);
top--; top--;
@@ -527,7 +530,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
if (tonumber(top-1)) { if (tonumber(top-1)) {
ttype(top) = LUA_TNUMBER; 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, TM_ADD);
} }
else else
nvalue(top-1) += (Number)GETARG_S(i); nvalue(top-1) += (Number)GETARG_S(i);
@@ -535,7 +538,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_SUB: { case OP_SUB: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_SUB); call_arith(L, top, TM_SUB);
else else
nvalue(top-2) -= nvalue(top-1); nvalue(top-2) -= nvalue(top-1);
top--; top--;
@@ -543,7 +546,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_MULT: { case OP_MULT: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_MUL); call_arith(L, top, TM_MUL);
else else
nvalue(top-2) *= nvalue(top-1); nvalue(top-2) *= nvalue(top-1);
top--; top--;
@@ -551,14 +554,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
} }
case OP_DIV: { case OP_DIV: {
if (tonumber(top-2) || tonumber(top-1)) if (tonumber(top-2) || tonumber(top-1))
call_arith(L, top, IM_DIV); call_arith(L, top, TM_DIV);
else else
nvalue(top-2) /= nvalue(top-1); nvalue(top-2) /= nvalue(top-1);
top--; top--;
break; break;
} }
case OP_POW: { case OP_POW: {
if (!call_binTM(L, top, IM_POW)) if (!call_binTM(L, top, TM_POW))
lua_error(L, "undefined operation"); lua_error(L, "undefined operation");
top--; top--;
break; break;
@@ -574,7 +577,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; ttype(top) = LUA_TNIL;
call_arith(L, top+1, IM_UNM); call_arith(L, top+1, TM_UNM);
} }
else else
nvalue(top-1) = -nvalue(top-1); nvalue(top-1) = -nvalue(top-1);