summaryrefslogtreecommitdiff
path: root/ltm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-25 14:45:36 -0200
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2001-01-25 14:45:36 -0200
commita53d9b66ca6247818acaf41e28cdf123082a272b (patch)
tree8e909200d4d925fc7394e6adf83cc5941456c588 /ltm.c
parentc8559e3c8d12e052783e2952db1372c68cc16059 (diff)
downloadlua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.gz
lua-a53d9b66ca6247818acaf41e28cdf123082a272b.tar.bz2
lua-a53d9b66ca6247818acaf41e28cdf123082a272b.zip
first implementation for type names
Diffstat (limited to 'ltm.c')
-rw-r--r--ltm.c76
1 files changed, 50 insertions, 26 deletions
diff --git a/ltm.c b/ltm.c
index 3f10a7b1..8e6996ae 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.61 2001/01/19 13:20:30 roberto Exp roberto $ 2** $Id: ltm.c,v 1.62 2001/01/24 15:45:33 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -14,6 +14,8 @@
14#include "lmem.h" 14#include "lmem.h"
15#include "lobject.h" 15#include "lobject.h"
16#include "lstate.h" 16#include "lstate.h"
17#include "lstring.h"
18#include "ltable.h"
17#include "ltm.h" 19#include "ltm.h"
18 20
19 21
@@ -65,32 +67,38 @@ int luaT_validevent (int t, int e) { /* ORDER LUA_T */
65} 67}
66 68
67 69
68static void init_entry (lua_State *L, int tag) {
69 int i;
70 for (i=0; i<TM_N; i++)
71 luaT_gettm(G(L), tag, i) = NULL;
72 G(L)->TMtable[tag].collected = NULL;
73}
74
75
76void luaT_init (lua_State *L) { 70void luaT_init (lua_State *L) {
77 int t; 71 static const char *const typenames[NUM_TAGS] = {
78 G(L)->TMtable = luaM_newvector(L, NUM_TAGS+2, struct TM); 72 "userdata", "nil", "number", "string", "table", "function"
79 G(L)->sizeTM = NUM_TAGS+2; 73 };
80 G(L)->ntag = NUM_TAGS; 74 int i;
81 for (t=0; t<G(L)->ntag; t++) 75 for (i=0; i<NUM_TAGS; i++)
82 init_entry(L, t); 76 luaT_newtag(L, typenames[i], i);
83} 77}
84 78
85 79
86LUA_API int lua_newtag (lua_State *L) { 80int luaT_newtag (lua_State *L, const char *name, int basictype) {
87 int tag; 81 int tag;
88 LUA_ENTRY; 82 int i;
83 TString *ts;
89 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM, 84 luaM_growvector(L, G(L)->TMtable, G(L)->ntag, G(L)->sizeTM, struct TM,
90 MAX_INT, "tag table overflow"); 85 MAX_INT, "tag table overflow");
91 init_entry(L, G(L)->ntag); 86 tag = G(L)->ntag;
92 tag = G(L)->ntag++; 87 if (name == NULL)
93 LUA_EXIT; 88 ts = NULL;
89 else {
90 TObject *v;
91 ts = luaS_new(L, name);
92 v = luaH_setstr(L, G(L)->type2tag, ts);
93 if (ttype(v) != LUA_TNIL) return LUA_TNONE; /* invalid name */
94 setnvalue(v, tag);
95 }
96 for (i=0; i<TM_N; i++)
97 luaT_gettm(G(L), tag, i) = NULL;
98 G(L)->TMtable[tag].collected = NULL;
99 G(L)->TMtable[tag].name = ts;
100 G(L)->TMtable[tag].basictype = basictype;
101 G(L)->ntag++;
94 return tag; 102 return tag;
95} 103}
96 104
@@ -100,11 +108,6 @@ static void checktag (lua_State *L, int tag) {
100 luaO_verror(L, "%d is not a valid tag", tag); 108 luaO_verror(L, "%d is not a valid tag", tag);
101} 109}
102 110
103void luaT_realtag (lua_State *L, int tag) {
104 if (!validtag(G(L), tag))
105 luaO_verror(L, "tag %d was not created by `newtag'", tag);
106}
107
108 111
109LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) { 112LUA_API int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
110 int e; 113 int e;
@@ -130,6 +133,27 @@ int luaT_tag (const TObject *o) {
130} 133}
131 134
132 135
136const char *luaT_typename (global_State *G, const TObject *o) {
137 int t = ttype(o);
138 int tag;
139 TString *ts;
140 switch (t) {
141 case LUA_TUSERDATA:
142 tag = tsvalue(o)->u.d.tag;
143 break;
144 case LUA_TTABLE:
145 tag = hvalue(o)->htag;
146 break;
147 default:
148 tag = t;
149 }
150 ts = G->TMtable[tag].name;
151 if (ts == NULL)
152 ts = G->TMtable[t].name;
153 return ts->str;
154}
155
156
133LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) { 157LUA_API void lua_gettagmethod (lua_State *L, int t, const char *event) {
134 int e; 158 int e;
135 LUA_ENTRY; 159 LUA_ENTRY;
@@ -152,7 +176,7 @@ LUA_API void lua_settagmethod (lua_State *L, int t, const char *event) {
152 checktag(L, t); 176 checktag(L, t);
153 if (!luaT_validevent(t, e)) 177 if (!luaT_validevent(t, e))
154 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", 178 luaO_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
155 luaT_eventname[e], luaO_typenames[t], 179 luaT_eventname[e], basictypename(G(L), t),
156 (t == LUA_TTABLE || t == LUA_TUSERDATA) ? 180 (t == LUA_TTABLE || t == LUA_TUSERDATA) ?
157 " with default tag" : ""); 181 " with default tag" : "");
158 switch (ttype(L->top - 1)) { 182 switch (ttype(L->top - 1)) {