Browse Source

new implementation for udata (again they are just void *);
new implementation for the API: most operations now do not disturb
structures lua2C and C2lua.

Roberto Ierusalimschy 28 years ago
parent
commit
dd22ea4da5
7 changed files with 163 additions and 136 deletions
  1. 14 7
      fallback.c
  2. 42 28
      inout.c
  3. 11 8
      lua.h
  4. 36 59
      opcode.c
  5. 9 8
      table.c
  6. 41 20
      tree.c
  7. 10 6
      tree.h

+ 14 - 7
fallback.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
  
-char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $";
+char *rcs_fallback="$Id: fallback.c,v 2.5 1997/04/24 22:59:57 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <string.h>
@@ -116,16 +116,20 @@ struct IM *luaI_IMtable = NULL;
 static int IMtable_size = 0;
 static int last_tag = LUA_T_NIL;  /* ORDER LUA_T */
 
+
+/* events in LUA_T_LINE are all allowed, since this is used as a
+*  'placeholder' for "default" fallbacks
+*/
 static char validevents[NUM_TYPES][IM_N] = { /* ORDER LUA_T, ORDER IM */
-{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},  /* LUA_T_USERDATA */
-{0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},  /* LUA_T_LINE */
+{1, 1, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 1},  /* LUA_T_USERDATA */
+{1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},  /* LUA_T_LINE */
 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},  /* LUA_T_CMARK */
 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},  /* LUA_T_MARK */
 {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},  /* LUA_T_CFUNCTION */
 {1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0},  /* LUA_T_FUNCTION */
 {0, 0, 1, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},  /* LUA_T_ARRAY */
 {1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1},  /* LUA_T_STRING */
-{1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1},  /* LUA_T_NUMBER */
+{1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1},  /* LUA_T_NUMBER */
 {0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0}   /* LUA_T_NIL */
 };
 
@@ -186,6 +190,9 @@ void luaI_settag (int tag, TObject *o)
     case LUA_T_ARRAY:
       o->value.a->htag = tag;
       break;
+    case LUA_T_USERDATA:
+      o->value.ts->tag = tag;
+      break;
     default:
       luaL_verror("cannot change the tag of a %s", luaI_typenames[-ttype(o)]);
   }
@@ -318,7 +325,7 @@ void luaI_setfallback (void)
       break;
     case 2: {  /* old arith fallback */
       int i;
-      oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW);
+      oldfunc = *luaI_getim(LUA_T_NUMBER, IM_POW);
       for (i=IM_ADD; i<=IM_UNM; i++)  /* ORDER IM */
         fillvalids(i, luaI_Address(func));
       replace = typeFB;
@@ -326,7 +333,7 @@ void luaI_setfallback (void)
     }
     case 3: {  /* old order fallback */
       int i;
-      oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT);
+      oldfunc = *luaI_getim(LUA_T_LINE, IM_LT);
       for (i=IM_LT; i<=IM_GE; i++)  /* ORDER IM */
         fillvalids(i, luaI_Address(func));
       replace = typeFB;
@@ -335,7 +342,7 @@ void luaI_setfallback (void)
     default: {
       int e;
       if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
-        oldfunc = *luaI_getim(LUA_T_USERDATA, e);
+        oldfunc = *luaI_getim(LUA_T_LINE, e);
         fillvalids(e, luaI_Address(func));
         replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
       }

+ 42 - 28
inout.c

@@ -5,7 +5,7 @@
 ** Also provides some predefined lua functions.
 */
 
-char *rcs_inout="$Id: inout.c,v 2.58 1997/04/15 17:32:47 roberto Exp roberto $";
+char *rcs_inout="$Id: inout.c,v 2.59 1997/05/26 14:42:51 roberto Exp roberto $";
 
 #include <stdio.h>
 #include <string.h>
@@ -103,6 +103,7 @@ void lua_closestring (void)
 }
 
 
+
 static int passresults (void)
 {
   int arg = 0;
@@ -111,6 +112,25 @@ static int passresults (void)
     lua_pushobject(obj);
   return arg-1;
 }
