Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lua_pop vs lua_remove

Tags:

c++

c

lua

Currently I'm building my own script VM manager class in C++. I have no problems with any of the Lua & Lua C or C++ stuff, but the one section that confuses me is when to use lua_pop and when to use lua_remove.

From what I understand, lua_pop is to remove multiple values(on the stack) from the top down, eliminating data that is no longer needed, where as lua_remove is for removing a single value from any arbitrary, valid stack index (basically what the Lua manual says for both :P).

But I've noticed certain segments of code scattered around the web that intermix lua_pop and lua_remove, but when I tried to use lua_pop instead of lua_remove where the call just removed the top stack element, I ran into problems. So would it be possible to get a definitive example or explanation on how and when to use these two functions correctly, as well as relative speed & efficiency for these two? I assume lua_pop is faster than lua_remove, which is one of the reasons why I want to use lua_pop as much as possible, apart from coding 'correct code'.

like image 610
Necrolis Avatar asked Jan 20 '10 16:01

Necrolis


People also ask

What is lua_ gettop?

The lua_gettop function returns the number of elements in the stack, which is also the index of the top element.

How does Lua stack work?

The Stack. Lua uses a virtual stack to pass values to and from C. Each element in this stack represents a Lua value ( nil , number, string, etc.). Whenever Lua calls C, the called function gets a new stack, which is independent of previous stacks and of stacks of C functions that are still active.

What is a Lua state?

The lua_State is basically a way to access what's going on in the Lua "box" during execution of your program and allows you to glue the two languages together.


2 Answers

A typical example of lua_remove is accessing tables. Snippets from Lua reference manual.

lua_getfield(L, LUA_GLOBALSINDEX, "t");   /* table to be indexed */
lua_getfield(L, -1, "x");        /* push result of t.x (2nd arg) */
lua_remove(L, -2);                  /* remove 't' from the stack */

lua_getfield pushes t[x] on the stack. You no longer need t, so you remove it.

A typical example of lua_pop is iterating over a table.

lua_pushnil(L);  /* first key */
while (lua_next(L, t) != 0) {
    /* uses 'key' (at index -2) and 'value' (at index -1) */
    /* do whatever you like with the key and the value */

    lua_pop(L, 1);
}

After you are done with a single iteration, you need to have the key on top of the stack, so that lua_next knows which key/value pair comes next. You need to remove the value, which is on top of the stack.

It's not a definitive example. In Lua you do whatever works for you. Always keep in mind what's on your lua_State stack, and you'll be fine.

like image 191
Tadeusz A. Kadłubowski Avatar answered Oct 09 '22 19:10

Tadeusz A. Kadłubowski


The following pieces of C code are entirely equivalent:

lua_pop(L, 1); // same as:
lua_remove(L, -1);

So are these:

lua_pop(L, n); // same as:
lua_settop(L, lua_gettop(L)-n);
like image 32
Jonathan Zrake Avatar answered Oct 09 '22 20:10

Jonathan Zrake