瀏覽代碼

new functions 'lua_geti/lua_seti' (non raw)

Roberto Ierusalimschy 11 年之前
父節點
當前提交
7f1a2ad699
共有 4 個文件被更改,包括 35 次插入29 次删除
  1. 25 1
      lapi.c
  2. 2 3
      lbaselib.c
  3. 5 24
      ltablib.c
  4. 3 1
      lua.h

+ 25 - 1
lapi.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lapi.c,v 2.232 2014/07/30 14:00:14 roberto Exp roberto $
+** $Id: lapi.c,v 2.233 2014/08/01 17:33:08 roberto Exp roberto $
 ** Lua API
 ** Lua API
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -611,6 +611,18 @@ LUA_API int lua_getfield (lua_State *L, int idx, const char *k) {
 }
 }
 
 
 
 
+LUA_API int lua_geti (lua_State *L, int idx, lua_Integer n) {
+  StkId t;
+  lua_lock(L);
+  t = index2addr(L, idx);
+  setivalue(L->top, n);
+  api_incr_top(L);
+  luaV_gettable(L, t, L->top - 1, L->top - 1);
+  lua_unlock(L);
+  return ttnov(L->top - 1);
+}
+
+
 LUA_API int lua_rawget (lua_State *L, int idx) {
 LUA_API int lua_rawget (lua_State *L, int idx) {
   StkId t;
   StkId t;
   lua_lock(L);
   lua_lock(L);
@@ -743,6 +755,18 @@ LUA_API void lua_setfield (lua_State *L, int idx, const char *k) {
 }
 }
 
 
 
 
+LUA_API void lua_seti (lua_State *L, int idx, lua_Integer n) {
+  StkId t;
+  lua_lock(L);
+  api_checknelems(L, 1);
+  t = index2addr(L, idx);
+  setivalue(L->top++, n);
+  luaV_settable(L, t, L->top - 1, L->top - 2);
+  L->top -= 2;  /* pop value and key */
+  lua_unlock(L);
+}
+
+
 LUA_API void lua_rawset (lua_State *L, int idx) {
 LUA_API void lua_rawset (lua_State *L, int idx) {
   StkId o;
   StkId o;
   Table *t;
   Table *t;

+ 2 - 3
lbaselib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lbaselib.c,v 1.294 2014/08/01 17:22:57 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.295 2014/08/01 17:33:08 roberto Exp roberto $
 ** Basic library
 ** Basic library
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -266,8 +266,7 @@ static int ipairsaux (lua_State *L) {
   }
   }
   else {
   else {
     lua_pushinteger(L, i);
     lua_pushinteger(L, i);
-    lua_pushinteger(L, i);  /* key for indexing table */
-    lua_gettable(L, 1);
+    lua_geti(L, 1, i);
     return 2;
     return 2;
   }
   }
 }
 }

+ 5 - 24
ltablib.c

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: ltablib.c,v 1.73 2014/07/29 16:01:00 roberto Exp roberto $
+** $Id: ltablib.c,v 1.74 2014/08/21 19:13:55 roberto Exp roberto $
 ** Library for Table Manipulation
 ** Library for Table Manipulation
 ** See Copyright Notice in lua.h
 ** See Copyright Notice in lua.h
 */
 */
@@ -27,25 +27,6 @@ typedef struct {
 } TabA;
 } TabA;
 
 
 
 
-/*
-** equivalent to 'lua_rawgeti', but not raw
-*/
-static int geti (lua_State *L, int idx, lua_Integer n) {
-  lua_pushinteger(L, n);
-  return lua_gettable(L, idx);  /* assume 'idx' is not negative */
-}
-
-
-/*
-** equivalent to 'lua_rawseti', but not raw
-*/
-static void seti (lua_State *L, int idx, lua_Integer n) {
-  lua_pushinteger(L, n);
-  lua_rotate(L, -2, 1);  /* exchange key and value */
-  lua_settable(L, idx);  /* assume 'idx' is not negative */
-}
-
-
 /*
 /*
 ** Check that 'arg' has a table and set access functions in 'ta' to raw
 ** Check that 'arg' has a table and set access functions in 'ta' to raw
 ** or non-raw according to the presence of corresponding metamethods.
 ** or non-raw according to the presence of corresponding metamethods.
@@ -55,10 +36,10 @@ static void checktab (lua_State *L, int arg, TabA *ta) {
   if (lua_getmetatable(L, arg)) {
   if (lua_getmetatable(L, arg)) {
     lua_pushliteral(L, "__index");  /* 'index' metamethod */
     lua_pushliteral(L, "__index");  /* 'index' metamethod */
     if (lua_rawget(L, -2) != LUA_TNIL)
     if (lua_rawget(L, -2) != LUA_TNIL)
