Manually write the Lua extension using C/C ++

Source: Internet
Author: User

Manually write the Lua extension using C/C ++

Recently, when studying how to embed Lua in Windows to complete the compilation of business modules, we found some problems with Lua. First, as a scripting language, Lua has high flexibility and scalability, otherwise, the Cocos2d-x will not embed him to write business logic, but because of the small amount of domestic information, few people go to the 8 th after the study to write an article to share their results, many Lua extensions are implemented by writing extension libraries directly. If you don't understand the principles, you won't even use them flexibly, this is the purpose of writing this article.

First, I need Lua to embed my application, which is easy to achieve.

Second, I need to use Lua to perform operations on the data layer. I hope to have an ORM framework similar to Java PHP to complete the operations on the Sqlite3 database. However, I have studied it to find out that it is a good solution, only the following two databases are supported. LuaSql has not been updated for several years. The latest code on Github is based on Sqlite3 and versions earlier than Lua5.1, And the other lsqlite3 is also, although relatively powerful, but it seems that the test passes only in Linux... Therefore, in the face of these pitfalls, it is better to barely compile these old codes to compile their own Lua extensions Based on sqlite3.

Enter the subject below:

Write your own Lua extension step by step. Now I assume that you already have the foundation for Lua embedded development.

First, create our own DLL project as the Lua extension library, set the path of the Lua library, the header file contains the path, and then add the code in the extension library project source file for Lua initialization.

Extern "C" {# include "lua. h "# include" lualib. h "# include" lauxlib. h "}; // open Lua Libslua_State * GetLua () {lua_State * lua = luaL_newstate (); luaL_openlibs (lua); return lua ;}
For example

Export/export + PC9wPgo8cD48c3Ryb25nPs/Cw + export/export + CjwvcD4KPHA + export = "brush: java;"> # define LUA_COMPAT_MODULE # define LUA_CORE # define LUA_BUILD_AS_DLLAt this time, I began to write our extension library in Lua mode .. You can use either of the following methods:

The first nested extension is to directly write the extension into our application, which is usually used during development. Instead of DLL, the Code is as follows.

# Define LUA_COMPAT_MODULEextern "C" {# include "lua. h "# include" lualib. h "# include" lauxlib. h "}; // open Lua Libslua_State * GetLua () {lua_State * lua = luaL_newstate (); luaL_openlibs (lua); return lua;} static int extFunc (lua_State * L) {printf ("I am a embed Lua Extension By Programmer Xiaowei! \ N "); return 0;} const luaL_Reg reg [] = {" func ", extFunc}, {NULL, NULL}; LUALIB_API int luaopen_usher_luaex (lua_State * L) {// lua function luaL_openlib (L, "usher", reg, 0); return 0; // No return value} int _ tmain (int argc, _ TCHAR * argv []) {lua_State * lua = GetLua (); luaopen_usher_luaex (lua); if (0! = LuaL_dofile (lua, "./luaext. lua") {printf ("load error! \ N ") ;}return 0 ;}
The Lua test code is as follows:

Require ("usher") // For more information about the require loading mechanism, see usher. func ()


The second plug-in-type extension is used by many third-party Lua extensions to develop DLL extensions in this way.

The Code is as follows:

# Define LUA_COMPAT_MODULEextern "C" {# include "lua. h "# include" lualib. h "# include" lauxlib. h "}; lua_State * GetLua () {lua_State * lua = luaL_newstate (); luaL_openlibs (lua); return lua;} static int extFunc (lua_State * L) {printf ("I am a Lua Extension By Programmer Xiaowei! "); Return 0;} static const luaL_Reg reg [] = {" func ", extFunc}, {NULL, NULL }}; // Module name dll name // The loader loads extern "C" _ declspec (dllexport) int luaopen_usher_luaex (lua_State * L) {// lua function luaL_openlib (L, "usher", reg, 0); return 0; // No return value} bool winapi DllMain (_ in HINSTANCE hinstDLL ,__ in DWORD fdwReason ,__ in LPVOID lpvReserved) {return TRUE ;}
// The lua plug-in Call Code is as follows: require ("usher. luaex ") // load the current package. usher \ luaex under cpath. dll plug-in and load the entry function to load the lua extension library usher. func ()


After the generated dll is generated, we can find that the following function luaopen_usher_luaex is exported to the lua module for loading.

The generated extension module dll is as follows:


The final running result is as follows:




Now you can write extension libraries for Lua. The next detailed analysis of Lua's require inclusion mechanism











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.