Browse Source

new API functions lua_getstr/lua_setstr

Roberto Ierusalimschy 23 years ago
parent
commit
9cd36059ad
7 changed files with 30 additions and 45 deletions
  1. 4 4
      lapi.c
  2. 1 2
      lauxlib.c
  3. 1 2
      lbaselib.c
  4. 8 14
      ldblib.c
  5. 8 16
      liolib.c
  6. 2 3
      lua.c
  7. 6 4
      lua.h

+ 4 - 4
lapi.c

@@ -329,11 +329,11 @@ LUA_API void lua_pushcclosure (lua_State *L, lua_CFunction fn, int n) {
 */
 */
 
 
 
 
-LUA_API void lua_getglobal (lua_State *L, const char *name) {
+LUA_API void lua_getstr (lua_State *L, int index, const char *name) {
   TObject o;
   TObject o;
   lua_lock(L);
   lua_lock(L);
   setsvalue(&o, luaS_new(L, name));
   setsvalue(&o, luaS_new(L, name));
-  luaV_gettable(L, gt(L), &o, L->top);
+  luaV_gettable(L, luaA_index(L, index), &o, L->top);
   api_incr_top(L);
   api_incr_top(L);
   lua_unlock(L);
   lua_unlock(L);
 }
 }
@@ -406,12 +406,12 @@ LUA_API void lua_geteventtable (lua_State *L, int objindex) {
 */
 */
 
 
 
 
-LUA_API void lua_setglobal (lua_State *L, const char *name) {
+LUA_API void lua_setstr (lua_State *L, int index, const char *name) {
   TObject o;
   TObject o;
   lua_lock(L);
   lua_lock(L);
   api_checknelems(L, 1);
   api_checknelems(L, 1);
   setsvalue(&o, luaS_new(L, name));
   setsvalue(&o, luaS_new(L, name));
-  luaV_settable(L, gt(L), &o, L->top - 1);
+  luaV_settable(L, luaA_index(L, index), &o, L->top - 1);
   L->top--;  /* remove element from the top */
   L->top--;  /* remove element from the top */
   lua_unlock(L);
   lua_unlock(L);
 }
 }

+ 1 - 2
lauxlib.c

@@ -228,9 +228,8 @@ LUALIB_API int luaL_ref (lua_State *L, int t) {
   }
   }
   else {  /* no free elements */
   else {  /* no free elements */
     ref = lua_getn(L, t) + 1;  /* use next `n' */
     ref = lua_getn(L, t) + 1;  /* use next `n' */
-    lua_pushliteral(L, "n");
     lua_pushnumber(L, ref);
     lua_pushnumber(L, ref);
-    lua_settable(L, t);  /* n = n+1 */
+    lua_setstr(L, t, "n");  /* n = n+1 */
   }
   }
   lua_rawseti(L, t, ref);
   lua_rawseti(L, t, ref);
   return ref;
   return ref;

+ 1 - 2
lbaselib.c

@@ -20,9 +20,8 @@
 
 
 
 
 static void aux_setn (lua_State *L, int t, int n) {
 static void aux_setn (lua_State *L, int t, int n) {
-  lua_pushliteral(L, "n");
   lua_pushnumber(L, n);
   lua_pushnumber(L, n);
-  lua_settable(L, t);
+  lua_setstr(L, t, "n");
 }
 }
 
 
 
 

+ 8 - 14
ldblib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ldblib.c,v 1.40 2001/10/26 17:33:30 roberto Exp $
+** $Id: ldblib.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
 ** Interface from Lua to its debug API
 ** Interface from Lua to its debug API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -18,16 +18,14 @@
 
 
 
 
 static void settabss (lua_State *L, const char *i, const char *v) {
 static void settabss (lua_State *L, const char *i, const char *v) {
-  lua_pushstring(L, i);
   lua_pushstring(L, v);
   lua_pushstring(L, v);
-  lua_settable(L, -3);
+  lua_setstr(L, -2, i);
 }
 }
 
 
 
 
 static void settabsi (lua_State *L, const char *i, int v) {
 static void settabsi (lua_State *L, const char *i, int v) {
-  lua_pushstring(L, i);
   lua_pushnumber(L, v);
   lua_pushnumber(L, v);
-  lua_settable(L, -3);
+  lua_setstr(L, -2, i);
 }
 }
 
 
 
 
@@ -71,9 +69,8 @@ static int getinfo (lua_State *L) {
         settabss(L, "namewhat", ar.namewhat);
         settabss(L, "namewhat", ar.namewhat);
         break;
         break;
       case 'f':
       case 'f':
-        lua_pushliteral(L, "func");
-        lua_pushvalue(L, -3);
-        lua_settable(L, -3);
+        lua_pushvalue(L, -2);
+        lua_setstr(L, -2, "func");
         break;
         break;
     }
     }
   }
   }
