Browse Source

new function lua_nextvar

Roberto Ierusalimschy 26 years ago
parent
commit
d5feffdb60
4 changed files with 36 additions and 12 deletions
  1. 28 2
      lapi.c
  2. 2 1
      lapi.h
  3. 4 8
      lbuiltin.c
  4. 2 1
      lua.h

+ 28 - 2
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 1.35 1999/02/08 17:07:59 roberto Exp roberto $
+** $Id: lapi.c,v 1.36 1999/02/12 19:23:02 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -426,10 +426,36 @@ void lua_settag (int tag)
 }
 }
 
 
 
 
+TaggedString *luaA_nextvar (TaggedString *g) {
+  if (g == NULL)
+    g = (TaggedString *)L->rootglobal.next;  /* first variable */
+  else {
+    /* check whether name is in global var list */
+    luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
+    g = (TaggedString *)g->head.next;  /* get next */
+  }
+  while (g && g->u.s.globalval.ttype == LUA_T_NIL)  /* skip globals with nil */
+    g = (TaggedString *)g->head.next;
+  return g;
+}
+
+
+char *lua_nextvar (char *varname) {
+  TaggedString *g = (varname == NULL) ? NULL : luaS_new(varname);
+  g = luaA_nextvar(g);
+  if (g) {
+    luaA_pushobject(&g->u.s.globalval);
+    return g->str;
+  }
+  else
+    return NULL;
+}
+
+
 
 
 /*
 /*
 ** {======================================================
 ** {======================================================
-** To manipulate the implementation global variables
+** To manipulate some state information
 ** =======================================================
 ** =======================================================
 */
 */
 
 

+ 2 - 1
lapi.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.h,v 1.1 1997/09/16 19:25:59 roberto Exp roberto $
+** $Id: lapi.h,v 1.2 1998/06/19 16:14:09 roberto Exp roberto $
 ** Auxiliary functions from Lua API
 ** Auxiliary functions from Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -16,5 +16,6 @@ TObject *luaA_Address (lua_Object o);
 void luaA_pushobject (TObject *o);
 void luaA_pushobject (TObject *o);
 void luaA_packresults (void);
 void luaA_packresults (void);
 int luaA_passresults (void);
 int luaA_passresults (void);
+TaggedString *luaA_nextvar (TaggedString *g);
 
 
 #endif
 #endif

+ 4 - 8
lbuiltin.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbuiltin.c,v 1.51 1999/02/12 19:23:02 roberto Exp roberto $
+** $Id: lbuiltin.c,v 1.52 1999/02/22 14:17:24 roberto Exp roberto $
 ** Built-in functions
 ** Built-in functions
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -517,16 +517,12 @@ static void luaB_nextvar (void) {
   TObject *o = luaA_Address(luaL_nonnullarg(1));
   TObject *o = luaA_Address(luaL_nonnullarg(1));
   TaggedString *g;
   TaggedString *g;
   if (ttype(o) == LUA_T_NIL)
   if (ttype(o) == LUA_T_NIL)
-    g = (TaggedString *)L->rootglobal.next;  /* first variable */
+    g = NULL;
   else {
   else {
     luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
     luaL_arg_check(ttype(o) == LUA_T_STRING, 1, "variable name expected");
-    g = tsvalue(o);  /* find given variable name */
-    /* check whether name is in global var list */
-    luaL_arg_check((GCnode *)g != g->head.next, 1, "variable name expected");
-    g = (TaggedString *)g->head.next;  /* get next */
+    g = tsvalue(o);
   }
   }
-  while (g && g->u.s.globalval.ttype == LUA_T_NIL)  /* skip globals with nil */
-    g = (TaggedString *)g->head.next;
+  g = luaA_nextvar(g);
   if (g) {
   if (g) {
     pushtagstring(g);
     pushtagstring(g);
     luaA_pushobject(&g->u.s.globalval);
     luaA_pushobject(&g->u.s.globalval);

+ 2 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.26 1999/01/26 15:31:17 roberto Exp roberto $
+** $Id: lua.h,v 1.27 1999/02/09 15:59:22 roberto Exp roberto $
 ** Lua - An Extensible Extension Language
 ** Lua - An Extensible Extension Language
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** TeCGraf: Grupo de Tecnologia em Computacao Grafica, PUC-Rio, Brazil
 ** e-mail: [email protected]
 ** e-mail: [email protected]
@@ -89,6 +89,7 @@ lua_Object     lua_rawgettable		(void); /* In: table, index */
 
 
 int            lua_tag			(lua_Object object);
 int            lua_tag			(lua_Object object);
 
 
+char          *lua_nextvar		(char *varname);  /* Out: value */
 
 
 int            lua_ref			(int lock); /* In: value */
 int            lua_ref			(int lock); /* In: value */
 lua_Object     lua_getref		(int ref);
 lua_Object     lua_getref		(int ref);