lua-users home
lua-l archive

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


Thank you so much.
>   i = lua_absindex(L, i);
>   j = lua_absindex(L, j);  /* In case either of them is negative. */
It's a magic!
After studying your code, I find that it is easier to achieve this
goal (i.e the title) by using positive indexes(or converting negative
indexes to positive ones) than directly using negative ones. Am I
right?

Best regards.
sunshilong

On Fri, Jan 22, 2021 at 4:12 PM Francisco Olarte <folarte@peoplecall.com> wrote:
>
> Sunshi...
>
> On Fri, Jan 22, 2021 at 8:43 AM 孙世龙 sunshilong <sunshilong369@gmail.com> wrote:
> > Is there a C API that could exchange the values at different index of the stack?
> > For example:
> > index j is "hello" , index i is 100
> > invoke a specific API,
> > index j is 100,  index i is "hello"
>
> I think not, but you can easily do something like....
> int lua_swapvalues(lua_State * L, int i, int j) {
>    i = lua_absindex(L, i);
>    j = lua_absindex(L, j);  /* In case either of them is negative. */
>    if (i!=j) {
>       if (!lua_checkstack(L,2))   return 0; /* Out of memory */
>       lua_pushvalue(L,i);
>       lua_pushvalue(L,j);
>       lua_replace(L, i);
>       lua_replace(L,j);
>    }
>    return 1;
> }
>
> FOS