+
+
+static void packresults (void)
+{
+  int arg = 0;
+  lua_Object obj;
+  lua_Object table = lua_createtable();
+  while ((obj = lua_getresult(++arg)) != LUA_NOOBJECT) {
+    lua_pushobject(table);
+    lua_pushnumber(arg);
+    lua_pushobject(obj);
+    lua_rawsettable();
+  }
+  lua_pushobject(table);
+  lua_pushstring("n");
+  lua_pushnumber(arg-1);
+  lua_rawsettable();
+  lua_pushobject(table);  /* final result */
+}
  
 /*
 ** Internal function: do a string
@@ -144,14 +164,8 @@ static char *tostring (lua_Object obj)
     case LUA_T_CFUNCTION: case LUA_T_NIL:
       return luaI_typenames[-ttype(o)];
     case LUA_T_USERDATA: {
-      char *buff = luaI_buffer(100);
-      int size = o->value.ts->size;
-      int i;
-      strcpy(buff, "userdata: ");
-      if (size > 10) size = 10;
-      for (i=0; i<size; i++)
-        sprintf(buff+strlen(buff), "%.2X",
-               (int)(unsigned char)o->value.ts->str[i]);
+      char *buff = luaI_buffer(30);
+      sprintf(buff, "userdata: %p", o->value.ts->u.v);
       return buff;
     }
     default: return "<unknown object>";
@@ -237,37 +251,37 @@ static void luatag (void)
   lua_pushnumber(lua_tag(lua_getparam(1)));
 }
 
-#define MAXPARAMS	256
+
+static int getnarg (lua_Object table)
+{
+  lua_Object temp;
+  /* temp = table.n */
+  lua_pushobject(table); lua_pushstring("n"); temp = lua_gettable();
+  return (lua_isnumber(temp) ? lua_getnumber(temp) : MAX_WORD);
+}
+
 static void luaI_call (void)
 {
   lua_Object f = lua_getparam(1);
   lua_Object arg = lua_getparam(2);
-  lua_Object temp, params[MAXPARAMS];
+  int withtable = (luaL_opt_string(3, NULL) != NULL);
   int narg, i;
   luaL_arg_check(lua_isfunction(f), 1, "function expected");
   luaL_arg_check(lua_istable(arg), 2, "table expected");
-  /* narg = arg.n */
-  lua_pushobject(arg);
-  lua_pushstring("n");
-  temp = lua_getsubscript();
-  narg = lua_isnumber(temp) ? lua_getnumber(temp) : MAXPARAMS+1;
-  /* read arg[1...n] */
+  narg = getnarg(arg);
+  /* push arg[1...n] */
   for (i=0; i<narg; i++) {
-    if (i>=MAXPARAMS)
-      lua_error("argument list too long in function `call'");
-    lua_pushobject(arg);
-    lua_pushnumber(i+1);
-    params[i] = lua_getsubscript();
-    if (narg == MAXPARAMS+1 && lua_isnil(params[i])) {
-      narg = i;
+    lua_Object temp;
+    /* temp = arg[i+1] */
+    lua_pushobject(arg); lua_pushnumber(i+1); temp = lua_gettable();
+    if (narg == MAX_WORD && lua_isnil(temp))
       break;
-    }
+    lua_pushobject(temp);
   }
-  /* push parameters and do the call */
-  for (i=0; i<narg; i++)
-    lua_pushobject(params[i]);
   if (lua_callfunction(f))
     lua_error(NULL);
+  else if (withtable)
+    packresults();
   else
     passresults();
 }

+ 11 - 8
lua.h

@@ -2,7 +2,7 @@
 ** LUA - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
-** $Id: lua.h,v 4.4 1997/05/26 14:42:51 roberto Exp roberto $
+** $Id: lua.h,v 4.5 1997/06/06 20:54:40 roberto Exp roberto $
 */
 
 
@@ -16,6 +16,8 @@
 
 #define LUA_NOOBJECT  0
 
+#define LUA_ANYTAG    (-1)
+
 typedef void (*lua_CFunction) (void);
 typedef unsigned int lua_Object;
 
@@ -31,8 +33,6 @@ int            lua_dofile 		(char *filename); /* Out: returns */
 int            lua_dostring 		(char *string); /* Out: returns */
 int            lua_callfunction		(lua_Object f);
 					  /* In: parameters; Out: returns */
-int	       lua_call			(char *funcname);
-					  /* In: parameters; Out: returns */
 
 void	       lua_beginblock		(void);
 void	       lua_endblock		(void);
