Use LUA for interaction in C ++, LUA for closure, C ++ for LUA for mutual closure, and lua for Interaction

Source: Internet
Author: User

Use LUA for interaction in C ++, LUA for closure, C ++ for LUA for mutual closure, and lua for Interaction

LUA can be described as a configuration file artifact. It is known only when the specific functions are used. It has not been used for two hours in the last two years and is familiar with the basic usage.

Including the mutual calls of C/LUA stack operation functions and the closure of lua c ++ and LUA

To use LUA flexibly, you must first learn the stack Interaction Model of LUA and C, which is similar to the calling method of assembler functions.

To learn about LUA, you must first understand the stack lua_State of interaction between LUA and C/C ++. Here we reference an article by netizens in detail.

Http://wind-catalpa.blog.163.com/blog/static/1147535432013119103150929/

Code on

C ++ code

<Span style = "font-size: 14px; color: #000000;"> # include "string. h "extern" C "{# include" lualib. h "// contains lua lib # include" lauxlib. h "// helper function}; # pragma comment (lib," lua. lib ") // The Lua and C Programs exchange data through a stack: lua_Statelua_State * GetLua () {lua_State * lu = luaL_newstate (); /* Create a Lua object */luaL_openlibs (lu); // open all the shared library functions to the lua object return lu;} // press batch data into the stack # define FOR_PUSH (I, J, STEP, LUA) \ for (int I = I; I <= J; I ++ = STEP) \ {\ lua_pushinteger (LUA, I) ;\}// retrieve the data of the specified index in the stack // print the stack data # define FOR_LIST (I, J, STEP, LUA) \ for (int I = I; I <= J; I + = STEP) \ {\ int n = lua_tointeger (LUA, I); \ printf ("Index in the stack: % d, data: % d \ n ", I, n) ;\}# define CLEAR (LUA) \ for (int I = 1; I <= lua_gettop (LUA ); I ++) \ lua_pop (LUA, I) // returns 1 result // The function prototype. For details, refer to the LUA5.2 document int callCPP (lua_State * lua) {int a = lua_tointeger (lua, 1); int B = lua_tointeger (lua, 2); lua_pushnumber (lua, a + B); // result pressure stack return 1 ;} Int _ tmain (int argc, _ TCHAR * argv []) {// gets the stack pointer lua_State * Lua = GetLua () for interaction between C and lua; if (lua = nullptr) {printf ("Lua Open Error"); return 0;} // FOR_PUSH (, 1, Lua) on the lua stack operation ); // The int n = lua_gettop (lua) parameter of the cyclically ordered stack; printf ("% d parameters in the lua stack \ n", n); FOR_LIST (, 1, lua); // lua_pop (lua, 3); // the three parameters n = lua_gettop (lua) are displayed in the method of stack first-in-first-out ); printf ("lua Stack has % d parameters \ n", n); FOR_LIST (1, n, 1, lua ); //// execute the simple memory LUA script char * pLua = "print (\" hello, lua! \ ")"; LuaL_loadbuffer (lua, pLua, strlen (pLua), "testLuaScript0Chunk"); if (LUA_ OK = lua_pcall (lua, 0, 0 )) {printf ("lua script called successfully! \ N ") ;}// the CLEAR (lua) of all data in the stack is displayed ); /// load the lua script and compile and run the lua script // load if (luaL_dofile (lua ,". /c. lua ") {printf (" lua script loaded successfully! \ N ");} lua_getglobal (lua," num1 "); // load to the stack lua_getglobal (lua," num2 "); // load to the stack lua_getglobal (lua, "str1"); // load string int num1 = lua_tointeger (lua,-3); // reverse value from Stack LUA stack to bidirectional printf ("num1: % d \ n ", num1); n = lua_gettop (lua); int num2 = lua_tointeger (lua,-2 ); // The Reverse value is from the stack LUA stack to bidirectional printf ("num2: % d \ n", num2); n = lua_gettop (lua ); printf ("lua stack contains % d parameters \ n", n); CLEAR (lua); // load the function to the stack // call the non-Parameter Function lua_getglobal (lua, "testHello "); N = lua_gettop (lua); printf ("% d parameters in the lua stack \ n", n); // The lua function call automatically clears the stack lua_pcall (lua, 0); n = lua_gettop (lua); printf ("% d parameters in the lua stack \ n", n); lua_getglobal (lua, "Closer "); // The function is pushed to the top of the stack. lua_pushinteger (lua, 1); lua_pushinteger (lua, 2); // The push parameter // The closure function calls if (LUA_ OK! = Lua_pcall (lua, 0) {printf ("function call failed! \ N "); return 0;} int result = lua_tointeger (lua,-1); // extract the top data of the stack printf (" Closer result: % d \ n ", result); // note that the returned values of stack cleanup POP lua_pop (lua, 1) at the top of the stack; n = lua_gettop (lua ); printf ("% d parameters \ n", n in the lua stack); // LUA calls the C ++ function // registers the lua_register (lua, "CallC ", callCPP); // load if (luaL_dofile (lua ,". /c1.lua ") {printf (" lua c1 script loaded successfully! \ N ") ;}/// C/LUA closure calls lua_pushcfunction (lua, callCPP); lua_setglobal (lua," CallCT "); // sets the call lua_getglobal (lua, "CloserT"); // load the lua closure function to the C ++ stack lua_pushinteger (lua, 2); // The function stack parameter must be correct lua_pushinteger (lua, 3 ); lua_pcall (lua, 0); n = lua_gettop (lua); printf ("The lua stack contains % d parameters \ n", n); return 0 ;} </span>
C.Lua and c1.lua files

-- C. lua

Str1 = "hello, I am Luaer" num1 = 2num2 = 3 -- test the function output str1function testHello () print (str1) end -- lua implements the closure function Closer (I, j) function add (I, j) return I + j end return add (I, j) endfunction CallC (a, B) return callCPP (a, B) end



-- C1.lua call C ++ functionnum = CallC (3, 4) print ("num is:", num) -- function CloserT (a, B) num1 = CallCT (a, B) print ("C ++/LUA closure Num1 is:", num1) end





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.