** Add constant 'v' to prototype's list of constants (field 'k'). ** Use scanner's table to cache position of constants in constant list ** and try to reuse constants. Because some values should not be used ** as keys (nil cannot be a key, integer keys can collapse with float ** keys), the caller must provide a useful 'key' for indexing the cache. */
| 433 | ** keys), the caller must provide a useful 'key' for indexing the cache. |
| 434 | */ |
| 435 | static int addk (FuncState *fs, TValue *key, TValue *v) { |
| 436 | lua_State *L = fs->ls->L; |
| 437 | Proto *f = fs->f; |
| 438 | TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */ |
| 439 | int k, oldsize; |
| 440 | if (ttisinteger(idx)) { /* is there an index there? */ |
| 441 | k = cast_int(ivalue(idx)); |
| 442 | /* correct value? (warning: must distinguish floats from integers!) */ |
| 443 | if (k < fs->nk && ttype(&f->k[k]) == ttype(v) && |
| 444 | luaV_rawequalobj(&f->k[k], v)) |
| 445 | return k; /* reuse index */ |
| 446 | } |
| 447 | /* constant not found; create a new entry */ |
| 448 | oldsize = f->sizek; |
| 449 | k = fs->nk; |
| 450 | /* numerical value does not need GC barrier; |
| 451 | table has no metatable, so it does not need to invalidate cache */ |
| 452 | setivalue(idx, k); |
| 453 | luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); |
| 454 | while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); |
| 455 | setobj(L, &f->k[k], v); |
| 456 | fs->nk++; |
| 457 | luaC_barrier(L, f, v); |
| 458 | return k; |
| 459 | } |
| 460 | |
| 461 | |
| 462 | /* |
no test coverage detected