lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


>>>>> "Francisco" == Francisco Olarte <folarte@peoplecall.com> writes:

 Francisco> I think not, but you can easily do something like....
 Francisco> int lua_swapvalues(lua_State * L, int i, int j) {
 Francisco>    i = lua_absindex(L, i);
 Francisco>    j = lua_absindex(L, j);  /* In case either of them is negative. */
 Francisco>    if (i!=j) {
 Francisco>       if (!lua_checkstack(L,2))   return 0; /* Out of memory */
 Francisco>       lua_pushvalue(L,i);
 Francisco>       lua_pushvalue(L,j);
 Francisco>       lua_replace(L, i);
 Francisco>       lua_replace(L,j);
 Francisco>    }
 Francisco>    return 1;
 Francisco> }

If you're not tied to lua 5.1, you could simplify that:

void lua_swapvalues(lua_State *L, int i, int j) {
  i = lua_absindex(L, i);
  j = lua_absindex(L, j);
  if (i != j) {
    luaL_checkstack(L, 1, NULL);
    lua_pushvalue(L, j);
    lua_copy(L, i, j);   /* needs 5.2+ */
    lua_replace(L, i);
  }
}

(The stack check could also be omitted and made the responsibility of
the caller)

In 5.3 or later you can do it without needing an extra stack slot,
though this is probably less efficient (needs 4 rotates).

-- 
Andrew.