@@ -52,17 +52,16 @@ int            lua_isfunction           (lua_Object object);
 float          lua_getnumber 		(lua_Object object);
 char          *lua_getstring 		(lua_Object object);
 lua_CFunction  lua_getcfunction 	(lua_Object object);
-void          *lua_getbindata		(lua_Object object);
-int            lua_getbindatasize	(lua_Object object);
 
 void 	       lua_pushnil 		(void);
 void           lua_pushnumber 		(float n);
 void           lua_pushstring 		(char *s);
 void           lua_pushcfunction	(lua_CFunction fn);
-void           lua_pushbindata		(void *buff, int size, int tag);
-void           lua_pushusertag     	(void *u, int tag);
+void           lua_pushusertag          (void *u, int tag);
 void           lua_pushobject       	(lua_Object object);
 
+lua_Object     lua_pop			(void);
+
 lua_Object     lua_getglobal 		(char *name);
 lua_Object     lua_rawgetglobal		(char *name);
 void           lua_setglobal		(char *name); /* In: value */
@@ -82,6 +81,8 @@ void	       lua_unref		(int ref);
 
 lua_Object     lua_createtable		(void);
 
+lua_Object     lua_getudata		(void *u, int tag);
+
 
 long	       lua_collectgarbage	(long limit);
 
@@ -89,7 +90,9 @@ long	       lua_collectgarbage	(long limit);
 /* =============================================================== */
 /* some useful macros */
 
-#define lua_pushref(ref)	(lua_pushobject(lua_getref(ref)))
+#define lua_call(name)		lua_callfunction(lua_getglobal(name))
+
+#define lua_pushref(ref)	lua_pushobject(lua_getref(ref))
 
 #define lua_refobject(o,l)	(lua_pushobject(o), lua_ref(l))
 

+ 36 - 59
opcode.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
 
-char *rcs_opcode="$Id: opcode.c,v 4.5 1997/05/26 14:23:55 roberto Exp roberto $";
+char *rcs_opcode="$Id: opcode.c,v 4.6 1997/06/06 20:54:40 roberto Exp roberto $";
 
 #include <setjmp.h>
 #include <stdio.h>
@@ -52,13 +52,13 @@ static TObject *top = &initial_stack;
 #define incr_top	if (++top >= stackLimit) growstack()
 
 struct C_Lua_Stack {
- StkId base;  /* when Lua calls C or C calls Lua, points to */
-              /* the first slot after the last parameter. */
- int num;     /* when Lua calls C, has the number of parameters; */
-              /* when C calls Lua, has the number of results. */
+  StkId base;  /* when Lua calls C or C calls Lua, points to */
+               /* the first slot after the last parameter. */
+  StkId lua2C; /* points to first element of "array" lua2C */
+  int num;     /* size of "array" lua2C */
 };
 
-static struct C_Lua_Stack CLS_current = {0, 0};
+static struct C_Lua_Stack CLS_current = {0, 0, 0};
 
 static  jmp_buf *errorJmp = NULL; /* current error recover point */
 
@@ -228,6 +228,13 @@ static lua_Object put_luaObjectonTop (void)
 }
 
 
+lua_Object lua_pop (void)
+{
+  checkCparams(1);
+  return put_luaObjectonTop();
+}
+
+
 
 /*
 ** call Line hook
@@ -235,7 +242,7 @@ static lua_Object put_luaObjectonTop (void)
 static void lineHook (int line)
 {
   struct C_Lua_Stack oldCLS = CLS_current;
-  StkId old_top = CLS_current.base = top-stack;
+  StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
   CLS_current.num = 0;
   (*lua_linehook)(line);
   top = stack+old_top;
@@ -250,7 +257,7 @@ static void lineHook (int line)
 static void callHook (StkId base, lua_Type type, int isreturn)
 {
   struct C_Lua_Stack oldCLS = CLS_current;
-  StkId old_top = CLS_current.base = top-stack;
+  StkId old_top = CLS_current.lua2C = CLS_current.base = top-stack;
   CLS_current.num = 0;
   if (isreturn)
     (*lua_callhook)(LUA_NOOBJECT, "(return)", 0);
@@ -278,6 +285,7 @@ static StkId callC (lua_CFunction func, StkId base)
   StkId firstResult;
   CLS_current.num = (top-stack) - base;
   /* incorporate parameters on the stack */
