aboutsummaryrefslogtreecommitdiff
path: root/ltm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-03-20 16:14:54 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2000-03-20 16:14:54 -0300
commitbb9605bbc954d53b7d5997c53fceafe4450a6917 (patch)
treef62d6f64623221e2ba807aa080b84f2572640975 /ltm.c
parent67592998809bf5816e2256409b384a712270c7b2 (diff)
downloadlua-bb9605bbc954d53b7d5997c53fceafe4450a6917.tar.gz
lua-bb9605bbc954d53b7d5997c53fceafe4450a6917.tar.bz2
lua-bb9605bbc954d53b7d5997c53fceafe4450a6917.zip
tags can be positive values
Diffstat (limited to 'ltm.c')
-rw-r--r--ltm.c54
1 files changed, 24 insertions, 30 deletions
diff --git a/ltm.c b/ltm.c
index 6a73006e..30be02ad 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 1.33 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: ltm.c,v 1.34 2000/03/10 18:37:44 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*/
@@ -19,12 +19,16 @@
19 19
20const char *const luaT_eventname[] = { /* ORDER IM */ 20const char *const luaT_eventname[] = { /* ORDER IM */
21 "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub", 21 "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
22 "mul", "div", "pow", "unm", "lt", "concat", "gc", "function", NULL 22 "mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
23 "le", "gt", "ge", /* deprecated options!! */
24 NULL
23}; 25};
24 26
25 27
26static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) { 28static int luaI_checkevent (lua_State *L, const char *name) {
27 int e = luaL_findstring(name, list); 29 int e = luaL_findstring(name, luaT_eventname);
30 if (e >= IM_N)
31 luaL_verror(L, "event `%.50s' is deprecated", name);
28 if (e < 0) 32 if (e < 0)
29 luaL_verror(L, "`%.50s' is not a valid event name", name); 33 luaL_verror(L, "`%.50s' is not a valid event name", name);
30 return e; 34 return e;
@@ -46,12 +50,12 @@ static const char luaT_validevents[NUM_TAGS][IM_N] = {
46{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */ 50{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1} /* TAG_NIL */
47}; 51};
48 52
49int luaT_validevent (int t, int e) { /* ORDER LUA_T */ 53static int luaT_validevent (int t, int e) { /* ORDER LUA_T */
50#ifdef LUA_COMPAT_GC 54#ifdef LUA_COMPAT_GC
51 if (t == TAG_ARRAY && e == IM_GC) 55 if (t == TAG_ARRAY && e == IM_GC)
52 return 1; /* old versions allowed gc tag method for tables */ 56 return 1; /* old versions allowed gc tag method for tables */
53#endif 57#endif
54 return (t < TAG_NIL) ? 1 : luaT_validevents[-t][e]; 58 return (t > TAG_NIL) ? 1 : luaT_validevents[t][e];
55} 59}
56 60
57 61
@@ -64,28 +68,28 @@ static void init_entry (lua_State *L, int tag) {
64 68
65void luaT_init (lua_State *L) { 69void luaT_init (lua_State *L) {
66 int t; 70 int t;
67 L->last_tag = -(NUM_TAGS-1); 71 L->last_tag = NUM_TAGS-1;
68 luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT); 72 luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
69 for (t=L->last_tag; t<=0; t++) 73 for (t=0; t<=L->last_tag; t++)
70 init_entry(L, t); 74 init_entry(L, t);
71} 75}
72 76
73 77
74int lua_newtag (lua_State *L) { 78int lua_newtag (lua_State *L) {
75 --L->last_tag; 79 ++L->last_tag;
76 luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT); 80 luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, arrEM, MAX_INT);
77 init_entry(L, L->last_tag); 81 init_entry(L, L->last_tag);
78 return L->last_tag; 82 return L->last_tag;
79} 83}
80 84
81 85
82static void checktag (lua_State *L, int tag) { 86static void checktag (lua_State *L, int tag) {
83 if (!(L->last_tag <= tag && tag <= 0)) 87 if (!(0 <= tag && tag <= L->last_tag))
84 luaL_verror(L, "%d is not a valid tag", tag); 88 luaL_verror(L, "%d is not a valid tag", tag);
85} 89}
86 90
87void luaT_realtag (lua_State *L, int tag) { 91void luaT_realtag (lua_State *L, int tag) {
88 if (!(L->last_tag <= tag && tag < TAG_NIL)) 92 if (!(NUM_TAGS <= tag && tag <= L->last_tag))
89 luaL_verror(L, "tag %d was not created by `newtag'", tag); 93 luaL_verror(L, "tag %d was not created by `newtag'", tag);
90} 94}
91 95
@@ -102,32 +106,27 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
102} 106}
103 107
104 108
105int luaT_effectivetag (const TObject *o) { 109int luaT_effectivetag (lua_State *L, const TObject *o) {
106 static const int realtag[] = { /* ORDER LUA_T */ 110 static const int realtag[] = { /* ORDER LUA_T */
107 TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_ARRAY, 111 TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_ARRAY,
108 TAG_LPROTO, TAG_CPROTO, TAG_NIL, 112 TAG_LPROTO, TAG_CPROTO, TAG_NIL,
109 TAG_LPROTO, TAG_CPROTO, /* TAG_LCLOSURE, TAG_CCLOSURE */ 113 TAG_LPROTO, TAG_CPROTO, /* TAG_LCLOSURE, TAG_CCLOSURE */
110 }; 114 };
111 int t; 115 lua_Type t;
112 switch (t = ttype(o)) { 116 switch (t = ttype(o)) {
113 case TAG_USERDATA: { 117 case TAG_USERDATA: {
114 int tag = o->value.ts->u.d.tag; 118 int tag = o->value.ts->u.d.tag;
115 return (tag >= 0) ? TAG_USERDATA : tag; /* deprecated test */ 119 return (tag > L->last_tag) ? TAG_USERDATA : tag; /* deprecated test */
116 } 120 }
117 case TAG_ARRAY: return o->value.a->htag; 121 case TAG_ARRAY: return o->value.a->htag;
118 default: return realtag[-t]; 122 default: return realtag[t];
119 } 123 }
120} 124}
121 125
122 126
123const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) { 127const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
124 int e; 128 int e;
125#ifdef LUA_COMPAT_ORDER_TM 129 e = luaI_checkevent(L, event);
126 static const char *old_order[] = {"le", "gt", "ge", NULL};
127 if (luaL_findstring(event, old_order) >= 0)
128 return &luaO_nilobject;
129#endif
130 e = luaI_checkevent(L, event, luaT_eventname);
131 checktag(L, t); 130 checktag(L, t);
132 if (luaT_validevent(t, e)) 131 if (luaT_validevent(t, e))
133 return luaT_getim(L, t,e); 132 return luaT_getim(L, t,e);
@@ -139,16 +138,11 @@ const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
139void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) { 138void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
140 TObject temp; 139 TObject temp;
141 int e; 140 int e;
142#ifdef LUA_COMPAT_ORDER_TM 141 e = luaI_checkevent(L, event);
143 static const char *old_order[] = {"le", "gt", "ge", NULL};
144 if (luaL_findstring(event, old_order) >= 0)
145 return; /* do nothing for old operators */
146#endif
147 e = luaI_checkevent(L, event, luaT_eventname);
148 checktag(L, t); 142 checktag(L, t);
149 if (!luaT_validevent(t, e)) 143 if (!luaT_validevent(t, e))
150 luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s", 144 luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
151 luaT_eventname[e], luaO_typenames[-t], 145 luaT_eventname[e], luaO_typenames[t],
152 (t == TAG_ARRAY || t == TAG_USERDATA) ? " with default tag" 146 (t == TAG_ARRAY || t == TAG_USERDATA) ? " with default tag"
153 : ""); 147 : "");
154 temp = *func; 148 temp = *func;
@@ -162,7 +156,7 @@ const char *luaT_travtagmethods (lua_State *L,
162 int e; 156 int e;
163 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { 157 for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
164 int t; 158 int t;
165 for (t=0; t>=L->last_tag; t--) 159 for (t=0; t<=L->last_tag; t++)
166 if (fn(L, luaT_getim(L, t,e))) 160 if (fn(L, luaT_getim(L, t,e)))
167 return luaT_eventname[e]; 161 return luaT_eventname[e];
168 } 162 }