Call C language function instance in Lua _lua

Source: Internet
Author: User

In the previous article (c called the LUA function), we talked about how to invoke the LUA function in C, which is usually a language that can invoke B and vice versa. Just as the Java and C languages use JNI to tune in, Lua and C can also be tuned to each other.

When Lua calls the C function, it uses the same stack in the C call Lua, and the C function gets the function from the stack and pushes the result onto the stack. To differentiate between returning results and other values in the stack, each function returns the number of results.
Here's an important concept: this stack is not a global structure, and each function has its own private local stack. Even if the C function calls the LUA code, the LUA code calls the C function again, and they have separate local stacks. The index of the first parameter is 1.
As the first example, let's look at how to implement a simple function that returns the number of arguments given 2 parameters:

Copy Code code as follows:

static int L_plus (lua_state* l)
{
Lua_integer a = Lua_tointeger (L, 1);
Lua_integer B = Lua_tointeger (L, 2);
Lua_pushinteger (L, a+b);
return 1;
}

Each LUA-registered function must be this prototype, which is already defined in LUA.H:
Copy Code code as follows:

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

Because the C function returns the number of return values for an int type. Therefore, if you do not need to clean up the stack before pressing the return value, LUA automatically removes any data below the return value.

Before we use this function in Lua, we need to register it. Using Lua_pushcfunction, it accepts a C function and then creates a value of the function type within LUA to represent the function.

Copy Code code as follows:

Lua_pushcfunction (L, Lua_plus);
Lua_setglobal (L, "Myplus");

A professional point of writing is that we have to check its parameter types:
Copy Code code as follows:

static int L_plus (lua_state* l)
{
Lua_integer a = Lual_checkinteger (L, 1);
Lua_integer B = Lual_checkinteger (L, 2);
Lua_pushinteger (L, a+b);
return 1;
}

Complete code:
Copy Code code as follows:

#include <lua.h>
#include <lauxlib.h>
#include <lualib.h>
static int L_plus (lua_state* l)
{
Lua_integer a = Lual_checkinteger (L, 1);
Lua_integer B = Lual_checkinteger (L, 2);
Lua_pushinteger (L, a+b);
return 1;
}

int main ()
{
Lua_state *l = Lual_newstate ();
Lual_openlibs (L);
Lua_pushcfunction (L, L_plus);
Lua_setglobal (L, "Myplus");
if (lual_dostring (L, "Print (Myplus (2,2))")) {
Lua_close (L);
Error ("Failed to invoke");
}
Lua_close (L);
return 0;
}


Finish

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.