Three C + + calls Lua
We can often use Lua files as configuration files. File configuration information such as Ini,xml . Now let's use C + + to read the variables in the Lua file, table, function.
Lua and C communication have such a convention: All LUA values are managed by LUA, the values produced in C + + do not know, similar to the meaning of the expression: " if you (c + +) Want what, you tell Me (LUA), I come to produce, and then put on the stack, You can only manipulate this value through the API, I am just in my world ", this is important because:
"If you want something, you tell me, I'll make it." It is guaranteed that LUA is responsible for the life cycle and garbage collection of variables in Lua, so the values must be created by Lua (the bookkeeping information used for life cycle management is added at the time of Creation )
"Then put on the stack, you can only manipulate the value through the API," the LUA API gives C a complete set of operating interfaces, which is equivalent to the agreed communication protocol , if LUA customers use this interface, then LUA itself will not have any "unexpected" error.
The phrase "I am my World" embodies Lua and C + + as a demarcation of two different systems, the value in C/s + +, which Lua does not know, and Lua is only responsible for its world.
There is now such a Hello.lua file:
str = "I am so cool" tbl = {name = "shun", id = 20114442} function Add (b) return a + B end
we write a test.cpp to read it:
#include <iostream> #include <string.h> using namespace std; extern "C" {#include "lua.h" #include "lauxlib.h" #include "lualib.h"} void Main () {//1. Create LUA State Lua_state *l = Lual_newstate (); if (L = = NULL) {return; }//2. load lua file int bRet = Lual_loadfile (L, "Hello.lua"); if (bRet) {cout<< "Load file Error" <<endl; return; }//3. Run lua file BRet = Lua_pcall (l,0,0,0); if (bRet) {cout<< "Pcall error" <<endl; return; }//4. Read variable Lua_getglobal (L, "str"); String str = lua_tostring (l,-1); cout<< "str =" <<str.c_str () <<endl; str = I am SO cool~//5. Read Table Lua_getglobal (L, "tbl"); Lua_getfield (l,-1, "name"); str = lua_tostring (l,-1); cout<< "Tbl:name =" <<str.c_str () <<endl; Tbl:name = Shun//6. Read function Lua_getglobaL (L, "add"); Get the function, press into the stack lua_pushnumber (L, 10); Press in the first parameter Lua_pushnumber (L, 20); Press the second parameter int iret= lua_pcall (L, 2, 1, 0);//Call function, after the call is completed, the return value will be pressed into the stack, 2 indicates the number of arguments, and 1 indicates the number of results returned. if (IRet)//Call error {const char *perrormsg = lua_tostring (L,-1); cout << perrormsg << Endl; Lua_close (L); return; } if (Lua_isnumber (L,-1))//value output {double fvalue = Lua_tonumber (L,-1); cout << "Result is" << fvalue << Endl; }//To this point, the scenario in the stack is://=================== stack top ===================//Index type value//4 Int:3 0//3 String:shun//2 TABLE:TBL//1 string:i am so cool~//============== ===== Bottom ===================//7. Close State lua_close (L); return; }
knowing how to read it, let's look at how to modify the value of table in the code above:
Set the value you want to set to the stack lua_pushstring (L, "I'm a good person ~"); Set this value to table (at which time the TBL position on the stack is 2) Lua_setfield (L, 2, "name");
We can also create a new table:
Create a new table and press it into the stack lua_newtable (L); Set the value in table lua_pushstring (L, "Give me a girl friend!"); Press the value into the stack Lua_setfield (L,-2, "str");//Set the value to table and G ive me a girl friend out of the stack
It is important to note that the stack operation is based on top of the stack, that is, it only goes to the value of the top of the stack.
To give a simpler example, the function call process is to first put the function into the stack, the parameters into the stack, and then call the function with Lua_pcall, when the stack top is a parameter, the stack is a function, so the stack process will be roughly: parameter out of the stack, save parameters, such as parameter, save parameter Function---call function---Returns the result into the stack.
Similar to the Lua_setfield, set the value of a table, you must first put the value out of the stack, save, and then find the table position.
If you don't understand it, see the following example:
Lua_getglobal (L, "add"); Gets the function, presses into the stack lua_pushnumber (L, ten); Press in the first parameter lua_pushnumber (L, x); Press into the second parameter int iret= lua_pcall (L, 2, 1, 0);//2 arguments out of the stack, function out of the stack, press the function to return the result lua_pushstring (l, "I am a good person ~"); Lua_setfield (L, 2, "name"); Will be "I am a handsome pot ~" Out of the stack
In addition, please add:
Lua_getglobal (L, "var") performs two steps: 1. put Var in the stack, 2. The value of Var is found by Lua and the value of VAR is returned to the top of the stack (replacing Var).
The function of Lua_getfield (l,-1, "name") is equivalent to lua_pushstring (L, "name") + lua_gettable (l,-2)
The correspondence between LUA value and C value:
|
C |
Lua |
Nil |
No |
{value=0, tt = T_nil} |
Boolean |
int not 0, 0 |
{value= non-0/0, tt = T_boolean} |
number |
int/float 1.5 |
{value=1.5, tt = t_number} |
Lightuserdata |
void*, int*, various * point |
{value=point, tt = T_lightuserdata} |
String |
Char str[] |
{VALUE=GCO, tt = t_string} gco=tstring obj |
Table |
No |
{VALUE=GCO, tt = t_table} gco=table obj |
UserData |
No |
{VALUE=GCO, tt = t_udata} gco=udata obj |
Closure |
No |
{VALUE=GCO, tt = t_function} gco=closure obj |
As you can see, some of the types provided in Lua are corresponding to those in C, and also some types that are not available in C. Some of these medications are particularly illustrative:
Nil value, there is no correspondence in C, but a nil value can be pressed into Lua via Lua_pushnil
Note: The lua_push* family functions have the semantics of "create a type of value and press in" because all of the variables in Lua are created and saved in Lua, and for those LUA types that have a corresponding relationship with C, LUA passes an additional parameter from the API, The LUA variable that creates the corresponding type is placed on top of the stack, and LUA creates the corresponding variable at the top of the stack for no LUA type of the corresponding type in C.
For example: Lua_pushstring (L, "string") Lua creates a tstring obj based on "string", bound to the newly allocated top element of the stack
Lua_pushcclosure (l,func, 0) Lua creates a Closure obj based on Func, bound to the newly allocated top element of the stack
Lua_pushnumber (l,5) Lua modifies the top element of the newly allocated stack directly, assigning 5 to the corresponding domain
Lua_createtable (l,0, 0) Lua creates a Tabke obj, bound to the newly allocated top element of the stack
In summary, this is the flow of C value–> Lua value, whether you want to put a simple 5 into the world of Lua, or create a table that will cause
1. Stack top new allocation element 2. Bind or assign a value
Or to repeat a sentence, a C value into the stack is into the world of LUA, LUA will generate a corresponding structure and management, and henceforth no longer rely on this C value
Lua value–> C value, is implemented through the Lua_to* family API, it is simple to remove the corresponding C field value is OK, only those who have the corresponding value in C LUA value, such as table can not to C value, so API Bud No Provides an interface such as lua_totable.
Lua and C + + Interactive detailed summary _3_c++ call LUA