Recently in the study of how to embed Lua in Windows to complete the business module authoring some problems, first Lua as a scripting language, its flexibility and scalability is very high, otherwise cocos2d-x will not embed him to write business logic, But because of the relatively small domestic data, very few people go to the eight after the study to write an article to share their results, want to go deep understanding of the application of some things, it seems very weak, many of the LUA extension is directly write extension library to complete, if not understand the principle even you will not be flexible to use, This is the purpose of this paper.
First I need LUA to embed my application, which is easy to do.
Second I need to use LUA for the data layer operation, I want to have a similar Java PHP ORM framework to complete my Sqlite3 database operation, but research to find good pit Dad, only the following two libraries are OK. Luasql has not been updated for several years, the latest code on GitHub is based on Sqlite3 and the previous version of Lua5.1, the other lsqlite3 is also, although relatively tough, but it seems that the document is only tested under Linux ... So when it comes to these problems, it's better to write your own LUA extensions according to Sqlite3, rather than just compiling these old code.
Below to get to the chase:
Step-by-step programming your own LUA extensions, so I assume that you already have the foundation for LUA Embedded development.
First create our own DLL project as a LUA extension library, set the path to the LUA library, the header file contains the path, and then add the code in the Extension library project source file to use as 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;}
Such as
Second step to write the LUA extension prototype I'm using a compatible Lua5.1 extension, so please add it in the LUA library and our extension library project so that we can extend LUA using Lua5.1.
Of course, we can also use Lua5.2 the latest way to expand but it doesn't matter.
The following tells the compiler that I am using a 5.1 compatible extension to build the DLL shared library.
#define Lua_compat_module#define Lua_core#define Lua_build_as_dll
this time I started to write our extension library in the way Lua did. There can be two ways of
The first inline extension is to write the extension directly into our application, which we usually do when we develop it. Instead of the 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"); return 0; } const lual_reg reg[]={ {"func",extfunc}, {null, NULL}}; LUALIB_API int Luaopen_usher_luaex (lua_state *l) { //lua functions 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") //<span style= "color: #FF0000;" > About require loading mechanism in follow-up </span>usher.func ()
The second plug-in extension we've used a lot of third-party LUA extensions that are developing 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 David!"); return 0; } static const Lual_reg reg[]={{"func", Extfunc}, {null, NULL}}; Module name DLL name//loader load 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 follows require ("USHER.LUAEX") //loading the Usher\luaex.dll plug-in under the current Package.cpath and loading the ingress function to load the LUA extension Library Usher.func ()
The generated DLL we can see that the following function Luaopen_usher_luaex is exported to the LUA module for loading
The resulting expansion module DLL is as follows
The results of the final operation are
Well, by now, you can also write an extension library for Lua, and the next detailed analysis of LUA's require containment mechanism
To write a LUA extension by using C + + in manual