Browse Source

userdata can have different tags

Roberto Ierusalimschy 31 years ago
parent
commit
1929ddcf49
3 changed files with 21 additions and 19 deletions
  1. 5 5
      hash.c
  2. 11 10
      lua.h
  3. 5 4
      opcode.c

+ 5 - 5
hash.c

@@ -3,7 +3,7 @@
 ** hash manager for lua
 */
 
-char *rcs_hash="$Id: hash.c,v 2.17 1994/11/16 17:38:08 roberto Exp roberto $";
+char *rcs_hash="$Id: hash.c,v 2.18 1994/11/17 13:58:57 roberto Exp roberto $";
 
 #include "mem.h"
 #include "opcode.h"
@@ -49,6 +49,9 @@ static int hashindex (Hash *t, Object *ref)		/* hash function */
 {
  switch (tag(ref))
  {
+  case LUA_T_NIL:
+   lua_reportbug ("unexpected type to index table");
+   return -1;  /* UNREACHEABLE */
   case LUA_T_NUMBER:
    return (((int)nvalue(ref))%nhash(t));
   case LUA_T_STRING:
@@ -69,11 +72,8 @@ static int hashindex (Hash *t, Object *ref)		/* hash function */
    return (((int)fvalue(ref))%nhash(t));
   case LUA_T_ARRAY:
    return (((int)avalue(ref))%nhash(t));
-  case LUA_T_USERDATA:
+  default:  /* user data */
    return (((int)uvalue(ref))%nhash(t));
-  default:
-   lua_reportbug ("unexpected type to index table");
-   return -1;  /* UNREACHEABLE */
  }
 }
 

+ 11 - 10
lua.h

@@ -2,7 +2,7 @@
 ** LUA - Linguagem para Usuarios de Aplicacao
 ** Grupo de Tecnologia em Computacao Grafica
 ** TeCGraf - PUC-Rio
-** $Id: lua.h,v 3.7 1994/11/13 16:17:04 roberto Exp $
+** $Id: lua.h,v 3.8 1994/11/17 16:41:42 roberto Exp roberto $
 */
 
 
@@ -13,14 +13,13 @@
  
 typedef enum
 {
- LUA_T_MARK,
- LUA_T_NIL,
- LUA_T_NUMBER,
- LUA_T_STRING,
- LUA_T_ARRAY,
- LUA_T_FUNCTION,
- LUA_T_CFUNCTION,
- LUA_T_USERDATA
+ LUA_T_NIL	= -1,
+ LUA_T_NUMBER	= -2,
+ LUA_T_STRING	= -3,
+ LUA_T_ARRAY	= -4,
+ LUA_T_FUNCTION	= -5,
+ LUA_T_CFUNCTION= -6,
+ LUA_T_USERDATA = 0
 } lua_Type;
  
 
@@ -53,7 +52,7 @@ int 	       lua_pushnil 		(void);
 int            lua_pushnumber 		(float n);
 int            lua_pushstring 		(char *s);
 int            lua_pushcfunction	(lua_CFunction fn);
-int            lua_pushuserdata     	(void *u);
+int            lua_pushusertag     	(void *u, int tag);
 int            lua_pushobject       	(lua_Object object);
 
 lua_Object     lua_getglobal 		(char *name);
@@ -77,6 +76,8 @@ lua_Object     lua_createTable		(int initSize);
 #define lua_getindexed(o,n) (lua_pushobject(o), lua_pushnumber(n), lua_getsubscript())
 #define lua_getfield(o,f)   (lua_pushobject(o), lua_pushstring(f), lua_getsubscript())
 
+#define lua_pushuserdata(u)     lua_pushusertag(u,LUA_USERDATA)
+
 #define lua_isnil(_)            (lua_type(_)==LUA_T_NIL)
 #define lua_isnumber(_)         (lua_type(_)==LUA_T_NUMBER)
 #define lua_isstring(_)         (lua_type(_)==LUA_T_STRING)

+ 5 - 4
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 3.15 1994/11/17 16:41:42 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 3.16 1994/11/17 19:43:34 roberto Exp roberto $";
 
 #include <setjmp.h>
 #include <stdio.h>
@@ -536,7 +536,7 @@ lua_CFunction lua_getcfunction (lua_Object object)
 void *lua_getuserdata (lua_Object object)
 {
  if (object == 0) return NULL;
- if (tag(Address(object)) != LUA_T_USERDATA) return NULL;
+ if (tag(Address(object)) < LUA_T_USERDATA) return NULL;
  else return (uvalue(Address(object)));
 }
 
@@ -621,10 +621,11 @@ int lua_pushcfunction (lua_CFunction fn)
 /*
 ** Push an object (tag=userdata) to stack. Return 0 on success or 1 on error.
 */
-int lua_pushuserdata (void *u)
+int lua_pushusertag (void *u, int tag)
 {
  lua_checkstack(top-stack+1);
- tag(top) = LUA_T_USERDATA; uvalue(top++) = u;
+ if (tag < LUA_T_USERDATA) return 1;
+ tag(top) = tag; uvalue(top++) = u;
  return 0;
 }