LuaWithC ++: Basic Lua syntax, luawithclua
Procedure
1. The host language creates a Lua interpreter (lua state machine) object.
2. register the Lua extensions (if any) implemented by the host language, such as functions, to the Lua interpreter for use.
3. Read the Lua source program or pre-compiled Lua Program (from any sources such as files, strings, and networks ).
4. Execute the Lua program.
Interaction between Lua and host language
The host language uses virtual machines to add, delete, read, and write variables in the Lua script.
The host language calls the functions in the Lua script through a VM.
The host language defines a new data type for Lua scripts.
Lua calls functions written in the host language
Basic usage
Header file to be introduced:
Extern "C" // ** use C language to compile **! {# Include
// Lua parser # include
// Lua standard library # include
// Lua auxiliary tool}
C ++ calls and parses Lua scripts without interaction
Char * code = "for I = 0, 5 do print (\ 'hello, world! \ ') End "; void TestCWithLua () {cout <"/******** test C ++ call lua code ********/" <endl; // 1. create lua interpreter object lua_State * s = luaL_newstate ();/* lua_open (); * // 2. open all lua library files luaL_openlibs (s); // 3. run the lua string code luaL_dostring (s, code); // luaL_dofile (s, "LuaSrc \ testcwithlua. lua "); // 4. close lua interpreter object lua_close (s); cout <"/********* end test *******/" <endl ;}
In the above example, only parsing Lua scripts is implemented, and data exchange and interoperability between Lua and the host language is not realized.
Similar to the typical scripting language engine, the Lua virtual machine is a stack machine, and all its operations are basically completed on the stack. This stack is also a key part of the Lua API, it is a means for Lua to exchange data with the host language.
Subject: The host language can use strings to construct arbitrary Lua scripts and transmit arbitrary data to the Lua program. Just like constructing SQL statements, it is the most stupid interaction mode.
Lua Stack
The Lua virtual machine has a stack inside it. The Lua API provides operations on it, not only for inbound and outbound stack operations, but also for random read/write stack elements through index values in the form of arrays, this is the main way for both parties to exchange data.
The host language can be used to compile functions called by Lua. The host language must comply with the call conventions, obtain parameters from the stack, and finally import the results to the stack. Register the host function with lua_register into the Lua Virtual Machine (this process is essentially to add a global variable to the Lua language), and you can call the function by the Lua language.
The host language can also press the Lua function to stack, then the parameters are pushed to the stack in sequence, and then lua_call is used to call the Lua function.
Lua stack Index
If the Lua VM stack contains N elements, 1 ~ N can be indexed from the bottom of the stack or-1 ~ -N is indexed down from the top of the stack. Generally, the latter is more commonly used.
Each element of the stack can be of any complicated Lua data type. There is no element vacancy in the stack, and it is implicitly containing a "null" type data.
Lua calls C ++
Testluawithc. lua
a = 13 b = 5 q, r = p(a, b) print(q, r)
Int Divided (lua_State * s) // General function prototype for Lua {double a = lua_tonumber (s,-2 ); // obtain the first parameter double B = lua_tonumber (s,-1); // obtain the second parameter int ia = static_cast
(A); int ib = static_cast
(B); int quot = ia/ib; int rem = ia % ib; lua_pushnumber (s, quot); // Add the first return value to the stack lua_pushnumber (s, rem ); // return the second return value to the stack return 2; // return value to the number of results} void TestLuaWithC () {cout <"/******** test the code ********/" <endl; lua_State * s = luaL_newstate (); luaL_openlibs (s); lua_register (s, "p", Divided); int ret = luaL_dofile (s, "LuaSrc \ testluawithc. lua "); // luaL_dostring (s," a = 13 B = 5 q, r = p (a, B) print (q, r )"); lua_close (s); cout <"/******** end test ********/" <endl ;}
Note: parameters are transmitted through the Lua stack.
As shown in the preceding example, the host function that can be called by Lua has a unified prototype: int f (lua_State * s). Data is transmitted through the stack instead of its parameters; the integer return value indicates the number of values that the function actually returns to Lua, that is, the number of results returned by the stack. After the function is returned, the Lua virtual opportunity automatically clears the stack and does not need to be done within the function.
Obviously, in Lua, the function can have more than one return value, which is also reflected in the Lua syntax. It can assign values to multiple variables returned by the function.
C ++ calls lua, lua global variables, and function calls, and interacts with each other.
Testglobal. lua
show = function(m) print('Lua has got: ' ..m) return 'It is from Lua'end
Void TestGlobalAndCall () {cout <"/******** test the code for using the lua global variable *******/" <endl; lua_State * s = luaL_newstate (); luaL_openlibs (s);/* luaL_dostring (s, "show = function (m) \ print ('lua has got :'.. m )\//.. string connector return 'it is from lua' \ end "); */luaL_dofile (s," LuaSrc \ testglobal. lua "); lua_getglobal (s," show "); // obtain the global variable show lua_pushstring (s," It is from C "); // press the string to stack lua_call (s, 1, 1); // call the lua function. One Parameter and one return value const char * result = lua_tostring (s, -1); // obtain the top element of the stack after execution, that is, the function execution result cout <"C has got:" <result <endl; lua_pop (s, 1); // The top element lua_close (s) of the stack is displayed ); cout <"/********* end test *******/" <endl ;}