/* 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.  */
static TValue* newkey(lua_State *L, Table *t, const TValue *key)
/* 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.  */
static TValue* newkey(lua_State *L, Table *t, const TValue *key)
{
    Node *othern;
    Node *n = getfreepos(t);
    if (n == NULL) {
      rehash(L, t, key);
      return luaH_set(L, t, key);
    }
    lua_assert(n != dummynode);
    othern = mainposition(t, key2tval(mp));
    if (othern != mp) {
      while (gnext(othern) != mp) othern = gnext(othern);
      gnext(othern) = n;
      *n = *mp;
      gnext(mp) = NULL;
      setnilvalue(gval(mp));
    }
    else {
      gnext(n) = gnext(mp);
      gnext(mp) = n;
      mp = n;
    }
  }
  setobj2t(L, gkey(mp), key);
  luaC_barriert(L, t, key);
  lua_assert(ttisnil(gval(mp)));
  return gval(mp);
}