From a53d9b66ca6247818acaf41e28cdf123082a272b Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Thu, 25 Jan 2001 14:45:36 -0200 Subject: first implementation for type names --- ltm.c | 76 ++++++++++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 50 insertions(+), 26 deletions(-) (limited to 'ltm.c') diff --git a/ltm.c b/ltm.c index 3f10a7b1..8e6996ae 100644 --- a/ltm.c +++ b/ltm.c @@ -1,5 +1,5 @@ /* -** $Id: ltm.c,v 1.61 2001/01/19 13:20:30 roberto Exp roberto $ +** $Id: ltm.c,v 1.62 2001/01/24 15:45:33 roberto Exp roberto $ ** Tag methods ** See Copyright Notice in lua.h */ @@ -14,6 +14,8 @@ #include "lmem.h" #include "lobject.h" #include "lstate.h" +#include "lstring.h" +#include "ltable.h" #include "ltm.h" @@ -65,32 +67,38 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */ } -static void init_entry (lua_State *L, int tag) { - int i; - for (i=0; iTMtable[tag].collected = NULL; -} - - void luaT_init (lua_State *L) { - int t; - G(L)->TMtable = luaM_newvector(L, NUM_TAGS+2, struct TM); - G(L)->sizeTM = NUM_TAGS+2; - G(L)->ntag = NUM_TAGS; - for (t=0; tntag; t++) - init_entry(L, t); + static const char *const typenames[NUM_TAGS] = { + "userdata", "nil", "number", "string", "table", "function" + }; + int i; + for (i=0; iTMtable, G(L)->ntag, G(L)->sizeTM, struct TM, MAX_INT, "tag table overflow"); - init_entry(L, G(L)->ntag); - tag = G(L)->ntag++; - LUA_EXIT; + tag = G(L)->ntag; + if (name == NULL) + ts = NULL; + else { + TObject *v; + ts = luaS_new(L, name); + v = luaH_setstr(L, G(L)->type2tag, ts); + if (ttype(v) != LUA_TNIL) return LUA_TNONE; /* invalid name */ + setnvalue(v, tag); + } + for (i=0; iTMtable[tag].collected = NULL; + G(L)->TMtable[tag].name = ts; + G(L)->TMtable[tag].basictype = basictype; + G(L)->ntag++; return tag; } @@ -100,11 +108,6 @@ static void checktag (lua_State *L, int tag) { luaO_verror(L, "%d is not a valid tag", tag); } -void luaT_realtag (lua_State *L, int tag) { - if (!validtag(G(L), tag)) - luaO_verror(L, "tag %d was not created by `newtag'", tag); -} - LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) { int e; @@ -130,6 +133,27 @@ int luaT_tag (const TObject *o) { } +const char *luaT_typename (global_State *G, const TObject *o) { + int t = ttype(o); + int tag; + TString *ts; + switch (t) { + case LUA_TUSERDATA: + tag = tsvalue(o)->u.d.tag; + break; + case LUA_TTABLE: + tag = hvalue(o)->htag; + break; + default: + tag = t; + } + ts = G->TMtable[tag].name; + if (ts == NULL) + ts = G->TMtable[t].name; + return ts->str; +} + + LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) { int e; LUA_ENTRY; @@ -152,7 +176,7 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) { 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], + luaT_eventname[e], basictypename(G(L), t), (t == LUA_TTABLE || t == LUA_TUSERDATA) ? " with default tag" : ""); switch (ttype(L->top - 1)) { -- cgit v1.2.3-55-g6feb