-      ta->geti = geti;
+      ta->geti = lua_geti;
     lua_pushliteral(L, "__newindex");  /* 'newindex' metamethod */
     lua_pushliteral(L, "__newindex");  /* 'newindex' metamethod */
     if (lua_rawget(L, -3) != LUA_TNIL)
     if (lua_rawget(L, -3) != LUA_TNIL)
-      ta->seti = seti;
+      ta->seti = lua_seti;
     lua_pop(L, 3);  /* pop metatable plus both metamethods */
     lua_pop(L, 3);  /* pop metatable plus both metamethods */
   }
   }
   if (ta->geti == NULL || ta->seti == NULL) {
   if (ta->geti == NULL || ta->seti == NULL) {
@@ -147,10 +128,10 @@ static int tmove (lua_State *L) {
     lua_Integer n, i;
     lua_Integer n, i;
     ta.geti = (!luaL_getmetafield(L, 1, "__index"))
     ta.geti = (!luaL_getmetafield(L, 1, "__index"))
       ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
       ? (luaL_checktype(L, 1, LUA_TTABLE), lua_rawgeti)
-      : geti;
+      : lua_geti;
     ta.seti = (!luaL_getmetafield(L, tt, "__newindex"))
     ta.seti = (!luaL_getmetafield(L, tt, "__newindex"))
       ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
       ? (luaL_checktype(L, tt, LUA_TTABLE), lua_rawseti)
-      : seti;
+      : lua_seti;
     n = e - f + 1;  /* number of elements to move */
     n = e - f + 1;  /* number of elements to move */
     if (t > f) {
     if (t > f) {
       for (i = n - 1; i >= 0; i--) {
       for (i = n - 1; i >= 0; i--) {

+ 3 - 1
lua.h

@@ -1,5 +1,5 @@
 /*
 /*
-** $Id: lua.h,v 1.312 2014/07/31 13:44:30 roberto Exp roberto $
+** $Id: lua.h,v 1.313 2014/08/01 17:33:08 roberto Exp roberto $
 ** Lua - A Scripting Language
 ** Lua - A Scripting Language
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** Lua.org, PUC-Rio, Brazil (http://www.lua.org)
 ** See Copyright Notice at the end of this file
 ** See Copyright Notice at the end of this file
@@ -239,6 +239,7 @@ LUA_API int   (lua_pushthread) (lua_State *L);
 LUA_API int (lua_getglobal) (lua_State *L, const char *var);
 LUA_API int (lua_getglobal) (lua_State *L, const char *var);
 LUA_API int (lua_gettable) (lua_State *L, int idx);
 LUA_API int (lua_gettable) (lua_State *L, int idx);
 LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
 LUA_API int (lua_getfield) (lua_State *L, int idx, const char *k);
+LUA_API int (lua_geti) (lua_State *L, int idx, lua_Integer n);
 LUA_API int (lua_rawget) (lua_State *L, int idx);
 LUA_API int (lua_rawget) (lua_State *L, int idx);
 LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
 LUA_API int (lua_rawgeti) (lua_State *L, int idx, lua_Integer n);
 LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
 LUA_API int (lua_rawgetp) (lua_State *L, int idx, const void *p);
@@ -255,6 +256,7 @@ LUA_API int  (lua_getuservalue) (lua_State *L, int idx);
 LUA_API void  (lua_setglobal) (lua_State *L, const char *var);
 LUA_API void  (lua_setglobal) (lua_State *L, const char *var);
 LUA_API void  (lua_settable) (lua_State *L, int idx);
 LUA_API void  (lua_settable) (lua_State *L, int idx);
 LUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);
 LUA_API void  (lua_setfield) (lua_State *L, int idx, const char *k);
+LUA_API void  (lua_seti) (lua_State *L, int idx, lua_Integer n);
 LUA_API void  (lua_rawset) (lua_State *L, int idx);
 LUA_API void  (lua_rawset) (lua_State *L, int idx);
 LUA_API void  (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
 LUA_API void  (lua_rawseti) (lua_State *L, int idx, lua_Integer n);
 LUA_API void  (lua_rawsetp) (lua_State *L, int idx, const void *p);
 LUA_API void  (lua_rawsetp) (lua_State *L, int idx, const void *p);