MCPcopy Create free account
hub / github.com/EmmyLua/EmmyLuaDebugger / luaH_newkey

Function luaH_newkey

third-party/lua-5.2.4/src/ltable.c:405–440  ·  view source on GitHub ↗

** inserts a new key into a hash table; first, check whether key's main ** position is free. If not, check whether colliding node is in its main ** position or not: if it is not, move colliding node to an empty place and ** put new key in its main position; otherwise (colliding node is in its main ** position), new key goes to an empty position. */

Source from the content-addressed store, hash-verified

403** position), new key goes to an empty position.
404*/
405TValue *luaH_newkey (lua_State *L, Table *t, const TValue *key) {
406 Node *mp;
407 if (ttisnil(key)) luaG_runerror(L, "table index is nil");
408 else if (ttisnumber(key) && luai_numisnan(L, nvalue(key)))
409 luaG_runerror(L, "table index is NaN");
410 mp = mainposition(t, key);
411 if (!ttisnil(gval(mp)) || isdummy(mp)) { /* main position is taken? */
412 Node *othern;
413 Node *n = getfreepos(t); /* get a free place */
414 if (n == NULL) { /* cannot find a free place? */
415 rehash(L, t, key); /* grow table */
416 /* whatever called 'newkey' take care of TM cache and GC barrier */
417 return luaH_set(L, t, key); /* insert key into grown table */
418 }
419 lua_assert(!isdummy(n));
420 othern = mainposition(t, gkey(mp));
421 if (othern != mp) { /* is colliding node out of its main position? */
422 /* yes; move colliding node into free position */
423 while (gnext(othern) != mp) othern = gnext(othern); /* find previous */
424 gnext(othern) = n; /* redo the chain with `n' in place of `mp' */
425 *n = *mp; /* copy colliding node into free pos. (mp->next also goes) */
426 gnext(mp) = NULL; /* now `mp' is free */
427 setnilvalue(gval(mp));
428 }
429 else { /* colliding node is in its own main position */
430 /* new node will go into free position */
431 gnext(n) = gnext(mp); /* chain new position */
432 gnext(mp) = n;
433 mp = n;
434 }
435 }
436 setobj2t(L, gkey(mp), key);
437 luaC_barrierback(L, obj2gco(t), key);
438 lua_assert(ttisnil(gval(mp)));
439 return gval(mp);
440}
441
442
443/*

Callers 3

luaV_settableFunction · 0.70
luaH_setFunction · 0.70
luaH_setintFunction · 0.70

Calls 6

luaC_barrierbackFunction · 0.85
luaG_runerrorFunction · 0.70
mainpositionFunction · 0.70
getfreeposFunction · 0.70
rehashFunction · 0.70
luaH_setFunction · 0.70

Tested by

no test coverage detected