aboutsummaryrefslogtreecommitdiff
path: root/ltm.c
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-02-27 14:48:28 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2018-02-27 14:48:28 -0300
commitb7edf5d2d89ed2ce1e9087de496bcb451e39d131 (patch)
tree88d94c159ee2034fffd9195a60e306c9565bf361 /ltm.c
parentef8263f81fdde2310ebb15c9a3fe5e954d57cab5 (diff)
downloadlua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.tar.gz
lua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.tar.bz2
lua-b7edf5d2d89ed2ce1e9087de496bcb451e39d131.zip
metamethods for 'removekey'/'keyin'
Diffstat (limited to 'ltm.c')
-rw-r--r--ltm.c31
1 files changed, 30 insertions, 1 deletions
diff --git a/ltm.c b/ltm.c
index c06c8296..2a9a4cbe 100644
--- a/ltm.c
+++ b/ltm.c
@@ -1,5 +1,5 @@
1/* 1/*
2** $Id: ltm.c,v 2.64 2018/02/23 13:13:31 roberto Exp roberto $ 2** $Id: ltm.c,v 2.65 2018/02/26 14:16:05 roberto Exp roberto $
3** Tag methods 3** Tag methods
4** See Copyright Notice in lua.h 4** See Copyright Notice in lua.h
5*/ 5*/
@@ -38,6 +38,7 @@ LUAI_DDEF const char *const luaT_typenames_[LUA_TOTALTAGS] = {
38void luaT_init (lua_State *L) { 38void luaT_init (lua_State *L) {
39 static const char *const luaT_eventname[] = { /* ORDER TM */ 39 static const char *const luaT_eventname[] = { /* ORDER TM */
40 "__index", "__newindex", 40 "__index", "__newindex",
41 "__undef", "__isdef",
41 "__gc", "__mode", "__len", "__eq", 42 "__gc", "__mode", "__len", "__eq",
42 "__add", "__sub", "__mul", "__mod", "__pow", 43 "__add", "__sub", "__mul", "__mod", "__pow",
43 "__div", "__idiv", 44 "__div", "__idiv",
@@ -248,3 +249,31 @@ void luaT_getvarargs (lua_State *L, CallInfo *ci, StkId where, int wanted) {
248 for (; i < wanted; i++) /* complete required results with nil */ 249 for (; i < wanted; i++) /* complete required results with nil */
249 setnilvalue(s2v(where + i)); 250 setnilvalue(s2v(where + i));
250} 251}
252
253
254int luaT_keydef (lua_State *L, TValue *obj, TValue *key, int remove) {
255 const TValue *tm;
256 TMS event = remove ? TM_UNDEF : TM_ISDEF;
257 if (!ttistable(obj)) { /* not a table? */
258 tm = luaT_gettmbyobj(L, obj, event); /* get its metamethod */
259 if (notm(tm)) { /* no metamethod? */
260 const char *msg = remove ? "remove key from" : "check key from";
261 luaG_typeerror(L, obj, msg); /* error */
262 }
263 /* else will call metamethod 'tm' */
264 }
265 else { /* 'obj' is a table */
266 Table *t = hvalue(obj);
267 tm = fasttm(L, t->metatable, event);
268 if (tm == NULL) { /* no metamethod? */
269 const TValue *val = luaH_get(t, key); /* get entry */
270 int res = !isempty(val); /* true if entry is not empty */
271 if (remove && res) /* key is present and should be removed? */
272 setempty(cast(TValue*, val)); /* remove it */
273 return res;
274 }
275 /* else will call metamethod 'tm' */
276 }
277 luaT_callTMres(L, tm, obj, key, L->top);
278 return !l_isfalse(s2v(L->top));
279}