Lua Tutorial (20): Lua calls C function _lua

Source: Internet
Author: User
Tags first string lua require

The ability of LUA to invoke the C function will greatly improve the scalability and availability of LUA. For some operating-system-related functions, or modules that require high efficiency, we can do this through the C function, and then call the specified C function through Lua. For those C functions that can be called by LUA, their interfaces must follow the form required by LUA, the typedef int (*lua_cfunction) (lua_state* L). To put it simply, the function type contains only a pointer to the LUA environment as its only parameter, through which the implementation can further obtain the actual parameters that are actually passed in the LUA code. The return value is an integer that indicates the number of return values that the C function will return to the LUA code, or return 0 if there are no returned values. It should be explained that the C function cannot return the true return value directly to the LUA code, but rather passes the call parameters and return values between the LUA code and the C function through the virtual stack. Here we'll introduce two rules for LUA calling C functions.

1. C functions as part of the application.

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

The C registration function to be called by Lua.
static int add2 (lua_state* L)
{
Check that the parameters in the stack are valid, 1 represents the first argument (left to right) when Lua calls, and so on.
If the LUA code passes a parameter that is not number number when invoked, the function will complain and terminate the execution of the program.
Double OP1 = Lual_checknumber (l,1);
Double OP2 = Lual_checknumber (l,2);
The result of the function is pressed into the stack. If you have more than one return value, you can push it into the stack multiple times here.
Lua_pushnumber (L,OP1 + OP2);
The return value is used to prompt the number of return values for the C function, that is, the number of return values in the stack.
return 1;
}

Another C-registration function to be called by Lua.
static int sub2 (lua_state* L)
{
Double OP1 = Lual_checknumber (l,1);
Double OP2 = Lual_checknumber (l,2);
Lua_pushnumber (L,OP1-OP2);
return 1;
}

Const char* TestFunc = "Print (ADD2 (1.0,2.0)) print (Sub2 (20.1,19))";

int main ()
{
lua_state* L = Lual_newstate ();
Lual_openlibs (L);
Registers the specified function as a global function variable for LUA, where the first string parameter is the LUA code
The name of the global function to use when calling the C function, and the second parameter is a pointer to the actual C function.
Lua_register (L, "Add2", ADD2);
Lua_register (L, "Sub2", SUB2);
After registering all the C functions, you can use these registered C functions in the LUA code block.
if (lual_dostring (L,testfunc))
printf ("Failed to invoke.\n");
Lua_close (L);
return 0;
}

2. The C function library becomes the LUA module.

Generate a library of code that contains C functions, such as Linux so, or Windows DLLs, and copy to the current directory where the LUA code is located, or to the directory that the LUA_CPATH environment variable points to so that the LUA parser can locate them correctly. In my current Windows system, I copy it to "C:\Program files\lua\5.1\clibs\", which contains all of the Lua callable C libraries. See the C Language code and key annotations as follows:

Copy Code code as follows:

#include <stdio.h>
#include <string.h>
#include <lua.hpp>
#include <lauxlib.h>
#include <lualib.h>

The C function to be registered, the Declaration form of the function is given in the example above.
It should be explained that the function must be exported as C, so extern "C" is required.
The function code is the same as the previous example, and this is no longer repeat.
extern "C" int Add (lua_state* L)
{
Double OP1 = Lual_checknumber (l,1);
Double OP2 = Lual_checknumber (l,2);
Lua_pushnumber (L,OP1 + OP2);
return 1;
}

extern "C" int sub (lua_state* L)
{
Double OP1 = Lual_checknumber (l,1);
Double OP2 = Lual_checknumber (l,2);
Lua_pushnumber (L,OP1-OP2);
return 1;
}

The first field in the Lual_reg structure is a string that is used to notify Lua of the name of the function when registering.
The first field is a C function pointer.
Two fields of the last element in the structure array are null and are used to hint that the LUA registration function has reached the end of the array.
Static Lual_reg mylibs[] = {
{"Add", add},
{"Sub", sub},
{NULL, NULL}
};

The unique entry function for the C library. Its function signature is equivalent to the registration function above. See below for a few notes:
1. We can simply interpret the function as the factory function of the module.
2. The function name must be luaopen_xxx, where xxx represents the library name. The LUA code require "XXX" needs to correspond to it.
3. In a lual_register call, its first string argument is the module name "XXX" and the second parameter is an array of functions to be registered.
4. It is important to emphasize that all code that requires "XXX", whether C or LUA, must be consistent, a LUA convention,
Otherwise it will not be invoked.
extern "C" __declspec (dllexport)
int Luaopen_mytestlib (lua_state* L)
{
Const char* libname = "Mytestlib";
Lual_register (L,libname,mylibs);
return 1;
}

See the following LUA code:

Copy Code code as follows:

Require "Mytestlib"--Specify package name

-When called, it must be package.function
Print (Mytestlib.add (1.0,2.0))
Print (Mytestlib.sub (20.1,19))

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.