diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-27 14:48:28 -0300 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2018-02-27 14:48:28 -0300 |
commit | b7edf5d2d89ed2ce1e9087de496bcb451e39d131 (patch) | |
tree | 88d94c159ee2034fffd9195a60e306c9565bf361 /lapi.c | |
parent | ef8263f81fdde2310ebb15c9a3fe5e954d57cab5 (diff) | |
download | lua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.tar.gz lua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.tar.bz2 lua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.zip |
metamethods for 'removekey'/'keyin'
Diffstat (limited to 'lapi.c')
-rw-r--r-- | lapi.c | 19 |
1 files changed, 8 insertions, 11 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lapi.c,v 2.287 2018/02/25 12:48:16 roberto Exp roberto $ | 2 | ** $Id: lapi.c,v 2.288 2018/02/26 14:16:05 roberto Exp roberto $ |
3 | ** Lua API | 3 | ** Lua API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -679,6 +679,7 @@ LUA_API int lua_rawget (lua_State *L, int idx) { | |||
679 | Table *t; | 679 | Table *t; |
680 | const TValue *val; | 680 | const TValue *val; |
681 | lua_lock(L); | 681 | lua_lock(L); |
682 | api_checknelems(L, 1); | ||
682 | t = gettable(L, idx); | 683 | t = gettable(L, idx); |
683 | val = luaH_get(t, s2v(L->top - 1)); | 684 | val = luaH_get(t, s2v(L->top - 1)); |
684 | L->top--; /* remove key */ | 685 | L->top--; /* remove key */ |
@@ -704,29 +705,24 @@ LUA_API int lua_rawgetp (lua_State *L, int idx, const void *p) { | |||
704 | } | 705 | } |
705 | 706 | ||
706 | 707 | ||
707 | static int auxkeyman (lua_State *L, int idx, int remove) { | 708 | static int auxkeydef (lua_State *L, int idx, int remove) { |
708 | Table *t; | ||
709 | const TValue *val; | ||
710 | int res; | 709 | int res; |
711 | lua_lock(L); | 710 | lua_lock(L); |
712 | t = gettable(L, idx); | 711 | api_checknelems(L, 1); |
713 | val = luaH_get(t, s2v(L->top - 1)); | 712 | res = luaT_keydef(L, index2value(L, idx), s2v(L->top - 1), remove); |
714 | L->top--; /* remove key */ | 713 | L->top--; /* remove key */ |
715 | res = !isempty(val); | ||
716 | if (remove && res) /* key is present and should be removed? */ | ||
717 | setempty(cast(TValue*, val)); | ||
718 | lua_unlock(L); | 714 | lua_unlock(L); |
719 | return res; | 715 | return res; |
720 | } | 716 | } |
721 | 717 | ||
722 | 718 | ||
723 | LUA_API void lua_removekey (lua_State *L, int idx) { | 719 | LUA_API void lua_removekey (lua_State *L, int idx) { |
724 | auxkeyman(L, idx, 1); | 720 | auxkeydef(L, idx, 1); |
725 | } | 721 | } |
726 | 722 | ||
727 | 723 | ||
728 | LUA_API int lua_keyin (lua_State *L, int idx) { | 724 | LUA_API int lua_keyin (lua_State *L, int idx) { |
729 | return auxkeyman(L, idx, 0); | 725 | return auxkeydef(L, idx, 0); |
730 | } | 726 | } |
731 | 727 | ||
732 | 728 | ||
@@ -1223,6 +1219,7 @@ LUA_API int lua_next (lua_State *L, int idx) { | |||
1223 | Table *t; | 1219 | Table *t; |
1224 | int more; | 1220 | int more; |
1225 | lua_lock(L); | 1221 | lua_lock(L); |
1222 | api_checknelems(L, 1); | ||
1226 | t = gettable(L, idx); | 1223 | t = gettable(L, idx); |
1227 | more = luaH_next(L, t, L->top - 1); | 1224 | more = luaH_next(L, t, L->top - 1); |
1228 | if (more) { | 1225 | if (more) { |