Lua and C + + Interactive learning Record VI: global function interaction

Source: Internet
Author: User
Tags lua

The main content is reproduced from: Zilong People Blog (strongly recommended to go to the sub-Longshan blog completely learn it again)

Part of the content is read from: "Lua 5.3 Reference Manual," the Chinese version of the translator Cloud wind production KAVCC

vs2013+lua-5.3.3

1.c++ calling Lua in functions

Functions in the ①lua

1 function Add (x, y) 2     return x + y3End

②c++ calling function

1     //Lua->stack, global function position -12Lua_getglobal (L,"Add");3 4     //c->stack, passing in function arguments5Lua_pushinteger (L,Ten);6Lua_pushinteger (L, -);7 8     //Lua->stack, calling the function, 2 means passing in 2 arguments, 1 representing a return value, and putting the return value to the position of-19Lua_call (L,2,1);Ten  One     //stack->c Aprintf"Add function return value:%d\n", Lua_tointeger (L,-1));

2.Lua calling a function in C + +

C + + functions that can be called by ①lua must conform to the following function signatures:

typedef INT (*lua_cfunction) (lua_state *l);

In the function, the corresponding data is stack operation;

The return value represents the number of return values that are used by LUA in the stack.

Functions in ②c++

1 intAverage (lua_state*L) {2     //Lua->stack, get the number of Lua call function inputs3     intLua_args_count =lua_gettop (L);4 5     //lua->c, get the values of all the input parameters in Lua6Lua_number sum =0;7      for(inti =1; I <= Lua_args_count; ++i) {8Sum + =Lua_tonumber (L, i);9     }Ten  One     //C->stack, returns the mean and the total value back to the LUA ALua_pushnumber (L, Sum/lua_args_count); - lua_pushnumber (L, sum); -  the     //returns the number to Lua -     return 2; -}

③ registering C + + functions into Lua

1 " Average " , average); 2 3 /// /lua_register Macro replaces the following two functions 4 // lua_pushcfunction (L, average); 5 // Lua_setglobal (L, "average");

To invoke a function in C + + in ④lua

1 local ave,sum = average (ten,2)print("  ", Ave,"", sum)

Lua and C + + Interactive learning Record VI: global function interaction

Related Article

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.