Using LUA interaction in C + +, Lua implements closures, C++/lua mutual closures

Source: Internet
Author: User
Tags function prototype lua stack pop

Lua is a configuration file artifact, specific features used to know, nearly two years useless smoked two hours familiar with the next basic usage.

Includes C/lua stack operation function calls to each other and LUA closures for C + + and LUA closures

To be flexible with LUA, it is interesting to learn that the stack interaction model of LUA and C is similar to the way assembly functions are called.

To learn Lua, first understand the stacks of LUA and C + + interactions lua_state Here is a very detailed article citing netizens

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

On the Code

C + + code

< /span>

<span style= "Font-size:14px;color: #000000;" > #include "string.h" extern "C" {#include "lualib.h"//contains LUA lib#include "lauxlib.h"//auxiliary function}; #pragma comment (lib, "Lua . Lib ")//lua and C programs Exchange data through a stack: lua_statelua_state* Getlua () {lua_state* lu = lual_newstate ();/* Create LUA Objects */Lual_openlibs (LU ); Open all shared library functions to the Lua object return LU;} Bulk data pressed into stack # define For_push (I,j,step,lua) for (int i=i;i<=j;i+=step) {Lua_pushinteger (lua,i);} Take out the data//print stack data for the specified index in the stack # # for_list (I,j,step,lua) for (int i=i;i<=j;i+=step) {int N=lua  _tointeger (Lua,i);   printf ("Stack index:%d, Data:%d\n", i,n); } #define CLEAR (LUA) for (int i=1;i<=lua_gettop (LUA); i++) Lua_pop (lua,i)//return 1 Results//function prototype specific reference LUA5.2 document INT Callcpp (LU    A_state *lua) {int a = Lua_tointeger (LUA, 1);    int b = Lua_tointeger (LUA, 2);    Lua_pushnumber (LUA, a+b);             Result press stack return 1; } int _tmain (int argc, _tchar* argv[]) {//Get stack pointer for C and LUA interaction lua_state *lua =getlua (); if (lua==nullptr) {printf ("Lua Open Error"); return 0;} The stack operation for Lua For_push (1,10,1,lua);//the parameter int n=lua_gettop (LUA) that loops sequentially into the stack;p rintf ("%d arguments \ n" in the Lua stack); For_list (1,10,1,lua); Lua_pop (lua,3);//pops up three parameters N=lua_gettop (LUA);p rintf ("%d parameters \ n" in the Lua stack) in a stack LIFO mode; For_list (1,n,1,lua);  Executes 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,0)) {printf ("Lua Script call succeeded!\n ");} pops up stack all data clear (LUA),/////Load Lua script and compile run LUA script//load from current working directory if (Lual_dofile (Lua, "./c.lua")) {printf ("Lua script loaded successfully!\n");} Lua_getglobal (LUA, "NUM1");//load to stack Lua_getglobal (LUA, "num2");//load to stack Lua_getglobal (LUA, "str1");    Load string int num1 = Lua_tointeger (LUA,-3); The inverse value from the stack Lua stack is bidirectional printf ("num1:%d\n", NUM1); N=lua_gettop (LUA);    int num2 = Lua_tointeger (Lua,-2); The inverse value from the stack Lua stack is bidirectional printf ("num2:%d\n", num2); N=lua_gettop (LUA); printf (there are%d parameters \ n in the LUA stack); CLEAR (LUA); The load function to the stack//call is the parameterless function Lua_getglobal (LUA, "Testhello"); N=lua_gettop (LUA); printf (there are%d parameters \ n in the LUA stack); The LUA function call automatically cleans up the stack lua_pcall (LUA, 0,0,0); N=lua_gettop (LUA); printf (there are%d parameters \ n in the LUA stack); Lua_getglobal (LUA, "Closer"); The function is pressed into the stack top Lua_pushinteger (lua,1); Lua_pushinteger (lua,2);//press-in Parameter//closure function call if (Lua_ok!=lua_pcall (lua,2,1,0)) {printf ("function call failed!\n"); return 0;} int result=l Ua_tointeger (lua,-1);//Remove the stack top data printf ("Closer result:%d\n", result); Note Clean up the stack return value at the top of the stack pop Lua_pop (lua,1); N=lua_gettop (LUA); printf (there are%d parameters \ n in the LUA stack); LUA calls C + + functions//Registration Functions Lua_register (LUA, "CALLC", callcpp); Loads the IF (Lual_dofile (Lua, "./c1.lua")) {printf ("Lua C1 script load succeeded!\n") from the current working directory;///c/lua closure calls Lua_pushcfunction (lua,callcpp ); Lua_setglobal (LUA, "CALLCT");//Set Call Lua_getglobal (LUA, "Closert") in Lua,//Load LUA closure function to C + + stack Lua_pushinteger (lua,2); The function stack parameter must be correct Lua_pushinteger (lua,3), Lua_pcall (lua,2,0,0), N=lua_gettop (LUA);p rintf ("%d arguments \ n" in the Lua stack); return 0;} </span>
C. Lua and C1.lua files

--c.lua

str1= "Hello,i am Luaer" num1=2num2=3--test function output str1function Testhello ()   print (STR1) End--lua implement 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)--c++ and LUA mutual closure function Closert (A, B)    num1=callct (A, B)    print ("C++/lua Mutual closure Num1 is:", NUM1) end





Using LUA interaction in C + +, Lua implements closures, C++/lua mutual closures

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.