diff options
Diffstat (limited to 'ltm.c')
-rw-r--r-- | ltm.c | 31 |
1 files changed, 30 insertions, 1 deletions
@@ -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] = { | |||
38 | void luaT_init (lua_State *L) { | 38 | void 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 | |||
254 | int 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 | } | ||