Use C language to expand the lua module (Entry level) and lua Module

Source: Internet
Author: User

Use C language to expand the lua module (Entry level) and lua Module

In lua, modules are often loaded to implement some functions. If there is no ready-made module for require, we have to write the module by ourselves.

A few days ago, I used C to extend the sha1 Algorithm Module to lua. Extract and take notes.

The lua sample code is as follows:

require "libencode"local str = "source str"local des = libencode.sha1(str)print(des)

I need to use the sha1 () function in the libencode module to find des. Simple: require this module and then directly call it. So how does this database come from?

Three steps: first use C to write the module (follow the Rules), then compile the source code into a dynamic library, and finally copy the dynamic library to the corresponding directory (which must be a directory recognized by lua)

First paste the Code:

  

# Include <lua. h> # include <lauxlib. h> # include <lualib. h> static void encode_sha (const char * src, char * des) {/** sha-hash // hash algorithm */} static int l_sha1 (lua_State * lua) {const char * src = NULL; char des [40] = {0}; src = luaL_checkstring (lua, 1); // obtain the source string encode_sha (src, des); // something lua_pushstring (lua, des); // The pressure stack returns to lua return 1; // tells lua to return a variable} // ing table, "sha1" is the function name in lua, and l_sha1 is the function address in real C. static const struct luaL_Reg encode [] ={{ "sha1", l_sha1}, {NULL, NULL },}; // module registration function int luaopen_libencode (lua_State * lua) {// register all function functions in this module. libencode is the module name, the encode array stores the ing relationships of all functions luaL_register (lua, "libencode", encode); return 1 ;}

The encode [] array stores the actual address of each function name and function in lua.

The luaopen_xxx () function registers the module. For example, when lua executes the require "libencode" command, it searches for the luaopen_libencode () function registration module.

The luaL_register () function creates a table with a given name. Fill the table with array content. That is to say, this function registers all the API functions in the module to the name of this module, so that you can access the C function in lua in the format of (module. fun.

Compile:

Gcc source. c-fPIC-shared-o source. so (if the call fails during lua, add other compilation parameters as needed)

Copy:

Mv.../source. so LUA_C_PATH (copy the path to the c Module of lua, and run print ("C path:", package. cpath) on the command line to obtain the path)

 

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.