Accidentally heading the party again? Sorry, ha ~
Look over the previous blog, send a lot of nonsense too many of their own can not see, ah, well, as far as possible to write a little more concise.
This article does not involve LUA grammar learning, if necessary, please visit: http://book.luaer.cn/
A. Lua stack
To understand Lua and C + + interactions, first understand the LUA stack.
To put it simply, the main method of communication between Lua and C + + languages is a ubiquitous virtual stack. The stack is characterized by advanced post-out.
In Lua, the LUA stack is a struct, and the stack index is either positive or negative, the difference being that positive index 1 always represents the bottom of the stack, and negative index 1 always represents the top of the stack.
Two. Operation of the Stack
Because Lua communicates with C + + through the stack, LUA provides the C API to manipulate the stack.
Let's look at one of the simplest examples:
#include <iostream> #include <string.h>using namespace Std;extern "C" {#include "lua.h" #include "lauxlib.h" #include "Lualib.h"}void Main () {//1. Create a statelua_state *l = Lual_newstate ();//2. into the stack operation lua_pushstring (L, "I am so cool~"); Lua_pushnumber (l,20);//3. Value action if (lua_isstring (l,1)) { //determine if it can be converted to stringcout<<lua_tostring (l,1) << endl;//to String and returns}if (Lua_isnumber (l,2)) {Cout<<lua_tonumber (l,2) <<endl;} 4. Close Statelua_close (L); return;}
It can be simply understood that Lual_newstate returns a pointer to the stack, and other comments should be understood.
If you are unfamiliar with the extern "C" You can click here: http://blog.csdn.net/shun_fzll/article/details/39078971.
Some other stack operations:
int lua_gettop (lua_state *l);//returns the top index of the stack (that is, the stack length) void lua_settop (lua_state *l, int idx);//void Lua_pushvalue (lua_state *l, int idx);//Press the copy of the value on the IDX index into the stack top void lua_remove (lua_state *l, int idx);//Remove the value on the idx index void Lua_insert (lua_state *l, int idx);//pop up the top element of the stack and insert the index idx position void lua_replace (lua_state *l, int idx);//eject the top element of the stack and replace the value of the index IDX position
Lua_settop sets the top of the stack to a specified position, which is to modify the number of elements in the stack. If the value is higher than the original stack, then the high part nil complements, if the value is lower than the original stack, then the portion of the original stack is discarded.
so you can use Lua_settop (0) to empty the stack.
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.
There is now such a Hello.lua file:
str = "I am so cool" TBL = {name = "shun", id = 20114442}function Add (A, b) return a + bend
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 the variable Lua_getglobal (L, "str"); string str = lua_tostring (l,-1);cout<< "str =" <<str.c_str () <<endl ;//str = I am so cool~//5. Read Tablelua_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 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;} At this point, the situation in the stack is://=================== stack top ===================//Index type value//4 int:30//3 String:shun// 2 table:tbl//1 string:i am so cool~//=================== bottom ===================//7. Close Statelua_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 hot pot ~");//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 the Stack lua_newtable (l);//Set the value lua_pushstring (L, "Give me a girl friend!") into the table; Press the value into the stack Lua_setfield (L,-2, "str"); Set the value to table and give 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");//Get function, press into the stack lua_pushnumber (l, 10);//press into the first parameter Lua_pushnumber (L, 20);//press in 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 handsome pot ~"); 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)
four. Lua calls C + +
We implement it in three ways.
method One: Write the module directly into the LUA source code
In Lua, we can write a function in LUA.C and then recompile the LUA file.
Maybe you're using a LUA for Windows integrated environment, okay, not compiling LUA can refer to this article: http://blog.csdn.net/snlscript/article/details/15533373
After compiling it, it's like this: ()
Then we can add our own functions to the LUA.C. The function to follow the specification (which can be viewed in lua.h) is as follows:
typedef int (*lua_cfunction) (Lua_state *l);
In other words, all functions must receive a lua_state as an argument and return an integer value. Because this function uses the LUA stack as a parameter, it can read any number and any type of arguments from the stack. The return value of this function indicates how much of the return value is pressed into the LUA stack when the function returns. (because LUA functions are capable of returning multiple values)
We then add the following function to the LUA.C:
This is my functionstatic int gettwovar (lua_state *l) { //pushes 2 values into the function stack lua_pushnumber (L, x); Lua_pushstring (L, "Hello"); return 2;}
In the pmain function, add the following code after the Lual_openlibs function:
Registration function Lua_pushcfunction (L, Gettwovar); Put the function into the stack Lua_setglobal (L, "Gettwovar"); Set LUA global Variables Gettwovar
by finding Lua.h
/#define Lua_register (L,N,F) (Lua_pushcfunction (L, (f)), Lua_setglobal (L, (n)))
We found that the previous registration function can be written like this:
Lua_register (L, "Gettwovar", Gettwovar);
Run, results
Of course, in general, we do not recommend to modify other people's code, more inclined to write their own independent C + + module for the LUA call, the following is how to implement.
method Two: Use a static dependency method
1. Create a new empty Win32 console project, Remember that in the VC + + directory, the header files in Lua and the directory of LIB files are included, and then the linker-and additional dependencies, including Lua51.lib and Lua5.1.lib.
2. Create a new Avg.lua under the directory as follows:
AVG, sum = average ("The average is", avg) print ("The sum is", sum)
3. The new test.cpp is as follows:
#include <stdio.h>extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h"}/* pointers to the LUA interpreter */lua_state * l;static int average (lua_state *l) {/* Gets the number of arguments */int n = lua_gettop (L);d ouble sum = 0;int i;/* loop to find the sum of the parameters */for (i = 1; I < ; = N; i++) {/* summation */sum + = Lua_tonumber (L, i);} /* Press average */lua_pushnumber (L, sum/n);/* Press in and */lua_pushnumber (l, sum);/* Returns the number of return values */return 2;} int main (int argc, char *argv[]) {/* Initialize LUA */l = Lua_open ();/* Load the LUA base library */lual_openlibs (L);/* Register function */lua_register (l, "a Verage ", average);/* Run Script */lual_dofile (L," Avg.lua ");/* Clear Lua */lua_close (l);/* Pause */printf (" press ENTER to exit ... "); GetChar (); return 0;}
To execute, we can get the result:
The approximate order is that we write a module function in C + +, register the function in the LUA interpreter, then execute our LUA file by C + + and invoke the function that we just registered in Lua.
It looks awkward, there are wood ... The next step is to describe how the DLL is called.
method Three: How to use DLL dynamic link
Let's start with a new DLL project named Mlualib. (so the last DLL exported is also MLualib.dll)
Then write our C + + module, take the function as an example, we will first create a new. h file and a. cpp file.
h file is as follows: (if you are not quite able to understand the contents of the header file, click here: http://blog.csdn.net/shun_fzll/article/details/39078971. )
#pragma onceextern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h"} #ifdef lua_exports#define Lua_api __ Declspec (dllexport) #else # define LUA_API __declspec (dllimport) #endifextern "C" Lua_api int luaopen_mlualib (lua_state * L);//define Export function
The . cpp files are as follows:
#include <stdio.h> #include "mLualib.h" static int averagefunc (lua_state *l) {int n = lua_gettop (L);d ouble sum = 0;int i;/* */for (i = 1; I <= n; i++) sum + = Lua_tonumber (l, i); Lua_pushnumber (L, sum/n);//press-in average Lua_pushnumber (L, S UM);//press-in and return 2; Returns two results}static int sayhellofunc (lua_state* L) {printf ("Hello world!"); return 0;} static const struct Lual_reg mylib[] = {{"average", averagefunc},{"SayHello", Sayhellofunc},{null, NULL} // The last pair in the array must be {null, NULL}, which is used to denote the end };int luaopen_mlualib (lua_state *l) {lual_register (L, "SS", Mylib); return 1;// The Mylib table is pressed into the stack, so you need to return 1}
don't understand it's okay, let's compile it and then create a new Lua file that we'll call in Lua:
(remember to copy the DLL file to the Lua file directory before calling)
Require "mlualib" local ave,sum = Ss.average (1,2,3,4,5)//parameter corresponds to data in the stack print (ave,sum) --3 15ss.sayhello ()- - Hello world!
successful call to have wood there? We see the output information.
(Wood has success you leave a message, I teach you!) )
What's going on here? Comb:
1. We have written averagefunc averaging and sayhellofunc functions,
2. Then wrap the function inside the mylib array, the type must be Lual_reg
3. The two functions are exported by the Luaopen_mlualib function and registered in LUA.
So why write like this? In fact, when we're in Lua:
Require "Mlualib"
That 's what LUA does when it's written like this:
Local Path = "MLualib.dll" local f = package.loadlib (path, "Luaopen_mlualib")- -Returns the Luaopen_mlualib function f () -- Perform
So when we write a module like this, when
writing the luaopen_xxx export function, XXX is best to be the same as the project name (because the project name and DLL).
It is important to note that the lua_state in the function arguments are private and each function has its own stack. The stack is automatically emptied when a C/C + + function presses the return value into the LUA stack.
Five. Summary
This article took a few genius to tidy up, the final summary is:
Lua and C + + interact through a virtual stack.
C + + Call Lua is actually: C + + first put the data in the stack, the LUA to the stack to fetch the data, and then return the data corresponding to the top of the stack, and then back to C + + from the top of the stack.
LUA calls the same thing: Write your own C module, then register the function in the LUA interpreter, and then call the module's function by Lua.
Reprint Please specify source: http://blog.csdn.net/shun_fzll/article/details/39120965
Lua and C + + interaction Summary (very detailed)