+  CLS_current.lua2C = base;
   CLS_current.base = base+CLS_current.num;  /* == top-stack */
   if (lua_callhook)
     callHook(base, LUA_T_CMARK, 0);
@@ -518,7 +526,7 @@ int lua_setlocal (lua_Function func, int local_number)
 {
   TObject *f = Address(func);
   char *name = luaI_getlocalname(f->value.tf, local_number, lua_currentline(func));
-  adjustC(1);
+  checkCparams(1);
   --top;
   if (name)
   {
@@ -537,9 +545,11 @@ int lua_setlocal (lua_Function func, int local_number)
 */
 static void do_callinc (int nResults)
 {
-  do_call(CLS_current.base+1, nResults);
-  CLS_current.num = (top-stack) - CLS_current.base;  /* number of results */
-  CLS_current.base += CLS_current.num;  /* incorporate results on the stack */
+  StkId base = CLS_current.base;
+  do_call(base+1, nResults);
+  CLS_current.lua2C = base;  /* position of the new results */
+  CLS_current.num = (top-stack) - base;  /* number of results */
+  CLS_current.base = base + CLS_current.num;  /* incorporate results on stack */
 }
 
 static void do_unprotectedrun (lua_CFunction f, int nParams, int nResults)
@@ -628,15 +638,6 @@ int lua_callfunction (lua_Object function)
 }
 
 
-int lua_call (char *funcname)
-{
- Word n = luaI_findsymbolbyname(funcname);
- open_stack((top-stack)-CLS_current.base);
- stack[CLS_current.base] = s_object(n);
- return do_protectedrun(MULT_RET);
-}
-
-
 /*
 ** Open file, generate opcode and execute global statement. Return 0 on
 ** success or non 0 on error.
@@ -791,9 +792,9 @@ lua_Object lua_createtable (void)
 lua_Object lua_lua2C (int number)
 {
   if (number <= 0 || number > CLS_current.num) return LUA_NOOBJECT;
-  /* Ref(stack+(CLS_current.base-CLS_current.num+number-1)) ==
-     stack+(CLS_current.base-CLS_current.num+number-1)-stack+1 == */
-  return CLS_current.base-CLS_current.num+number;
+  /* Ref(stack+(CLS_current.lua2C+number-1)) ==
+     stack+(CLS_current.lua2C+number-1)-stack+1 == */
+  return CLS_current.lua2C+number;
 }
 
 int lua_isnil (lua_Object o)
@@ -850,33 +851,20 @@ real lua_getnumber (lua_Object object)
 */
 char *lua_getstring (lua_Object object)
 {
- if (object == LUA_NOOBJECT) return NULL;
- if (tostring (Address(object))) return NULL;
- else return (svalue(Address(object)));
-}
-
-void *lua_getbindata (lua_Object object)
-{
-  if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
+  if (object == LUA_NOOBJECT || tostring (Address(object)))
     return NULL;
-  else return svalue(Address(object));
-}
-
-void *lua_getuserdata (lua_Object object)
-{
-  void *add = lua_getbindata(object);
-  if (add == NULL) return NULL;
-  else return *(void **)add;
+  else return (svalue(Address(object)));
 }
 
 
-int lua_getbindatasize (lua_Object object)
+void *lua_getuserdata (lua_Object object)
 {
   if (object == LUA_NOOBJECT || ttype(Address(object)) != LUA_T_USERDATA)
-    return 0;
-  else return (Address(object))->value.ts->size;
+    return NULL;
+  else return tsvalue(Address(object))->u.v;
 }
 
+
 /*
 ** Given an object handle, return its cfuntion pointer. On error, return NULL.
 */
@@ -1000,25 +988,14 @@ void lua_pushcfunction (lua_CFunction fn)
  incr_top;
 }
 
-void lua_pushbindata (void *buff, int size, int tag)
-{
-  if (buff == NULL)
-    ttype(top) = LUA_T_NIL;
-  else {
-    if (tag < 0)
-      luaI_realtag(tag);
-    tsvalue(top) = luaI_createuserdata(buff, size, tag);
-    ttype(top) = LUA_T_USERDATA;
-  }
-  incr_top;
-}
 
