A study on LUA learning note--lua Call C

Source: Internet
Author: User

Last time I learned how to use C to call Lua's function and return a result, this time look at how to use Lua to invoke C's function.

I. INTRODUCTION

C Call LUA function is relatively simple, only need to manipulate the relevant stack on it, but Lua calls C, a little bit of trouble, although still with the stack to carry out the data, but because LUA itself does not have a function written in C, so it is necessary to register the C function in Lua step.

Lua in turn calls the C function, first, we want to write a function to be called, the function has a format requirement, the return value is int, but this int does not represent the return value of the LUA function, but the number of function return value, LUA supports multiple return values, so, It is also necessary to support multiple return values in C functions, but this is not done by return, but by pressing the return value into the universal stack and returning the number of returned values.

The function prototypes are as follows:

Returns the number of results, the result exists in the stack int function name (lua_state* L)
After writing the function, you also need to register the function with LUA, otherwise LUA does not know about this function. Register using the Lua_register function, you need to give the function name in Lua and the function pointer in C. The function prototypes are as follows:

Register the C function in Lua (l, function name in Lua, function pointer) lua_register (l, "function name", corresponding to the function pointer in c);

See an example:


LUA Program:

--Call C's function print (Addfunc (1, 2, 3))

C + + programs:

LuaTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdio.h> #include <windows.h>//because Lua is a function of C, and our program is C + +, so use extern "C" Introduce the header file extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "luaconf.h"}//lua the function to be called (number of numbers and) Note that the return value is not the result, but the number of results, and LUA supports multiple return values, so there are several returned values in the stack. int Addfunc (lua_state* l) {Double sum = 0;for (int i = 1; I <= lua_gettop (l); i++) {if (!lua_isnumber (l, i)) {Lua_pushstri Ng (L, "argument error!"); Lua_error (L);} Sum + = Lua_tonumber (L, i);} Press the result into the stack lua_pushnumber (L, sum);//Returns the number of results!!! return 1;} int _tmain (int argc, _tchar* argv[]) {//open lualua_state* L = Lual_newstate ();//Load lib file lual_openlibs (L);// Register the C function you just wrote into Lua (L, LUA function name, function pointer) lua_register (L, "Addfunc", addfunc);//execute LUA file Lual_dofile (L, "Test.lua");//End Lua_ Close (L); System ("pause"); return 0;}
Results:

6.0
Please press any key to continue ...


In fact, this call is relatively simple. Having finished writing functions and registering functions, this function is available in Lua, and we can use this function in LUA like library functions. For simplicity, Dofile directly, executes the LUA file, invokes the function in C, and prints the result in Lua.

To illustrate, when the registered C/C + + is called by Lua, this function has a unique stack, which is not shared. This stack is generated when entering the function body, from the inside index 1 to N, respectively, with the 1 to N parameters passed from the LUA program, these functions can only see their own stack, see no other stack.


Two. LUA interacts with CThe above example simply registers a function with Lua, and Lua uses this function, and finally the Lua file that is executed by Dofile as a whole does not reflect the interaction. The following example is the above function, but this time only executes in Lua, the result is stored in a global variable in Lua, and then the C function is used to extract the result and print it in C. This shows the function of C and LUA interaction.
LUA Program:
--Call C's function, and the result is saved in a result global variable, waiting for C to extract result = Addfunc (1, 2, 3)

C + + programs:
LuaTest.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <stdio.h> #include <windows.h>//because Lua is a function of C, and our program is C + +, so use extern "C" Introduce the header file extern "C" {#include "lua.h" #include "lualib.h" #include "lauxlib.h" #include "luaconf.h"}//lua the function to be called (number of numbers and) Note that the return value is not the result, but the number of results, and LUA supports multiple return values, so there are several returned values in the stack. int Addfunc (lua_state* l) {Double sum = 0;for (int i = 1; I <= lua_gettop (l); i++) {if (!lua_isnumber (l, i)) {Lua_pushstri Ng (L, "argument error!"); Lua_error (L);} Sum + = Lua_tonumber (L, i);} Press the result into the stack lua_pushnumber (L, sum);//Returns the number of results!!! return 1;} int _tmain (int argc, _tchar* argv[]) {//open lualua_state* L = Lual_newstate ();//Load lib file lual_openlibs (L);// Register the C function you just wrote into Lua (L, LUA function name, function pointer) lua_register (L, "Addfunc", addfunc);//execute LUA file Lual_dofile (L, "Test.lua");// The results returned in Lua are extracted from the global variable result, Lua_getglobal (l, "result"), and if ("Result" (!lua_isnumber (L,-1)) printf ("Results are not number!\n");// Obtain the result in C and print printf ("Result is%g\n", Lua_tonumber (L,-1));//End Lua_close (l); System ("pause"); return 0;}

Results:

Result is 6
Please press any key to continue ...



Of course, this is only a small example of a call to each other, the actual invocation of what needs to be done may be more complex, such as function registration, packaging and so on.







Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

A study on LUA learning note--lua Call C

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.