mirror of
https://github.com/lua/lua
synced 2025-03-26 15:42:52 +03:00
tag methods are always functions, so don't need to store a whole object
This commit is contained in:
parent
001f2bdd0e
commit
046a3d6173
16
ldebug.c
16
ldebug.c
@ -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
|
||||
** 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) {
|
||||
int e;
|
||||
for (e=0; e<IM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (luaO_equalObj(o, luaT_getim(L, t,e)))
|
||||
return luaT_eventname[e];
|
||||
if (ttype(o) == LUA_TFUNCTION) {
|
||||
int e;
|
||||
for (e=0; e<TM_N; e++) {
|
||||
int t;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
if (clvalue(o) == luaT_gettm(L, t, e))
|
||||
return luaT_eventname[e];
|
||||
}
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
15
ldo.c
15
ldo.c
@ -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
|
||||
** 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;
|
||||
luaD_openstack(L, base);
|
||||
*base = *f;
|
||||
clvalue(base) = f;
|
||||
ttype(base) = LUA_TFUNCTION;
|
||||
luaD_call(L, base, nResults);
|
||||
}
|
||||
|
||||
@ -163,12 +164,12 @@ void luaD_call (lua_State *L, StkId func, int nResults) {
|
||||
Closure *cl;
|
||||
if (ttype(func) != LUA_TFUNCTION) {
|
||||
/* `func' is not a function; check the `function' tag method */
|
||||
const TObject *im = luaT_getimbyObj(L, func, IM_FUNCTION);
|
||||
if (ttype(im) == LUA_TNIL)
|
||||
Closure *tm = luaT_gettmbyObj(L, func, TM_FUNCTION);
|
||||
if (tm == NULL)
|
||||
luaG_typeerror(L, func, "call");
|
||||
luaD_openstack(L, func);
|
||||
*func = *im; /* tag method is the new function to be called */
|
||||
LUA_ASSERT(ttype(func) == LUA_TFUNCTION, "invalid tag method");
|
||||
clvalue(func) = tm; /* tag method is the new function to be called */
|
||||
ttype(func) = LUA_TFUNCTION;
|
||||
}
|
||||
cl = clvalue(func);
|
||||
ci.func = cl;
|
||||
|
4
ldo.h
4
ldo.h
@ -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
|
||||
** 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,
|
||||
const char *event);
|
||||
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_breakrun (lua_State *L, int errcode);
|
||||
|
39
lgc.c
39
lgc.c
@ -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
|
||||
** 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) {
|
||||
if (!ismarked(cl)) {
|
||||
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) {
|
||||
switch (ttype(o)) {
|
||||
case LUA_TUSERDATA: case LUA_TSTRING:
|
||||
@ -269,8 +271,8 @@ static void collectudata (lua_State *L, int all) {
|
||||
else { /* collect */
|
||||
int tag = next->u.d.tag;
|
||||
*p = next->nexthash;
|
||||
next->nexthash = L->IMtable[tag].collected; /* chain udata */
|
||||
L->IMtable[tag].collected = next;
|
||||
next->nexthash = L->TMtable[tag].collected; /* chain udata */
|
||||
L->TMtable[tag].collected = next;
|
||||
L->nblocks -= gcsizeudata;
|
||||
L->udt.nuse--;
|
||||
}
|
||||
@ -292,12 +294,13 @@ static void checkMbuffer (lua_State *L) {
|
||||
|
||||
|
||||
static void callgcTM (lua_State *L, const TObject *o) {
|
||||
const TObject *im = luaT_getimbyObj(L, o, IM_GC);
|
||||
if (ttype(im) != LUA_TNIL) {
|
||||
Closure *tm = luaT_gettmbyObj(L, o, TM_GC);
|
||||
if (tm != NULL) {
|
||||
int oldah = L->allowhooks;
|
||||
L->allowhooks = 0; /* stop debug hooks during GC tag methods */
|
||||
luaD_checkstack(L, 2);
|
||||
*(L->top) = *im;
|
||||
clvalue(L->top) = tm;
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
*(L->top+1) = *o;
|
||||
L->top += 2;
|
||||
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 */
|
||||
for (tag=L->last_tag; tag>=0; tag--) { /* for each tag (in reverse order) */
|
||||
TString *udata;
|
||||
while ((udata = L->IMtable[tag].collected) != NULL) {
|
||||
L->IMtable[tag].collected = udata->nexthash; /* remove it from list */
|
||||
while ((udata = L->TMtable[tag].collected) != NULL) {
|
||||
L->TMtable[tag].collected = udata->nexthash; /* remove it from list */
|
||||
tsvalue(&o) = udata;
|
||||
callgcTM(L, &o);
|
||||
luaM_free(L, udata);
|
||||
|
8
lstate.c
8
lstate.c
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -73,7 +73,7 @@ lua_State *lua_open (int stacksize) {
|
||||
L->rootproto = NULL;
|
||||
L->rootcl = NULL;
|
||||
L->roottable = NULL;
|
||||
L->IMtable = NULL;
|
||||
L->TMtable = NULL;
|
||||
L->last_tag = -1;
|
||||
L->refArray = NULL;
|
||||
L->refSize = 0;
|
||||
@ -103,8 +103,8 @@ void lua_close (lua_State *L) {
|
||||
if (L->stack)
|
||||
L->nblocks -= (L->stack_last - L->stack + 1)*sizeof(TObject);
|
||||
luaM_free(L, L->stack);
|
||||
L->nblocks -= (L->last_tag+1)*sizeof(struct IM);
|
||||
luaM_free(L, L->IMtable);
|
||||
L->nblocks -= (L->last_tag+1)*sizeof(struct TM);
|
||||
luaM_free(L, L->TMtable);
|
||||
L->nblocks -= (L->refSize)*sizeof(struct Ref);
|
||||
luaM_free(L, L->refArray);
|
||||
L->nblocks -= (L->Mbuffsize)*sizeof(char);
|
||||
|
7
lstate.h
7
lstate.h
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -32,6 +32,7 @@ struct Ref {
|
||||
|
||||
|
||||
struct lua_longjmp; /* defined in ldo.c */
|
||||
struct TM; /* defined in ltm.h */
|
||||
|
||||
|
||||
typedef struct stringtable {
|
||||
@ -59,8 +60,8 @@ struct lua_State {
|
||||
stringtable strt; /* hash table for strings */
|
||||
stringtable udt; /* hash table for udata */
|
||||
Hash *gt; /* table for globals */
|
||||
struct IM *IMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in IMtable */
|
||||
struct TM *TMtable; /* table for tag methods */
|
||||
int last_tag; /* last used tag in TMtable */
|
||||
struct Ref *refArray; /* locked objects */
|
||||
int refSize; /* size of refArray */
|
||||
int refFree; /* list of free positions in refArray */
|
||||
|
12
ltests.c
12
ltests.c
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -438,6 +438,16 @@ static int testC (lua_State *L) {
|
||||
else if EQ("dostring") {
|
||||
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") {
|
||||
lua_pushstring(L, lua_typename(L, lua_type(L, getnum)));
|
||||
}
|
||||
|
63
ltm.c
63
ltm.c
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -17,7 +17,7 @@
|
||||
#include "ltm.h"
|
||||
|
||||
|
||||
const char *const luaT_eventname[] = { /* ORDER IM */
|
||||
const char *const luaT_eventname[] = { /* ORDER TM */
|
||||
"gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
|
||||
"mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
|
||||
"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) {
|
||||
int e = findevent(name);
|
||||
if (e >= IM_N)
|
||||
if (e >= TM_N)
|
||||
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");
|
||||
if (e < 0)
|
||||
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
|
||||
* 'placeholder' for "default" fallbacks
|
||||
*/
|
||||
/* ORDER LUA_T, ORDER IM */
|
||||
static const char luaT_validevents[NUM_TAGS][IM_N] = {
|
||||
/* ORDER LUA_T, ORDER TM */
|
||||
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, 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 */
|
||||
@ -67,16 +67,16 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
|
||||
|
||||
static void init_entry (lua_State *L, int tag) {
|
||||
int i;
|
||||
for (i=0; i<IM_N; i++)
|
||||
ttype(luaT_getim(L, tag, i)) = LUA_TNIL;
|
||||
L->IMtable[tag].collected = NULL;
|
||||
for (i=0; i<TM_N; i++)
|
||||
luaT_gettm(L, tag, i) = NULL;
|
||||
L->TMtable[tag].collected = NULL;
|
||||
}
|
||||
|
||||
|
||||
void luaT_init (lua_State *L) {
|
||||
int t;
|
||||
luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, "", MAX_INT);
|
||||
L->nblocks += NUM_TAGS*sizeof(struct IM);
|
||||
luaM_growvector(L, L->TMtable, 0, NUM_TAGS, struct TM, "", MAX_INT);
|
||||
L->nblocks += NUM_TAGS*sizeof(struct TM);
|
||||
L->last_tag = NUM_TAGS-1;
|
||||
for (t=0; t<=L->last_tag; t++)
|
||||
init_entry(L, t);
|
||||
@ -84,9 +84,9 @@ void luaT_init (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);
|
||||
L->nblocks += sizeof(struct IM);
|
||||
L->nblocks += sizeof(struct TM);
|
||||
L->last_tag++;
|
||||
init_entry(L, L->last_tag);
|
||||
return L->last_tag;
|
||||
@ -108,9 +108,9 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
|
||||
int e;
|
||||
checktag(L, tagto);
|
||||
checktag(L, tagfrom);
|
||||
for (e=0; e<IM_N; e++) {
|
||||
for (e=0; e<TM_N; 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;
|
||||
}
|
||||
@ -130,8 +130,10 @@ void lua_gettagmethod (lua_State *L, int t, const char *event) {
|
||||
int e;
|
||||
e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (luaT_validevent(t, e))
|
||||
*L->top = *luaT_getim(L, t,e);
|
||||
if (luaT_validevent(t, e) && luaT_gettm(L, t, e)) {
|
||||
clvalue(L->top) = luaT_gettm(L, t, e);
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
}
|
||||
else
|
||||
ttype(L->top) = LUA_TNIL;
|
||||
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) {
|
||||
TObject temp;
|
||||
int e;
|
||||
LUA_ASSERT(lua_isnil(L, -1) || lua_isfunction(L, -1),
|
||||
"function or nil expected");
|
||||
e = luaI_checkevent(L, event, t);
|
||||
Closure *oldtm;
|
||||
int e = luaI_checkevent(L, event, t);
|
||||
checktag(L, t);
|
||||
if (!luaT_validevent(t, e))
|
||||
luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
|
||||
luaT_eventname[e], luaO_typenames[t],
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ? " with default tag"
|
||||
: "");
|
||||
temp = *(L->top - 1);
|
||||
*(L->top - 1) = *luaT_getim(L, t,e);
|
||||
*luaT_getim(L, t, e) = temp;
|
||||
(t == LUA_TTABLE || t == LUA_TUSERDATA) ?
|
||||
" with default tag" : "");
|
||||
oldtm = luaT_gettm(L, t, e);
|
||||
switch (ttype(L->top - 1)) {
|
||||
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
48
ltm.h
@ -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
|
||||
** See Copyright Notice in lua.h
|
||||
*/
|
||||
@ -13,36 +13,36 @@
|
||||
|
||||
/*
|
||||
* WARNING: if you change the order of this enumeration,
|
||||
* grep "ORDER IM"
|
||||
* grep "ORDER TM"
|
||||
*/
|
||||
typedef enum {
|
||||
IM_GETTABLE = 0,
|
||||
IM_SETTABLE,
|
||||
IM_INDEX,
|
||||
IM_GETGLOBAL,
|
||||
IM_SETGLOBAL,
|
||||
IM_ADD,
|
||||
IM_SUB,
|
||||
IM_MUL,
|
||||
IM_DIV,
|
||||
IM_POW,
|
||||
IM_UNM,
|
||||
IM_LT,
|
||||
IM_CONCAT,
|
||||
IM_GC,
|
||||
IM_FUNCTION,
|
||||
IM_N /* number of elements in the enum */
|
||||
} IMS;
|
||||
TM_GETTABLE = 0,
|
||||
TM_SETTABLE,
|
||||
TM_INDEX,
|
||||
TM_GETGLOBAL,
|
||||
TM_SETGLOBAL,
|
||||
TM_ADD,
|
||||
TM_SUB,
|
||||
TM_MUL,
|
||||
TM_DIV,
|
||||
TM_POW,
|
||||
TM_UNM,
|
||||
TM_LT,
|
||||
TM_CONCAT,
|
||||
TM_GC,
|
||||
TM_FUNCTION,
|
||||
TM_N /* number of elements in the enum */
|
||||
} TMS;
|
||||
|
||||
|
||||
struct IM {
|
||||
TObject int_method[IM_N];
|
||||
TString *collected; /* list of G. collected udata with this tag */
|
||||
struct TM {
|
||||
Closure *method[TM_N];
|
||||
TString *collected; /* list of garbage-collected udata with this tag */
|
||||
};
|
||||
|
||||
|
||||
#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
|
||||
#define luaT_getimbyObj(L,o,e) (luaT_getim((L),luaT_tag(o),(e)))
|
||||
#define luaT_gettm(L,tag,event) (L->TMtable[tag].method[event])
|
||||
#define luaT_gettmbyObj(L,o,e) (luaT_gettm((L),luaT_tag(o),(e)))
|
||||
|
||||
|
||||
#define validtag(t) (NUM_TAGS <= (t) && (t) <= L->last_tag)
|
||||
|
77
lvm.c
77
lvm.c
@ -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
|
||||
** 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.
|
||||
*/
|
||||
const TObject *luaV_gettable (lua_State *L, StkId t) {
|
||||
const TObject *im;
|
||||
Closure *tm;
|
||||
int tg;
|
||||
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
|
||||
((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 */
|
||||
const TObject *h = luaH_get(L, hvalue(t), L->top-1);
|
||||
/* result is no nil or there is no `index' tag method? */
|
||||
if (ttype(h) != LUA_TNIL ||
|
||||
(ttype(im=luaT_getim(L, tg, IM_INDEX)) == LUA_TNIL))
|
||||
if (ttype(h) != LUA_TNIL || ((tm=luaT_gettm(L, tg, TM_INDEX)) == NULL))
|
||||
return h; /* return result */
|
||||
/* else call `index' 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);
|
||||
*(L->top+1) = *(L->top-1); /* key */
|
||||
*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;
|
||||
luaD_call(L, L->top - 3, 1);
|
||||
return L->top - 1; /* call result */
|
||||
@ -155,16 +155,17 @@ void luaV_settable (lua_State *L, StkId t, StkId key) {
|
||||
int tg;
|
||||
if (ttype(t) == LUA_TTABLE && /* `t' is a table? */
|
||||
((tg = hvalue(t)->htag) == LUA_TTABLE || /* with default tag? */
|
||||
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 */
|
||||
else { /* try a `settable' tag method */
|
||||
const TObject *im = luaT_getimbyObj(L, t, IM_SETTABLE);
|
||||
if (ttype(im) != LUA_TNIL) {
|
||||
Closure *tm = luaT_gettmbyObj(L, t, TM_SETTABLE);
|
||||
if (tm != NULL) {
|
||||
luaD_checkstack(L, 3);
|
||||
*(L->top+2) = *(L->top-1);
|
||||
*(L->top+1) = *key;
|
||||
*(L->top) = *t;
|
||||
*(L->top-1) = *im;
|
||||
clvalue(L->top-1) = tm;
|
||||
ttype(L->top-1) = LUA_TFUNCTION;
|
||||
L->top += 3;
|
||||
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 *value = luaH_getstr(L->gt, s);
|
||||
const TObject *im = luaT_getimbyObj(L, value, IM_GETGLOBAL);
|
||||
if (ttype(im) == LUA_TNIL) /* is there a tag method? */
|
||||
Closure *tm = luaT_gettmbyObj(L, value, TM_GETGLOBAL);
|
||||
if (tm == NULL) /* is there a tag method? */
|
||||
return value; /* default behavior */
|
||||
else { /* tag method */
|
||||
luaD_checkstack(L, 3);
|
||||
*L->top = *im;
|
||||
ttype(L->top+1) = LUA_TSTRING;
|
||||
clvalue(L->top) = tm;
|
||||
ttype(L->top) = LUA_TFUNCTION;
|
||||
tsvalue(L->top+1) = s; /* global name */
|
||||
ttype(L->top+1) = LUA_TSTRING;
|
||||
*(L->top+2) = *value;
|
||||
L->top += 3;
|
||||
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) {
|
||||
const TObject *oldvalue = luaH_getstr(L->gt, s);
|
||||
const TObject *im = luaT_getimbyObj(L, oldvalue, IM_SETGLOBAL);
|
||||
if (ttype(im) == LUA_TNIL) { /* is there a tag method? */
|
||||
Closure *tm = luaT_gettmbyObj(L, oldvalue, TM_SETGLOBAL);
|
||||
if (tm == NULL) { /* is there a tag method? */
|
||||
if (oldvalue != &luaO_nilobject) {
|
||||
/* cast to remove `const' is OK, because `oldvalue' != luaO_nilobject */
|
||||
*(TObject *)oldvalue = *(L->top - 1);
|
||||
@ -213,32 +215,33 @@ void luaV_setglobal (lua_State *L, TString *s) {
|
||||
*(L->top+1) = *oldvalue;
|
||||
ttype(L->top) = LUA_TSTRING;
|
||||
tsvalue(L->top) = s;
|
||||
*(L->top-1) = *im;
|
||||
clvalue(L->top-1) = tm;
|
||||
ttype(L->top-1) = LUA_TFUNCTION;
|
||||
L->top += 3;
|
||||
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 */
|
||||
const TObject *im = luaT_getimbyObj(L, top-2, event);
|
||||
Closure *tm = luaT_gettmbyObj(L, top-2, event);
|
||||
L->top = top;
|
||||
if (ttype(im) == LUA_TNIL) {
|
||||
im = luaT_getimbyObj(L, top-1, event); /* try second operand */
|
||||
if (ttype(im) == LUA_TNIL) {
|
||||
im = luaT_getim(L, 0, event); /* try a `global' method */
|
||||
if (ttype(im) == LUA_TNIL)
|
||||
if (tm == NULL) {
|
||||
tm = luaT_gettmbyObj(L, top-1, event); /* try second operand */
|
||||
if (tm == NULL) {
|
||||
tm = luaT_gettm(L, 0, event); /* try a `global' method */
|
||||
if (tm == NULL)
|
||||
return 0; /* error */
|
||||
}
|
||||
}
|
||||
lua_pushstring(L, luaT_eventname[event]);
|
||||
luaD_callTM(L, im, 3, 1);
|
||||
luaD_callTM(L, tm, 3, 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))
|
||||
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);
|
||||
*top++ = *l;
|
||||
*top++ = *r;
|
||||
if (!call_binTM(L, top, IM_LT))
|
||||
if (!call_binTM(L, top, TM_LT))
|
||||
luaG_ordererror(L, top-2);
|
||||
L->top--;
|
||||
return (ttype(L->top) != LUA_TNIL);
|
||||
@ -287,7 +290,7 @@ void luaV_strconc (lua_State *L, int total, StkId top) {
|
||||
do {
|
||||
int n = 2; /* number of elements handled in this pass (at least 2) */
|
||||
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");
|
||||
}
|
||||
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: {
|
||||
if (tonumber(top-2) || tonumber(top-1))
|
||||
call_arith(L, top, IM_ADD);
|
||||
call_arith(L, top, TM_ADD);
|
||||
else
|
||||
nvalue(top-2) += nvalue(top-1);
|
||||
top--;
|
||||
@ -527,7 +530,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
if (tonumber(top-1)) {
|
||||
ttype(top) = LUA_TNUMBER;
|
||||
nvalue(top) = (Number)GETARG_S(i);
|
||||
call_arith(L, top+1, IM_ADD);
|
||||
call_arith(L, top+1, TM_ADD);
|
||||
}
|
||||
else
|
||||
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: {
|
||||
if (tonumber(top-2) || tonumber(top-1))
|
||||
call_arith(L, top, IM_SUB);
|
||||
call_arith(L, top, TM_SUB);
|
||||
else
|
||||
nvalue(top-2) -= nvalue(top-1);
|
||||
top--;
|
||||
@ -543,7 +546,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
}
|
||||
case OP_MULT: {
|
||||
if (tonumber(top-2) || tonumber(top-1))
|
||||
call_arith(L, top, IM_MUL);
|
||||
call_arith(L, top, TM_MUL);
|
||||
else
|
||||
nvalue(top-2) *= nvalue(top-1);
|
||||
top--;
|
||||
@ -551,14 +554,14 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
}
|
||||
case OP_DIV: {
|
||||
if (tonumber(top-2) || tonumber(top-1))
|
||||
call_arith(L, top, IM_DIV);
|
||||
call_arith(L, top, TM_DIV);
|
||||
else
|
||||
nvalue(top-2) /= nvalue(top-1);
|
||||
top--;
|
||||
break;
|
||||
}
|
||||
case OP_POW: {
|
||||
if (!call_binTM(L, top, IM_POW))
|
||||
if (!call_binTM(L, top, TM_POW))
|
||||
lua_error(L, "undefined operation");
|
||||
top--;
|
||||
break;
|
||||
@ -574,7 +577,7 @@ StkId luaV_execute (lua_State *L, const Closure *cl, StkId base) {
|
||||
case OP_MINUS: {
|
||||
if (tonumber(top-1)) {
|
||||
ttype(top) = LUA_TNIL;
|
||||
call_arith(L, top+1, IM_UNM);
|
||||
call_arith(L, top+1, TM_UNM);
|
||||
}
|
||||
else
|
||||
nvalue(top-1) = -nvalue(top-1);
|
||||
|
Loading…
x
Reference in New Issue
Block a user