-/*
-** Push an object (ttype=userdata) to stack.
-*/
+
 void lua_pushusertag (void *u, int tag)
 {
-  lua_pushbindata(&u, sizeof(void *), tag);
+  if (tag < 0) luaI_realtag(tag);  /* error if tag is not valid */
+  tsvalue(top) = luaI_createudata(u, tag);
+  ttype(top) = LUA_T_USERDATA;
+  incr_top;
 }
 
 /*

+ 9 - 8
table.c

@@ -3,7 +3,7 @@
 ** Module to control static tables
 */
 
-char *rcs_table="$Id: table.c,v 2.69 1997/05/14 18:38:29 roberto Exp roberto $";
+char *rcs_table="$Id: table.c,v 2.70 1997/05/26 14:42:51 roberto Exp roberto $";
 
 #include "luamem.h"
 #include "auxlib.h"
@@ -59,17 +59,17 @@ void luaI_initconstant (void)
 */
 Word luaI_findsymbol (TaggedString *t)
 {
- if (t->varindex == NOT_USED)
+ if (t->u.s.varindex == NOT_USED)
  {
   if (lua_ntable == lua_maxsymbol)
     lua_maxsymbol = growvector(&lua_table, lua_maxsymbol, Symbol,
                       symbolEM, MAX_WORD);
-  t->varindex = lua_ntable;
+  t->u.s.varindex = lua_ntable;
   lua_table[lua_ntable].varname = t;
   s_ttype(lua_ntable) = LUA_T_NIL;
   lua_ntable++;
  }
- return t->varindex;
+ return t->u.s.varindex;
 }
 
 
@@ -85,16 +85,16 @@ Word luaI_findsymbolbyname (char *name)
 */
 Word luaI_findconstant (TaggedString *t)
 {
- if (t->constindex == NOT_USED)
+ if (t->u.s.constindex == NOT_USED)
  {
   if (lua_nconstant == lua_maxconstant)
     lua_maxconstant = growvector(&lua_constant, lua_maxconstant, TaggedString *,
                         constantEM, MAX_WORD);
-  t->constindex = lua_nconstant;
+  t->u.s.constindex = lua_nconstant;
   lua_constant[lua_nconstant] = t;
   lua_nconstant++;
  }
- return t->constindex;
+ return t->u.s.constindex;
 }
 
 
