aboutsummaryrefslogtreecommitdiff
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
parent67592998809bf5816e2256409b384a712270c7b2 (diff)
downloadlua-bb9605bbc954d53b7d5997c53fceafe4450a6917.tar.gz
lua-bb9605bbc954d53b7d5997c53fceafe4450a6917.tar.bz2
lua-bb9605bbc954d53b7d5997c53fceafe4450a6917.zip
tags can be positive values
-rw-r--r--lapi.c11
-rw-r--r--ldebug.c4
-rw-r--r--lobject.h46
-rw-r--r--ltm.c54
-rw-r--r--ltm.h9
5 files changed, 57 insertions, 67 deletions
diff --git a/lapi.c b/lapi.c
index 1391511f..decea1b5 100644
--- a/lapi.c
+++ b/lapi.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lapi.c,v 1.73 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: lapi.c,v 1.74 2000/03/10 18:37:44 roberto Exp roberto $
3** Lua API 3** Lua API
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -306,9 +306,9 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
306 luaC_checkGC(L); 306 luaC_checkGC(L);
307} 307}
308 308
309void lua_pushusertag (lua_State *L, void *u, int tag) { 309void lua_pushusertag (lua_State *L, void *u, int tag) { /* ORDER LUA_T */
310 if (tag < 0 && tag != LUA_ANYTAG) 310 if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
311 luaT_realtag(L, tag); /* error if tag is not valid */ 311 luaL_verror(L, "invalid tag for a userdata (%d)", tag);
312 tsvalue(L->top) = luaS_createudata(L, u, tag); 312 tsvalue(L->top) = luaS_createudata(L, u, tag);
313 ttype(L->top) = TAG_USERDATA; 313 ttype(L->top) = TAG_USERDATA;
314 incr_top; 314 incr_top;
@@ -329,13 +329,12 @@ void lua_pushobject (lua_State *L, lua_Object o) {
329 329
330 330
331int lua_tag (lua_State *L, lua_Object o) { 331int lua_tag (lua_State *L, lua_Object o) {
332 UNUSED(L);
333 if (o == LUA_NOOBJECT) 332 if (o == LUA_NOOBJECT)
334 return TAG_NIL; 333 return TAG_NIL;
335 else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */ 334 else if (ttype(o) == TAG_USERDATA) /* to allow `old' tags (deprecated) */
336 return o->value.ts->u.d.tag; 335 return o->value.ts->u.d.tag;
337 else 336 else
338 return luaT_effectivetag(o); 337 return luaT_effectivetag(L, o);
339} 338}
340 339
341 340
diff --git a/ldebug.c b/ldebug.c
index 1353dfc7..d96a373b 100644
--- a/ldebug.c
+++ b/ldebug.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ldebug.c,v 1.10 2000/03/03 14:58:26 roberto Exp roberto $ 2** $Id: ldebug.c,v 1.11 2000/03/10 18:37:44 roberto Exp roberto $
3** Debug Interface 3** Debug Interface
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -33,7 +33,7 @@ static const lua_Type normtype[] = { /* ORDER LUA_T */
33 33
34static void setnormalized (TObject *d, const TObject *s) { 34static void setnormalized (TObject *d, const TObject *s) {
35 d->value = s->value; 35 d->value = s->value;
36 d->ttype = normtype[-ttype(s)]; 36 d->ttype = normtype[ttype(s)];
37} 37}
38 38
39 39
diff --git a/lobject.h b/lobject.h
index ad35d0d4..558af2de 100644
--- a/lobject.h
+++ b/lobject.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: lobject.h,v 1.51 2000/03/10 18:37:44 roberto Exp roberto $ 2** $Id: lobject.h,v 1.52 2000/03/16 21:06:16 roberto Exp roberto $
3** Type definitions for Lua objects 3** Type definitions for Lua objects
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -67,34 +67,32 @@ typedef unsigned long Instruction;
67*/ 67*/
68typedef enum { 68typedef enum {
69 TAG_USERDATA = 0, /* default tag for userdata */ 69 TAG_USERDATA = 0, /* default tag for userdata */
70 TAG_NUMBER = -1, /* fixed tag for numbers */ 70 TAG_NUMBER, /* fixed tag for numbers */
71 TAG_STRING = -2, /* fixed tag for strings */ 71 TAG_STRING, /* fixed tag for strings */
72 TAG_ARRAY = -3, /* default tag for tables (or arrays) */ 72 TAG_ARRAY, /* default tag for tables (or arrays) */
73 TAG_LPROTO = -4, /* fixed tag for Lua functions */ 73 TAG_LPROTO, /* fixed tag for Lua functions */
74 TAG_CPROTO = -5, /* fixed tag for C functions */ 74 TAG_CPROTO, /* fixed tag for C functions */
75 TAG_NIL = -6, /* last "pre-defined" tag */ 75 TAG_NIL, /* last "pre-defined" tag */
76 76
77 TAG_LCLOSURE = -7, /* Lua closure */ 77 TAG_LCLOSURE, /* Lua closure */
78 TAG_CCLOSURE = -8, /* C closure */ 78 TAG_CCLOSURE, /* C closure */
79 79
80 TAG_LCLMARK = -9 ,/* mark for Lua closures */ 80 TAG_LCLMARK, /* mark for Lua closures */
81 TAG_CCLMARK = -10,/* mark for C closures */ 81 TAG_CCLMARK, /* mark for C closures */
82 TAG_LMARK = -11,/* mark for Lua prototypes */ 82 TAG_LMARK, /* mark for Lua prototypes */
83 TAG_CMARK = -12,/* mark for C prototypes */ 83 TAG_CMARK, /* mark for C prototypes */
84 84
85 TAG_LINE = -13 85 TAG_LINE
86} lua_Type; 86} lua_Type;
87 87
88#define NUM_TAGS 7 /* tags for values visible from Lua */ 88/* tags for values visible from Lua == first user-created tag */
89#define NUM_TAGS 7
89 90
90 91
91#define LAST_REGULAR_TAG TAG_CCLOSURE /* after that, are all marks */
92
93/* 92/*
94** check whether `t' is a mark; ttypes are negative numbers, so the 93** check whether `t' is a mark
95** comparisons look reversed. (ORDER LUA_T)
96*/ 94*/
97#define is_T_MARK(t) (TAG_CMARK <= (t) && (t) <= TAG_LCLMARK) 95#define is_T_MARK(t) (TAG_LCLMARK <= (t) && (t) <= TAG_CMARK)
98 96
99 97
100typedef union { 98typedef union {
@@ -221,7 +219,7 @@ typedef struct Hash {
221extern const char *const luaO_typenames[]; 219extern const char *const luaO_typenames[];
222extern const TObject luaO_nilobject; 220extern const TObject luaO_nilobject;
223 221
224#define luaO_typename(o) luaO_typenames[-ttype(o)] 222#define luaO_typename(o) luaO_typenames[ttype(o)]
225 223
226#define MINPOWER2 4 /* minimum size for "growing" vectors */ 224#define MINPOWER2 4 /* minimum size for "growing" vectors */
227 225
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 }
diff --git a/ltm.h b/ltm.h
index 21eec17c..34da30e0 100644
--- a/ltm.h
+++ b/ltm.h
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.h,v 1.9 2000/02/22 18:12:46 roberto Exp roberto $ 2** $Id: ltm.h,v 1.10 2000/03/03 14:58:26 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*/
@@ -40,19 +40,18 @@ struct IM {
40}; 40};
41 41
42 42
43#define luaT_getim(L,tag,event) (&L->IMtable[-(tag)].int_method[event]) 43#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
44#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(o),(e))) 44#define luaT_getimbyObj(L,o,e) (luaT_getim(L, luaT_effectivetag(L, o),(e)))
45 45
46extern const char *const luaT_eventname[]; 46extern const char *const luaT_eventname[];
47 47
48 48
49void luaT_init (lua_State *L); 49void luaT_init (lua_State *L);
50void luaT_realtag (lua_State *L, int tag); 50void luaT_realtag (lua_State *L, int tag);
51int luaT_effectivetag (const TObject *o); 51int luaT_effectivetag (lua_State *L, const TObject *o);
52void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func); 52void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
53const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event); 53const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
54const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *)); 54const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *));
55 55
56int luaT_validevent (int t, int e);
57 56
58#endif 57#endif