Lua Tutorial (iv): Calling C language, C + + functions in Lua _lua

Source: Internet
Author: User
Tags lua

This tutorial will explain how to call C + + functions in Lua.

In Lua, it is simpler to call C + + functions, and this article will demonstrate a concrete approach with two examples: one for averages and the other for printing parameter information for LUA functions.

Finally, this article describes how to define these two functions as a module so that the global namespace is no longer available in the LUA code.

Objective

When we need to call a C + + function in Lua, all functions must be signed with the following function:

Copy Code code as follows:

typedef int (*lua_cfunction) (Lua_state *l);

In other words, all functions must receive a lua_state as an argument and return an integer value. Because this function uses the LUA stack as a parameter, it can read any number of arguments and any type of argument from the stack. The return value of this function indicates how many return values are pressed into the LUA stack when the function returns. (because Lua's functions can return multiple values)

Example One

Define C + + function pointers

Copy Code code as follows:

int average (lua_state *l)
{
Get number of arguments
int n = lua_gettop (L);
Double sum = 0;
int i;
Loop through each argument
for (i = 1; I <= n; i++)
{
Total the arguments
Sum + + lua_tonumber (L, i);
}
Push the average
Lua_pushnumber (L, sum/n);
Push the sum
Lua_pushnumber (L, sum);
Return the number of results
return 2;
}

Register this function for LUA

Copy Code code as follows:

Lua_register (L, "average", average);

This function is called within LUA

Copy Code code as follows:

AVG, sum = average (10, 20, 30, 40, 50)
Print ("The average is", avg)
Print ("The sum is", sum)

Example Two

Defining C + + functions

Copy Code code as follows:

int displayluafunction (lua_state *l)
{
Number of input arguments
int argc = Lua_gettop (l);
Print input Arguments
Std::cout << "[C + +] Function called from Lua with" << argc
<< "input arguments" << Std::endl;
for (int i=0; i<argc; i++)
{
Std::cout << "input argument #" << argc-i << ":"
<< lua_tostring (L, Lua_gettop (L)) << Std::endl;
Lua_pop (L, 1);
}
Push to the stack the multiple return values
Std::cout << "[C + +] returning some values" << Std::endl;
Lua_pushnumber (L, 12);
Lua_pushstring (L, "you Cowboy");
Number of return values
return 2;
}

Register this LUA function

Copy Code code as follows:

Push the C + + function to is called from Lua
Std::cout << "[C + +] pushing the C + + function" << Std::endl;
Lua_pushcfunction (L, displayluafunction);
Lua_setglobal (L, "displayluafunction");

Note that in the previous example, we used a function that was

Copy Code code as follows:

Lua_register (L, "average", average);

It's actually just a macro definition, and its implementation is made up of two functions above.

Call this function in Lua

Copy Code code as follows:

Io.write (' [Lua] calling the C functionn ')
A,b = Displayluafunction (3.141592, ' Hola ')
--Print the return values
Io.write (' [Lua] the C function returned < ' ... a.. ' > and < '. B.. ' >\n ')

Implement a LUA module

First, we encapsulate these two C functions into an array:

Copy Code code as follows:

static const Lual_reg mylibs[]=
{
{"Average", average},
{"Displayluafunction", displayluafunction},
{NULL, NULL}
};

Next, we define another C function and let it register our LUA module:

Copy Code code as follows:

int Lua_openmylib (lua_state *l)
{
Lual_newlib (L, mylibs);
return 1;
};

The lual_newlib here will generate a table and fill in all the functions inside the mylibs. Finally, the Lua_openmylib return value is 1, which means that the table just generated will be pushed into the stack.

Finally, like the standard library for LUA, we register our new library and give it a name of Mylib:

Copy Code code as follows:

static const Lual_reg lualibs[] =
{
{"Base", luaopen_base},
{"Io", luaopen_io},
{"Mylib", Lua_openmylib},
{NULL, NULL}
};

At this point, the first two functions we call in LUA need to be prefixed with the module name:

Copy Code code as follows:

AVG, sum = mylib.average (10, 20, 30, 40, 50)
A,b = Mylib.displayluafunction (3.141592, ' Hola ')

Conclusion

Note: The LUA stack in the C function argument is private, and each function has its own stack. The stack is automatically emptied when a C/C + + function presses the return value into the LUA stack.

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.