@@ -154,7 +154,7 @@ int luaI_ismarked (TObject *o)
 {
   switch (o->ttype)
   {
-   case LUA_T_STRING:
+   case LUA_T_STRING: case LUA_T_USERDATA:
      return o->value.ts->marked;
    case LUA_T_FUNCTION:
     return o->value.tf->marked;
@@ -196,6 +196,7 @@ long lua_collectgarbage (long limit)
   TaggedString *freestr;
   TFunc *freefunc;
   markall();
+  luaI_invalidaterefs();
   freetable = luaI_hashcollector(&recovered);
   freestr = luaI_strcollector(&recovered);
   freefunc = luaI_funccollector(&recovered);

+ 41 - 20
tree.c

@@ -3,7 +3,7 @@
 ** TecCGraf - PUC-Rio
 */
  
-char *rcs_tree="$Id: tree.c,v 1.25 1997/05/05 20:21:23 roberto Exp roberto $";
+char *rcs_tree="$Id: tree.c,v 1.26 1997/05/14 18:38:29 roberto Exp roberto $";
 
 
 #include <string.h>
@@ -29,15 +29,21 @@ static int initialized = 0;
 
 static stringtable string_root[NUM_HASHS];
 
-static TaggedString EMPTY = {LUA_T_STRING, NULL, 0, NOT_USED, NOT_USED,
+static TaggedString EMPTY = {LUA_T_STRING, NULL, {{NOT_USED, NOT_USED}},
                              0, 2, {0}};
 
 
-static unsigned long hash (char *buff, long size)
+static unsigned long hash (char *s, int tag)
 {
-  unsigned long h = 0;
-  while (size--)
-    h = ((h<<5)-h)^(unsigned char)*(buff++);
+  unsigned long h;
+  if (tag != LUA_T_STRING)
+    h = (unsigned long)s;
+  else {
+    long size = strlen(s);
+    h = 0;
+    while (size--)
+      h = ((h<<5)-h)^(unsigned char)*(s++);
+  }
   return h;
 }
 
@@ -86,10 +92,30 @@ static void grow (stringtable *tb)
   tb->hash = newhash;
 }
 
-static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
+
+static TaggedString *newone(char *buff, int tag, unsigned long h)
 {
   TaggedString *ts;
-  unsigned long h = hash(buff, size);
+  if (tag == LUA_T_STRING) {
+    ts = (TaggedString *)luaI_malloc(sizeof(TaggedString)+strlen(buff));
+    strcpy(ts->str, buff);
+    ts->u.s.varindex = ts->u.s.constindex = NOT_USED;
+    ts->tag = LUA_T_STRING;
+  }
+  else {
+    ts = (TaggedString *)luaI_malloc(sizeof(TaggedString));
+    ts->u.v = buff;
+    ts->tag = tag == LUA_ANYTAG ? 0 : tag;
+  }
+  ts->marked = 0;
+  ts->hash = h;
+  return ts;
+}
+
+static TaggedString *insert (char *buff, int tag, stringtable *tb)
+{
+  TaggedString *ts;
+  unsigned long h = hash(buff, tag);
   int i;
   int j = -1;
   if ((Long)tb->nuse*3 >= (Long)tb->size*2)
@@ -103,8 +129,9 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
   {
     if (ts == &EMPTY)
       j = i;
-    else if (ts->size == size && ts->tag == tag &&
-             memcmp(buff, ts->str, size) == 0)
+    else if ((ts->tag == LUA_T_STRING) ?
+              (tag == LUA_T_STRING && (strcmp(buff, ts->str) == 0)) :
+              ((tag == ts->tag || tag == LUA_ANYTAG) && buff == ts->u.v))
       return ts;
     i = (i+1)%tb->size;
   }
@@ -114,24 +141,18 @@ static TaggedString *insert (char *buff, long size, int tag, stringtable *tb)
     i = j;
   else
     tb->nuse++;
-  ts = tb->hash[i] = (TaggedString *)luaI_malloc(sizeof(TaggedString)+size-1);
-  memcpy(ts->str, buff, size);
-  ts->tag = tag;
-  ts->size = size;
-  ts->marked = 0;
-  ts->hash = h;
-  ts->varindex = ts->constindex = NOT_USED;
+  ts = tb->hash[i] = newone(buff, tag, h);
   return ts;
 }
 
-TaggedString *luaI_createuserdata (char *buff, long size, int tag)
+TaggedString *luaI_createudata (void *udata, int tag)
 {
-  return insert(buff, size, tag, &string_root[(unsigned)buff[0]%NUM_HASHS]);
+  return insert(udata, tag, &string_root[(unsigned)udata%NUM_HASHS]);
 }
 
 TaggedString *lua_createstring (char *str)
 {
-  return luaI_createuserdata(str, strlen(str)+1, LUA_T_STRING);
+  return insert(str, LUA_T_STRING, &string_root[(unsigned)str[0]%NUM_HASHS]);
 }
 
 

+ 10 - 6
tree.h

@@ -1,7 +1,7 @@
 /*
 ** tree.h
 ** TecCGraf - PUC-Rio
-** $Id: tree.h,v 1.16 1997/03/19 19:41:10 roberto Exp roberto $
+** $Id: tree.h,v 1.17 1997/05/14 18:38:29 roberto Exp roberto $
 */
 
 #ifndef tree_h
@@ -16,17 +16,21 @@ typedef struct TaggedString
 {
   int tag;  /* if != LUA_T_STRING, this is a userdata */
   struct TaggedString *next;
-  long size;
-  Word varindex;  /* != NOT_USED  if this is a symbol */
-  Word constindex;  /* != NOT_USED  if this is a constant */
+  union {
+    struct {
+      Word varindex;  /* != NOT_USED  if this is a symbol */
+      Word constindex;  /* != NOT_USED  if this is a constant */
+    } s;
+    void *v;  /* if this is a userdata, here is its value */
+  } u;
   unsigned long hash;  /* 0 if not initialized */
   int marked;   /* for garbage collection; never collect (nor change) if > 1 */
-  char str[1];   /* \0 byte already reserved; MAY BE NOT 0 TERMINATED!! */
+  char str[1];   /* \0 byte already reserved */
 } TaggedString;
  
 
 TaggedString *lua_createstring (char *str);
-TaggedString *luaI_createuserdata (char *buff, long size, int tag);
+TaggedString *luaI_createudata (void *udata, int tag);
 TaggedString *luaI_strcollector (long *cont);
 void luaI_strfree (TaggedString *l);
 void luaI_strcallIM (TaggedString *l);