diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-01-27 11:46:16 -0200 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2003-01-27 11:46:16 -0200 |
| commit | 41ed3c47719b29dd02dc9fcf07f79dc6f2c4381d (patch) | |
| tree | 0481135889c06eded52f6573f203742ab64186c1 /ltablib.c | |
| parent | 635b7c707d146ab56127260601b6fb84c0c140f3 (diff) | |
| download | lua-41ed3c47719b29dd02dc9fcf07f79dc6f2c4381d.tar.gz lua-41ed3c47719b29dd02dc9fcf07f79dc6f2c4381d.tar.bz2 lua-41ed3c47719b29dd02dc9fcf07f79dc6f2c4381d.zip | |
getn/setn in C moved to lauxlib
Diffstat (limited to 'ltablib.c')
| -rw-r--r-- | ltablib.c | 65 |
1 files changed, 7 insertions, 58 deletions
| @@ -1,5 +1,5 @@ | |||
| 1 | /* | 1 | /* |
| 2 | ** $Id: ltablib.c,v 1.17 2002/12/04 17:38:31 roberto Exp roberto $ | 2 | ** $Id: ltablib.c,v 1.18 2002/12/20 10:26:33 roberto Exp roberto $ |
| 3 | ** Library for Table Manipulation | 3 | ** Library for Table Manipulation |
| 4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
| 5 | */ | 5 | */ |
| @@ -15,52 +15,7 @@ | |||
| 15 | #include "lualib.h" | 15 | #include "lualib.h" |
| 16 | 16 | ||
| 17 | 17 | ||
| 18 | 18 | #define aux_getn(L,n) (luaL_checktype(L, n, LUA_TTABLE), luaL_getn(L, n)) | |
| 19 | static int checkint (lua_State *L) { | ||
| 20 | int n = (int)lua_tonumber(L, -1); | ||
| 21 | if (n == 0 && !lua_isnumber(L, -1)) n = -1; | ||
| 22 | lua_pop(L, 1); | ||
| 23 | return n; | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | static void aux_setn (lua_State *L, int t, int n) { | ||
| 28 | lua_pushliteral(L, "n"); | ||
| 29 | lua_rawget(L, t); | ||
| 30 | if (checkint(L) >= 0) { | ||
| 31 | lua_pushliteral(L, "n"); /* use it */ | ||
| 32 | lua_pushnumber(L, n); | ||
| 33 | lua_rawset(L, t); | ||
| 34 | } | ||
| 35 | else { /* use N */ | ||
| 36 | lua_pushvalue(L, t); | ||
| 37 | lua_pushnumber(L, n); | ||
| 38 | lua_rawset(L, lua_upvalueindex(1)); /* N[t] = n */ | ||
| 39 | } | ||
| 40 | } | ||
| 41 | |||
| 42 | |||
| 43 | static int aux_getn (lua_State *L, int t) { | ||
| 44 | int n; | ||
| 45 | luaL_checktype(L, t, LUA_TTABLE); | ||
| 46 | lua_pushliteral(L, "n"); /* try t.n */ | ||
| 47 | lua_rawget(L, t); | ||
| 48 | if ((n = checkint(L)) >= 0) return n; | ||
| 49 | lua_pushvalue(L, t); /* try N[t] */ | ||
| 50 | lua_rawget(L, lua_upvalueindex(1)); | ||
| 51 | if ((n = checkint(L)) >= 0) return n; | ||
| 52 | else { /* must count elements */ | ||
| 53 | n = 0; | ||
| 54 | for (;;) { | ||
| 55 | lua_rawgeti(L, t, ++n); | ||
| 56 | if (lua_isnil(L, -1)) break; | ||
| 57 | lua_pop(L, 1); | ||
| 58 | } | ||
| 59 | lua_pop(L, 1); | ||
| 60 | aux_setn(L, t, n - 1); | ||
| 61 | return n - 1; | ||
| 62 | } | ||
| 63 | } | ||
| 64 | 19 | ||
| 65 | 20 | ||
| 66 | static int luaB_foreachi (lua_State *L) { | 21 | static int luaB_foreachi (lua_State *L) { |
| @@ -106,7 +61,7 @@ static int luaB_getn (lua_State *L) { | |||
| 106 | 61 | ||
| 107 | static int luaB_setn (lua_State *L) { | 62 | static int luaB_setn (lua_State *L) { |
| 108 | luaL_checktype(L, 1, LUA_TTABLE); | 63 | luaL_checktype(L, 1, LUA_TTABLE); |
| 109 | aux_setn(L, 1, luaL_checkint(L, 2)); | 64 | luaL_setn(L, 1, luaL_checkint(L, 2)); |
| 110 | return 0; | 65 | return 0; |
| 111 | } | 66 | } |
| 112 | 67 | ||
| @@ -122,7 +77,7 @@ static int luaB_tinsert (lua_State *L) { | |||
| 122 | if (pos > n) n = pos; /* `grow' array if necessary */ | 77 | if (pos > n) n = pos; /* `grow' array if necessary */ |
| 123 | v = 3; /* function may be called with more than 3 args */ | 78 | v = 3; /* function may be called with more than 3 args */ |
| 124 | } | 79 | } |
| 125 | aux_setn(L, 1, n); /* new size */ | 80 | luaL_setn(L, 1, n); /* new size */ |
| 126 | while (--n >= pos) { /* move up elements */ | 81 | while (--n >= pos) { /* move up elements */ |
| 127 | lua_rawgeti(L, 1, n); | 82 | lua_rawgeti(L, 1, n); |
| 128 | lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ | 83 | lua_rawseti(L, 1, n+1); /* t[n+1] = t[n] */ |
| @@ -137,7 +92,7 @@ static int luaB_tremove (lua_State *L) { | |||
| 137 | int n = aux_getn(L, 1); | 92 | int n = aux_getn(L, 1); |
| 138 | int pos = luaL_optint(L, 2, n); | 93 | int pos = luaL_optint(L, 2, n); |
| 139 | if (n <= 0) return 0; /* table is `empty' */ | 94 | if (n <= 0) return 0; /* table is `empty' */ |
| 140 | aux_setn(L, 1, n-1); /* t.n = n-1 */ | 95 | luaL_setn(L, 1, n-1); /* t.n = n-1 */ |
| 141 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ | 96 | lua_rawgeti(L, 1, pos); /* result = t[pos] */ |
| 142 | for ( ;pos<n; pos++) { | 97 | for ( ;pos<n; pos++) { |
| 143 | lua_rawgeti(L, 1, pos+1); | 98 | lua_rawgeti(L, 1, pos+1); |
| @@ -156,7 +111,7 @@ static int str_concat (lua_State *L) { | |||
| 156 | int i = luaL_optint(L, 3, 1); | 111 | int i = luaL_optint(L, 3, 1); |
| 157 | int n = luaL_optint(L, 4, 0); | 112 | int n = luaL_optint(L, 4, 0); |
| 158 | luaL_checktype(L, 1, LUA_TTABLE); | 113 | luaL_checktype(L, 1, LUA_TTABLE); |
| 159 | if (n == 0) n = aux_getn(L, 1); | 114 | if (n == 0) n = luaL_getn(L, 1); |
| 160 | luaL_buffinit(L, &b); | 115 | luaL_buffinit(L, &b); |
| 161 | for (; i <= n; i++) { | 116 | for (; i <= n; i++) { |
| 162 | lua_rawgeti(L, 1, i); | 117 | lua_rawgeti(L, 1, i); |
| @@ -289,13 +244,7 @@ static const luaL_reg tab_funcs[] = { | |||
| 289 | 244 | ||
| 290 | 245 | ||
| 291 | LUALIB_API int lua_tablibopen (lua_State *L) { | 246 | LUALIB_API int lua_tablibopen (lua_State *L) { |
| 292 | lua_newtable(L); /* create N (table to store num. elements in tables) */ | 247 | luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 0); |
| 293 | lua_pushvalue(L, -1); /* `N' will be its own metatable */ | ||
| 294 | lua_setmetatable(L, -2); | ||
| 295 | lua_pushliteral(L, "__mode"); | ||
| 296 | lua_pushliteral(L, "k"); | ||
| 297 | lua_rawset(L, -3); /* metatable(N).__mode = "k" */ | ||
| 298 | luaL_openlib(L, LUA_TABLIBNAME, tab_funcs, 1); | ||
| 299 | return 1; | 248 | return 1; |
| 300 | } | 249 | } |
| 301 | 250 | ||
