** 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. ** Note that all functions share the same table, s
| 540 | ** a function can make some indices wrong. |
| 541 | */ |
| 542 | static int addk (FuncState *fs, TValue *key, TValue *v) { |
| 543 | TValue val; |
| 544 | lua_State *L = fs->ls->L; |
| 545 | Proto *f = fs->f; |
| 546 | const TValue *idx = luaH_get(fs->ls->h, key); /* query scanner table */ |
| 547 | int k, oldsize; |
| 548 | if (ttisinteger(idx)) { /* is there an index there? */ |
| 549 | k = cast_int(ivalue(idx)); |
| 550 | /* correct value? (warning: must distinguish floats from integers!) */ |
| 551 | if (k < fs->nk && ttypetag(&f->k[k]) == ttypetag(v) && |
| 552 | luaV_rawequalobj(&f->k[k], v)) |
| 553 | return k; /* reuse index */ |
| 554 | } |
| 555 | /* constant not found; create a new entry */ |
| 556 | oldsize = f->sizek; |
| 557 | k = fs->nk; |
| 558 | /* numerical value does not need GC barrier; |
| 559 | table has no metatable, so it does not need to invalidate cache */ |
| 560 | setivalue(&val, k); |
| 561 | luaH_finishset(L, fs->ls->h, key, idx, &val); |
| 562 | luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants"); |
| 563 | while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]); |
| 564 | setobj(L, &f->k[k], v); |
| 565 | fs->nk++; |
| 566 | luaC_barrier(L, f, v); |
| 567 | return k; |
| 568 | } |
| 569 | |
| 570 | |
| 571 | /* |