Stack of Lua

xiaoxiao2021-03-05  20

Learning Lua has some time, individual thinks about the understanding of the stack in Lua, um, write a small article (really small)

If you look at Lua's document, you should clearly use the LUA and C interactive data to use the so-called Stack in Lua. So what is the stack when I call Lua_Open a function? Empty (Luaopen_Base, etc. will add something on the stack). So as for how to operate the data on the stack, I think it is very clear on the official document, but I was in the order of the stack, so I said this. Now if I have the following code:

Code:

Lua_State * L = Lua_Open ();

Lua_Pushnumber (L, 211);

Lua_Pushnumber (L, 2222);

Lua_NewTable (L);

Lua_Close (L);

Then there are three elements on the stack after LUA_NEWTABLE, which is roughly this:

211

222

TABLE

Now 211 is the first element, index is 1, but Lua can also use a negative number, then how much is he now?

Index -index Value

1 -3 211

2 -2 222

3 -1 table

Well, it is very simple, then look at how if we want to set a TABLE value? In the document, use Lua_SETTABLE or Lua_rawSet (what is the difference between the two should be unrelated to this, the meaning of the parameters, and preparation work, -1 is value, -2 is the key value

Lua_SETTABLE (Lua_State *, Int)

The first parameter is the script environment to be operated, the second is the location where the table to be operated is on the stack.

The general writing may be like this

Code:

// code A

Lua_getglobal (L, "MyTable"); // Get Table to set the value

Lua_pushString (L, "HP"); // "HP" location on the stack is -1

Lua_PushNumber (L, 211); // "HP" The position on the stack becomes -2, and 211 is -1

Lua_SetTable (L, -3); // Value is correctly set to the mYTable in the global variable (table)

If I want to set this value to the global table? Generally by calling Lua_SETGLOBAL macro

Code:

Lua_Pushnumber (L, 211);

Lua_SETGLOBAL (L, "HP");

It's so simple, but let's take a look at Lua_SETGLOBAL this macro

Code:

#define Lua_SETGLOBAL (L, S) /

(Lua_PushString (L, S), Lua_insert (L, -2), Lua_SetTable (L, Lua_GlobalSindex)

It seems that the code we have is replaced by the code.

Code:

Lua_Pushnumber (L, 211);

Lua_PushString (L, "HP");

Lua_insert (L, -2); // This step seems to be strange, in fact, put the value of -1 to the second parameter referred to Lua_Insert, then move the parameters behind this position

// The final result is actually -1 and -2, but it is not a call from logically.

Lua_SetTable (L, Lua_GlobalSindex); // Why not use Lua_rawSet? I think it is reasonable ^ @ ^ combines the above code with code A, the index value is different when Lua_SetTable, and it works if it is found to be Lua_GlobalSindex, then remove the global table (there is a Lua_RegisterIndex, similar ), Otherwise take the element from Stack, of course, this stack position is not a table that will fail. Therefore, the code A specified is -3 is a MyTable table that is just taken from the global table (where he is a table), the above code snippet is the oversight global table. So the value of Lua_SetTable's index can be, as long as it point to a TABLE

In fact, the interface between Lua and C is mainly in the operation of the stack. Basically, when you write a procedure that LUA and C, what you need to do is to understand what elements on your current stack and their location . I usually draw their position on the paper, if you are familiar, to see the changes in the stack for a few words associated with the associated Lua. Such like Lua_Gettable / Lua_Rawget

Code:

Lua_PushString (L, "HP");

Lua_getTable (L, Lua_GlobalSindex);

Only about the first sentence, the top of the stack is a string, but two sentences are put together, the final stack is a global table named HP actual value.

Code:

Lua_PushString (L, "HP");

Lua_Pushnumber (L, 211);

Lua_SetTable (L, Lua_GlobalSindex);

Whether the second Pushnumber is still PushValue, what is PushString, and after the implementation of the three sentences, there is no change to the stack, because Lua_SetTable / Lua_rawSet will remove -1 and -2, for the stack change, watch one When the function's documentation, look at the parameters and need the elements of those positions on the stack and correctly set the value on the stack. On the stack, finally pay attention to the element after the function is completed, I think there should be no problem.

Author: Wind Dances days Transfer: Lua Chinese website (www.luachina.net)

转载请注明原文地址:https://www.9cbs.com/read-32745.html

New Post(0)