** Finish a table assignment 't[key] = val'. ** If 'slot' is NULL, 't' is not a table. Otherwise, 'slot' points ** to the entry 't[key]', or to 'luaO_nilobject' if there is no such ** entry. (The value at 'slot' must be nil, otherwise 'luaV_fastset' ** would have done the job.) */
| 201 | ** would have done the job.) |
| 202 | */ |
| 203 | void luaV_finishset (lua_State *L, const TValue *t, TValue *key, |
| 204 | StkId val, const TValue *slot) { |
| 205 | int loop; /* counter to avoid infinite loops */ |
| 206 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 207 | const TValue *tm; /* '__newindex' metamethod */ |
| 208 | if (slot != NULL) { /* is 't' a table? */ |
| 209 | Table *h = hvalue(t); /* save 't' table */ |
| 210 | lua_assert(ttisnil(slot)); /* old value must be nil */ |
| 211 | tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ |
| 212 | if (tm == NULL) { /* no metamethod? */ |
| 213 | if (slot == luaO_nilobject) /* no previous entry? */ |
| 214 | slot = luaH_newkey(L, h, key); /* create one */ |
| 215 | /* no metamethod and (now) there is an entry with given key */ |
| 216 | setobj2t(L, cast(TValue *, slot), val); /* set its new value */ |
| 217 | invalidateTMcache(h); |
| 218 | luaC_barrierback(L, h, val); |
| 219 | return; |
| 220 | } |
| 221 | /* else will try the metamethod */ |
| 222 | } |
| 223 | else { /* not a table; check metamethod */ |
| 224 | if (ttisnil(tm = luaT_gettmbyobj(L, t, TM_NEWINDEX))) |
| 225 | luaG_typeerror(L, t, "index"); |
| 226 | } |
| 227 | /* try the metamethod */ |
| 228 | if (ttisfunction(tm)) { |
| 229 | luaT_callTM(L, tm, t, key, val, 0); |
| 230 | return; |
| 231 | } |
| 232 | t = tm; /* else repeat assignment over 'tm' */ |
| 233 | if (luaV_fastset(L, t, key, slot, luaH_get, val)) |
| 234 | return; /* done */ |
| 235 | /* else loop */ |
| 236 | } |
| 237 | luaG_runerror(L, "'__newindex' chain too long; possible loop"); |
| 238 | } |
| 239 | |
| 240 | |
| 241 | /* |
no test coverage detected