** 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 a value with an absent key if there ** is no such entry. (The value at 'slot' must be empty, otherwise ** 'luaV_fastget' would have done the job.) */
| 328 | ** 'luaV_fastget' would have done the job.) |
| 329 | */ |
| 330 | void luaV_finishset (lua_State *L, const TValue *t, TValue *key, |
| 331 | TValue *val, const TValue *slot) { |
| 332 | int loop; /* counter to avoid infinite loops */ |
| 333 | for (loop = 0; loop < MAXTAGLOOP; loop++) { |
| 334 | const TValue *tm; /* '__newindex' metamethod */ |
| 335 | if (slot != NULL) { /* is 't' a table? */ |
| 336 | Table *h = hvalue(t); /* save 't' table */ |
| 337 | lua_assert(isempty(slot)); /* slot must be empty */ |
| 338 | tm = fasttm(L, h->metatable, TM_NEWINDEX); /* get metamethod */ |
| 339 | if (tm == NULL) { /* no metamethod? */ |
| 340 | luaH_finishset(L, h, key, slot, val); /* set new value */ |
| 341 | invalidateTMcache(h); |
| 342 | luaC_barrierback(L, obj2gco(h), val); |
| 343 | return; |
| 344 | } |
| 345 | /* else will try the metamethod */ |
| 346 | } |
| 347 | else { /* not a table; check metamethod */ |
| 348 | tm = luaT_gettmbyobj(L, t, TM_NEWINDEX); |
| 349 | if (l_unlikely(notm(tm))) |
| 350 | luaG_typeerror(L, t, "index"); |
| 351 | } |
| 352 | /* try the metamethod */ |
| 353 | if (ttisfunction(tm)) { |
| 354 | luaT_callTM(L, tm, t, key, val); |
| 355 | return; |
| 356 | } |
| 357 | t = tm; /* else repeat assignment over 'tm' */ |
| 358 | if (luaV_fastget(L, t, key, slot, luaH_get)) { |
| 359 | luaV_finishfastset(L, t, slot, val); |
| 360 | return; /* done */ |
| 361 | } |
| 362 | /* else 'return luaV_finishset(L, t, key, val, slot)' (loop) */ |
| 363 | } |
| 364 | luaG_runerror(L, "'__newindex' chain too long; possible loop"); |
| 365 | } |
| 366 | |
| 367 | |
| 368 | /* |
no test coverage detected