lua-users home
lua-l archive

Re: Is there a C API that could exchange the values at different index of the stack?

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


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

AltStyle によって変換されたページ (->オリジナル) /