MCPcopy Create free account
hub / github.com/DFHack/dfhack / addk

Function addk

depends/lua/src/lcode.c:428–452  ·  view source on GitHub ↗

** 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. */

Source from the content-addressed store, hash-verified

426** keys), the caller must provide a useful 'key' for indexing the cache.
427*/
428static int addk (FuncState *fs, TValue *key, TValue *v) {
429 lua_State *L = fs->ls->L;
430 Proto *f = fs->f;
431 TValue *idx = luaH_set(L, fs->ls->h, key); /* index scanner table */
432 int k, oldsize;
433 if (ttisinteger(idx)) { /* is there an index there? */
434 k = cast_int(ivalue(idx));
435 /* correct value? (warning: must distinguish floats from integers!) */
436 if (k < fs->nk && ttype(&f->k[k]) == ttype(v) &&
437 luaV_rawequalobj(&f->k[k], v))
438 return k; /* reuse index */
439 }
440 /* constant not found; create a new entry */
441 oldsize = f->sizek;
442 k = fs->nk;
443 /* numerical value does not need GC barrier;
444 table has no metatable, so it does not need to invalidate cache */
445 setivalue(idx, k);
446 luaM_growvector(L, f->k, k, f->sizek, TValue, MAXARG_Ax, "constants");
447 while (oldsize < f->sizek) setnilvalue(&f->k[oldsize++]);
448 setobj(L, &f->k[k], v);
449 fs->nk++;
450 luaC_barrier(L, f, v);
451 return k;
452}
453
454
455/*

Callers 5

luaK_stringKFunction · 0.85
luaK_intKFunction · 0.85
luaK_numberKFunction · 0.85
boolKFunction · 0.85
nilKFunction · 0.85

Calls 1

luaH_setFunction · 0.85

Tested by

no test coverage detected