@@ -115,8 +112,7 @@ static int setlocal (lua_State *L) {
 
 
 
 
 static void hookf (lua_State *L, const char *key) {
 static void hookf (lua_State *L, const char *key) {
-  lua_pushstring(L, key);
-  lua_gettable(L, LUA_REGISTRYINDEX);
+  lua_getstr(L, LUA_REGISTRYINDEX, key);
   if (lua_isfunction(L, -1)) {
   if (lua_isfunction(L, -1)) {
     lua_pushvalue(L, -2);  /* original argument (below function) */
     lua_pushvalue(L, -2);  /* original argument (below function) */
     lua_rawcall(L, 1, 0);
     lua_rawcall(L, 1, 0);
@@ -147,11 +143,9 @@ static void sethook (lua_State *L, const char *key, lua_Hook hook,
     (*sethookf)(L, hook);
     (*sethookf)(L, hook);
   else
   else
     luaL_argerror(L, 1, "function expected");
     luaL_argerror(L, 1, "function expected");
-  lua_pushstring(L, key);
-  lua_gettable(L, LUA_REGISTRYINDEX);   /* get old value */
-  lua_pushstring(L, key);
+  lua_getstr(L, LUA_REGISTRYINDEX, key);   /* get old value */
   lua_pushvalue(L, 1);
   lua_pushvalue(L, 1);
-  lua_settable(L, LUA_REGISTRYINDEX);  /* set new value */
+  lua_setstr(L, LUA_REGISTRYINDEX, key);  /* set new value */
 }
 }
 
 
 
 

+ 8 - 16
liolib.c

@@ -73,8 +73,7 @@ static int pushresult (lua_State *L, int i) {
 static int checkfile (lua_State *L, int findex, const char *tname) {
 static int checkfile (lua_State *L, int findex, const char *tname) {
   int res;
   int res;
   lua_geteventtable(L, findex);
   lua_geteventtable(L, findex);
-  lua_pushstring(L, tname);
-  lua_gettable(L, LUA_REGISTRYINDEX);
+  lua_getstr(L, LUA_REGISTRYINDEX, tname);
   res = lua_equal(L, -1, -2);
   res = lua_equal(L, -1, -2);
   lua_pop(L, 2);
   lua_pop(L, 2);
   return res;
   return res;
@@ -112,8 +111,7 @@ static FILE *getopthandle (lua_State *L, int inout) {
 
 
 static void newfile (lua_State *L, FILE *f) {
 static void newfile (lua_State *L, FILE *f) {
   lua_newuserdatabox(L, f);
   lua_newuserdatabox(L, f);
-  lua_pushliteral(L, FILEHANDLE);
-  lua_gettable(L, LUA_REGISTRYINDEX);
+  lua_getstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
   lua_seteventtable(L, -2);
   lua_seteventtable(L, -2);
 }
 }
 
 
@@ -149,8 +147,7 @@ static int io_close (lua_State *L) {
   int status = 1;
   int status = 1;
   if (f != stdin && f != stdout && f != stderr) {
   if (f != stdin && f != stdout && f != stderr) {
     lua_settop(L, 1);  /* make sure file is on top */
     lua_settop(L, 1);  /* make sure file is on top */
-    lua_pushliteral(L, CLOSEDFILEHANDLE);
-    lua_gettable(L, LUA_REGISTRYINDEX);
+    lua_getstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
     lua_seteventtable(L, 1);
     lua_seteventtable(L, 1);
     status = (CLOSEFILE(L, f) == 0);
     status = (CLOSEFILE(L, f) == 0);
   }
   }
@@ -470,16 +467,14 @@ static int io_clock (lua_State *L) {
 */
 */
 
 
 static void setfield (lua_State *L, const char *key, int value) {
 static void setfield (lua_State *L, const char *key, int value) {
-  lua_pushstring(L, key);
   lua_pushnumber(L, value);
   lua_pushnumber(L, value);
-  lua_rawset(L, -3);
+  lua_setstr(L, -2, key);
 }
 }
 
 
 
 
 static int getfield (lua_State *L, const char *key, int d) {
 static int getfield (lua_State *L, const char *key, int d) {
   int res;
   int res;
-  lua_pushstring(L, key);
-  lua_rawget(L, -2);
+  lua_getstr(L, -1, key);
   if (lua_isnumber(L, -1))
   if (lua_isnumber(L, -1))
     res = (int)(lua_tonumber(L, -1));
     res = (int)(lua_tonumber(L, -1));
   else {
   else {
@@ -698,18 +693,15 @@ static const luaL_reg iolib[] = {
 
 
 
 
 LUALIB_API int lua_iolibopen (lua_State *L) {
 LUALIB_API int lua_iolibopen (lua_State *L) {
-  lua_pushliteral(L, FILEHANDLE);
   lua_newtable(L);  /* event table for FILEHANDLE */
   lua_newtable(L);  /* event table for FILEHANDLE */
   /* close files when collected */
   /* close files when collected */
-  lua_pushliteral(L, "gc");
   lua_pushcfunction(L, file_collect);
   lua_pushcfunction(L, file_collect);
-  lua_settable(L, -3);
+  lua_setstr(L, -2, "gc");
   /* put new eventtable into registry */
   /* put new eventtable into registry */
-  lua_settable(L, LUA_REGISTRYINDEX);  /* registry.FILEHANDLE = eventtable */
-  lua_pushliteral(L, CLOSEDFILEHANDLE);
+  lua_setstr(L, LUA_REGISTRYINDEX, FILEHANDLE);
   /* event table for CLOSEDFILEHANDLE */
   /* event table for CLOSEDFILEHANDLE */
   lua_newtable(L);
   lua_newtable(L);
-  lua_settable(L, LUA_REGISTRYINDEX);
+  lua_setstr(L, LUA_REGISTRYINDEX, CLOSEDFILEHANDLE);
   luaL_openl(L, iolib);
   luaL_openl(L, iolib);
   /* predefined file handles */
   /* predefined file handles */
   newfilewithname(L, stdin, basicfiles[INFILE]);
   newfilewithname(L, stdin, basicfiles[INFILE]);

+ 2 - 3
lua.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.c,v 1.72 2001/11/27 20:56:47 roberto Exp $
+** $Id: lua.c,v 1.1 2001/11/29 22:14:34 rieru Exp rieru $
 ** Lua stand-alone interpreter
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -133,9 +133,8 @@ static void getargs (char *argv[]) {
     lua_settable(L, -3);
     lua_settable(L, -3);
   }
   }
   /* arg.n = maximum index in table `arg' */
   /* arg.n = maximum index in table `arg' */
-  lua_pushliteral(L, "n");
   lua_pushnumber(L, i-1);
   lua_pushnumber(L, i-1);
-  lua_settable(L, -3);
+  lua_setstr(L, -2, "n");
 }
 }
 
 
 
 

+ 6 - 4
lua.h

@@ -146,7 +146,7 @@ LUA_API void  lua_pushcclosure (lua_State *L, lua_CFunction fn, int n);
 /*
 /*
 ** get functions (Lua -> stack)
 ** get functions (Lua -> stack)
 */
 */
-LUA_API void  lua_getglobal (lua_State *L, const char *name);
+LUA_API void  lua_getstr (lua_State *L, int index, const char *name);
 LUA_API void  lua_gettable (lua_State *L, int index);
 LUA_API void  lua_gettable (lua_State *L, int index);
 LUA_API void  lua_rawget (lua_State *L, int index);
 LUA_API void  lua_rawget (lua_State *L, int index);
 LUA_API void  lua_rawgeti (lua_State *L, int index, int n);
 LUA_API void  lua_rawgeti (lua_State *L, int index, int n);
@@ -158,7 +158,7 @@ LUA_API void  lua_geteventtable (lua_State *L, int objindex);
 /*
 /*
 ** set functions (stack -> Lua)
 ** set functions (stack -> Lua)
 */
 */
-LUA_API void  lua_setglobal (lua_State *L, const char *name);
+LUA_API void  lua_setstr (lua_State *L, int index, const char *name);
 LUA_API void  lua_settable (lua_State *L, int index);
 LUA_API void  lua_settable (lua_State *L, int index);
 LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawset (lua_State *L, int index);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
 LUA_API void  lua_rawseti (lua_State *L, int index, int n);
@@ -227,8 +227,10 @@ LUA_API int   lua_getweakmode (lua_State *L, int index);
 #define lua_pushliteral(L, s)	lua_pushlstring(L, "" s, \
 #define lua_pushliteral(L, s)	lua_pushlstring(L, "" s, \
                                                 (sizeof(s)/sizeof(char))-1)
                                                 (sizeof(s)/sizeof(char))-1)
 
 
-#define lua_getregistry(L)	lua_pushvalue(L, LUA_REGISTRYINDEX);
-#define lua_getglobals(L)	lua_pushvalue(L, LUA_GLOBALSINDEX);
+#define lua_getregistry(L)	lua_pushvalue(L, LUA_REGISTRYINDEX)
+#define lua_getglobals(L)	lua_pushvalue(L, LUA_GLOBALSINDEX)
+#define lua_getglobal(L,s)	lua_getstr(L, LUA_GLOBALSINDEX, s)
+#define lua_setglobal(L,s)	lua_setstr(L, LUA_GLOBALSINDEX, s)