Explorar o código

new API function and built-in "lua_copytagmethods"

Roberto Ierusalimschy %!s(int64=28) %!d(string=hai) anos
pai
achega
4daae2165d
Modificáronse 3 ficheiros con 26 adicións e 4 borrados
  1. 9 1
      lbuiltin.c
  2. 14 2
      ltm.c
  3. 3 1
      lua.h

+ 9 - 1
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbuiltin.c,v 1.14 1997/12/01 20:30:44 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.15 1997/12/09 13:35:19 roberto Exp roberto $
 ** Built-in functions
 ** See Copyright Notice in lua.h
 */
@@ -299,6 +299,13 @@ static void newtag (void)
 }
 
 
+static void copytagmethods (void)
+{
+  lua_pushnumber(lua_copytagmethods(luaL_check_number(1),
+                                    luaL_check_number(2)));
+}
+
+
 static void rawgettable (void)
 {
   lua_Object t = luaL_nonnullarg(1);
@@ -431,6 +438,7 @@ static struct luaL_reg int_funcs[] = {
   {"call", luaI_call},
   {"collectgarbage", luaI_collectgarbage},
   {"dofile", internaldofile},
+  {"copytagmethods", copytagmethods},
   {"dostring", internaldostring},
   {"error", luaI_error},
   {"foreach", foreach},

+ 14 - 2
ltm.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ltm.c,v 1.9 1997/11/19 18:16:33 roberto Exp roberto $
+** $Id: ltm.c,v 1.10 1997/12/11 14:48:46 roberto Exp roberto $
 ** Tag methods
 ** See Copyright Notice in lua.h
 */
@@ -46,7 +46,7 @@ static char validevents[NUM_TAGS][IM_N] = { /* ORDER LUA_T, ORDER IM */
 };
 
 
-static int validevent (lua_Type t, int e)
+static int validevent (int t, int e)
 { /* ORDER LUA_T */
   return (t < LUA_T_NIL) ?  1 : validevents[-t][e];
 }
@@ -95,6 +95,18 @@ void luaT_realtag (int tag)
 }
 
 
+int lua_copytagmethods (int tagto, int tagfrom)
+{
+  int e;
+  checktag(tagto);
+  checktag(tagfrom);
+  for (e=0; e<IM_N; e++) {
+    if (validevent(tagto, e))
+      *luaT_getim(tagto, e) = *luaT_getim(tagfrom, e);
+  }
+  return tagto;
+}
+
 
 int luaT_efectivetag (TObject *o)
 {

+ 3 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.h,v 1.8 1997/12/01 20:31:25 roberto Exp roberto $
+** $Id: lua.h,v 1.9 1997/12/09 13:35:19 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
@@ -64,6 +64,7 @@ lua_Object     lua_gettagmethod	(int tag, char *event);
 lua_Object     lua_seterrormethod (void);  /* In: new method */
 
 int            lua_newtag		(void);
+int            lua_copytagmethods	(int tagto, int tagfrom);
 void           lua_settag		(int tag); /* In: object */
 
 void           lua_error		(char *s);
@@ -139,6 +140,7 @@ long	       lua_collectgarbage	(long limit);
 
 #define lua_pushcfunction(f)	lua_pushCclosure(f, 0)
 
+#define lua_clonetag(t)		lua_copytagmethods(lua_newtag(), (t))
 
 
 /* ==========================================================================