Browse Source

tags can be positive values

Roberto Ierusalimschy 25 years ago
parent
commit
bb9605bbc9
5 changed files with 57 additions and 67 deletions
  1. 5 6
      lapi.c
  2. 2 2
      ldebug.c
  3. 22 24
      lobject.h
  4. 24 30
      ltm.c
  5. 4 5
      ltm.h

+ 5 - 6
lapi.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lapi.c,v 1.73 2000/03/03 14:58:26 roberto Exp roberto $
+** $Id: lapi.c,v 1.74 2000/03/10 18:37:44 roberto Exp roberto $
 ** Lua API
 ** See Copyright Notice in lua.h
 */
@@ -306,9 +306,9 @@ void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
   luaC_checkGC(L);
 }
 
-void lua_pushusertag (lua_State *L, void *u, int tag) {
-  if (tag < 0 && tag != LUA_ANYTAG)
-    luaT_realtag(L, tag);  /* error if tag is not valid */
+void lua_pushusertag (lua_State *L, void *u, int tag) {  /* ORDER LUA_T */
+  if (tag != LUA_ANYTAG && tag != TAG_USERDATA && tag < NUM_TAGS)
+    luaL_verror(L, "invalid tag for a userdata (%d)", tag);
   tsvalue(L->top) = luaS_createudata(L, u, tag);
   ttype(L->top) = TAG_USERDATA;
   incr_top;
@@ -329,13 +329,12 @@ void lua_pushobject (lua_State *L, lua_Object o) {
 
 
 int lua_tag (lua_State *L, lua_Object o) {
-  UNUSED(L);
   if (o == LUA_NOOBJECT)
     return TAG_NIL;
   else if (ttype(o) == TAG_USERDATA)  /* to allow `old' tags (deprecated) */
     return o->value.ts->u.d.tag;
   else
-    return luaT_effectivetag(o);
+    return luaT_effectivetag(L, o);
 }
 
 

+ 2 - 2
ldebug.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldebug.c,v 1.10 2000/03/03 14:58:26 roberto Exp roberto $
+** $Id: ldebug.c,v 1.11 2000/03/10 18:37:44 roberto Exp roberto $
 ** Debug Interface
 ** See Copyright Notice in lua.h
 */
@@ -33,7 +33,7 @@ static const lua_Type normtype[] = {  /* ORDER LUA_T */
 
 static void setnormalized (TObject *d, const TObject *s) {
   d->value = s->value;
-  d->ttype = normtype[-ttype(s)];
+  d->ttype = normtype[ttype(s)];
 }
 
 

+ 22 - 24
lobject.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lobject.h,v 1.51 2000/03/10 18:37:44 roberto Exp roberto $
+** $Id: lobject.h,v 1.52 2000/03/16 21:06:16 roberto Exp roberto $
 ** Type definitions for Lua objects
 ** See Copyright Notice in lua.h
 */
@@ -67,34 +67,32 @@ typedef unsigned long Instruction;
 */
 typedef enum {
   TAG_USERDATA  =  0, /* default tag for userdata */
-  TAG_NUMBER    = -1, /* fixed tag for numbers */
-  TAG_STRING    = -2, /* fixed tag for strings */
-  TAG_ARRAY     = -3, /* default tag for tables (or arrays) */
-  TAG_LPROTO    = -4, /* fixed tag for Lua functions */
-  TAG_CPROTO    = -5, /* fixed tag for C functions */
-  TAG_NIL       = -6, /* last "pre-defined" tag */
-
-  TAG_LCLOSURE  = -7, /* Lua closure */
-  TAG_CCLOSURE  = -8, /* C closure */
-
-  TAG_LCLMARK   = -9 ,/* mark for Lua closures */
-  TAG_CCLMARK   = -10,/* mark for C closures */
-  TAG_LMARK     = -11,/* mark for Lua prototypes */
-  TAG_CMARK     = -12,/* mark for C prototypes */
-
-  TAG_LINE     = -13
+  TAG_NUMBER,	/* fixed tag for numbers */
+  TAG_STRING,	/* fixed tag for strings */
+  TAG_ARRAY,	/* default tag for tables (or arrays) */
+  TAG_LPROTO,	/* fixed tag for Lua functions */
+  TAG_CPROTO,	/* fixed tag for C functions */
+  TAG_NIL,	/* last "pre-defined" tag */
+
+  TAG_LCLOSURE,	/* Lua closure */
+  TAG_CCLOSURE,	/* C closure */
+
+  TAG_LCLMARK,	/* mark for Lua closures */
+  TAG_CCLMARK,	/* mark for C closures */
+  TAG_LMARK,	/* mark for Lua prototypes */
+  TAG_CMARK,	/* mark for C prototypes */
+
+  TAG_LINE
 } lua_Type;
 
-#define NUM_TAGS	7	/* tags for values visible from Lua */
+/* tags for values visible from Lua == first user-created tag */
+#define NUM_TAGS	7
 
 
-#define LAST_REGULAR_TAG  TAG_CCLOSURE  /* after that, are all marks */
-
 /*
-** check whether `t' is a mark; ttypes are negative numbers, so the
-** comparisons look reversed.  (ORDER LUA_T)
+** check whether `t' is a mark
 */
-#define is_T_MARK(t)	(TAG_CMARK <= (t) && (t) <= TAG_LCLMARK)
+#define is_T_MARK(t)	(TAG_LCLMARK <= (t) && (t) <= TAG_CMARK)
 
 
 typedef union {
@@ -221,7 +219,7 @@ typedef struct Hash {
 extern const char *const luaO_typenames[];
 extern const TObject luaO_nilobject;
 
-#define luaO_typename(o)        luaO_typenames[-ttype(o)]
+#define luaO_typename(o)        luaO_typenames[ttype(o)]
 
 #define MINPOWER2	4	/* minimum size for "growing" vectors */
 

+ 24 - 30
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 1.33 2000/03/03 14:58:26 roberto Exp roberto $
+** $Id: ltm.c,v 1.34 2000/03/10 18:37:44 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -19,12 +19,16 @@
 
 const char *const luaT_eventname[] = {  /* ORDER IM */
   "gettable", "settable", "index", "getglobal", "setglobal", "add", "sub",
-  "mul", "div", "pow", "unm", "lt", "concat", "gc", "function", NULL
+  "mul", "div", "pow", "unm", "lt", "concat", "gc", "function",
+  "le", "gt", "ge",  /* deprecated options!! */
+  NULL
 };
 
 
-static int luaI_checkevent (lua_State *L, const char *name, const char *const list[]) {
-  int e = luaL_findstring(name, list);
+static int luaI_checkevent (lua_State *L, const char *name) {
+  int e = luaL_findstring(name, luaT_eventname);
+  if (e >= IM_N)
+    luaL_verror(L, "event `%.50s' is deprecated", name);
   if (e < 0)
     luaL_verror(L, "`%.50s' is not a valid event name", name);
   return e;
@@ -46,12 +50,12 @@ static const char luaT_validevents[NUM_TAGS][IM_N] = {
 {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1}   /* TAG_NIL */
 };
 
-int luaT_validevent (int t, int e) {  /* ORDER LUA_T */
+static int luaT_validevent (int t, int e) {  /* ORDER LUA_T */
 #ifdef LUA_COMPAT_GC
   if (t == TAG_ARRAY && e == IM_GC)
     return 1;  /* old versions allowed gc tag method for tables */
 #endif
-  return (t < TAG_NIL) ?  1 : luaT_validevents[-t][e];
+  return (t > TAG_NIL) ?  1 : luaT_validevents[t][e];
 }
 
 
@@ -64,28 +68,28 @@ static void init_entry (lua_State *L, int tag) {
 
 void luaT_init (lua_State *L) {
   int t;
-  L->last_tag = -(NUM_TAGS-1);
+  L->last_tag = NUM_TAGS-1;
   luaM_growvector(L, L->IMtable, 0, NUM_TAGS, struct IM, arrEM, MAX_INT);
-  for (t=L->last_tag; t<=0; t++)
+  for (t=0; t<=L->last_tag; t++)
     init_entry(L, t);
 }
 
 
 int lua_newtag (lua_State *L) {
-  --L->last_tag;
-  luaM_growvector(L, L->IMtable, -(L->last_tag), 1, struct IM, arrEM, MAX_INT);
+  ++L->last_tag;
+  luaM_growvector(L, L->IMtable, L->last_tag, 1, struct IM, arrEM, MAX_INT);
   init_entry(L, L->last_tag);
   return L->last_tag;
 }
 
 
 static void checktag (lua_State *L, int tag) {
-  if (!(L->last_tag <= tag && tag <= 0))
+  if (!(0 <= tag && tag <= L->last_tag))
     luaL_verror(L, "%d is not a valid tag", tag);
 }
 
 void luaT_realtag (lua_State *L, int tag) {
-  if (!(L->last_tag <= tag && tag < TAG_NIL))
+  if (!(NUM_TAGS <= tag && tag <= L->last_tag))
     luaL_verror(L, "tag %d was not created by `newtag'", tag);
 }
 
@@ -102,32 +106,27 @@ int lua_copytagmethods (lua_State *L, int tagto, int tagfrom) {
 }
 
 
-int luaT_effectivetag (const TObject *o) {
+int luaT_effectivetag (lua_State *L, const TObject *o) {
   static const int realtag[] = {  /* ORDER LUA_T */
     TAG_USERDATA, TAG_NUMBER, TAG_STRING, TAG_ARRAY,
     TAG_LPROTO, TAG_CPROTO, TAG_NIL,
     TAG_LPROTO, TAG_CPROTO,       /* TAG_LCLOSURE, TAG_CCLOSURE */
   };
-  int t;
+  lua_Type t;
   switch (t = ttype(o)) {
     case TAG_USERDATA: {
       int tag = o->value.ts->u.d.tag;
-      return (tag >= 0) ? TAG_USERDATA : tag;  /* deprecated test */
+      return (tag > L->last_tag) ? TAG_USERDATA : tag;  /* deprecated test */
     }
     case TAG_ARRAY:    return o->value.a->htag;
-    default:             return realtag[-t];
+    default:             return realtag[t];
   }
 }
 
 
 const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
   int e;
-#ifdef LUA_COMPAT_ORDER_TM
-  static const char *old_order[] = {"le", "gt", "ge", NULL};
-  if (luaL_findstring(event, old_order) >= 0)
-     return &luaO_nilobject;
-#endif
-  e = luaI_checkevent(L, event, luaT_eventname);
+  e = luaI_checkevent(L, event);
   checktag(L, t);
   if (luaT_validevent(t, e))
     return luaT_getim(L, t,e);
@@ -139,16 +138,11 @@ const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event) {
 void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func) {
   TObject temp;
   int e;
-#ifdef LUA_COMPAT_ORDER_TM
-  static const char *old_order[] = {"le", "gt", "ge", NULL};
-  if (luaL_findstring(event, old_order) >= 0)
-     return;  /* do nothing for old operators */
-#endif
-  e = luaI_checkevent(L, event, luaT_eventname);
+  e = luaI_checkevent(L, event);
   checktag(L, t);
   if (!luaT_validevent(t, e))
     luaL_verror(L, "cannot change `%.20s' tag method for type `%.20s'%.20s",
-                luaT_eventname[e], luaO_typenames[-t],
+                luaT_eventname[e], luaO_typenames[t],
                 (t == TAG_ARRAY || t == TAG_USERDATA) ? " with default tag"
                                                           : "");
   temp = *func;
@@ -162,7 +156,7 @@ const char *luaT_travtagmethods (lua_State *L,
   int e;
   for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) {
     int t;
-    for (t=0; t>=L->last_tag; t--)
+    for (t=0; t<=L->last_tag; t++)
       if (fn(L, luaT_getim(L, t,e)))
         return luaT_eventname[e];
   }

+ 4 - 5
ltm.h

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.h,v 1.9 2000/02/22 18:12:46 roberto Exp roberto $
+** $Id: ltm.h,v 1.10 2000/03/03 14:58:26 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -40,19 +40,18 @@ struct IM {
 };
 
 
-#define luaT_getim(L,tag,event) (&L->IMtable[-(tag)].int_method[event])
-#define luaT_getimbyObj(L,o,e)  (luaT_getim(L, luaT_effectivetag(o),(e)))
+#define luaT_getim(L,tag,event) (&L->IMtable[tag].int_method[event])
+#define luaT_getimbyObj(L,o,e)  (luaT_getim(L, luaT_effectivetag(L, o),(e)))
 
 extern const char *const luaT_eventname[];
 
 
 void luaT_init (lua_State *L);
 void luaT_realtag (lua_State *L, int tag);
-int luaT_effectivetag (const TObject *o);
+int luaT_effectivetag (lua_State *L, const TObject *o);
 void luaT_settagmethod (lua_State *L, int t, const char *event, TObject *func);
 const TObject *luaT_gettagmethod (lua_State *L, int t, const char *event);
 const char *luaT_travtagmethods (lua_State *L, int (*fn)(lua_State *, TObject *));
 
-int luaT_validevent (int t, int e);
 
 #endif